@dynamatix/cat-shared 0.0.89 → 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.
@@ -87,6 +87,13 @@ async function resolveEntityDescription(doc, auditConfig) {
87
87
  return value;
88
88
  }
89
89
 
90
+ // Add new function to resolve deletion description
91
+ async function resolveDeletionDescription(doc, auditConfig) {
92
+ if (!auditConfig.descriptionResolutorForDeletion) return '';
93
+ const expression = auditConfig.descriptionResolutorForDeletion;
94
+ return await resolveExpressionDescription(doc, expression, 'direct');
95
+ }
96
+
90
97
  function applyAuditMiddleware(schema, collectionName) {
91
98
  // Handle creation audit
92
99
  schema.post('save', async function (doc) {
@@ -212,28 +219,39 @@ function applyAuditMiddleware(schema, collectionName) {
212
219
  });
213
220
 
214
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
+
215
228
  schema.post(['findOneAndDelete', 'findByIdAndDelete', 'deleteOne', 'deleteMany'], async function (result) {
216
229
  if (!result) return;
217
230
 
218
231
  const auditConfig = await AuditConfigModel.findOne({ collectionName });
219
232
  if (!auditConfig?.trackDeletion) return;
220
233
 
234
+ // Fetch the document before it was deleted
235
+ const deletedDoc = await this.model.findOne(this._deleteQuery).lean();
236
+ if (!deletedDoc) return;
237
+
221
238
  const context = getContext();
222
239
  const userId = context?.userId || 'anonymous';
223
240
  const contextId = context?.contextId;
224
241
 
225
- const entityDescription = await resolveEntityDescription(result, auditConfig);
242
+ const entityDescription = await resolveEntityDescription(deletedDoc, auditConfig);
243
+ const deletionDescription = await resolveDeletionDescription(deletedDoc, auditConfig);
226
244
 
227
245
  const log = {
228
- name: 'Entity Deletion',
246
+ name: deletionDescription || 'Entity Deletion',
229
247
  entity: entityDescription,
230
- recordId: contextId || result._id,
248
+ recordId: contextId || deletedDoc._id,
231
249
  oldValue: '',
232
250
  newValue: 'Deleted',
233
251
  createdBy: userId,
234
252
  externalData: {
235
253
  description: entityDescription,
236
- contextId: contextId || result._id
254
+ contextId: contextId || deletedDoc._id
237
255
  }
238
256
  };
239
257
 
@@ -6,6 +6,7 @@ const auditConfigSchema = new mongoose.Schema({
6
6
  trackCreation: { type: Boolean, default: false },
7
7
  trackDeletion: { type: Boolean, default: false },
8
8
  descriptionResolutorForExternalData: { type: String, default: null },
9
+ descriptionResolutorForDeletion: { type: String, default: null },
9
10
  });
10
11
 
11
12
  const AuditConfigModel = mongoose.models.AuditConfig || mongoose.model('AuditConfig', auditConfigSchema);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/cat-shared",
3
- "version": "0.0.89",
3
+ "version": "0.0.91",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"