@ironcode/vas-lib 0.0.17 → 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.
@@ -439,7 +439,7 @@ class VasFieldModel extends VasJobDataModel {
439
439
  * @return {VasFieldModel}
440
440
  */
441
441
  static fromDto(fieldDto) {
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 || [], 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);
443
443
  }
444
444
  /**
445
445
  * @return {VasFieldDto}
@@ -468,7 +468,7 @@ class VasFieldModel extends VasJobDataModel {
468
468
  return {
469
469
  ...super.toApiDto(),
470
470
  control: this.control,
471
- value: this.prepareApiValue(this.value)
471
+ value: this.toApiValue(this.value)
472
472
  };
473
473
  }
474
474
  /**
@@ -479,7 +479,7 @@ class VasFieldModel extends VasJobDataModel {
479
479
  * @return {string | number | boolean}
480
480
  * @protected
481
481
  */
482
- prepareApiValue(value) {
482
+ toApiValue(value) {
483
483
  switch (typeof value) {
484
484
  case 'object': {
485
485
  return JSON.stringify(value);
@@ -498,6 +498,14 @@ class VasFieldModel extends VasJobDataModel {
498
498
  }
499
499
  }
500
500
  }
501
+ static fromApiValue(value) {
502
+ try {
503
+ return JSON.parse(value);
504
+ }
505
+ catch (e) {
506
+ return value;
507
+ }
508
+ }
501
509
  }
502
510
 
503
511
  /**
@@ -1103,15 +1111,67 @@ class VasJobModel extends VasRestrictedAccountObjectModel {
1103
1111
  /**
1104
1112
  * This method will hydrate the `fields` property of the model. The reason for
1105
1113
  * this is that we have different ways to store the field data. One way, is
1106
- * we store them as dynamic properties of the job. For example job.foo.bar,
1107
- * where `foo` is the name of a Group, and `bar` is the name of a control.
1108
- * Thus, when we create a job using a form in the client, the job object will
1109
- * have its static properties (id, account, reference etc), and also a number
1110
- * of dynamic properties determined by the Groups and Controls. This kind of
1111
- * object is nice to work with in certain circumstances. However, the api
1112
- * works differently. In the API a Job is a record, and references a number of
1113
- * Field records. Each Field stores the value. Comparing these two models we
1114
- * 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
+ *
1115
1175
  * A) job with dynamic properties, e.g.
1116
1176
  * {
1117
1177
  * id: <guid>,
@@ -1178,7 +1238,7 @@ class VasJobModel extends VasRestrictedAccountObjectModel {
1178
1238
  * be generated, or to reuse an existing one from the map.
1179
1239
  * @param {Array<string>} controlNames if a value is provided, it will be used
1180
1240
  * to filter the fields that are returned.
1181
- * @return {Array<VasFieldDto>}
1241
+ * @return {void}
1182
1242
  */
1183
1243
  hydrateFields(formModel, controlFieldIdMap = new Map(), controlNames) {
1184
1244
  const fields = [];
@@ -1243,11 +1303,20 @@ class VasJobModel extends VasRestrictedAccountObjectModel {
1243
1303
  return;
1244
1304
  }
1245
1305
  case 2: {
1306
+ if (!this.$this[path[0]]) {
1307
+ this.$this[path[0]] = {};
1308
+ }
1246
1309
  // eslint-disable-next-line max-len
1247
1310
  this.$this[path[0]][path[1]] = value;
1248
1311
  return;
1249
1312
  }
1250
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
+ }
1251
1320
  // eslint-disable-next-line max-len
1252
1321
  this.$this[path[0]][path[1]][path[2]] = value;
1253
1322
  return;