@dynamatix/cat-shared 0.0.90 → 0.0.91

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.
@@ -219,29 +219,39 @@ function applyAuditMiddleware(schema, collectionName) {
219
219
  });
220
220
 
221
221
  // Handle delete audits
222
+ schema.pre(['findOneAndDelete', 'findByIdAndDelete', 'deleteOne', 'deleteMany'], async function (next) {
223
+ // Store the query to fetch document before deletion
224
+ this._deleteQuery = this.getQuery();
225
+ next();
226
+ });
227
+
222
228
  schema.post(['findOneAndDelete', 'findByIdAndDelete', 'deleteOne', 'deleteMany'], async function (result) {
223
229
  if (!result) return;
224
230
 
225
231
  const auditConfig = await AuditConfigModel.findOne({ collectionName });
226
232
  if (!auditConfig?.trackDeletion) return;
227
233
 
234
+ // Fetch the document before it was deleted
235
+ const deletedDoc = await this.model.findOne(this._deleteQuery).lean();
236
+ if (!deletedDoc) return;
237
+
228
238
  const context = getContext();
229
239
  const userId = context?.userId || 'anonymous';
230
240
  const contextId = context?.contextId;
231
241
 
232
- const entityDescription = await resolveEntityDescription(result, auditConfig);
233
- const deletionDescription = await resolveDeletionDescription(result, auditConfig);
242
+ const entityDescription = await resolveEntityDescription(deletedDoc, auditConfig);
243
+ const deletionDescription = await resolveDeletionDescription(deletedDoc, auditConfig);
234
244
 
235
245
  const log = {
236
246
  name: deletionDescription || 'Entity Deletion',
237
247
  entity: entityDescription,
238
- recordId: contextId || result._id,
248
+ recordId: contextId || deletedDoc._id,
239
249
  oldValue: '',
240
250
  newValue: 'Deleted',
241
251
  createdBy: userId,
242
252
  externalData: {
243
253
  description: entityDescription,
244
- contextId: contextId || result._id
254
+ contextId: contextId || deletedDoc._id
245
255
  }
246
256
  };
247
257
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/cat-shared",
3
- "version": "0.0.90",
3
+ "version": "0.0.91",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"