fng-audit 0.12.0-beta.213 → 0.12.0-beta.214

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.
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ /// <reference path="../index.d.ts" />
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
4
  exports.plugin = exports.auditAdHocEvent = exports.getSnapshot = exports.getVersion = exports.getAuditTrail = exports.clean = exports.controller = exports.Audit = void 0;
4
5
  var jsondiffpatch = require("jsondiffpatch");
@@ -61,6 +62,7 @@ function controller(fng, processArgs, options) {
61
62
  _a[options.userRef] = [{
62
63
  resource: {
63
64
  resourceName: 'audit',
65
+ resourceNameLower: 'audit',
64
66
  model: exports.Audit
65
67
  },
66
68
  keys: ['user']
@@ -127,41 +129,40 @@ exports.clean = clean;
127
129
  function getAuditTrail(fng, modelName, id, qry, callback) {
128
130
  if (exports.Audit) {
129
131
  if (fng.getResource(modelName)) {
130
- exports.Audit.find(Object.assign(qry || {}, {
131
- c: modelName,
132
- cId: id
133
- })).sort({ _id: -1 }).exec(function (err, trail) {
134
- if (err) {
135
- callback(err);
136
- }
137
- else {
138
- async.map(trail, function (changeRec, mapCallback) {
139
- var retVal = {
140
- operation: changeRec.op,
141
- user: changeRec.user,
142
- changedAt: fng.extractTimestampFromMongoID(changeRec._id),
143
- oldVersion: changeRec.ver,
144
- };
145
- var changedValues = [];
146
- var changedFields = [];
147
- for (var key in changeRec.chg) {
148
- if (changeRec.chg.hasOwnProperty(key)) {
149
- changedFields.push(key);
150
- }
151
- }
152
- if (changedFields.length > 0) {
153
- retVal.comment = "modified " + changedFields.concat(changedValues).join(", ");
154
- retVal.chg = changeRec.chg;
155
- }
156
- else if (changeRec.op) {
157
- retVal.comment = changeRec.op;
158
- }
159
- else {
160
- retVal.comment = 'Audit entry';
132
+ exports.Audit
133
+ .find(Object.assign(qry || {}, { c: modelName, cId: id }))
134
+ .sort({ _id: -1 })
135
+ .exec()
136
+ .then(function (trail) {
137
+ async.map(trail, function (changeRec, mapCallback) {
138
+ var retVal = {
139
+ operation: changeRec.op,
140
+ user: changeRec.user,
141
+ changedAt: fng.extractTimestampFromMongoID(changeRec._id),
142
+ oldVersion: changeRec.ver,
143
+ };
144
+ var changedValues = [];
145
+ var changedFields = [];
146
+ for (var key in changeRec.chg) {
147
+ if (changeRec.chg.hasOwnProperty(key)) {
148
+ changedFields.push(key);
161
149
  }
162
- mapCallback(err, retVal);
163
- }, callback);
164
- }
150
+ }
151
+ if (changedFields.length > 0) {
152
+ retVal.comment = "modified " + changedFields.concat(changedValues).join(", ");
153
+ retVal.chg = changeRec.chg;
154
+ }
155
+ else if (changeRec.op) {
156
+ retVal.comment = changeRec.op;
157
+ }
158
+ else {
159
+ retVal.comment = 'Audit entry';
160
+ }
161
+ mapCallback(null, retVal);
162
+ }, callback);
163
+ })
164
+ .catch(function (err) {
165
+ callback(err);
165
166
  });
166
167
  }
167
168
  else {
@@ -176,16 +177,13 @@ function getAuditTrail(fng, modelName, id, qry, callback) {
176
177
  exports.getAuditTrail = getAuditTrail;
177
178
  function getRevision(model, id, revisionCrit, doCleaning, callback) {
178
179
  if (exports.Audit) {
179
- model.findOne({ _id: id }, function (err, latest) {
180
- if (err) {
181
- return callback(err, null);
182
- }
180
+ model.findOne({ _id: id })
181
+ .then(function (latest) {
183
182
  var criteria = { $and: [{ c: model.modelName }, { cId: id }, { chg: { $exists: true } }, revisionCrit] };
184
- exports.Audit.find(criteria, { ver: 1, chg: 1 }, { sort: "-ver" }, function (err, histories) {
185
- if (err) {
186
- console.error(err);
187
- return callback(err, null);
188
- }
183
+ var projection = { ver: 1, chg: 1 };
184
+ exports.Audit.find(criteria, projection)
185
+ .sort("-ver")
186
+ .then(function (histories) {
189
187
  var object = latest ? latest.toObject() : { _id: new Mongoose.Types.ObjectId(id) };
190
188
  async.each(histories, function (history, eachCallback) {
191
189
  try {
@@ -202,7 +200,14 @@ function getRevision(model, id, revisionCrit, doCleaning, callback) {
202
200
  }
203
201
  callback(null, doCleaning ? clean(object) : object);
204
202
  });
203
+ })
204
+ .catch(function (err) {
205
+ console.error(err);
206
+ return callback(err, null);
205
207
  });
208
+ })
209
+ .catch(function (err) {
210
+ return callback(err, null);
206
211
  });
207
212
  }
208
213
  else {
@@ -283,14 +288,15 @@ function auditFromObject(doc, orig, updated, options, next) {
283
288
  if (chg_1) {
284
289
  var c_1 = doc.constructor.modelName;
285
290
  var cId_1 = doc._id;
286
- exports.Audit.findOne({
291
+ var criteria = {
287
292
  c: c_1,
288
293
  cId: cId_1,
289
294
  ver: { $exists: true }
290
- }).sort("-ver").exec(function (err, prevAudit) {
291
- if (err) {
292
- return next(err);
293
- }
295
+ };
296
+ exports.Audit.findOne(criteria)
297
+ .sort("-ver")
298
+ .exec()
299
+ .then(function (prevAudit) {
294
300
  var auditRec = {
295
301
  c: c_1,
296
302
  cId: cId_1,
@@ -303,12 +309,21 @@ function auditFromObject(doc, orig, updated, options, next) {
303
309
  if (op_1) {
304
310
  auditRec.op = op_1;
305
311
  }
306
- exports.Audit.create(auditRec, function (err) {
312
+ exports.Audit.create(auditRec)
313
+ .then(function () {
314
+ // nothing to do
315
+ })
316
+ .catch(function (err) {
307
317
  if (err) {
308
318
  console.log("Error creating audit object: ".concat(err.message));
309
319
  }
320
+ })
321
+ .finally(function () {
310
322
  next();
311
323
  });
324
+ })
325
+ .catch(function (err) {
326
+ return next(err);
312
327
  });
313
328
  }
314
329
  else {
@@ -353,72 +368,71 @@ function auditFromUpdate(docUpdate, options, next) {
353
368
  else {
354
369
  var queryObject_1 = typeof docUpdate.clone === "function" ? docUpdate.clone() : docUpdate;
355
370
  var queryOp_1 = queryObject_1.op;
356
- queryObject_1.find(queryObject_1._conditions, function (err, results) {
357
- if (err) {
358
- return next(err);
359
- }
360
- else {
361
- var original_1;
362
- var updated_1;
363
- async.eachSeries(results, function (currentObject, callback) {
364
- updated_1 = {};
365
- if (['findOneAndRemove', 'deleteOne', 'findOneAndDelete'].includes(queryOp_1)) {
366
- original_1 = currentObject;
371
+ queryObject_1.find(queryObject_1._conditions)
372
+ .then(function (results) {
373
+ var original;
374
+ var updated;
375
+ async.eachSeries(results, function (currentObject, callback) {
376
+ updated = {};
377
+ if (['findOneAndRemove', 'deleteOne', 'findOneAndDelete'].includes(queryOp_1)) {
378
+ original = currentObject;
379
+ }
380
+ else {
381
+ original = {};
382
+ if (queryObject_1.options && queryObject_1.options._user || queryObject_1.options.__usr) {
383
+ original.__user = queryObject_1.options._user || queryObject_1.options.__usr;
367
384
  }
368
- else {
369
- original_1 = {};
370
- if (queryObject_1.options && queryObject_1.options._user || queryObject_1.options.__usr) {
371
- original_1.__user = queryObject_1.options._user || queryObject_1.options.__usr;
372
- }
373
- if (queryObject_1.options && queryObject_1.options._op) {
374
- original_1.__op = queryObject_1.options._op;
375
- }
376
- if (queryObject_1._update) {
377
- Object.keys(queryObject_1._update).forEach(function (key) {
378
- if (key[0] === '$') {
379
- Object.keys(queryObject_1._update[key]).forEach(function (attrib) {
380
- if (key === '$set') {
381
- assignPossiblyNested(updated_1, queryObject_1._update.$set[attrib], attrib);
382
- }
383
- assignPossiblyNested(original_1, currentObject, attrib);
384
- });
385
- switch (key) {
386
- case '$set':
387
- case '$unset':
388
- // ignore $set - already dealt with
389
- break;
390
- case '$push':
391
- Object.keys(queryObject_1._update[key]).forEach(function (attrib) {
392
- updated_1[attrib] = [];
393
- Object.assign(updated_1[attrib], original_1[attrib]);
394
- updated_1[attrib].push(queryObject_1._update[key][attrib]);
395
- });
396
- break;
397
- default:
398
- var errMessage = 'No audit trail support for ' + key;
399
- if (auditOptions.errorHandler) {
400
- auditOptions.errorHandler(errMessage);
401
- }
402
- else {
403
- console.error(errMessage);
404
- }
405
- break;
385
+ if (queryObject_1.options && queryObject_1.options._op) {
386
+ original.__op = queryObject_1.options._op;
387
+ }
388
+ if (queryObject_1._update) {
389
+ Object.keys(queryObject_1._update).forEach(function (key) {
390
+ if (key[0] === '$') {
391
+ Object.keys(queryObject_1._update[key]).forEach(function (attrib) {
392
+ if (key === '$set') {
393
+ assignPossiblyNested(updated, queryObject_1._update.$set[attrib], attrib);
406
394
  }
395
+ assignPossiblyNested(original, currentObject, attrib);
396
+ });
397
+ switch (key) {
398
+ case '$set':
399
+ case '$unset':
400
+ // ignore $set - already dealt with
401
+ break;
402
+ case '$push':
403
+ Object.keys(queryObject_1._update[key]).forEach(function (attrib) {
404
+ updated[attrib] = [];
405
+ Object.assign(updated[attrib], original[attrib]);
406
+ updated[attrib].push(queryObject_1._update[key][attrib]);
407
+ });
408
+ break;
409
+ default:
410
+ var errMessage = 'No audit trail support for ' + key;
411
+ if (auditOptions.errorHandler) {
412
+ auditOptions.errorHandler(errMessage);
413
+ }
414
+ else {
415
+ console.error(errMessage);
416
+ }
417
+ break;
407
418
  }
408
- else {
409
- // an assignment
410
- assignPossiblyNested(original_1, currentObject, key);
411
- }
412
- });
413
- }
419
+ }
420
+ else {
421
+ // an assignment
422
+ assignPossiblyNested(original, currentObject, key);
423
+ }
424
+ });
414
425
  }
415
- auditFromObject(currentObject, original_1, updated_1, options, function () {
416
- callback();
417
- });
418
- }, function done() {
419
- return next();
426
+ }
427
+ auditFromObject(currentObject, original, updated, options, function () {
428
+ callback();
420
429
  });
421
- }
430
+ }, function done() {
431
+ return next();
432
+ });
433
+ })
434
+ .catch(function (err) {
435
+ return next(err);
422
436
  });
423
437
  }
424
438
  }
@@ -450,10 +464,14 @@ function plugin(schema, options) {
450
464
  op: 'create',
451
465
  user: user
452
466
  };
453
- exports.Audit.create(auditRec, function (err) {
454
- if (err) {
455
- console.log("Error creating audit object: ".concat(err.message));
456
- }
467
+ exports.Audit.create(auditRec)
468
+ .then(function () {
469
+ // nothing to do
470
+ })
471
+ .catch(function (err) {
472
+ console.log("Error creating audit object: ".concat(err.message));
473
+ })
474
+ .finally(function () {
457
475
  next();
458
476
  });
459
477
  }
@@ -465,8 +483,15 @@ function plugin(schema, options) {
465
483
  var that_1 = this;
466
484
  try {
467
485
  getHiddenFields(that_1.constructor.collection.collectionName, options);
468
- that_1.constructor.findOne({ _id: that_1._id }, function (err, original) {
486
+ that_1.constructor.findOne({ _id: that_1._id })
487
+ .then(function (original) {
469
488
  auditFromObject(that_1, original, that_1, options, next);
489
+ })
490
+ .catch(function (err) {
491
+ if (auditOptions.errorHandler) {
492
+ auditOptions.errorHandler(err.message);
493
+ }
494
+ next();
470
495
  });
471
496
  }
472
497
  catch (e) {
@@ -517,6 +542,26 @@ function plugin(schema, options) {
517
542
  }
518
543
  };
519
544
  }
545
+ schema.methods.saveNoAudit = function (options) {
546
+ var _this = this;
547
+ this._noAudit = true;
548
+ return this.save(options).then(function () {
549
+ _this._noAudit = false;
550
+ return _this;
551
+ });
552
+ };
553
+ schema.methods.deleteNoAudit = function () {
554
+ this._noAudit = true;
555
+ if (this.deleteOne) {
556
+ return this.deleteOne();
557
+ }
558
+ else if (this.remove) {
559
+ return this.remove();
560
+ }
561
+ else {
562
+ throw new Error("Don't know how to delete a document with this version of Mongoose");
563
+ }
564
+ };
520
565
  schema.pre("findOneAndUpdate", doUpdateHandling());
521
566
  schema.pre("update", doUpdateHandling());
522
567
  schema.pre("updateOne", doUpdateHandling());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fng-audit",
3
- "version": "0.12.0-beta.213",
3
+ "version": "0.12.0-beta.214",
4
4
  "description": "Client part of forms-angular audit plugin",
5
5
  "homepage": "https://www.forms-angular.org/#/plugins",
6
6
  "files": [
@@ -26,7 +26,7 @@
26
26
  "@types/mocha": "10.0.1",
27
27
  "angular": "^1.8.3",
28
28
  "chai": "4.3.7",
29
- "forms-angular": "0.12.0-beta.213",
29
+ "forms-angular": "0.12.0-beta.214",
30
30
  "grunt": "^1.5.3",
31
31
  "grunt-angular-templates": "^1.2.0",
32
32
  "grunt-contrib-concat": "2.1.0",
@@ -34,7 +34,7 @@
34
34
  "grunt-contrib-uglify": "5.2.2",
35
35
  "grunt-legacy-log": "3.0.0",
36
36
  "mocha": "^10.2.0",
37
- "mongoose": "6.4.6"
37
+ "mongoose": "^7.2.1"
38
38
  },
39
39
  "license": "MIT",
40
40
  "authors": [