@dynamatix/cat-shared 0.0.87 → 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.
@@ -209,6 +209,39 @@ function applyAuditMiddleware(schema, collectionName) {
209
209
  }
210
210
  }
211
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
+ });
212
245
  }
213
246
 
214
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.87",
3
+ "version": "0.0.88",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"