init
This commit is contained in:
21
activity/activity.controller.js
Normal file
21
activity/activity.controller.js
Normal file
@@ -0,0 +1,21 @@
|
||||
// MODELS / TOOLS
|
||||
const Activity = require("./activity.model");
|
||||
|
||||
exports.GetAllActivities = async (req, res) => {
|
||||
|
||||
let activityRequest;
|
||||
if (req.body.type) {
|
||||
activityRequest = { type: req.body.type };
|
||||
} else if (req.body.username) {
|
||||
activityRequest = { username: req.body.username };
|
||||
}
|
||||
else {
|
||||
activityRequest = { };
|
||||
}
|
||||
|
||||
const a = await Activity.Get(activityRequest);
|
||||
if (!a) return res.status(500).send({ error: "Failed!!" });
|
||||
|
||||
return res.status(200).send(a);
|
||||
};
|
||||
|
||||
56
activity/activity.model.js
Normal file
56
activity/activity.model.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const mongoose = require("mongoose");
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const activitySchema = new Schema(
|
||||
{
|
||||
type: {type: String},
|
||||
username: {type: String},
|
||||
timestamp: {type: Date},
|
||||
data: {type: Object, _id: false},
|
||||
});
|
||||
|
||||
activitySchema.methods.toObj = function () {
|
||||
var elem = this.toObject();
|
||||
delete elem.__v;
|
||||
delete elem._id;
|
||||
return elem;
|
||||
};
|
||||
|
||||
const Activity = mongoose.model("Activity", activitySchema);
|
||||
exports.Activity = Activity;
|
||||
|
||||
// ------------------------------
|
||||
|
||||
exports.LogActivity = async (type, username, data) => {
|
||||
var activity_data = {
|
||||
type: type,
|
||||
username: username,
|
||||
timestamp: Date.now(),
|
||||
data: data
|
||||
}
|
||||
try {
|
||||
const activity = new Activity(activity_data);
|
||||
return await activity.save();
|
||||
}
|
||||
catch{
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
exports.GetAll = async () => {
|
||||
try {
|
||||
const logs = await Activity.find({});
|
||||
return logs;
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
exports.Get = async (data) => {
|
||||
try {
|
||||
const logs = await Activity.find(data);
|
||||
return logs;
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
19
activity/activity.routes.js
Normal file
19
activity/activity.routes.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const ActivityController = require("./activity.controller");
|
||||
const AuthTool = require("../authorization/auth.tool");
|
||||
const config = require("../config");
|
||||
|
||||
const ADMIN = config.permissions.ADMIN; //Highest permision, can read and write all users
|
||||
const SERVER = config.permissions.SERVER; //Middle permission, can read all users
|
||||
const USER = config.permissions.USER; //Lowest permision, can only do things on same user
|
||||
|
||||
exports.route = function (app) {
|
||||
|
||||
app.get("/activity", [
|
||||
AuthTool.isValidJWT,
|
||||
AuthTool.isPermissionLevel(ADMIN),
|
||||
ActivityController.GetAllActivities,
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user