@dynamatix/cat-shared 0.0.86 → 0.0.88

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.
@@ -128,21 +128,6 @@ function applyAuditMiddleware(schema, collectionName) {
128
128
  }
129
129
  }
130
130
 
131
- // Overall document creation log (optional)
132
- logs.push({
133
- name: 'created',
134
- entity: entityDescription,
135
- recordId: contextId || doc._id,
136
- oldValue: null,
137
- newValue: 'Document created',
138
- createdBy: userId,
139
- contextId,
140
- externalData: {
141
- description: entityDescription,
142
- contextId
143
- }
144
- });
145
-
146
131
  if (logs.length) {
147
132
  await AuditLog.insertMany(logs);
148
133
  await updateContextAuditCount(contextId, logs.length);
@@ -224,6 +209,39 @@ function applyAuditMiddleware(schema, collectionName) {
224
209
  }
225
210
  }
226
211
  });
212
+
213
+ // Handle delete audits
214
+ schema.post(['findOneAndDelete', 'findByIdAndDelete', 'deleteOne', 'deleteMany'], async function (result) {
215
+ if (!result) return;
216
+
217
+ const auditConfig = await AuditConfigModel.findOne({ collectionName });
218
+ if (!auditConfig?.trackDeletion) return;
219
+
220
+ const context = getContext();
221
+ const userId = context?.userId || 'anonymous';
222
+ const contextId = context?.contextId;
223
+
224
+ const entityDescription = await resolveEntityDescription(result, auditConfig);
225
+
226
+ const log = {
227
+ name: 'Entity Deletion',
228
+ entity: entityDescription,
229
+ recordId: contextId || result._id,
230
+ oldValue: '',
231
+ newValue: 'Deleted',
232
+ createdBy: userId,
233
+ externalData: {
234
+ description: entityDescription,
235
+ contextId: contextId || result._id
236
+ }
237
+ };
238
+
239
+ await AuditLog.create(log);
240
+ await updateContextAuditCount(contextId, 1);
241
+ if (onAuditLogCreated) {
242
+ await onAuditLogCreated([log], contextId);
243
+ }
244
+ });
227
245
  }
228
246
 
229
247
  async function updateContextAuditCount(contextId, count) {
@@ -2,8 +2,9 @@ import mongoose from 'mongoose';
2
2
 
3
3
  const auditConfigSchema = new mongoose.Schema({
4
4
  collectionName: String,
5
- fields: [String],
5
+ fields: [String],
6
6
  trackCreation: { type: Boolean, default: false },
7
+ trackDeletion: { type: Boolean, default: false },
7
8
  descriptionResolutorForExternalData: { type: String, default: null },
8
9
  });
9
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/cat-shared",
3
- "version": "0.0.86",
3
+ "version": "0.0.88",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"