-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from manipalutsav/feature-audit
Added Audit
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |