@dynamatix/cat-shared 0.0.73 → 0.0.75

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.
@@ -56,6 +56,7 @@ function applyAuditMiddleware(schema, collectionName) {
56
56
  log.externalData.contextId = contextId;
57
57
  }
58
58
  await AuditLog.create(log);
59
+ await updateContextAuditCount(contextId, 1);
59
60
 
60
61
  if (onAuditLogCreated) {
61
62
  await onAuditLogCreated([log], contextId);
@@ -70,7 +71,7 @@ function applyAuditMiddleware(schema, collectionName) {
70
71
 
71
72
  // Handle update audits
72
73
  schema.post(['findOneAndUpdate', 'findByIdAndUpdate'], async function (result) {
73
- if (!result || !this._originalDoc) return;
74
+ if (!result) return;
74
75
 
75
76
  const auditConfig = await AuditConfigModel.findOne({ collectionName });
76
77
  if (!auditConfig?.fields?.length) return;
@@ -87,14 +88,14 @@ function applyAuditMiddleware(schema, collectionName) {
87
88
 
88
89
  if (hasChanged) {
89
90
  const newValue = update?.$set?.[field] ?? update?.[field];
90
- const oldValue = this._originalDoc[field];
91
+ const oldValue = this._originalDoc ? this._originalDoc[field] : '';
91
92
 
92
93
  if (oldValue !== newValue) {
93
94
  // Resolve human-readable values
94
95
  const { oldValue: resolvedOld, newValue: resolvedNew } =
95
96
  await resolveAuditValues(field, oldValue, newValue);
96
97
 
97
- let externalData={};
98
+ let externalData = {};
98
99
  if (auditConfig.descriptionResolutorForExternalData) {
99
100
  externalData.description = await resolveExternalData(auditConfig.descriptionResolutorForExternalData, result[auditConfig.descriptionResolutorForExternalData])
100
101
  externalData.contextId = contextId || result._id;
@@ -113,6 +114,7 @@ function applyAuditMiddleware(schema, collectionName) {
113
114
 
114
115
  if (logs.length) {
115
116
  await AuditLog.insertMany(logs);
117
+ await updateContextAuditCount(contextId, logs.length);
116
118
  if (onAuditLogCreated) {
117
119
  await onAuditLogCreated(logs, contextId);
118
120
  }
@@ -120,6 +122,14 @@ function applyAuditMiddleware(schema, collectionName) {
120
122
  });
121
123
  }
122
124
 
125
+ async function updateContextAuditCount(contextId, count) {
126
+ if(!contextId) return;
127
+ const model = mongoose.models['Application'];
128
+ const context = await model.findById(contextId);
129
+ context.newAuditRecordsCount = (context.newAuditRecordsCount || 0) + (count ? count : 1);
130
+ await context.save();
131
+ }
132
+
123
133
  export function registerAuditHook(callback) {
124
134
  onAuditLogCreated = callback;
125
135
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/cat-shared",
3
- "version": "0.0.73",
3
+ "version": "0.0.75",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"