@dynamatix/cat-shared 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +0 -0
- package/middlewares/audit.middleware.js +45 -0
- package/middlewares/index.js +1 -0
- package/models/audit-config.model.js +11 -0
- package/models/audit.model.js +14 -0
- package/models/index.js +2 -0
- package/package.json +15 -0
package/index.js
ADDED
|
File without changes
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const { default: AuditConfigModel } = require("../models/audit-config.model");
|
|
2
|
+
|
|
3
|
+
function applyAuditMiddleware(schema, collectionName) {
|
|
4
|
+
schema.pre(['save','updateOne'], async function (next) {
|
|
5
|
+
const doc = this;
|
|
6
|
+
const isNewDoc = doc.isNew;
|
|
7
|
+
const auditConfig = await AuditConfig.findOne({ collectionName });
|
|
8
|
+
if (!auditConfig) return next();
|
|
9
|
+
|
|
10
|
+
const logs = [];
|
|
11
|
+
|
|
12
|
+
if (isNewDoc && auditConfig.trackCreation) {
|
|
13
|
+
logs.push({
|
|
14
|
+
collectionName,
|
|
15
|
+
documentId: doc._id,
|
|
16
|
+
field: '__created__',
|
|
17
|
+
oldValue: null,
|
|
18
|
+
newValue: 'Document created',
|
|
19
|
+
createdBy: doc._updatedBy || null
|
|
20
|
+
});
|
|
21
|
+
} else if (!isNewDoc && auditConfig.fields?.length) {
|
|
22
|
+
const original = await doc.constructor.findById(doc._id).lean();
|
|
23
|
+
if (!original) return next();
|
|
24
|
+
|
|
25
|
+
for (const field of auditConfig.fields) {
|
|
26
|
+
if (doc.isModified(field)) {
|
|
27
|
+
logs.push({
|
|
28
|
+
collectionName,
|
|
29
|
+
documentId: doc._id,
|
|
30
|
+
field,
|
|
31
|
+
oldValue: original[field],
|
|
32
|
+
newValue: doc[field],
|
|
33
|
+
createdBy: doc._updatedBy || null
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (logs.length) await AuditLog.insertMany(logs);
|
|
40
|
+
next();
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
export default applyAuditMiddleware;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {default as applyAuditMiddleware} from './audit.middleware';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
|
|
3
|
+
const auditConfigSchema = new mongoose.Schema({
|
|
4
|
+
collectionName: String,
|
|
5
|
+
fields: [String],
|
|
6
|
+
trackCreation: { type: Boolean, default: false }
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
const AuditConfigModel = mongoose.model('AuditConfig', auditConfigSchema);
|
|
10
|
+
|
|
11
|
+
export default AuditConfigModel;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
const auditSchema = new mongoose.Schema({
|
|
3
|
+
collection: String,
|
|
4
|
+
recordId: mongoose.Schema.Types.ObjectId,
|
|
5
|
+
field: String,
|
|
6
|
+
oldValue: mongoose.Schema.Types.Mixed,
|
|
7
|
+
newValue: mongoose.Schema.Types.Mixed,
|
|
8
|
+
timestamp: { type: Date, default: Date.now },
|
|
9
|
+
createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const AuditLog = mongoose.model('AuditLog', auditSchema);
|
|
13
|
+
|
|
14
|
+
export default AuditLog;
|
package/models/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dynamatix/cat-shared",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
},
|
|
8
|
+
"author": "",
|
|
9
|
+
"license": "ISC",
|
|
10
|
+
"description": "",
|
|
11
|
+
"exports": {
|
|
12
|
+
"./models": "./models/index.js",
|
|
13
|
+
"./middlewares": "./middlewares/index.js"
|
|
14
|
+
}
|
|
15
|
+
}
|