@dynamatix/cat-shared 0.0.67 → 0.0.69

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.
@@ -25,6 +25,14 @@ async function resolveAuditValues(field, oldValue, newValue) {
25
25
  };
26
26
  }
27
27
 
28
+ async function resolveExternalData(descriptionResolutorForExternalData, recordId) {
29
+ const map = await ValueReferenceMap.findOne({ field: descriptionResolutorForExternalData });
30
+ const Model = mongoose.models[map.model];
31
+ if (!Model) return "";
32
+ const doc = Model.findById(recordId).lean()
33
+ return doc[descriptionResolutorForExternalData];
34
+ }
35
+
28
36
  function applyAuditMiddleware(schema, collectionName) {
29
37
  // Handle creation audit
30
38
  schema.post('save', async function (doc) {
@@ -43,7 +51,9 @@ function applyAuditMiddleware(schema, collectionName) {
43
51
  createdBy: userId,
44
52
  contextId
45
53
  };
46
-
54
+ if (auditConfig.descriptionResolutorForExternalData) {
55
+ log.externalData.description = resolveExternalData(auditConfig.descriptionResolutorForExternalData, doc[descriptionResolutorForExternalData])
56
+ }
47
57
  await AuditLog.create(log);
48
58
 
49
59
  if (onAuditLogCreated) {
@@ -83,13 +93,18 @@ function applyAuditMiddleware(schema, collectionName) {
83
93
  const { oldValue: resolvedOld, newValue: resolvedNew } =
84
94
  await resolveAuditValues(field, oldValue, newValue);
85
95
 
96
+ let externalData={};
97
+ if (auditConfig.descriptionResolutorForExternalData) {
98
+ externalData.description = resolveExternalData(auditConfig.descriptionResolutorForExternalData, doc[descriptionResolutorForExternalData])
99
+ }
86
100
  logs.push({
87
101
  name: `${collectionName}.${field}`,
88
102
  recordId: result._id,
89
103
  oldValue: resolvedOld,
90
104
  newValue: resolvedNew,
91
105
  createdBy: userId,
92
- contextId
106
+ contextId,
107
+ externalData
93
108
  });
94
109
  }
95
110
  }
@@ -7,6 +7,7 @@ const auditConfigSchema = new mongoose.Schema({
7
7
  descriptionResolutorForExternalData: { type: String, default: null },
8
8
  });
9
9
 
10
- const AuditConfigModel = mongoose.model('AuditConfig', auditConfigSchema);
10
+ const AuditConfigModel = mongoose.models.AuditConfig || mongoose.model('AuditConfig', auditConfigSchema);
11
+
11
12
 
12
13
  export default AuditConfigModel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/cat-shared",
3
- "version": "0.0.67",
3
+ "version": "0.0.69",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"