Skip to content

Commit

Permalink
Merge pull request #97 from manipalutsav/feature-audit
Browse files Browse the repository at this point in the history
Added Audit
  • Loading branch information
r21meghashyam authored Mar 14, 2024
2 parents 274358f + 57ef812 commit 3456e81
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const handle404 = require("./middlewares/handle404");
const errorHandler = require("./middlewares/errorHandler");
const headers = require("./middlewares/headers");
const auth = require("./middlewares/auth");
const audit = require("./middlewares/audit")

// Configure application

Expand Down Expand Up @@ -51,6 +52,8 @@ app.use(logger(function (tokens, req, res) {
+ " " + chalk.white((req.user && req.user.email) || "(anonymous)");
}));

app.use(audit);

// Routes
const collegesRouter = require("./routes/colleges");
const eventsRouter = require("./routes/events");
Expand Down
24 changes: 24 additions & 0 deletions src/middlewares/audit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

const AuditModel = require("../models/Audit");

module.exports = async (req, res, next) => {
res.on("finish", async () => {
if (req.method == "GET")
return;
let { method, url, baseUrl, user } = req;
let entry = {
method,
url: `${baseUrl}/${url}`,
user: user ? user.email : "(unknown)",
time: new Date()
};
try {
await AuditModel.create(entry);
}
catch (e) {
console.log("Audit Log failed", e);
}
});
next();
};
28 changes: 28 additions & 0 deletions src/models/Audit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const mongoose = require("mongoose");

const schema = {
method: {
type: String,
required: true,
},
time: {
type: Date,
required: true,
},
user: {
type: String,
required: true
},
url: {
type: String,
required: true,
},
};

const options = {
autoCreate: true,
};

const auditSchema = new mongoose.Schema(schema, options);

module.exports = mongoose.model("Audit", auditSchema);

0 comments on commit 3456e81

Please sign in to comment.