@steedos/objectql 3.0.13-beta.3 → 3.0.13-beta.30

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.
Files changed (136) hide show
  1. package/lib/actions/field_updates.js +84 -153
  2. package/lib/actions/field_updates.js.map +1 -1
  3. package/lib/actions/index.js +1 -1
  4. package/lib/actions/workflow_notifications.js +50 -111
  5. package/lib/actions/workflow_notifications.js.map +1 -1
  6. package/lib/actions/workflow_outbound_messages.js +43 -94
  7. package/lib/actions/workflow_outbound_messages.js.map +1 -1
  8. package/lib/actions/workflow_rule.js +80 -177
  9. package/lib/actions/workflow_rule.js.map +1 -1
  10. package/lib/broker.js +19 -49
  11. package/lib/broker.js.map +1 -1
  12. package/lib/driver/field-encrytion/index.js +1 -1
  13. package/lib/driver/field-encrytion/mongosharedconst.js +17 -17
  14. package/lib/driver/field-encrytion/mongosharedconst.js.map +1 -1
  15. package/lib/driver/format.js +3 -3
  16. package/lib/driver/format.js.map +1 -1
  17. package/lib/driver/index.js +1 -1
  18. package/lib/driver/metadata.js +283 -387
  19. package/lib/driver/metadata.js.map +1 -1
  20. package/lib/driver/metadataOld.js +104 -105
  21. package/lib/driver/metadataOld.js.map +1 -1
  22. package/lib/driver/meteorMongo.js +5 -8
  23. package/lib/driver/meteorMongo.js.map +1 -1
  24. package/lib/driver/mongo.js +321 -506
  25. package/lib/driver/mongo.js.map +1 -1
  26. package/lib/driver/mysql.js +16 -20
  27. package/lib/driver/mysql.js.map +1 -1
  28. package/lib/driver/oracle.js +25 -38
  29. package/lib/driver/oracle.js.map +1 -1
  30. package/lib/driver/postgres.js +16 -20
  31. package/lib/driver/postgres.js.map +1 -1
  32. package/lib/driver/sqlite3.js +15 -19
  33. package/lib/driver/sqlite3.js.map +1 -1
  34. package/lib/driver/sqlserver.js +20 -33
  35. package/lib/driver/sqlserver.js.map +1 -1
  36. package/lib/errors/index.js +10 -11
  37. package/lib/errors/index.js.map +1 -1
  38. package/lib/formula/core.js +307 -594
  39. package/lib/formula/core.js.map +1 -1
  40. package/lib/formula/field_formula.js +80 -126
  41. package/lib/formula/field_formula.js.map +1 -1
  42. package/lib/formula/index.js +56 -97
  43. package/lib/formula/index.js.map +1 -1
  44. package/lib/formula/params.js +11 -11
  45. package/lib/formula/params.js.map +1 -1
  46. package/lib/formula/recompute.js +41 -166
  47. package/lib/formula/recompute.js.map +1 -1
  48. package/lib/formula/simple_params.js +12 -12
  49. package/lib/formula/simple_params.js.map +1 -1
  50. package/lib/formula/util.js +43 -67
  51. package/lib/formula/util.js.map +1 -1
  52. package/lib/functions/function.js +62 -87
  53. package/lib/functions/function.js.map +1 -1
  54. package/lib/index.js +3 -3
  55. package/lib/index.js.map +1 -1
  56. package/lib/services/datasourceServiceFactory.js +77 -111
  57. package/lib/services/datasourceServiceFactory.js.map +1 -1
  58. package/lib/services/index.js +3 -3
  59. package/lib/services/index.js.map +1 -1
  60. package/lib/summary/core.js +162 -355
  61. package/lib/summary/core.js.map +1 -1
  62. package/lib/summary/field_summary.js +44 -71
  63. package/lib/summary/field_summary.js.map +1 -1
  64. package/lib/summary/index.js +65 -80
  65. package/lib/summary/index.js.map +1 -1
  66. package/lib/summary/recompute.js +19 -47
  67. package/lib/summary/recompute.js.map +1 -1
  68. package/lib/triggers/trigger.js +101 -142
  69. package/lib/triggers/trigger.js.map +1 -1
  70. package/lib/ts-types/index.js +1 -1
  71. package/lib/typeorm/driver.js +250 -450
  72. package/lib/typeorm/driver.js.map +1 -1
  73. package/lib/typeorm/index.js +1 -1
  74. package/lib/typeorm/util.js +28 -28
  75. package/lib/typeorm/util.js.map +1 -1
  76. package/lib/types/action.js +24 -33
  77. package/lib/types/action.js.map +1 -1
  78. package/lib/types/app.js +210 -355
  79. package/lib/types/app.js.map +1 -1
  80. package/lib/types/connection.js +42 -78
  81. package/lib/types/connection.js.map +1 -1
  82. package/lib/types/dashboard.js +56 -77
  83. package/lib/types/dashboard.js.map +1 -1
  84. package/lib/types/datasource.js +359 -705
  85. package/lib/types/datasource.js.map +1 -1
  86. package/lib/types/defaultValue.js +53 -65
  87. package/lib/types/defaultValue.js.map +1 -1
  88. package/lib/types/field.js +165 -137
  89. package/lib/types/field.js.map +1 -1
  90. package/lib/types/field_permission.js +35 -66
  91. package/lib/types/field_permission.js.map +1 -1
  92. package/lib/types/index.js +1 -1
  93. package/lib/types/list_view.js +36 -43
  94. package/lib/types/list_view.js.map +1 -1
  95. package/lib/types/metadata.js +6 -6
  96. package/lib/types/metadata.js.map +1 -1
  97. package/lib/types/method_base.js +95 -128
  98. package/lib/types/method_base.js.map +1 -1
  99. package/lib/types/object.js +2005 -3015
  100. package/lib/types/object.js.map +1 -1
  101. package/lib/types/object_events.js +24 -37
  102. package/lib/types/object_events.js.map +1 -1
  103. package/lib/types/object_layouts.js +15 -38
  104. package/lib/types/object_layouts.js.map +1 -1
  105. package/lib/types/object_permission.js +99 -85
  106. package/lib/types/object_permission.js.map +1 -1
  107. package/lib/types/report.js +70 -98
  108. package/lib/types/report.js.map +1 -1
  109. package/lib/types/restrictionRule.js +47 -96
  110. package/lib/types/restrictionRule.js.map +1 -1
  111. package/lib/types/schema.js +115 -177
  112. package/lib/types/schema.js.map +1 -1
  113. package/lib/types/shareRule.js +53 -102
  114. package/lib/types/shareRule.js.map +1 -1
  115. package/lib/types/trigger.js +43 -56
  116. package/lib/types/trigger.js.map +1 -1
  117. package/lib/types/validation_rules.js +18 -56
  118. package/lib/types/validation_rules.js.map +1 -1
  119. package/lib/util/convert.js +24 -24
  120. package/lib/util/convert.js.map +1 -1
  121. package/lib/util/field.js +16 -28
  122. package/lib/util/field.js.map +1 -1
  123. package/lib/util/function_expression.js +7 -8
  124. package/lib/util/function_expression.js.map +1 -1
  125. package/lib/util/index.js +79 -97
  126. package/lib/util/index.js.map +1 -1
  127. package/lib/util/locale.js +4 -4
  128. package/lib/util/locale.js.map +1 -1
  129. package/lib/util/permission_shares.js +7 -7
  130. package/lib/util/permission_shares.js.map +1 -1
  131. package/lib/util/suffix.js +19 -19
  132. package/lib/util/suffix.js.map +1 -1
  133. package/lib/util/transform.js +1 -1
  134. package/lib/validators/index.js +10 -10
  135. package/lib/validators/index.js.map +1 -1
  136. package/package.json +12 -12
@@ -1,244 +1,177 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isFormulaFieldQuotingObjectAndFields = exports.updateQuotedByDocsForFormulaType = exports.updateDocsFieldFormulaValue = exports.updateQuotedByObjectFieldFormulaValue = exports.getCurrentObjectFieldFormulasDoc = exports.runQuotedByObjectFieldFormulas = exports.runAmisFormula = exports.isAmisFormula = exports.getAmisGlobalVariables = exports.runFormula = exports.evalFieldFormula = exports.computeFieldFormulaValue = exports.computeFormulaParams = exports.pickFormulaVarFields = exports.pickFieldFormulaVarFields = exports.pickFormulaVars = void 0;
4
- var tslib_1 = require("tslib");
5
- var index_1 = require("../index");
6
- var type_1 = require("./type");
7
- var field_formula_1 = require("./field_formula");
8
- var summary_1 = require("../summary");
9
- var util_1 = require("./util");
10
- var _ = require("lodash");
11
- var formula_1 = require("@steedos/formula");
12
- var params_1 = require("./params");
13
- var simple_params_1 = require("./simple_params");
14
- var amis_formula_1 = require("amis-formula");
4
+ const index_1 = require("../index");
5
+ const type_1 = require("./type");
6
+ const field_formula_1 = require("./field_formula");
7
+ const summary_1 = require("../summary");
8
+ const util_1 = require("./util");
9
+ const _ = require("lodash");
10
+ const formula_1 = require("@steedos/formula");
11
+ const params_1 = require("./params");
12
+ const simple_params_1 = require("./simple_params");
13
+ const amis_formula_1 = require("amis-formula");
15
14
  function convertToNestedObject(flatObject) {
16
- var nestedObject = {};
17
- for (var flatKey in flatObject) {
15
+ const nestedObject = {};
16
+ for (const flatKey in flatObject) {
18
17
  _.set(nestedObject, flatKey, flatObject[flatKey]);
19
18
  }
20
19
  return nestedObject;
21
20
  }
22
- var pickFormulaVars = function (formula) {
21
+ const pickFormulaVars = (formula) => {
23
22
  return (0, formula_1.extract)(formula);
24
23
  };
25
24
  exports.pickFormulaVars = pickFormulaVars;
26
- var pickFieldFormulaVarFields = function (fieldFormulaConfigs) {
25
+ const pickFieldFormulaVarFields = (fieldFormulaConfigs) => {
27
26
  if (!_.isArray(fieldFormulaConfigs)) {
28
27
  fieldFormulaConfigs = [fieldFormulaConfigs];
29
28
  }
30
- var result = [];
31
- fieldFormulaConfigs.forEach(function (fieldFormulaConfig) {
32
- var vars = fieldFormulaConfig.vars;
29
+ let result = [];
30
+ fieldFormulaConfigs.forEach((fieldFormulaConfig) => {
31
+ let { vars } = fieldFormulaConfig;
33
32
  result = _.union(result, (0, exports.pickFormulaVarFields)(vars));
34
33
  });
35
34
  return _.uniq(result);
36
35
  };
37
36
  exports.pickFieldFormulaVarFields = pickFieldFormulaVarFields;
38
- var pickFormulaVarFields = function (vars) {
39
- var result = [];
40
- vars.forEach(function (varItem) {
37
+ const pickFormulaVarFields = (vars) => {
38
+ let result = [];
39
+ vars.forEach((varItem) => {
41
40
  if (varItem.paths.length) {
42
- var firstPath = varItem.paths[0];
43
- var firstKey = firstPath.field_name;
41
+ let firstPath = varItem.paths[0];
42
+ let firstKey = firstPath.field_name;
44
43
  result.push(firstKey);
45
44
  }
46
45
  });
47
46
  return _.uniq(result);
48
47
  };
49
48
  exports.pickFormulaVarFields = pickFormulaVarFields;
50
- var computeFormulaParams = function (doc, vars, userSession) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
51
- var params, spaceId, currentUserId, _loop_1, vars_1, vars_1_1, _a, key, paths, isUserVar, isUserSessionVar, isSimpleVar, e_1_1;
52
- var e_1, _b;
53
- return tslib_1.__generator(this, function (_c) {
54
- switch (_c.label) {
55
- case 0:
56
- params = [];
57
- spaceId = userSession === null || userSession === void 0 ? void 0 : userSession.spaceId;
58
- currentUserId = userSession === null || userSession === void 0 ? void 0 : userSession.userId;
59
- if (!(vars && vars.length)) return [3, 8];
60
- _loop_1 = function (key, paths, isUserVar, isUserSessionVar, isSimpleVar) {
61
- var tempValue;
62
- return tslib_1.__generator(this, function (_d) {
63
- switch (_d.label) {
64
- case 0:
65
- key = key.trim();
66
- tempValue = void 0;
67
- if (isUserVar || isUserSessionVar) {
68
- if (!userSession) {
69
- throw new Error("computeFormulaParams:The param 'userSession' is required for the formula var key ".concat(key, " while running"));
70
- }
71
- }
72
- if (isSimpleVar) {
73
- tempValue = doc[key];
74
- params.push({
75
- key: key,
76
- value: tempValue,
77
- });
78
- return [2, "continue"];
79
- }
80
- if (isUserSessionVar) {
81
- tempValue = _.reduce(key.split("."), function (reslut, next, index) {
82
- if (index === 0) {
83
- return reslut;
84
- }
85
- else {
86
- return reslut[next];
87
- }
88
- }, userSession);
89
- params.push({
90
- key: key,
91
- value: tempValue,
92
- });
93
- return [2, "continue"];
94
- }
95
- return [4, (function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
96
- var reslut, lastReferenceToField, index, next, sus, found;
97
- return tslib_1.__generator(this, function (_a) {
98
- switch (_a.label) {
99
- case 0:
100
- reslut = null;
101
- lastReferenceToField = null;
102
- index = 0;
103
- _a.label = 1;
104
- case 1:
105
- if (!(index < paths.length)) return [3, 11];
106
- next = paths[index];
107
- if (!(index === 0)) return [3, 5];
108
- lastReferenceToField = next.reference_to_field;
109
- if (!isUserVar) return [3, 3];
110
- return [4, (0, index_1.getSteedosSchema)()
111
- .getObject("space_users")
112
- .find({
113
- filters: [
114
- ["user", "=", currentUserId],
115
- ["space", "=", spaceId],
116
- ],
117
- fields: [next.field_name],
118
- })];
119
- case 2:
120
- sus = _a.sent();
121
- reslut = sus && sus.length && sus[0];
122
- if (reslut) {
123
- reslut = reslut[next.field_name];
124
- }
125
- else {
126
- reslut = null;
127
- }
128
- return [3, 4];
129
- case 3:
130
- reslut = doc[next.field_name];
131
- _a.label = 4;
132
- case 4: return [3, 10];
133
- case 5:
134
- if (!reslut) {
135
- return [3, 11];
136
- }
137
- found = void 0;
138
- if (!(lastReferenceToField && lastReferenceToField !== "_id")) return [3, 7];
139
- return [4, (0, index_1.getSteedosSchema)()
140
- .getObject(next.reference_from)
141
- .findOne({ filters: [lastReferenceToField, "=", reslut] }, { fields: [next.field_name] })];
142
- case 6:
143
- found = _a.sent();
144
- return [3, 9];
145
- case 7: return [4, (0, index_1.getSteedosSchema)()
146
- .getObject(next.reference_from)
147
- .findOne(reslut, { fields: [next.field_name] })];
148
- case 8:
149
- found = _a.sent();
150
- _a.label = 9;
151
- case 9:
152
- lastReferenceToField = next.reference_to_field;
153
- if (found) {
154
- reslut = found[next.field_name];
155
- }
156
- else {
157
- reslut = null;
158
- }
159
- _a.label = 10;
160
- case 10:
161
- index++;
162
- return [3, 1];
163
- case 11: return [2, reslut];
164
- }
165
- });
166
- }); })()];
167
- case 1:
168
- tempValue = _d.sent();
169
- params.push({
170
- key: key,
171
- path: _.last(paths),
172
- value: tempValue,
173
- });
174
- return [2];
49
+ const computeFormulaParams = async (doc, vars, userSession) => {
50
+ let params = [];
51
+ const spaceId = userSession?.spaceId;
52
+ const currentUserId = userSession?.userId;
53
+ if (vars && vars.length) {
54
+ for (let { key, paths, is_user_var: isUserVar, is_user_session_var: isUserSessionVar, is_simple_var: isSimpleVar, } of vars) {
55
+ key = key.trim();
56
+ let tempValue;
57
+ if (isUserVar || isUserSessionVar) {
58
+ if (!userSession) {
59
+ throw new Error(`computeFormulaParams:The param 'userSession' is required for the formula var key ${key} while running`);
60
+ }
61
+ }
62
+ if (isSimpleVar) {
63
+ tempValue = doc[key];
64
+ params.push({
65
+ key: key,
66
+ value: tempValue,
67
+ });
68
+ continue;
69
+ }
70
+ if (isUserSessionVar) {
71
+ tempValue = _.reduce(key.split("."), function (reslut, next, index) {
72
+ if (index === 0) {
73
+ return reslut;
74
+ }
75
+ else {
76
+ return reslut[next];
77
+ }
78
+ }, userSession);
79
+ params.push({
80
+ key: key,
81
+ value: tempValue,
82
+ });
83
+ continue;
84
+ }
85
+ tempValue = await (async () => {
86
+ let reslut = null;
87
+ let lastReferenceToField = null;
88
+ for (let index = 0; index < paths.length; index++) {
89
+ let next = paths[index];
90
+ if (index === 0) {
91
+ lastReferenceToField = next.reference_to_field;
92
+ if (isUserVar) {
93
+ const sus = await (0, index_1.getSteedosSchema)()
94
+ .getObject("space_users")
95
+ .find({
96
+ filters: [
97
+ ["user", "=", currentUserId],
98
+ ["space", "=", spaceId],
99
+ ],
100
+ fields: [next.field_name],
101
+ });
102
+ reslut = sus && sus.length && sus[0];
103
+ if (reslut) {
104
+ reslut = reslut[next.field_name];
105
+ }
106
+ else {
107
+ reslut = null;
108
+ }
175
109
  }
176
- });
177
- };
178
- _c.label = 1;
179
- case 1:
180
- _c.trys.push([1, 6, 7, 8]);
181
- vars_1 = tslib_1.__values(vars), vars_1_1 = vars_1.next();
182
- _c.label = 2;
183
- case 2:
184
- if (!!vars_1_1.done) return [3, 5];
185
- _a = vars_1_1.value, key = _a.key, paths = _a.paths, isUserVar = _a.is_user_var, isUserSessionVar = _a.is_user_session_var, isSimpleVar = _a.is_simple_var;
186
- return [5, _loop_1(key, paths, isUserVar, isUserSessionVar, isSimpleVar)];
187
- case 3:
188
- _c.sent();
189
- _c.label = 4;
190
- case 4:
191
- vars_1_1 = vars_1.next();
192
- return [3, 2];
193
- case 5: return [3, 8];
194
- case 6:
195
- e_1_1 = _c.sent();
196
- e_1 = { error: e_1_1 };
197
- return [3, 8];
198
- case 7:
199
- try {
200
- if (vars_1_1 && !vars_1_1.done && (_b = vars_1.return)) _b.call(vars_1);
110
+ else {
111
+ reslut = doc[next.field_name];
112
+ }
113
+ }
114
+ else {
115
+ if (!reslut) {
116
+ break;
117
+ }
118
+ let found;
119
+ if (lastReferenceToField && lastReferenceToField !== "_id") {
120
+ found = await (0, index_1.getSteedosSchema)()
121
+ .getObject(next.reference_from)
122
+ .findOne({ filters: [lastReferenceToField, "=", reslut] }, { fields: [next.field_name] });
123
+ }
124
+ else {
125
+ found = await (0, index_1.getSteedosSchema)()
126
+ .getObject(next.reference_from)
127
+ .findOne(reslut, { fields: [next.field_name] });
128
+ }
129
+ lastReferenceToField = next.reference_to_field;
130
+ if (found) {
131
+ reslut = found[next.field_name];
132
+ }
133
+ else {
134
+ reslut = null;
135
+ }
136
+ }
201
137
  }
202
- finally { if (e_1) throw e_1.error; }
203
- return [7];
204
- case 8: return [2, params];
138
+ return reslut;
139
+ })();
140
+ params.push({
141
+ key: key,
142
+ path: _.last(paths),
143
+ value: tempValue,
144
+ });
205
145
  }
206
- });
207
- }); };
146
+ }
147
+ return params;
148
+ };
208
149
  exports.computeFormulaParams = computeFormulaParams;
209
- var computeFieldFormulaValue = function (doc, fieldFormulaConfig, userSession) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
210
- var formula, vars, data_type, formula_blank_value, params;
211
- return tslib_1.__generator(this, function (_a) {
212
- switch (_a.label) {
213
- case 0:
214
- if (!userSession) {
215
- (0, util_1.checkUserSessionNotRequiredForFieldFormulas)(fieldFormulaConfig);
216
- }
217
- formula = fieldFormulaConfig.formula, vars = fieldFormulaConfig.vars, data_type = fieldFormulaConfig.data_type, formula_blank_value = fieldFormulaConfig.formula_blank_value;
218
- return [4, (0, exports.computeFormulaParams)(doc, vars, userSession)];
219
- case 1:
220
- params = _a.sent();
221
- return [2, (0, exports.runFormula)(formula, params, {
222
- returnType: data_type,
223
- blankValue: formula_blank_value,
224
- record: doc,
225
- userSession: userSession,
226
- }, fieldFormulaConfig)];
227
- }
228
- });
229
- }); };
150
+ const computeFieldFormulaValue = async (doc, fieldFormulaConfig, userSession) => {
151
+ if (!userSession) {
152
+ (0, util_1.checkUserSessionNotRequiredForFieldFormulas)(fieldFormulaConfig);
153
+ }
154
+ const { formula, vars, data_type, formula_blank_value } = fieldFormulaConfig;
155
+ let params = await (0, exports.computeFormulaParams)(doc, vars, userSession);
156
+ return (0, exports.runFormula)(formula, params, {
157
+ returnType: data_type,
158
+ blankValue: formula_blank_value,
159
+ record: doc,
160
+ userSession: userSession,
161
+ }, fieldFormulaConfig);
162
+ };
230
163
  exports.computeFieldFormulaValue = computeFieldFormulaValue;
231
- var evalFieldFormula = function (formula, formulaParams) {
164
+ const evalFieldFormula = function (formula, formulaParams) {
232
165
  try {
233
166
  return (0, formula_1.parse)(formula, formulaParams);
234
167
  }
235
168
  catch (ex) {
236
169
  formulaParams[type_1.FormulaUserKey] = "{...}";
237
- throw new Error("evalFieldFormula:Catch an error \"".concat(ex, "\" while eval formula \"").concat(formula, "\" with params \"").concat(JSON.stringify(formulaParams), "\""));
170
+ throw new Error(`evalFieldFormula:Catch an error "${ex}" while eval formula "${formula}" with params "${JSON.stringify(formulaParams)}"`);
238
171
  }
239
172
  };
240
173
  exports.evalFieldFormula = evalFieldFormula;
241
- var runFormula = function (formula, params, options, messageTag) {
174
+ const runFormula = function (formula, params, options, messageTag) {
242
175
  if ((0, exports.isAmisFormula)(formula)) {
243
176
  return (0, exports.runAmisFormula)(formula, params, options, messageTag);
244
177
  }
@@ -247,7 +180,7 @@ var runFormula = function (formula, params, options, messageTag) {
247
180
  }
248
181
  };
249
182
  exports.runFormula = runFormula;
250
- var getAmisGlobalVariables = function (userSession) {
183
+ const getAmisGlobalVariables = (userSession) => {
251
184
  if (!userSession) {
252
185
  return {};
253
186
  }
@@ -260,40 +193,39 @@ var getAmisGlobalVariables = function (userSession) {
260
193
  };
261
194
  };
262
195
  exports.getAmisGlobalVariables = getAmisGlobalVariables;
263
- var isAmisFormula = function (formula) {
196
+ const isAmisFormula = (formula) => {
264
197
  return /\$\{.+\}/.test(formula);
265
198
  };
266
199
  exports.isAmisFormula = isAmisFormula;
267
- var runAmisFormula = function (formula, params, options, messageTag) {
268
- var record = options.record, userSession = options.userSession;
200
+ const runAmisFormula = function (formula, params, options, messageTag) {
201
+ const { record, userSession } = options;
269
202
  try {
270
- var globalVariables = (0, exports.getAmisGlobalVariables)(userSession);
271
- var data_1 = {};
272
- _.each(params, function (item) {
273
- data_1[item.key] = item.value;
203
+ const globalVariables = (0, exports.getAmisGlobalVariables)(userSession);
204
+ let data = {};
205
+ _.each(params, (item) => {
206
+ data[item.key] = item.value;
274
207
  });
275
- data_1 = convertToNestedObject(data_1);
276
- var amisFormulaValue = (0, amis_formula_1.evaluate)(formula, Object.assign({}, globalVariables, record, data_1), {});
208
+ data = convertToNestedObject(data);
209
+ const amisFormulaValue = (0, amis_formula_1.evaluate)(formula, Object.assign({}, globalVariables, record, data), {});
277
210
  if (formula === amisFormulaValue) {
278
- throw new Error("Amis formula \"".concat(formula, "\" evaluate failed \"Function is not defined\"."));
211
+ throw new Error(`Amis formula "${formula}" evaluate failed "Function is not defined".`);
279
212
  }
280
213
  else {
281
214
  return amisFormulaValue;
282
215
  }
283
216
  }
284
217
  catch (e) {
285
- throw new Error("Catch an error \"".concat(e.message, "\" while evaluate amis formula \"").concat(formula, "\"."));
218
+ throw new Error(`Catch an error "${e.message}" while evaluate amis formula "${formula}".`);
286
219
  }
287
220
  };
288
221
  exports.runAmisFormula = runAmisFormula;
289
- var runSFFormula = function (formula, params, options, messageTag) {
222
+ const runSFFormula = function (formula, params, options, messageTag) {
290
223
  if (!options) {
291
224
  options = {};
292
225
  }
293
- var returnType = options.returnType, blankValue = options.blankValue;
294
- var formulaParams = {};
295
- params.forEach(function (_a) {
296
- var key = _a.key, path = _a.path, value = _a.value;
226
+ let { returnType, blankValue } = options;
227
+ let formulaParams = {};
228
+ params.forEach(({ key, path, value }) => {
297
229
  if (path) {
298
230
  formulaParams[key] = (0, params_1.getFieldSubstitution)(path.reference_from, path.field_name, value, blankValue);
299
231
  }
@@ -301,12 +233,12 @@ var runSFFormula = function (formula, params, options, messageTag) {
301
233
  formulaParams[key] = (0, simple_params_1.getSimpleParamSubstitution)(value, blankValue);
302
234
  }
303
235
  });
304
- var result = (0, exports.evalFieldFormula)(formula, formulaParams);
305
- var formulaValue = result.value;
306
- var formulaValueType = result.dataType;
236
+ let result = (0, exports.evalFieldFormula)(formula, formulaParams);
237
+ let formulaValue = result.value;
238
+ let formulaValueType = result.dataType;
307
239
  if (result.type === "error") {
308
240
  console.error(formula, formulaParams);
309
- throw new Error("runFormula:Catch an error \"".concat(result.message, "\" while eval formula \"").concat(formula, "\" with params: \"").concat(JSON.stringify(formulaParams), "\" for \"").concat(JSON.stringify(messageTag), "\""));
241
+ throw new Error(`runFormula:Catch an error "${result.message}" while eval formula "${formula}" with params: "${JSON.stringify(formulaParams)}" for "${JSON.stringify(messageTag)}"`);
310
242
  }
311
243
  if (formulaValueType === "number" && _.isNaN(formulaValue)) {
312
244
  formulaValue = null;
@@ -315,46 +247,46 @@ var runSFFormula = function (formula, params, options, messageTag) {
315
247
  switch (returnType) {
316
248
  case "boolean":
317
249
  if (formulaValueType !== "checkbox") {
318
- throw new Error("runFormula:The field formula \"".concat(formula, "\" with params \"").concat(JSON.stringify(formulaParams), "\" should return a boolean type result but got a ").concat(formulaValueType, " type value '").concat(formulaValue, "'."));
250
+ throw new Error(`runFormula:The field formula "${formula}" with params "${JSON.stringify(formulaParams)}" should return a boolean type result but got a ${formulaValueType} type value '${formulaValue}'.`);
319
251
  }
320
252
  break;
321
253
  case "number":
322
254
  if (formulaValueType !== "number") {
323
- throw new Error("runFormula:The field formula \"".concat(formula, "\" with params \"").concat(JSON.stringify(formulaParams), "\" should return a number type result but got a ").concat(formulaValueType, " type value '").concat(formulaValue, "'."));
255
+ throw new Error(`runFormula:The field formula "${formula}" with params "${JSON.stringify(formulaParams)}" should return a number type result but got a ${formulaValueType} type value '${formulaValue}'.`);
324
256
  }
325
257
  break;
326
258
  case "currency":
327
259
  if (formulaValueType !== "number") {
328
- throw new Error("runFormula:The field formula \"".concat(formula, "\" with params \"").concat(JSON.stringify(formulaParams), "\" should return a number type result but got a ").concat(formulaValueType, " type value '").concat(formulaValue, "'."));
260
+ throw new Error(`runFormula:The field formula "${formula}" with params "${JSON.stringify(formulaParams)}" should return a number type result but got a ${formulaValueType} type value '${formulaValue}'.`);
329
261
  }
330
262
  break;
331
263
  case "percent":
332
264
  if (formulaValueType !== "number") {
333
- throw new Error("runFormula:The field formula \"".concat(formula, "\" with params \"").concat(JSON.stringify(formulaParams), "\" should return a number type result but got a ").concat(formulaValueType, " type value '").concat(formulaValue, "'."));
265
+ throw new Error(`runFormula:The field formula "${formula}" with params "${JSON.stringify(formulaParams)}" should return a number type result but got a ${formulaValueType} type value '${formulaValue}'.`);
334
266
  }
335
267
  break;
336
268
  case "text":
337
269
  if (formulaValueType !== "text") {
338
- throw new Error("runFormula:The field formula \"".concat(formula, "\" with params \"").concat(JSON.stringify(formulaParams), "\" should return a string type result but got a ").concat(formulaValueType, " type value '").concat(formulaValue, "'."));
270
+ throw new Error(`runFormula:The field formula "${formula}" with params "${JSON.stringify(formulaParams)}" should return a string type result but got a ${formulaValueType} type value '${formulaValue}'.`);
339
271
  }
340
272
  break;
341
273
  case "date":
342
274
  if (formulaValueType !== "date") {
343
- throw new Error("runFormula:The field formula \"".concat(formula, "\" with params \"").concat(JSON.stringify(formulaParams), "\" should return a date type result but got a ").concat(formulaValueType, " type value '").concat(formulaValue, "'."));
275
+ throw new Error(`runFormula:The field formula "${formula}" with params "${JSON.stringify(formulaParams)}" should return a date type result but got a ${formulaValueType} type value '${formulaValue}'.`);
344
276
  }
345
277
  break;
346
278
  case "datetime":
347
279
  if (formulaValueType !== "datetime") {
348
- throw new Error("runFormula:The field formula \"".concat(formula, "\" with params \"").concat(JSON.stringify(formulaParams), "\" should return a date type result but got a ").concat(formulaValueType, " type value '").concat(formulaValue, "'."));
280
+ throw new Error(`runFormula:The field formula "${formula}" with params "${JSON.stringify(formulaParams)}" should return a date type result but got a ${formulaValueType} type value '${formulaValue}'.`);
349
281
  }
350
282
  break;
351
283
  }
352
284
  }
353
285
  return formulaValue;
354
286
  };
355
- var addToAggregatePaths = function (varItemToAggregatePaths, toAggregatePaths) {
356
- var pathLength = varItemToAggregatePaths.length;
357
- var existPath = toAggregatePaths.find(function (item) {
287
+ const addToAggregatePaths = (varItemToAggregatePaths, toAggregatePaths) => {
288
+ const pathLength = varItemToAggregatePaths.length;
289
+ let existPath = toAggregatePaths.find((item) => {
358
290
  return (JSON.stringify(item.slice(0, pathLength)) ===
359
291
  JSON.stringify(varItemToAggregatePaths.slice(0, pathLength)));
360
292
  });
@@ -362,378 +294,159 @@ var addToAggregatePaths = function (varItemToAggregatePaths, toAggregatePaths) {
362
294
  toAggregatePaths.push(varItemToAggregatePaths);
363
295
  }
364
296
  };
365
- var runQuotedByObjectFieldFormulas = function (objectName_1, recordId_1, userSession_1) {
366
- return tslib_1.__awaiter(this, arguments, void 0, function (objectName, recordId, userSession, options) {
367
- var fieldNames, escapeConfigs, quotedByConfigs, onlyForOwn, withoutCurrent, _a, _b, config, e_2_1;
368
- var e_2, _c;
369
- if (options === void 0) { options = {}; }
370
- return tslib_1.__generator(this, function (_d) {
371
- switch (_d.label) {
372
- case 0:
373
- fieldNames = options.fieldNames, escapeConfigs = options.escapeConfigs, quotedByConfigs = options.quotedByConfigs, onlyForOwn = options.onlyForOwn, withoutCurrent = options.withoutCurrent;
374
- if (!!quotedByConfigs) return [3, 2];
375
- return [4, (0, field_formula_1.getObjectQuotedByFieldFormulaConfigs)(objectName, fieldNames, escapeConfigs, { onlyForOwn: onlyForOwn, withoutCurrent: withoutCurrent })];
376
- case 1:
377
- quotedByConfigs = _d.sent();
378
- _d.label = 2;
379
- case 2:
380
- if (!quotedByConfigs.allConfigs.length) {
381
- return [2];
382
- }
383
- if (!userSession) {
384
- (0, util_1.checkUserSessionNotRequiredForFieldFormulas)(quotedByConfigs.allConfigs);
385
- }
386
- _d.label = 3;
387
- case 3:
388
- _d.trys.push([3, 8, 9, 10]);
389
- _a = tslib_1.__values(quotedByConfigs.allConfigs), _b = _a.next();
390
- _d.label = 4;
391
- case 4:
392
- if (!!_b.done) return [3, 7];
393
- config = _b.value;
394
- return [4, (0, exports.updateQuotedByObjectFieldFormulaValue)(objectName, recordId, config, userSession, quotedByConfigs.ownConfigs)];
395
- case 5:
396
- _d.sent();
397
- _d.label = 6;
398
- case 6:
399
- _b = _a.next();
400
- return [3, 4];
401
- case 7: return [3, 10];
402
- case 8:
403
- e_2_1 = _d.sent();
404
- e_2 = { error: e_2_1 };
405
- return [3, 10];
406
- case 9:
407
- try {
408
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
409
- }
410
- finally { if (e_2) throw e_2.error; }
411
- return [7];
412
- case 10: return [2];
413
- }
414
- });
415
- });
297
+ const runQuotedByObjectFieldFormulas = async function (objectName, recordId, userSession, options = {}) {
298
+ let { fieldNames, escapeConfigs, quotedByConfigs, onlyForOwn, withoutCurrent, } = options;
299
+ if (!quotedByConfigs) {
300
+ quotedByConfigs = await (0, field_formula_1.getObjectQuotedByFieldFormulaConfigs)(objectName, fieldNames, escapeConfigs, { onlyForOwn, withoutCurrent });
301
+ }
302
+ if (!quotedByConfigs.allConfigs.length) {
303
+ return;
304
+ }
305
+ if (!userSession) {
306
+ (0, util_1.checkUserSessionNotRequiredForFieldFormulas)(quotedByConfigs.allConfigs);
307
+ }
308
+ for (const config of quotedByConfigs.allConfigs) {
309
+ await (0, exports.updateQuotedByObjectFieldFormulaValue)(objectName, recordId, config, userSession, quotedByConfigs.ownConfigs);
310
+ }
416
311
  };
417
312
  exports.runQuotedByObjectFieldFormulas = runQuotedByObjectFieldFormulas;
418
- var getCurrentObjectFieldFormulasDoc = function (objectName, doc, userSession, configs) {
419
- return tslib_1.__awaiter(this, void 0, void 0, function () {
420
- var setDoc, configs_1, configs_1_1, config, _a, _b, e_3_1;
421
- var e_3, _c;
422
- return tslib_1.__generator(this, function (_d) {
423
- switch (_d.label) {
424
- case 0:
425
- if (!!configs) return [3, 2];
426
- return [4, (0, field_formula_1.getObjectFieldFormulaConfigs)(objectName)];
427
- case 1:
428
- configs = _d.sent();
429
- _d.label = 2;
430
- case 2:
431
- if (!configs.length) {
432
- return [2];
433
- }
434
- if (!userSession) {
435
- (0, util_1.checkUserSessionNotRequiredForFieldFormulas)(configs);
436
- }
437
- setDoc = {};
438
- _d.label = 3;
439
- case 3:
440
- _d.trys.push([3, 8, 9, 10]);
441
- configs_1 = tslib_1.__values(configs), configs_1_1 = configs_1.next();
442
- _d.label = 4;
443
- case 4:
444
- if (!!configs_1_1.done) return [3, 7];
445
- config = configs_1_1.value;
446
- doc = Object.assign({}, doc, setDoc);
447
- _a = setDoc;
448
- _b = config.field_name;
449
- return [4, (0, exports.computeFieldFormulaValue)(doc, config, userSession)];
450
- case 5:
451
- _a[_b] = _d.sent();
452
- _d.label = 6;
453
- case 6:
454
- configs_1_1 = configs_1.next();
455
- return [3, 4];
456
- case 7: return [3, 10];
457
- case 8:
458
- e_3_1 = _d.sent();
459
- e_3 = { error: e_3_1 };
460
- return [3, 10];
461
- case 9:
462
- try {
463
- if (configs_1_1 && !configs_1_1.done && (_c = configs_1.return)) _c.call(configs_1);
464
- }
465
- finally { if (e_3) throw e_3.error; }
466
- return [7];
467
- case 10: return [2, setDoc];
468
- }
469
- });
470
- });
313
+ const getCurrentObjectFieldFormulasDoc = async function (objectName, doc, userSession, configs) {
314
+ if (!configs) {
315
+ configs = await (0, field_formula_1.getObjectFieldFormulaConfigs)(objectName);
316
+ }
317
+ if (!configs.length) {
318
+ return;
319
+ }
320
+ if (!userSession) {
321
+ (0, util_1.checkUserSessionNotRequiredForFieldFormulas)(configs);
322
+ }
323
+ let setDoc = {};
324
+ for (const config of configs) {
325
+ doc = Object.assign({}, doc, setDoc);
326
+ setDoc[config.field_name] = await (0, exports.computeFieldFormulaValue)(doc, config, userSession);
327
+ }
328
+ return setDoc;
471
329
  };
472
330
  exports.getCurrentObjectFieldFormulasDoc = getCurrentObjectFieldFormulasDoc;
473
- var updateQuotedByObjectFieldFormulaValue = function (objectName, recordId, fieldFormulaConfig, userSession, escapeConfigs) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
474
- var vars, fieldFormulaObjectName, currentObject, fieldFormulaObject, toAggregatePaths, vars_2, vars_2_1, varItem, paths, isInPaths, varItemToAggregatePaths, paths_1, paths_1_1, pathItem, formulaVarFields, toAggregatePaths_1, toAggregatePaths_1_1, toAggregatePathsItem, tempPath, doc, referenceToValue, record, docs, aggregateLookups, lastLookup, referenceToValue, record, aggregateFilters, docs, e_4_1;
475
- var e_5, _a, e_6, _b, e_4, _c;
476
- return tslib_1.__generator(this, function (_d) {
477
- switch (_d.label) {
478
- case 0:
479
- vars = fieldFormulaConfig.vars, fieldFormulaObjectName = fieldFormulaConfig.object_name;
480
- currentObject = (0, index_1.getSteedosSchema)().getObject(objectName);
481
- fieldFormulaObject = (0, index_1.getSteedosSchema)().getObject(fieldFormulaObjectName);
482
- toAggregatePaths = [];
483
- try {
484
- for (vars_2 = tslib_1.__values(vars), vars_2_1 = vars_2.next(); !vars_2_1.done; vars_2_1 = vars_2.next()) {
485
- varItem = vars_2_1.value;
486
- paths = varItem.paths;
487
- isInPaths = false;
488
- varItemToAggregatePaths = [];
489
- try {
490
- for (paths_1 = (e_6 = void 0, tslib_1.__values(paths)), paths_1_1 = paths_1.next(); !paths_1_1.done; paths_1_1 = paths_1.next()) {
491
- pathItem = paths_1_1.value;
492
- varItemToAggregatePaths.push(pathItem);
493
- if (pathItem.reference_from === objectName) {
494
- isInPaths = true;
495
- break;
496
- }
497
- }
498
- }
499
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
500
- finally {
501
- try {
502
- if (paths_1_1 && !paths_1_1.done && (_b = paths_1.return)) _b.call(paths_1);
503
- }
504
- finally { if (e_6) throw e_6.error; }
505
- }
506
- if (isInPaths) {
507
- addToAggregatePaths(varItemToAggregatePaths, toAggregatePaths);
508
- }
509
- }
510
- }
511
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
512
- finally {
513
- try {
514
- if (vars_2_1 && !vars_2_1.done && (_a = vars_2.return)) _a.call(vars_2);
515
- }
516
- finally { if (e_5) throw e_5.error; }
517
- }
518
- formulaVarFields = (0, exports.pickFieldFormulaVarFields)(fieldFormulaConfig);
519
- _d.label = 1;
520
- case 1:
521
- _d.trys.push([1, 18, 19, 20]);
522
- toAggregatePaths_1 = tslib_1.__values(toAggregatePaths), toAggregatePaths_1_1 = toAggregatePaths_1.next();
523
- _d.label = 2;
524
- case 2:
525
- if (!!toAggregatePaths_1_1.done) return [3, 17];
526
- toAggregatePathsItem = toAggregatePaths_1_1.value;
527
- if (!(toAggregatePathsItem.length < 3)) return [3, 11];
528
- tempPath = toAggregatePathsItem[0];
529
- if (!(fieldFormulaObjectName === objectName &&
530
- tempPath.reference_from === objectName)) return [3, 5];
531
- return [4, fieldFormulaObject.findOne(recordId, {
532
- fields: formulaVarFields,
533
- })];
534
- case 3:
535
- doc = _d.sent();
536
- return [4, (0, exports.updateDocsFieldFormulaValue)(doc, fieldFormulaConfig, userSession, escapeConfigs)];
537
- case 4:
538
- _d.sent();
539
- return [3, 10];
540
- case 5:
541
- referenceToValue = recordId;
542
- if (!(tempPath.reference_to_field &&
543
- tempPath.reference_to_field !== "_id")) return [3, 7];
544
- return [4, currentObject.findOne(recordId, {
331
+ const updateQuotedByObjectFieldFormulaValue = async (objectName, recordId, fieldFormulaConfig, userSession, escapeConfigs) => {
332
+ const { vars, object_name: fieldFormulaObjectName } = fieldFormulaConfig;
333
+ const currentObject = (0, index_1.getSteedosSchema)().getObject(objectName);
334
+ const fieldFormulaObject = (0, index_1.getSteedosSchema)().getObject(fieldFormulaObjectName);
335
+ let toAggregatePaths = [];
336
+ for (let varItem of vars) {
337
+ const { paths } = varItem;
338
+ let isInPaths = false;
339
+ let varItemToAggregatePaths = [];
340
+ for (let pathItem of paths) {
341
+ varItemToAggregatePaths.push(pathItem);
342
+ if (pathItem.reference_from === objectName) {
343
+ isInPaths = true;
344
+ break;
345
+ }
346
+ }
347
+ if (isInPaths) {
348
+ addToAggregatePaths(varItemToAggregatePaths, toAggregatePaths);
349
+ }
350
+ }
351
+ const formulaVarFields = (0, exports.pickFieldFormulaVarFields)(fieldFormulaConfig);
352
+ for (let toAggregatePathsItem of toAggregatePaths) {
353
+ if (toAggregatePathsItem.length < 3) {
354
+ let tempPath = toAggregatePathsItem[0];
355
+ if (fieldFormulaObjectName === objectName &&
356
+ tempPath.reference_from === objectName) {
357
+ let doc = await fieldFormulaObject.findOne(recordId, {
358
+ fields: formulaVarFields,
359
+ });
360
+ await (0, exports.updateDocsFieldFormulaValue)(doc, fieldFormulaConfig, userSession, escapeConfigs);
361
+ }
362
+ else {
363
+ let referenceToValue = recordId;
364
+ if (tempPath.reference_to_field &&
365
+ tempPath.reference_to_field !== "_id") {
366
+ let record = await currentObject.findOne(recordId, {
545
367
  fields: [tempPath.reference_to_field],
546
- })];
547
- case 6:
548
- record = _d.sent();
549
- referenceToValue = record && record[tempPath.reference_to_field];
550
- _d.label = 7;
551
- case 7: return [4, fieldFormulaObject.find({
368
+ });
369
+ referenceToValue = record && record[tempPath.reference_to_field];
370
+ }
371
+ let docs = await fieldFormulaObject.find({
552
372
  filters: [[tempPath.field_name, "=", referenceToValue]],
553
373
  fields: formulaVarFields,
554
- })];
555
- case 8:
556
- docs = _d.sent();
557
- return [4, (0, exports.updateDocsFieldFormulaValue)(docs, fieldFormulaConfig, userSession, escapeConfigs)];
558
- case 9:
559
- _d.sent();
560
- _d.label = 10;
561
- case 10: return [3, 16];
562
- case 11:
563
- aggregateLookups = (0, util_1.getFormulaVarPathsAggregateLookups)(toAggregatePathsItem);
564
- lastLookup = aggregateLookups[aggregateLookups.length - 1]["$lookup"];
565
- referenceToValue = recordId;
566
- if (!(lastLookup.foreignField && lastLookup.foreignField !== "_id")) return [3, 13];
567
- return [4, currentObject.findOne(recordId, {
568
- fields: [lastLookup.foreignField],
569
- })];
570
- case 12:
571
- record = _d.sent();
374
+ });
375
+ await (0, exports.updateDocsFieldFormulaValue)(docs, fieldFormulaConfig, userSession, escapeConfigs);
376
+ }
377
+ }
378
+ else {
379
+ let aggregateLookups = (0, util_1.getFormulaVarPathsAggregateLookups)(toAggregatePathsItem);
380
+ let lastLookup = aggregateLookups[aggregateLookups.length - 1]["$lookup"];
381
+ let referenceToValue = recordId;
382
+ if (lastLookup.foreignField && lastLookup.foreignField !== "_id") {
383
+ let record = await currentObject.findOne(recordId, {
384
+ fields: [lastLookup.foreignField],
385
+ });
572
386
  referenceToValue = record && record[lastLookup.foreignField];
573
- _d.label = 13;
574
- case 13:
575
- aggregateFilters = [
576
- ["".concat(lastLookup.as, ".").concat(lastLookup.foreignField), "=", referenceToValue],
577
- ];
578
- return [4, fieldFormulaObject.directAggregatePrefixalPipeline({
579
- filters: aggregateFilters,
580
- fields: formulaVarFields,
581
- }, aggregateLookups)];
582
- case 14:
583
- docs = _d.sent();
584
- return [4, (0, exports.updateDocsFieldFormulaValue)(docs, fieldFormulaConfig, userSession, escapeConfigs)];
585
- case 15:
586
- _d.sent();
587
- _d.label = 16;
588
- case 16:
589
- toAggregatePaths_1_1 = toAggregatePaths_1.next();
590
- return [3, 2];
591
- case 17: return [3, 20];
592
- case 18:
593
- e_4_1 = _d.sent();
594
- e_4 = { error: e_4_1 };
595
- return [3, 20];
596
- case 19:
597
- try {
598
- if (toAggregatePaths_1_1 && !toAggregatePaths_1_1.done && (_c = toAggregatePaths_1.return)) _c.call(toAggregatePaths_1);
599
- }
600
- finally { if (e_4) throw e_4.error; }
601
- return [7];
602
- case 20: return [2];
387
+ }
388
+ let aggregateFilters = [
389
+ [`${lastLookup.as}.${lastLookup.foreignField}`, "=", referenceToValue],
390
+ ];
391
+ const docs = await fieldFormulaObject.directAggregatePrefixalPipeline({
392
+ filters: aggregateFilters,
393
+ fields: formulaVarFields,
394
+ }, aggregateLookups);
395
+ await (0, exports.updateDocsFieldFormulaValue)(docs, fieldFormulaConfig, userSession, escapeConfigs);
603
396
  }
604
- });
605
- }); };
397
+ }
398
+ };
606
399
  exports.updateQuotedByObjectFieldFormulaValue = updateQuotedByObjectFieldFormulaValue;
607
- var updateDocsFieldFormulaValue = function (docs, fieldFormulaConfig, userSession, escapeConfigs) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
608
- var fieldFormulaObjectName, fieldFormulaObject, docs_1, docs_1_1, doc, value, setDoc, e_7_1;
609
- var e_7, _a;
610
- return tslib_1.__generator(this, function (_b) {
611
- switch (_b.label) {
612
- case 0:
613
- fieldFormulaObjectName = fieldFormulaConfig.object_name;
614
- if (!_.isArray(docs)) {
615
- docs = [docs];
616
- }
617
- if (!docs.length) {
618
- return [2];
619
- }
620
- fieldFormulaObject = (0, index_1.getSteedosSchema)().getObject(fieldFormulaObjectName);
621
- _b.label = 1;
622
- case 1:
623
- _b.trys.push([1, 7, 8, 9]);
624
- docs_1 = tslib_1.__values(docs), docs_1_1 = docs_1.next();
625
- _b.label = 2;
626
- case 2:
627
- if (!!docs_1_1.done) return [3, 6];
628
- doc = docs_1_1.value;
629
- return [4, (0, exports.computeFieldFormulaValue)(doc, fieldFormulaConfig, userSession)];
630
- case 3:
631
- value = _b.sent();
632
- setDoc = {};
633
- setDoc[fieldFormulaConfig.field_name] = value;
634
- return [4, fieldFormulaObject.directUpdate(doc._id, setDoc)];
635
- case 4:
636
- _b.sent();
637
- _b.label = 5;
638
- case 5:
639
- docs_1_1 = docs_1.next();
640
- return [3, 2];
641
- case 6: return [3, 9];
642
- case 7:
643
- e_7_1 = _b.sent();
644
- e_7 = { error: e_7_1 };
645
- return [3, 9];
646
- case 8:
647
- try {
648
- if (docs_1_1 && !docs_1_1.done && (_a = docs_1.return)) _a.call(docs_1);
649
- }
650
- finally { if (e_7) throw e_7.error; }
651
- return [7];
652
- case 9: return [4, (0, exports.updateQuotedByDocsForFormulaType)(docs, fieldFormulaConfig, userSession, escapeConfigs)];
653
- case 10:
654
- _b.sent();
655
- return [2];
656
- }
657
- });
658
- }); };
400
+ const updateDocsFieldFormulaValue = async (docs, fieldFormulaConfig, userSession, escapeConfigs) => {
401
+ const { object_name: fieldFormulaObjectName } = fieldFormulaConfig;
402
+ if (!_.isArray(docs)) {
403
+ docs = [docs];
404
+ }
405
+ if (!docs.length) {
406
+ return;
407
+ }
408
+ const fieldFormulaObject = (0, index_1.getSteedosSchema)().getObject(fieldFormulaObjectName);
409
+ for (let doc of docs) {
410
+ let value = await (0, exports.computeFieldFormulaValue)(doc, fieldFormulaConfig, userSession);
411
+ let setDoc = {};
412
+ setDoc[fieldFormulaConfig.field_name] = value;
413
+ await fieldFormulaObject.directUpdate(doc._id, setDoc);
414
+ }
415
+ await (0, exports.updateQuotedByDocsForFormulaType)(docs, fieldFormulaConfig, userSession, escapeConfigs);
416
+ };
659
417
  exports.updateDocsFieldFormulaValue = updateDocsFieldFormulaValue;
660
- var updateQuotedByDocsForFormulaType = function (docs, fieldFormulaConfig, userSession, escapeConfigs) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
661
- var fieldFormulaObjectName, fieldNames, formulaQuotedByConfigs, summaryQuotedByConfigs, docs_2, docs_2_1, doc, e_8_1;
662
- var e_8, _a;
663
- return tslib_1.__generator(this, function (_b) {
664
- switch (_b.label) {
665
- case 0:
666
- fieldFormulaObjectName = fieldFormulaConfig.object_name;
667
- if (!_.isArray(docs)) {
668
- docs = [docs];
669
- }
670
- if (!docs.length) {
671
- return [2];
672
- }
673
- fieldNames = [fieldFormulaConfig.field_name];
674
- return [4, (0, field_formula_1.getObjectQuotedByFieldFormulaConfigs)(fieldFormulaObjectName, fieldNames, escapeConfigs)];
675
- case 1:
676
- formulaQuotedByConfigs = _b.sent();
677
- return [4, (0, summary_1.getObjectQuotedByFieldSummaryConfigs)(fieldFormulaObjectName, fieldNames)];
678
- case 2:
679
- summaryQuotedByConfigs = _b.sent();
680
- _b.label = 3;
681
- case 3:
682
- _b.trys.push([3, 9, 10, 11]);
683
- docs_2 = tslib_1.__values(docs), docs_2_1 = docs_2.next();
684
- _b.label = 4;
685
- case 4:
686
- if (!!docs_2_1.done) return [3, 8];
687
- doc = docs_2_1.value;
688
- return [4, (0, exports.runQuotedByObjectFieldFormulas)(fieldFormulaObjectName, doc._id, userSession, {
689
- fieldNames: fieldNames,
690
- quotedByConfigs: formulaQuotedByConfigs,
691
- escapeConfigs: escapeConfigs,
692
- })];
693
- case 5:
694
- _b.sent();
695
- return [4, (0, summary_1.runQuotedByObjectFieldSummaries)(fieldFormulaObjectName, doc._id, null, userSession, {
696
- fieldNames: fieldNames,
697
- quotedByConfigs: summaryQuotedByConfigs,
698
- })];
699
- case 6:
700
- _b.sent();
701
- _b.label = 7;
702
- case 7:
703
- docs_2_1 = docs_2.next();
704
- return [3, 4];
705
- case 8: return [3, 11];
706
- case 9:
707
- e_8_1 = _b.sent();
708
- e_8 = { error: e_8_1 };
709
- return [3, 11];
710
- case 10:
711
- try {
712
- if (docs_2_1 && !docs_2_1.done && (_a = docs_2.return)) _a.call(docs_2);
713
- }
714
- finally { if (e_8) throw e_8.error; }
715
- return [7];
716
- case 11: return [2];
717
- }
718
- });
719
- }); };
418
+ const updateQuotedByDocsForFormulaType = async (docs, fieldFormulaConfig, userSession, escapeConfigs) => {
419
+ const { object_name: fieldFormulaObjectName } = fieldFormulaConfig;
420
+ if (!_.isArray(docs)) {
421
+ docs = [docs];
422
+ }
423
+ if (!docs.length) {
424
+ return;
425
+ }
426
+ const fieldNames = [fieldFormulaConfig.field_name];
427
+ const formulaQuotedByConfigs = await (0, field_formula_1.getObjectQuotedByFieldFormulaConfigs)(fieldFormulaObjectName, fieldNames, escapeConfigs);
428
+ const summaryQuotedByConfigs = await (0, summary_1.getObjectQuotedByFieldSummaryConfigs)(fieldFormulaObjectName, fieldNames);
429
+ for (let doc of docs) {
430
+ await (0, exports.runQuotedByObjectFieldFormulas)(fieldFormulaObjectName, doc._id, userSession, {
431
+ fieldNames,
432
+ quotedByConfigs: formulaQuotedByConfigs,
433
+ escapeConfigs,
434
+ });
435
+ await (0, summary_1.runQuotedByObjectFieldSummaries)(fieldFormulaObjectName, doc._id, null, userSession, {
436
+ fieldNames,
437
+ quotedByConfigs: summaryQuotedByConfigs,
438
+ });
439
+ }
440
+ };
720
441
  exports.updateQuotedByDocsForFormulaType = updateQuotedByDocsForFormulaType;
721
- var isFormulaFieldQuotingObjectAndFields = function (formulaObjectName, formulaFieldName, objectName, fieldNames) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
722
- var configs;
723
- return tslib_1.__generator(this, function (_a) {
724
- switch (_a.label) {
725
- case 0: return [4, (0, field_formula_1.getObjectFieldFormulaConfigs)(formulaObjectName, formulaFieldName)];
726
- case 1:
727
- configs = _a.sent();
728
- if (configs && configs.length) {
729
- return [2, (0, util_1.isFieldFormulaConfigQuotingObjectAndFields)(configs[0], objectName, fieldNames)];
730
- }
731
- else {
732
- return [2, false];
733
- }
734
- return [2];
735
- }
736
- });
737
- }); };
442
+ const isFormulaFieldQuotingObjectAndFields = async (formulaObjectName, formulaFieldName, objectName, fieldNames) => {
443
+ const configs = await (0, field_formula_1.getObjectFieldFormulaConfigs)(formulaObjectName, formulaFieldName);
444
+ if (configs && configs.length) {
445
+ return (0, util_1.isFieldFormulaConfigQuotingObjectAndFields)(configs[0], objectName, fieldNames);
446
+ }
447
+ else {
448
+ return false;
449
+ }
450
+ };
738
451
  exports.isFormulaFieldQuotingObjectAndFields = isFormulaFieldQuotingObjectAndFields;
739
452
  //# sourceMappingURL=core.js.map