@dynamatix/cat-shared 0.0.9 → 0.0.11

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.
@@ -1,8 +1,9 @@
1
1
  import AuditConfigModel from '../models/audit-config.model.js';
2
-
2
+ import AuditLog from '../models/audit.model.js';
3
3
 
4
4
  function applyAuditMiddleware(schema, collectionName) {
5
- schema.pre(['save','updateOne'], async function (next) {
5
+ // Handle create and save
6
+ schema.pre('save', async function (next) {
6
7
  const doc = this;
7
8
  const isNewDoc = doc.isNew;
8
9
  const auditConfig = await AuditConfigModel.findOne({ collectionName });
@@ -12,24 +13,42 @@ function applyAuditMiddleware(schema, collectionName) {
12
13
 
13
14
  if (isNewDoc && auditConfig.trackCreation) {
14
15
  logs.push({
15
- name: collectionName + ' created',
16
+ name: `${collectionName} created`,
16
17
  documentId: doc._id,
17
18
  oldValue: null,
18
19
  newValue: 'Document created',
19
20
  createdBy: doc._updatedBy || null
20
21
  });
21
- } else if (!isNewDoc && auditConfig.fields?.length) {
22
- const original = await doc.constructor.findById(doc._id).lean();
23
- if (!original) return next();
22
+ }
24
23
 
25
- for (const field of auditConfig.fields) {
26
- if (doc.isModified(field)) {
24
+ if (logs.length) await AuditLog.insertMany(logs);
25
+ next();
26
+ });
27
+
28
+ // Handle update queries like findByIdAndUpdate
29
+ schema.pre(['findOneAndUpdate', 'findByIdAndUpdate'], async function (next) {
30
+ const query = this;
31
+ const update = query.getUpdate();
32
+ const auditConfig = await AuditConfigModel.findOne({ collectionName });
33
+ if (!auditConfig || !auditConfig.fields?.length) return next();
34
+
35
+ const original = await query.model.findOne(query.getQuery()).lean();
36
+ if (!original) return next();
37
+
38
+ const logs = [];
39
+
40
+ for (const field of auditConfig.fields) {
41
+ if (update?.$set?.hasOwnProperty(field) || update?.hasOwnProperty(field)) {
42
+ const newValue = update?.$set?.[field] ?? update?.[field];
43
+ const oldValue = original[field];
44
+
45
+ if (oldValue !== newValue) {
27
46
  logs.push({
28
- name: collectionName + '.'+ field,
29
- documentId: doc._id,
30
- oldValue: original[field],
31
- newValue: doc[field],
32
- createdBy: doc._updatedBy || null
47
+ name: `${collectionName}.${field}`,
48
+ documentId: original._id,
49
+ oldValue,
50
+ newValue,
51
+ createdBy: update._updatedBy || null
33
52
  });
34
53
  }
35
54
  }
@@ -40,5 +59,4 @@ function applyAuditMiddleware(schema, collectionName) {
40
59
  });
41
60
  }
42
61
 
43
-
44
62
  export default applyAuditMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/cat-shared",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"