@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}
@@ -454,7 +465,7 @@ class VasFieldModel extends VasJobDataModel {
454
465
  * @return {object}
455
466
  */
456
467
  toApiDto() {
457
- return Object.assign(Object.assign({}, super.toApiDto()), { control: this.control, value: this.prepareApiValue(this.value) });
468
+ return Object.assign(Object.assign({}, super.toApiDto()), { control: this.control, value: this.toApiValue(this.value) });
458
469
  }
459
470
  /**
460
471
  * Prepares the value to be sent to the API. The api will only accept strings,
@@ -464,7 +475,7 @@ class VasFieldModel extends VasJobDataModel {
464
475
  * @return {string | number | boolean}
465
476
  * @protected
466
477
  */
467
- prepareApiValue(value) {
478
+ toApiValue(value) {
468
479
  switch (typeof value) {
469
480
  case 'object': {
470
481
  return JSON.stringify(value);
@@ -483,6 +494,14 @@ class VasFieldModel extends VasJobDataModel {
483
494
  }
484
495
  }
485
496
  }
497
+ static fromApiValue(value) {
498
+ try {
499
+ return JSON.parse(value);
500
+ }
501
+ catch (e) {
502
+ return value;
503
+ }
504
+ }
486
505
  }
487
506
 
488
507
  /**
@@ -1083,15 +1102,67 @@ class VasJobModel extends VasRestrictedAccountObjectModel {
1083
1102
  /**
1084
1103
  * This method will hydrate the `fields` property of the model. The reason for
1085
1104
  * this is that we have different ways to store the field data. One way, is
1086
- * we store them as dynamic properties of the job. For example job.foo.bar,
1087
- * where `foo` is the name of a Group, and `bar` is the name of a control.
1088
- * Thus, when we create a job using a form in the client, the job object will
1089
- * have its static properties (id, account, reference etc), and also a number
1090
- * of dynamic properties determined by the Groups and Controls. This kind of
1091
- * object is nice to work with in certain circumstances. However, the api
1092
- * works differently. In the API a Job is a record, and references a number of
1093
- * Field records. Each Field stores the value. Comparing these two models we
1094
- * have:
1105
+ * we store them as dynamic properties of the job e.g. job.foo.bar, the other
1106
+ * is in an array of Field. The former is nice to work with in clients,
1107
+ * whereas the latter more suited to relational database.
1108
+ *
1109
+ * A) job with dynamic properties, e.g.
1110
+ * {
1111
+ * id: <guid>,
1112
+ * reference: "something"
1113
+ * <other static job properties>...
1114
+ * foo: {
1115
+ * bar: "value"
1116
+ * }
1117
+ * }
1118
+ *
1119
+ * B) job with array of Field
1120
+ * {
1121
+ * id: <guid>,
1122
+ * reference: "something"
1123
+ * <other static job properties>...
1124
+ * <will not have dynamic properties>...
1125
+ * fields: [
1126
+ * {
1127
+ * id: <guid>,
1128
+ * <other field properties>,
1129
+ * value: "value"
1130
+ * }
1131
+ * ]
1132
+ * }
1133
+ *
1134
+ * What this method does is given a VasJobModel that has been populated with
1135
+ * a VasJobDto in form B (i.e. `.fields` are populated - usually from a VASAPI
1136
+ * response), hydrate the dynamic properties of the model. NOTE: knowledge of
1137
+ * the Form that was used to crate the Job is required because it will contain
1138
+ * the Groups, which names are used in the hydrating
1139
+ *
1140
+ * @param {VasFormModel} formModel the VasFormModel that was used to create
1141
+ * the job
1142
+ * @return {void}
1143
+ */
1144
+ hydrateDynamicProps(formModel) {
1145
+ formModel.groups
1146
+ .forEach(group => {
1147
+ const p0 = group.name;
1148
+ group.controls
1149
+ .forEach(control => {
1150
+ const p1 = control.name;
1151
+ const f = this.fields.find(field => field.control === control.id);
1152
+ if (!f) {
1153
+ return;
1154
+ }
1155
+ this.setValueByPath(f.value, [p0, p1]);
1156
+ });
1157
+ });
1158
+ }
1159
+ /**
1160
+ * This method will hydrate the `fields` property of the model. The reason for
1161
+ * this is that we have different ways to store the field data. One way, is
1162
+ * we store them as dynamic properties of the job e.g. job.foo.bar, the other
1163
+ * is in an array of Field. The former is nice to work with in clients,
1164
+ * whereas the latter more suited to relational database.
1165
+ *
1095
1166
  * A) job with dynamic properties, e.g.
1096
1167
  * {
1097
1168
  * id: <guid>,
@@ -1158,7 +1229,7 @@ class VasJobModel extends VasRestrictedAccountObjectModel {
1158
1229
  * be generated, or to reuse an existing one from the map.
1159
1230
  * @param {Array<string>} controlNames if a value is provided, it will be used
1160
1231
  * to filter the fields that are returned.
1161
- * @return {Array<VasFieldDto>}
1232
+ * @return {void}
1162
1233
  */
1163
1234
  hydrateFields(formModel, controlFieldIdMap = new Map(), controlNames) {
1164
1235
  const fields = [];
@@ -1223,11 +1294,20 @@ class VasJobModel extends VasRestrictedAccountObjectModel {
1223
1294
  return;
1224
1295
  }
1225
1296
  case 2: {
1297
+ if (!this.$this[path[0]]) {
1298
+ this.$this[path[0]] = {};
1299
+ }
1226
1300
  // eslint-disable-next-line max-len
1227
1301
  this.$this[path[0]][path[1]] = value;
1228
1302
  return;
1229
1303
  }
1230
1304
  case 3: {
1305
+ if (!this.$this[path[0]]) {
1306
+ this.$this[path[0]] = {};
1307
+ }
1308
+ if (!this.$this[path[0]][path[1]]) {
1309
+ this.$this[path[0]][path[1]] = {};
1310
+ }
1231
1311
  // eslint-disable-next-line max-len
1232
1312
  this.$this[path[0]][path[1]][path[2]] = value;
1233
1313
  return;