@steedos/objectql 2.6.10-beta.9 → 2.7.0-beta.10

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.
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMetadata = void 0;
4
+ var SERVICE_NAME = 'metadata-cachers-service';
5
+ var getMetadata = function (metadataName) {
6
+ return {
7
+ find: function (filters, spaceId) {
8
+ return broker.call("".concat(SERVICE_NAME, ".find"), { metadataName: metadataName, filters: filters, spaceId: spaceId });
9
+ },
10
+ get: function (_id) {
11
+ return broker.call("".concat(SERVICE_NAME, ".get"), { _id: _id });
12
+ }
13
+ };
14
+ };
15
+ exports.getMetadata = getMetadata;
16
+ //# sourceMappingURL=metadata.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"metadata.js","sourceRoot":"","sources":["../../src/types/metadata.ts"],"names":[],"mappings":";;;AAOA,IAAM,YAAY,GAAG,0BAA0B,CAAA;AACxC,IAAM,WAAW,GAAG,UAAC,YAAY;IACpC,OAAO;QACH,IAAI,EAAE,UAAC,OAAO,EAAE,OAAQ;YACpB,OAAO,MAAM,CAAC,IAAI,CAAC,UAAG,YAAY,UAAO,EAAE,EAAC,YAAY,cAAA,EAAE,OAAO,SAAA,EAAE,OAAO,SAAA,EAAC,CAAC,CAAA;QAChF,CAAC;QACD,GAAG,EAAE,UAAC,GAAG;YACL,OAAO,MAAM,CAAC,IAAI,CAAC,UAAG,YAAY,SAAM,EAAE,EAAC,GAAG,KAAA,EAAC,CAAC,CAAC;QACrD,CAAC;KACJ,CAAA;AACL,CAAC,CAAA;AATY,QAAA,WAAW,eASvB"}
@@ -0,0 +1,4 @@
1
+ import { Dictionary } from "@salesforce/ts-types";
2
+ import { SteedosUserSession } from ".";
3
+ export declare function getMongoInsertBaseDoc(object: any, doc: Dictionary<any>, userSession?: SteedosUserSession): Promise<Dictionary<any>>;
4
+ export declare function getMongoUpdateBaseDoc(object: any, doc: Dictionary<any>, userSession?: SteedosUserSession): Promise<Dictionary<any>>;
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMongoUpdateBaseDoc = exports.getMongoInsertBaseDoc = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var _ = require("lodash");
6
+ var checkCompany = function (object, doc, userSession) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
7
+ var userObjectPermission, error_1, allowCompanyIds;
8
+ return tslib_1.__generator(this, function (_a) {
9
+ switch (_a.label) {
10
+ case 0:
11
+ if (!userSession || userSession.is_space_admin) {
12
+ return [2];
13
+ }
14
+ userObjectPermission = null;
15
+ _a.label = 1;
16
+ case 1:
17
+ _a.trys.push([1, 3, , 4]);
18
+ return [4, object.getUserObjectPermission(userSession)];
19
+ case 2:
20
+ userObjectPermission = _a.sent();
21
+ return [3, 4];
22
+ case 3:
23
+ error_1 = _a.sent();
24
+ return [2];
25
+ case 4:
26
+ if (userObjectPermission.modifyAllRecords) {
27
+ return [2, true];
28
+ }
29
+ allowCompanyIds = [];
30
+ if (!_.isEmpty(userSession.company_ids)) {
31
+ allowCompanyIds.push.apply(allowCompanyIds, tslib_1.__spreadArray([], tslib_1.__read(userSession.company_ids), false));
32
+ }
33
+ if (!_.isEmpty(userObjectPermission.modifyAssignCompanysRecords)) {
34
+ allowCompanyIds.push.apply(allowCompanyIds, tslib_1.__spreadArray([], tslib_1.__read(userObjectPermission.modifyAssignCompanysRecords), false));
35
+ }
36
+ if (_.has(doc, "company_id")) {
37
+ if (!_.includes(allowCompanyIds, doc.company_id)) {
38
+ throw new Error("\u672A\u83B7\u5F97\u5206\u90E8\u6388\u6743");
39
+ }
40
+ }
41
+ if (_.has(doc, "company_ids")) {
42
+ if (_.difference(doc.company_ids, allowCompanyIds).length > 0) {
43
+ throw new Error("\u672A\u83B7\u5F97\u5206\u90E8\u6388\u6743");
44
+ }
45
+ }
46
+ return [2];
47
+ }
48
+ });
49
+ }); };
50
+ function getMongoInsertBaseDoc(object, doc, userSession) {
51
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
52
+ var _a, userId, company_id, extras;
53
+ return tslib_1.__generator(this, function (_b) {
54
+ switch (_b.label) {
55
+ case 0:
56
+ _a = userSession || {}, userId = _a.userId, company_id = _a.company_id;
57
+ doc.created = new Date();
58
+ doc.modified = new Date();
59
+ if (userId) {
60
+ if (!doc.owner) {
61
+ doc.owner = userId;
62
+ }
63
+ if (doc.owner === '{userId}') {
64
+ doc.owner = userId;
65
+ }
66
+ doc.created_by = userId;
67
+ doc.modified_by = userId;
68
+ }
69
+ extras = ["spaces", "company", "organizations", "users", "space_users"];
70
+ if (!(extras.indexOf(object.name) < 0 && doc.space)) return [3, 2];
71
+ if (!doc.company_id || !doc.company_ids) {
72
+ if (!doc.company_id) {
73
+ if (doc.company_ids && doc.company_ids.length) {
74
+ doc.company_id = doc.company_ids[0];
75
+ }
76
+ else if (company_id) {
77
+ doc.company_id = company_id;
78
+ }
79
+ }
80
+ if (!doc.company_ids) {
81
+ if (doc.company_id) {
82
+ doc.company_ids = [doc.company_id];
83
+ }
84
+ else if (company_id) {
85
+ doc.company_ids = [company_id];
86
+ }
87
+ }
88
+ }
89
+ return [4, checkCompany(object, doc, userSession)];
90
+ case 1:
91
+ _b.sent();
92
+ _b.label = 2;
93
+ case 2: return [2, doc];
94
+ }
95
+ });
96
+ });
97
+ }
98
+ exports.getMongoInsertBaseDoc = getMongoInsertBaseDoc;
99
+ function getMongoUpdateBaseDoc(object, doc, userSession) {
100
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
101
+ var _a, userId, company_id, extras, firstCompanyId;
102
+ return tslib_1.__generator(this, function (_b) {
103
+ switch (_b.label) {
104
+ case 0:
105
+ _a = userSession || {}, userId = _a.userId, company_id = _a.company_id;
106
+ if (!doc) {
107
+ return [2];
108
+ }
109
+ doc.modified = new Date();
110
+ if (userId) {
111
+ doc.modified_by = userId;
112
+ }
113
+ extras = ["spaces", "company", "organizations", "users", "space_users"];
114
+ if (!(extras.indexOf(object.name) < 0)) return [3, 2];
115
+ if (_.has(doc, "company_ids")) {
116
+ firstCompanyId = doc.company_ids ? doc.company_ids[0] : null;
117
+ if (firstCompanyId) {
118
+ doc.company_id = firstCompanyId;
119
+ }
120
+ else {
121
+ doc.company_id = null;
122
+ }
123
+ }
124
+ else if (_.has(doc, "company_id")) {
125
+ if (doc.company_id) {
126
+ doc.company_ids = [doc.company_id];
127
+ }
128
+ else {
129
+ doc.company_ids = null;
130
+ }
131
+ }
132
+ return [4, checkCompany(object, doc, userSession)];
133
+ case 1:
134
+ _b.sent();
135
+ _b.label = 2;
136
+ case 2: return [2, doc];
137
+ }
138
+ });
139
+ });
140
+ }
141
+ exports.getMongoUpdateBaseDoc = getMongoUpdateBaseDoc;
142
+ //# sourceMappingURL=method_base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"method_base.js","sourceRoot":"","sources":["../../src/types/method_base.ts"],"names":[],"mappings":";;;;AAMA,0BAA4B;AAW5B,IAAM,YAAY,GAAG,UAAO,MAAW,EAAE,GAAoB,EAAE,WAAgC;;;;;gBAE3F,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,cAAc,EAAE;oBAC5C,WAAO;iBACV;gBAEG,oBAAoB,GAAG,IAAI,CAAC;;;;gBAGL,WAAM,MAAM,CAAC,uBAAuB,CAAC,WAAW,CAAC,EAAA;;gBAAxE,oBAAoB,GAAG,SAAiD,CAAC;;;;gBAEzE,WAAO;;gBAGX,IAAI,oBAAoB,CAAC,gBAAgB,EAAE;oBACvC,WAAO,IAAI,EAAC;iBACf;gBAEG,eAAe,GAAG,EAAE,CAAC;gBAGzB,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,WAAW,CAAC,EAAE;oBACrC,eAAe,CAAC,IAAI,OAApB,eAAe,2CAAS,WAAW,CAAC,WAAW,WAAE;iBACpD;gBACD,IAAI,CAAC,CAAC,CAAC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BAEG,MAAM,GAAG,CAAC,QAAQ,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC;yBACxE,CAAA,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA,EAA/B,cAA+B;oBAE/B,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,aAAa,CAAC,EAAE;wBAKvB,cAAc,GAAG,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;wBACjE,IAAI,cAAc,EAAE;4BAChB,GAAG,CAAC,UAAU,GAAG,cAAc,CAAC;yBACnC;6BACI;4BACD,GAAG,CAAC,UAAU,GAAG,IAAI,CAAC;yBACzB;qBACJ;yBACI,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,YAAY,CAAC,EAAE;wBAK/B,IAAI,GAAG,CAAC,UAAU,EAAE;4BAChB,GAAG,CAAC,WAAW,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;yBACtC;6BACI;4BACD,GAAG,CAAC,WAAW,GAAG,IAAI,CAAC;yBAC1B;qBACJ;oBACD,WAAM,YAAY,CAAC,MAAM,EAAE,GAAG,EAAE,WAAW,CAAC,EAAA;;oBAA5C,SAA4C,CAAC;;wBAEjD,WAAO,GAAG,EAAC;;;;CACd;AAzCD,sDAyCC"}
@@ -223,9 +223,12 @@ export declare class SteedosObjectType extends SteedosObjectProperties {
223
223
  private appendRecordPermission;
224
224
  private getTriggerContext;
225
225
  private processUnreadableField;
226
+ private getInsertBaseDoc;
227
+ private getUpdateBaseDoc;
226
228
  private formatRecord;
227
229
  private callAdapter;
228
- private runRecordFormula;
230
+ private getRecordFormulaDoc;
231
+ private runRecordQuotedByObjectFieldFormulas;
229
232
  private runRecordSummaries;
230
233
  private getObjectEditPermissionFilters;
231
234
  private dealWithFilters;
@@ -24,6 +24,7 @@ var cachers_1 = require("@steedos/cachers");
24
24
  var lodash_2 = require("lodash");
25
25
  var trigger_1 = require("../triggers/trigger");
26
26
  var metadata_registrar_1 = require("@steedos/metadata-registrar");
27
+ var method_base_1 = require("./method_base");
27
28
  var auth = require("@steedos/auth");
28
29
  var clone = require('clone');
29
30
  var PLATFORM_ENTERPRISE = 'platform-enterprise';
@@ -1389,15 +1390,18 @@ var SteedosObjectType = (function (_super) {
1389
1390
  return tslib_1.__generator(this, function (_a) {
1390
1391
  switch (_a.label) {
1391
1392
  case 0:
1392
- _a.trys.push([0, 2, , 3]);
1393
+ _a.trys.push([0, 3, , 4]);
1393
1394
  doc = this.formatRecord(doc);
1395
+ return [4, this.getInsertBaseDoc(doc, userSession)];
1396
+ case 1:
1397
+ doc = _a.sent();
1394
1398
  return [4, this.callAdapter('insert', this.table_name, doc, userSession)];
1395
- case 1: return [2, _a.sent()];
1396
- case 2:
1399
+ case 2: return [2, _a.sent()];
1400
+ case 3:
1397
1401
  error_5 = _a.sent();
1398
1402
  this.handlerDuplicateKeyError(error_5, userSession);
1399
1403
  throw error_5;
1400
- case 3: return [2];
1404
+ case 4: return [2];
1401
1405
  }
1402
1406
  });
1403
1407
  });
@@ -1408,16 +1412,19 @@ var SteedosObjectType = (function (_super) {
1408
1412
  return tslib_1.__generator(this, function (_a) {
1409
1413
  switch (_a.label) {
1410
1414
  case 0:
1411
- _a.trys.push([0, 2, , 3]);
1415
+ _a.trys.push([0, 3, , 4]);
1412
1416
  doc = this.formatRecord(doc);
1417
+ return [4, this.getUpdateBaseDoc(doc, userSession)];
1418
+ case 1:
1419
+ doc = _a.sent();
1413
1420
  clonedId = id;
1414
1421
  return [4, this.callAdapter('update', this.table_name, clonedId, doc, userSession)];
1415
- case 1: return [2, _a.sent()];
1416
- case 2:
1422
+ case 2: return [2, _a.sent()];
1423
+ case 3:
1417
1424
  error_6 = _a.sent();
1418
1425
  this.handlerDuplicateKeyError(error_6, userSession);
1419
1426
  throw error_6;
1420
- case 3: return [2];
1427
+ case 4: return [2];
1421
1428
  }
1422
1429
  });
1423
1430
  });
@@ -1429,9 +1436,12 @@ var SteedosObjectType = (function (_super) {
1429
1436
  switch (_a.label) {
1430
1437
  case 0:
1431
1438
  doc = this.formatRecord(doc);
1439
+ return [4, this.getUpdateBaseDoc(doc, userSession)];
1440
+ case 1:
1441
+ doc = _a.sent();
1432
1442
  clonedId = id;
1433
1443
  return [4, this.callAdapter('updateOne', this.table_name, clonedId, doc, userSession)];
1434
- case 1: return [2, _a.sent()];
1444
+ case 2: return [2, _a.sent()];
1435
1445
  }
1436
1446
  });
1437
1447
  });
@@ -1443,9 +1453,12 @@ var SteedosObjectType = (function (_super) {
1443
1453
  switch (_a.label) {
1444
1454
  case 0:
1445
1455
  doc = this.formatRecord(doc);
1456
+ return [4, this.getUpdateBaseDoc(doc, userSession)];
1457
+ case 1:
1458
+ doc = _a.sent();
1446
1459
  clonedQueryFilters = queryFilters;
1447
1460
  return [4, this.callAdapter('updateMany', this.table_name, clonedQueryFilters, doc, userSession)];
1448
- case 1: return [2, _a.sent()];
1461
+ case 2: return [2, _a.sent()];
1449
1462
  }
1450
1463
  });
1451
1464
  });
@@ -1970,6 +1983,9 @@ var SteedosObjectType = (function (_super) {
1970
1983
  });
1971
1984
  objectConfig.fields = this.getAccessFields(objectConfig.fields, objectLayout, objectConfig.permissions);
1972
1985
  _.each(objectConfig.fields, function (field) {
1986
+ if (field.override) {
1987
+ field = Object.assign(field, field.override);
1988
+ }
1973
1989
  if (field && field.static) {
1974
1990
  var fieldAmis = field.amis || {};
1975
1991
  fieldAmis.static = true;
@@ -2493,34 +2509,25 @@ var SteedosObjectType = (function (_super) {
2493
2509
  });
2494
2510
  });
2495
2511
  };
2496
- SteedosObjectType.prototype.getTriggerContext = function (when, method, args, recordId) {
2512
+ SteedosObjectType.prototype.getTriggerContext = function (when, method, args) {
2497
2513
  return tslib_1.__awaiter(this, void 0, void 0, function () {
2498
- var userSession, context, _a;
2499
- return tslib_1.__generator(this, function (_b) {
2500
- switch (_b.label) {
2501
- case 0:
2502
- userSession = args[args.length - 1];
2503
- context = { objectName: this.name, userId: userSession ? userSession.userId : undefined, spaceId: userSession ? userSession.spaceId : undefined };
2504
- if (method === 'find' || method === 'findOne' || method === 'count') {
2505
- context.query = args[args.length - 2];
2506
- }
2507
- if (method === 'aggregate' || method === 'aggregatePrefixalPipeline') {
2508
- context.query = args[args.length - 3];
2509
- }
2510
- if (method === 'findOne' || method === 'update' || method === 'delete') {
2511
- context.id = args[1];
2512
- }
2513
- if (method === 'insert' || method === 'update') {
2514
- context.doc = args[args.length - 2];
2515
- }
2516
- if (!(when === 'after' && (method === 'update' || method === 'delete'))) return [3, 2];
2517
- _a = context;
2518
- return [4, this.findOne(recordId, {}, userSession)];
2519
- case 1:
2520
- _a.previousDoc = _b.sent();
2521
- _b.label = 2;
2522
- case 2: return [2, context];
2514
+ var userSession, context;
2515
+ return tslib_1.__generator(this, function (_a) {
2516
+ userSession = args[args.length - 1];
2517
+ context = { objectName: this.name, userId: userSession ? userSession.userId : undefined, spaceId: userSession ? userSession.spaceId : undefined };
2518
+ if (method === 'find' || method === 'findOne' || method === 'count') {
2519
+ context.query = args[args.length - 2];
2520
+ }
2521
+ if (method === 'aggregate' || method === 'aggregatePrefixalPipeline') {
2522
+ context.query = args[args.length - 3];
2523
2523
  }
2524
+ if (method === 'findOne' || method === 'update' || method === 'delete') {
2525
+ context.id = args[1];
2526
+ }
2527
+ if (method === 'insert' || method === 'update') {
2528
+ context.doc = args[args.length - 2];
2529
+ }
2530
+ return [2, context];
2524
2531
  });
2525
2532
  });
2526
2533
  };
@@ -2569,6 +2576,36 @@ var SteedosObjectType = (function (_super) {
2569
2576
  });
2570
2577
  });
2571
2578
  };
2579
+ SteedosObjectType.prototype.getInsertBaseDoc = function (doc, userSession) {
2580
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
2581
+ var driver;
2582
+ return tslib_1.__generator(this, function (_a) {
2583
+ switch (_a.label) {
2584
+ case 0:
2585
+ driver = this._datasource && this._datasource.driver;
2586
+ if (!(driver == datasource_1.SteedosDatabaseDriverType.Mongo || driver == datasource_1.SteedosDatabaseDriverType.MeteorMongo)) return [3, 2];
2587
+ return [4, (0, method_base_1.getMongoInsertBaseDoc)(this, doc, userSession)];
2588
+ case 1: return [2, _a.sent()];
2589
+ case 2: return [2, doc];
2590
+ }
2591
+ });
2592
+ });
2593
+ };
2594
+ SteedosObjectType.prototype.getUpdateBaseDoc = function (doc, userSession) {
2595
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
2596
+ var driver;
2597
+ return tslib_1.__generator(this, function (_a) {
2598
+ switch (_a.label) {
2599
+ case 0:
2600
+ driver = this._datasource && this._datasource.driver;
2601
+ if (!(driver == datasource_1.SteedosDatabaseDriverType.Mongo || driver == datasource_1.SteedosDatabaseDriverType.MeteorMongo)) return [3, 2];
2602
+ return [4, (0, method_base_1.getMongoUpdateBaseDoc)(this, doc, userSession)];
2603
+ case 1: return [2, _a.sent()];
2604
+ case 2: return [2, doc];
2605
+ }
2606
+ });
2607
+ });
2608
+ };
2572
2609
  SteedosObjectType.prototype.formatRecord = function (doc) {
2573
2610
  var adapterFormat = this._datasource["formatRecord"];
2574
2611
  if (typeof adapterFormat == 'function') {
@@ -2582,7 +2619,7 @@ var SteedosObjectType = (function (_super) {
2582
2619
  args[_i - 1] = arguments[_i];
2583
2620
  }
2584
2621
  return tslib_1.__awaiter(this, void 0, void 0, function () {
2585
- var adapterMethod, userSession, allow, recordId, doc, objectName, paramRecordId, returnValue, beforeTriggerContext, afterTriggerContext, previousDoc, values, _records;
2622
+ var adapterMethod, userSession, allow, recordId, doc, objectName, paramRecordId, returnValue, previousDoc, formulaDoc, docAfterFormulaRun, beforeTriggerContext, afterTriggerContext, values, _records;
2586
2623
  return tslib_1.__generator(this, function (_a) {
2587
2624
  switch (_a.label) {
2588
2625
  case 0:
@@ -2608,12 +2645,15 @@ var SteedosObjectType = (function (_super) {
2608
2645
  }
2609
2646
  else {
2610
2647
  recordId = args[1];
2611
- doc = args[2];
2648
+ if (method !== "delete") {
2649
+ doc = args[2];
2650
+ }
2612
2651
  }
2613
2652
  }
2614
2653
  if (method === 'findOne' || method === 'update' || method === 'delete') {
2615
2654
  paramRecordId = args[1];
2616
2655
  }
2656
+ if (!!(this.enable_space_global === true && _.include(['find', 'findOne'], method))) return [3, 4];
2617
2657
  if (!(!_.isEmpty(userSession) && this._datasource.enable_space)) return [3, 4];
2618
2658
  this.dealWithFilters(method, args);
2619
2659
  return [4, this.dealWithMethodPermission(method, args)];
@@ -2626,66 +2666,83 @@ var SteedosObjectType = (function (_super) {
2626
2666
  return [4, adapterMethod.apply(this._datasource, args)];
2627
2667
  case 5:
2628
2668
  returnValue = _a.sent();
2629
- return [3, 26];
2630
- case 6: return [4, this.getTriggerContext('before', method, args)];
2669
+ return [3, 29];
2670
+ case 6:
2671
+ previousDoc = void 0;
2672
+ formulaDoc = doc;
2673
+ if (!(method === 'update' || method === 'delete')) return [3, 8];
2674
+ return [4, this.findOne(recordId, {}, userSession)];
2631
2675
  case 7:
2676
+ previousDoc = _a.sent();
2677
+ formulaDoc = Object.assign({}, previousDoc, doc);
2678
+ _a.label = 8;
2679
+ case 8: return [4, this.getRecordFormulaDoc(method, objectName, formulaDoc, userSession)];
2680
+ case 9:
2681
+ docAfterFormulaRun = _a.sent();
2682
+ if (docAfterFormulaRun) {
2683
+ Object.assign(doc, docAfterFormulaRun);
2684
+ }
2685
+ return [4, this.getTriggerContext('before', method, args)];
2686
+ case 10:
2632
2687
  beforeTriggerContext = _a.sent();
2633
2688
  if (paramRecordId) {
2634
2689
  beforeTriggerContext = Object.assign({}, beforeTriggerContext, { id: paramRecordId });
2635
2690
  }
2636
2691
  return [4, this.runBeforeTriggers(method, beforeTriggerContext)];
2637
- case 8:
2692
+ case 11:
2638
2693
  _a.sent();
2639
2694
  return [4, (0, validation_rules_1.runValidationRules)(method, beforeTriggerContext, args[0], userSession)];
2640
- case 9:
2695
+ case 12:
2641
2696
  _a.sent();
2642
- return [4, this.getTriggerContext('after', method, args, paramRecordId)];
2643
- case 10:
2697
+ return [4, this.getTriggerContext('after', method, args)];
2698
+ case 13:
2644
2699
  afterTriggerContext = _a.sent();
2700
+ if (method === 'update' || method === 'delete') {
2701
+ afterTriggerContext.previousDoc = previousDoc;
2702
+ }
2645
2703
  if (paramRecordId) {
2646
2704
  afterTriggerContext = Object.assign({}, afterTriggerContext, { id: paramRecordId });
2647
2705
  }
2648
- previousDoc = clone(afterTriggerContext.previousDoc);
2649
2706
  args.splice(args.length - 1, 1, userSession ? userSession.userId : undefined);
2650
2707
  return [4, adapterMethod.apply(this._datasource, args)];
2651
- case 11:
2708
+ case 14:
2652
2709
  returnValue = _a.sent();
2653
- if (!(method === 'find' || method == 'findOne' || method == 'count' || method == 'aggregate' || method == 'aggregatePrefixalPipeline')) return [3, 15];
2710
+ if (!(method === 'find' || method == 'findOne' || method == 'count' || method == 'aggregate' || method == 'aggregatePrefixalPipeline')) return [3, 18];
2654
2711
  values = clone(returnValue) || {};
2655
- if (!(method === 'count')) return [3, 12];
2712
+ if (!(method === 'count')) return [3, 15];
2656
2713
  values = returnValue || 0;
2657
- return [3, 14];
2658
- case 12:
2659
- if (!userSession) return [3, 14];
2714
+ return [3, 17];
2715
+ case 15:
2716
+ if (!userSession) return [3, 17];
2660
2717
  _records = values;
2661
2718
  if (method == 'findOne' && values) {
2662
2719
  _records = [_records];
2663
2720
  }
2664
2721
  return [4, this.appendRecordPermission(_records, userSession)];
2665
- case 13:
2722
+ case 16:
2666
2723
  _a.sent();
2667
- _a.label = 14;
2668
- case 14:
2724
+ _a.label = 17;
2725
+ case 17:
2669
2726
  Object.assign(afterTriggerContext, { data: { values: values } });
2670
- _a.label = 15;
2671
- case 15:
2727
+ _a.label = 18;
2728
+ case 18:
2672
2729
  if (method == 'insert' && _.has(returnValue, '_id')) {
2673
2730
  afterTriggerContext.doc = returnValue;
2674
2731
  afterTriggerContext = Object.assign({}, afterTriggerContext, { id: returnValue._id });
2675
2732
  }
2676
- if (!(method == "update")) return [3, 18];
2677
- if (!returnValue) return [3, 17];
2733
+ if (!(method == "update")) return [3, 21];
2734
+ if (!returnValue) return [3, 20];
2678
2735
  afterTriggerContext.doc = returnValue;
2679
2736
  return [4, this.runAfterTriggers(method, afterTriggerContext)];
2680
- case 16:
2681
- _a.sent();
2682
- _a.label = 17;
2683
- case 17: return [3, 20];
2684
- case 18: return [4, this.runAfterTriggers(method, afterTriggerContext)];
2685
2737
  case 19:
2686
2738
  _a.sent();
2687
2739
  _a.label = 20;
2688
- case 20:
2740
+ case 20: return [3, 23];
2741
+ case 21: return [4, this.runAfterTriggers(method, afterTriggerContext)];
2742
+ case 22:
2743
+ _a.sent();
2744
+ _a.label = 23;
2745
+ case 23:
2689
2746
  if (method === 'find' || method == 'findOne' || method == 'count' || method == 'aggregate' || method == 'aggregatePrefixalPipeline') {
2690
2747
  if (_.isEmpty(afterTriggerContext.data) || (_.isEmpty(afterTriggerContext.data.values) && !_.isNumber(afterTriggerContext.data.values))) {
2691
2748
  return [2, returnValue];
@@ -2701,53 +2758,60 @@ var SteedosObjectType = (function (_super) {
2701
2758
  user_session: userSession,
2702
2759
  previous_record: afterTriggerContext.previousDoc
2703
2760
  }).run()];
2704
- case 21:
2761
+ case 24:
2705
2762
  _a.sent();
2706
- if (!returnValue) return [3, 24];
2763
+ if (!returnValue) return [3, 27];
2707
2764
  if (method === "insert") {
2708
2765
  doc = returnValue;
2709
2766
  recordId = doc._id;
2710
2767
  }
2711
- return [4, this.runRecordFormula(method, objectName, recordId, doc, userSession)];
2712
- case 22:
2768
+ return [4, this.runRecordQuotedByObjectFieldFormulas(method, objectName, recordId, doc, userSession)];
2769
+ case 25:
2713
2770
  _a.sent();
2714
2771
  return [4, this.runRecordSummaries(method, objectName, recordId, doc, previousDoc, userSession)];
2715
- case 23:
2772
+ case 26:
2716
2773
  _a.sent();
2717
- _a.label = 24;
2718
- case 24: return [4, (0, object_events_1.brokeEmitEvents)(objectName, method, afterTriggerContext)];
2719
- case 25:
2774
+ _a.label = 27;
2775
+ case 27: return [4, (0, object_events_1.brokeEmitEvents)(objectName, method, afterTriggerContext)];
2776
+ case 28:
2720
2777
  _a.sent();
2721
- _a.label = 26;
2722
- case 26: return [2, returnValue];
2778
+ _a.label = 29;
2779
+ case 29: return [2, returnValue];
2723
2780
  }
2724
2781
  });
2725
2782
  });
2726
2783
  };
2727
2784
  ;
2728
- SteedosObjectType.prototype.runRecordFormula = function (method, objectName, recordId, doc, userSession) {
2785
+ SteedosObjectType.prototype.getRecordFormulaDoc = function (method, objectName, doc, userSession) {
2729
2786
  return tslib_1.__awaiter(this, void 0, void 0, function () {
2730
- var onlyForOwn, withoutCurrent;
2787
+ var setDoc;
2731
2788
  return tslib_1.__generator(this, function (_a) {
2732
2789
  switch (_a.label) {
2733
2790
  case 0:
2734
- if (!(["insert", "update", "updateMany", "delete"].indexOf(method) > -1)) return [3, 5];
2735
- if (!(method === "updateMany")) return [3, 1];
2736
- return [3, 5];
2791
+ if (!(["insert", "update"].indexOf(method) > -1)) return [3, 2];
2792
+ return [4, (0, formula_1.getCurrentObjectFieldFormulasDoc)(objectName, doc, userSession)];
2737
2793
  case 1:
2738
- if (!(method !== "delete")) return [3, 3];
2739
- return [4, (0, formula_1.runCurrentObjectFieldFormulas)(objectName, recordId, doc, userSession, true)];
2740
- case 2:
2741
- _a.sent();
2742
- _a.label = 3;
2743
- case 3:
2744
- onlyForOwn = method === "insert";
2745
- withoutCurrent = method === "delete";
2794
+ setDoc = _a.sent();
2795
+ _a.label = 2;
2796
+ case 2: return [2, setDoc];
2797
+ }
2798
+ });
2799
+ });
2800
+ };
2801
+ SteedosObjectType.prototype.runRecordQuotedByObjectFieldFormulas = function (method, objectName, recordId, doc, userSession) {
2802
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
2803
+ var onlyForOwn, withoutCurrent;
2804
+ return tslib_1.__generator(this, function (_a) {
2805
+ switch (_a.label) {
2806
+ case 0:
2807
+ if (!(["update", "delete"].indexOf(method) > -1)) return [3, 2];
2808
+ onlyForOwn = false;
2809
+ withoutCurrent = true;
2746
2810
  return [4, (0, formula_1.runQuotedByObjectFieldFormulas)(objectName, recordId, userSession, { onlyForOwn: onlyForOwn, withoutCurrent: withoutCurrent })];
2747
- case 4:
2811
+ case 1:
2748
2812
  _a.sent();
2749
- _a.label = 5;
2750
- case 5: return [2];
2813
+ _a.label = 2;
2814
+ case 2: return [2];
2751
2815
  }
2752
2816
  });
2753
2817
  });
@@ -2757,20 +2821,17 @@ var SteedosObjectType = (function (_super) {
2757
2821
  return tslib_1.__generator(this, function (_a) {
2758
2822
  switch (_a.label) {
2759
2823
  case 0:
2760
- if (!(["insert", "update", "updateMany", "delete"].indexOf(method) > -1)) return [3, 5];
2761
- if (!(method === "updateMany")) return [3, 1];
2762
- return [3, 5];
2763
- case 1:
2764
- if (!(method === "insert")) return [3, 3];
2824
+ if (!(["insert", "update", "delete"].indexOf(method) > -1)) return [3, 4];
2825
+ if (!(method === "insert")) return [3, 2];
2765
2826
  return [4, (0, summary_1.runCurrentObjectFieldSummaries)(objectName, recordId)];
2766
- case 2:
2827
+ case 1:
2767
2828
  _a.sent();
2768
- _a.label = 3;
2769
- case 3: return [4, (0, summary_1.runQuotedByObjectFieldSummaries)(objectName, recordId, previousDoc, userSession)];
2770
- case 4:
2829
+ _a.label = 2;
2830
+ case 2: return [4, (0, summary_1.runQuotedByObjectFieldSummaries)(objectName, recordId, previousDoc, userSession)];
2831
+ case 3:
2771
2832
  _a.sent();
2772
- _a.label = 5;
2773
- case 5: return [2];
2833
+ _a.label = 4;
2834
+ case 4: return [2];
2774
2835
  }
2775
2836
  });
2776
2837
  });