@dynamatix/cat-shared 0.0.87 → 0.0.89

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.
@@ -103,6 +103,7 @@ function applyAuditMiddleware(schema, collectionName) {
103
103
  // Per-field logs
104
104
  if (auditConfig.fields?.length) {
105
105
  for (const field of auditConfig.fields) {
106
+ let lookupName;
106
107
  const newValue = doc[field];
107
108
  if (field.endsWith('Lid')) {
108
109
  const newlookupDoc = await mongoose.models['Lookup'].findById(newValue).lean();
@@ -209,6 +210,39 @@ function applyAuditMiddleware(schema, collectionName) {
209
210
  }
210
211
  }
211
212
  });
213
+
214
+ // Handle delete audits
215
+ schema.post(['findOneAndDelete', 'findByIdAndDelete', 'deleteOne', 'deleteMany'], async function (result) {
216
+ if (!result) return;
217
+
218
+ const auditConfig = await AuditConfigModel.findOne({ collectionName });
219
+ if (!auditConfig?.trackDeletion) return;
220
+
221
+ const context = getContext();
222
+ const userId = context?.userId || 'anonymous';
223
+ const contextId = context?.contextId;
224
+
225
+ const entityDescription = await resolveEntityDescription(result, auditConfig);
226
+
227
+ const log = {
228
+ name: 'Entity Deletion',
229
+ entity: entityDescription,
230
+ recordId: contextId || result._id,
231
+ oldValue: '',
232
+ newValue: 'Deleted',
233
+ createdBy: userId,
234
+ externalData: {
235
+ description: entityDescription,
236
+ contextId: contextId || result._id
237
+ }
238
+ };
239
+
240
+ await AuditLog.create(log);
241
+ await updateContextAuditCount(contextId, 1);
242
+ if (onAuditLogCreated) {
243
+ await onAuditLogCreated([log], contextId);
244
+ }
245
+ });
212
246
  }
213
247
 
214
248
  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.89",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"