@dynamatix/cat-shared 0.0.72 → 0.0.74
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.
|
@@ -45,7 +45,7 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
45
45
|
|
|
46
46
|
const log = {
|
|
47
47
|
name: `${collectionName} created`,
|
|
48
|
-
recordId: doc._id,
|
|
48
|
+
recordId: contextId || doc._id,
|
|
49
49
|
oldValue: null,
|
|
50
50
|
newValue: 'Document created',
|
|
51
51
|
createdBy: userId,
|
|
@@ -53,6 +53,7 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
53
53
|
};
|
|
54
54
|
if (auditConfig.descriptionResolutorForExternalData) {
|
|
55
55
|
log.externalData.description = await resolveExternalData(auditConfig.descriptionResolutorForExternalData, doc[descriptionResolutorForExternalData])
|
|
56
|
+
log.externalData.contextId = contextId;
|
|
56
57
|
}
|
|
57
58
|
await AuditLog.create(log);
|
|
58
59
|
|
|
@@ -69,7 +70,7 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
69
70
|
|
|
70
71
|
// Handle update audits
|
|
71
72
|
schema.post(['findOneAndUpdate', 'findByIdAndUpdate'], async function (result) {
|
|
72
|
-
if (!result
|
|
73
|
+
if (!result) return;
|
|
73
74
|
|
|
74
75
|
const auditConfig = await AuditConfigModel.findOne({ collectionName });
|
|
75
76
|
if (!auditConfig?.fields?.length) return;
|
|
@@ -86,7 +87,7 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
86
87
|
|
|
87
88
|
if (hasChanged) {
|
|
88
89
|
const newValue = update?.$set?.[field] ?? update?.[field];
|
|
89
|
-
const oldValue = this._originalDoc[field];
|
|
90
|
+
const oldValue = this._originalDoc ? this._originalDoc[field] : '';
|
|
90
91
|
|
|
91
92
|
if (oldValue !== newValue) {
|
|
92
93
|
// Resolve human-readable values
|
|
@@ -96,14 +97,14 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
96
97
|
let externalData={};
|
|
97
98
|
if (auditConfig.descriptionResolutorForExternalData) {
|
|
98
99
|
externalData.description = await resolveExternalData(auditConfig.descriptionResolutorForExternalData, result[auditConfig.descriptionResolutorForExternalData])
|
|
100
|
+
externalData.contextId = contextId || result._id;
|
|
99
101
|
}
|
|
100
102
|
logs.push({
|
|
101
103
|
name: `${collectionName}.${field}`,
|
|
102
|
-
recordId: result._id,
|
|
104
|
+
recordId: contextId || result._id,
|
|
103
105
|
oldValue: resolvedOld,
|
|
104
106
|
newValue: resolvedNew,
|
|
105
107
|
createdBy: userId,
|
|
106
|
-
contextId,
|
|
107
108
|
externalData
|
|
108
109
|
});
|
|
109
110
|
}
|