@ironcode/vas-lib 0.0.16 → 0.0.18

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.
@@ -200,7 +200,7 @@ class VasControlModel extends VasBaseModel {
200
200
  * @param {string} defaultValue
201
201
  * @param {object} config
202
202
  */
203
- constructor(id, created, serverCreated, createdBy, modified, serverModified, modifiedBy, account, accessGroup, controlType, group, validators, name, title, linkable, sequence, description, defaultValue, config) {
203
+ constructor(id, created, serverCreated, createdBy, modified, serverModified, modifiedBy, account, accessGroup, controlType, group, validators, name, title, linkable, sequence, description, defaultValue, _config) {
204
204
  super(id, created, serverCreated, createdBy, modified, serverModified, modifiedBy);
205
205
  this.id = id;
206
206
  this.created = created;
@@ -220,8 +220,9 @@ class VasControlModel extends VasBaseModel {
220
220
  this.sequence = sequence;
221
221
  this.description = description;
222
222
  this.defaultValue = defaultValue;
223
- this.config = config;
223
+ this._config = _config;
224
224
  this.controlTypeModel = undefined;
225
+ this.configCache = undefined;
225
226
  }
226
227
  /**
227
228
  * The config property is returned from the api as a json string, this getter
@@ -229,7 +230,17 @@ class VasControlModel extends VasBaseModel {
229
230
  * @return {T}
230
231
  */
231
232
  getConfigModel() {
232
- return JSON.parse(this.config);
233
+ if (this.configCache !== undefined) {
234
+ return this.configCache;
235
+ }
236
+ return this.configCache = JSON.parse(this._config);
237
+ }
238
+ get config() {
239
+ return this._config;
240
+ }
241
+ set config(config) {
242
+ this._config = config;
243
+ this.configCache = undefined;
233
244
  }
234
245
  /**
235
246
  * The defaultValue property is returned from the api as a string or number,
@@ -428,7 +439,7 @@ class VasFieldModel extends VasJobDataModel {
428
439
  * @return {VasFieldModel}
429
440
  */
430
441
  static fromDto(fieldDto) {
431
- return new VasFieldModel(fieldDto.id || UUID.UUID(), fieldDto.created || '', fieldDto.serverCreated || '', fieldDto.createdBy || '', fieldDto.modified || '', fieldDto.serverModified || '', fieldDto.modifiedBy || '', fieldDto.account || '', fieldDto.job || '', fieldDto.control || '', fieldDto.fieldJobPointers || [], fieldDto.value || '', fieldDto.version || 0);
442
+ return new VasFieldModel(fieldDto.id || UUID.UUID(), fieldDto.created || '', fieldDto.serverCreated || '', fieldDto.createdBy || '', fieldDto.modified || '', fieldDto.serverModified || '', fieldDto.modifiedBy || '', fieldDto.account || '', fieldDto.job || '', fieldDto.control || '', fieldDto.fieldJobPointers || [], this.fromApiValue(fieldDto.value) || '', fieldDto.version || 0);
432
443
  }
433
444
  /**
434
445
  * @return {VasFieldDto}
@@ -457,7 +468,7 @@ class VasFieldModel extends VasJobDataModel {
457
468
  return {
458
469
  ...super.toApiDto(),
459
470
  control: this.control,
460
- value: this.prepareApiValue(this.value)
471
+ value: this.toApiValue(this.value)
461
472
  };
462
473
  }
463
474
  /**
@@ -468,7 +479,7 @@ class VasFieldModel extends VasJobDataModel {
468
479
  * @return {string | number | boolean}
469
480
  * @protected
470
481
  */
471
- prepareApiValue(value) {
482
+ toApiValue(value) {
472
483
  switch (typeof value) {
473
484
  case 'object': {
474
485
  return JSON.stringify(value);
@@ -487,6 +498,14 @@ class VasFieldModel extends VasJobDataModel {
487
498
  }
488
499
  }
489
500
  }
501
+ static fromApiValue(value) {
502
+ try {
503
+ return JSON.parse(value);
504
+ }
505
+ catch (e) {
506
+ return value;
507
+ }
508
+ }
490
509
  }
491
510
 
492
511
  /**
@@ -1092,15 +1111,67 @@ class VasJobModel extends VasRestrictedAccountObjectModel {
1092
1111
  /**
1093
1112
  * This method will hydrate the `fields` property of the model. The reason for
1094
1113
  * this is that we have different ways to store the field data. One way, is
1095
- * we store them as dynamic properties of the job. For example job.foo.bar,
1096
- * where `foo` is the name of a Group, and `bar` is the name of a control.
1097
- * Thus, when we create a job using a form in the client, the job object will
1098
- * have its static properties (id, account, reference etc), and also a number
1099
- * of dynamic properties determined by the Groups and Controls. This kind of
1100
- * object is nice to work with in certain circumstances. However, the api
1101
- * works differently. In the API a Job is a record, and references a number of
1102
- * Field records. Each Field stores the value. Comparing these two models we
1103
- * have:
1114
+ * we store them as dynamic properties of the job e.g. job.foo.bar, the other
1115
+ * is in an array of Field. The former is nice to work with in clients,
1116
+ * whereas the latter more suited to relational database.
1117
+ *
1118
+ * A) job with dynamic properties, e.g.
1119
+ * {
1120
+ * id: <guid>,
1121
+ * reference: "something"
1122
+ * <other static job properties>...
1123
+ * foo: {
1124
+ * bar: "value"
1125
+ * }
1126
+ * }
1127
+ *
1128
+ * B) job with array of Field
1129
+ * {
1130
+ * id: <guid>,
1131
+ * reference: "something"
1132
+ * <other static job properties>...
1133
+ * <will not have dynamic properties>...
1134
+ * fields: [
1135
+ * {
1136
+ * id: <guid>,
1137
+ * <other field properties>,
1138
+ * value: "value"
1139
+ * }
1140
+ * ]
1141
+ * }
1142
+ *
1143
+ * What this method does is given a VasJobModel that has been populated with
1144
+ * a VasJobDto in form B (i.e. `.fields` are populated - usually from a VASAPI
1145
+ * response), hydrate the dynamic properties of the model. NOTE: knowledge of
1146
+ * the Form that was used to crate the Job is required because it will contain
1147
+ * the Groups, which names are used in the hydrating
1148
+ *
1149
+ * @param {VasFormModel} formModel the VasFormModel that was used to create
1150
+ * the job
1151
+ * @return {void}
1152
+ */
1153
+ hydrateDynamicProps(formModel) {
1154
+ formModel.groups
1155
+ .forEach(group => {
1156
+ const p0 = group.name;
1157
+ group.controls
1158
+ .forEach(control => {
1159
+ const p1 = control.name;
1160
+ const f = this.fields.find(field => field.control === control.id);
1161
+ if (!f) {
1162
+ return;
1163
+ }
1164
+ this.setValueByPath(f.value, [p0, p1]);
1165
+ });
1166
+ });
1167
+ }
1168
+ /**
1169
+ * This method will hydrate the `fields` property of the model. The reason for
1170
+ * this is that we have different ways to store the field data. One way, is
1171
+ * we store them as dynamic properties of the job e.g. job.foo.bar, the other
1172
+ * is in an array of Field. The former is nice to work with in clients,
1173
+ * whereas the latter more suited to relational database.
1174
+ *
1104
1175
  * A) job with dynamic properties, e.g.
1105
1176
  * {
1106
1177
  * id: <guid>,
@@ -1167,7 +1238,7 @@ class VasJobModel extends VasRestrictedAccountObjectModel {
1167
1238
  * be generated, or to reuse an existing one from the map.
1168
1239
  * @param {Array<string>} controlNames if a value is provided, it will be used
1169
1240
  * to filter the fields that are returned.
1170
- * @return {Array<VasFieldDto>}
1241
+ * @return {void}
1171
1242
  */
1172
1243
  hydrateFields(formModel, controlFieldIdMap = new Map(), controlNames) {
1173
1244
  const fields = [];
@@ -1232,11 +1303,20 @@ class VasJobModel extends VasRestrictedAccountObjectModel {
1232
1303
  return;
1233
1304
  }
1234
1305
  case 2: {
1306
+ if (!this.$this[path[0]]) {
1307
+ this.$this[path[0]] = {};
1308
+ }
1235
1309
  // eslint-disable-next-line max-len
1236
1310
  this.$this[path[0]][path[1]] = value;
1237
1311
  return;
1238
1312
  }
1239
1313
  case 3: {
1314
+ if (!this.$this[path[0]]) {
1315
+ this.$this[path[0]] = {};
1316
+ }
1317
+ if (!this.$this[path[0]][path[1]]) {
1318
+ this.$this[path[0]][path[1]] = {};
1319
+ }
1240
1320
  // eslint-disable-next-line max-len
1241
1321
  this.$this[path[0]][path[1]][path[2]] = value;
1242
1322
  return;