@opencrvs/toolkit 2.0.0-rc.f035e63 → 2.0.0-rc.f1925c7

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.
@@ -190,6 +190,8 @@ function ensureImports(sourceFile) {
190
190
  var DEFINE_CONFIG_NAME2 = "defineConfig";
191
191
  var ACTIONS_PROPERTY_NAME2 = "actions";
192
192
  var TYPE_PROPERTY_NAME2 = "type";
193
+ var ID_PROPERTY_NAME = "id";
194
+ var FLAGS_PROPERTY_NAME = "flags";
193
195
  var CUSTOM_ACTION_TYPE_PROPERTY_NAME = "customActionType";
194
196
  var AUDIT_HISTORY_LABEL_PROPERTY_NAME = "auditHistoryLabel";
195
197
  var DEDUPLICATION_PROPERTY_NAME = "deduplication";
@@ -199,9 +201,12 @@ var FORM_PROPERTY_NAME = "form";
199
201
  var ACTION_TYPE_ENUM_NAME = "ActionType";
200
202
  var VALIDATE_MEMBER_NAME = "VALIDATE";
201
203
  var CUSTOM_MEMBER_NAME = "CUSTOM";
204
+ var REGISTER_MEMBER_NAME2 = "REGISTER";
202
205
  var VALIDATE_STRING_LITERAL = "VALIDATE";
203
206
  var CUSTOM_STRING_LITERAL = "CUSTOM";
207
+ var REGISTER_STRING_LITERAL2 = "REGISTER";
204
208
  var VALIDATE_DECLARATION_VALUE = "VALIDATE_DECLARATION";
209
+ var VALIDATED_FLAG_VALUE = "validated";
205
210
  function isValidateType(typeInitializer) {
206
211
  if (typeInitializer.getKind() === import_ts_morph2.SyntaxKind.PropertyAccessExpression) {
207
212
  const propAccess = typeInitializer.asKindOrThrow(
@@ -325,6 +330,50 @@ function transformValidateActionToCustom(obj, eventType) {
325
330
  }
326
331
  return true;
327
332
  }
333
+ function isRegisterAction(action) {
334
+ const typeProperty = action.getProperty(TYPE_PROPERTY_NAME2);
335
+ if (!typeProperty || !import_ts_morph2.Node.isPropertyAssignment(typeProperty)) return false;
336
+ const typeInit = typeProperty.getInitializer();
337
+ if (!typeInit) return false;
338
+ if (import_ts_morph2.Node.isPropertyAccessExpression(typeInit)) {
339
+ const enumExpr = typeInit.getExpression();
340
+ return import_ts_morph2.Node.isIdentifier(enumExpr) && enumExpr.getText() === ACTION_TYPE_ENUM_NAME && typeInit.getName() === REGISTER_MEMBER_NAME2;
341
+ }
342
+ if (import_ts_morph2.Node.isStringLiteral(typeInit)) {
343
+ return typeInit.getLiteralValue() === REGISTER_STRING_LITERAL2;
344
+ }
345
+ return false;
346
+ }
347
+ function arrayHasElementWithId(array3, id) {
348
+ for (const el of array3.getElements()) {
349
+ if (!import_ts_morph2.Node.isObjectLiteralExpression(el)) continue;
350
+ const idProp = el.getProperty(ID_PROPERTY_NAME);
351
+ if (!idProp || !import_ts_morph2.Node.isPropertyAssignment(idProp)) continue;
352
+ const idInit = idProp.getInitializer();
353
+ if (idInit && import_ts_morph2.Node.isStringLiteral(idInit) && idInit.getLiteralValue() === id) {
354
+ return true;
355
+ }
356
+ }
357
+ return false;
358
+ }
359
+ function ensureRegisterRemovesValidatedFlag(action) {
360
+ const entry = `{ id: '${VALIDATED_FLAG_VALUE}', operation: 'remove' }`;
361
+ const flagsProp = action.getProperty(FLAGS_PROPERTY_NAME);
362
+ if (flagsProp && import_ts_morph2.Node.isPropertyAssignment(flagsProp)) {
363
+ const flagsInit = flagsProp.getInitializer();
364
+ if (flagsInit && import_ts_morph2.Node.isArrayLiteralExpression(flagsInit)) {
365
+ if (arrayHasElementWithId(flagsInit, VALIDATED_FLAG_VALUE)) return false;
366
+ flagsInit.addElement(entry);
367
+ return true;
368
+ }
369
+ return false;
370
+ }
371
+ action.addPropertyAssignment({
372
+ name: FLAGS_PROPERTY_NAME,
373
+ initializer: `[${entry}]`
374
+ });
375
+ return true;
376
+ }
328
377
  function processFile2(filePath, project) {
329
378
  const sourceFile = project.getSourceFile(filePath);
330
379
  if (!sourceFile) return 0;
@@ -390,6 +439,18 @@ function processFile2(filePath, project) {
390
439
  ` [${import_path2.default.relative(process.cwd(), filePath)}] Added top-level 'flags' array to defineConfig`
391
440
  );
392
441
  }
442
+ if (configHadValidateAction) {
443
+ for (const element of actionsInitializer.getElements()) {
444
+ if (!import_ts_morph2.Node.isObjectLiteralExpression(element)) continue;
445
+ if (!isRegisterAction(element)) continue;
446
+ if (ensureRegisterRemovesValidatedFlag(element)) {
447
+ transformedCount++;
448
+ console.log(
449
+ ` [${import_path2.default.relative(process.cwd(), filePath)}] Added '${VALIDATED_FLAG_VALUE}' remove-flag to REGISTER action`
450
+ );
451
+ }
452
+ }
453
+ }
393
454
  if (fileNeedsImports) {
394
455
  ensureImports(sourceFile);
395
456
  }
@@ -436,7 +497,7 @@ Saving ${modifiedFiles.length} modified file(s)...`);
436
497
  }
437
498
  console.log(
438
499
  `
439
- Done. Transformed ${totalTransformed} VALIDATE action(s) to CUSTOM with customActionType: '${VALIDATE_DECLARATION_VALUE}'.`
500
+ Done. Applied ${totalTransformed} change(s): VALIDATE \u2192 CUSTOM (${VALIDATE_DECLARATION_VALUE}) and '${VALIDATED_FLAG_VALUE}' remove-flag on matching REGISTER actions.`
440
501
  );
441
502
  }
442
503
 
@@ -679,14 +740,14 @@ var QUERY_PROPERTY_NAME = "query";
679
740
  var TYPE_PROPERTY_NAME5 = "type";
680
741
  var CLAUSES_PROPERTY_NAME = "clauses";
681
742
  var STATUS_PROPERTY_NAME = "status";
682
- var FLAGS_PROPERTY_NAME = "flags";
743
+ var FLAGS_PROPERTY_NAME2 = "flags";
683
744
  var TERMS_PROPERTY_NAME = "terms";
684
745
  var TERM_PROPERTY_NAME = "term";
685
746
  var ANY_OF_PROPERTY_NAME = "anyOf";
686
747
  var VALIDATED_STATUS_VALUE = "VALIDATED";
687
- var VALIDATED_FLAG_VALUE = "validated";
748
+ var VALIDATED_FLAG_VALUE2 = "validated";
688
749
  function addValidatedFlag(clause) {
689
- const flagsProperty = clause.getProperty(FLAGS_PROPERTY_NAME);
750
+ const flagsProperty = clause.getProperty(FLAGS_PROPERTY_NAME2);
690
751
  if (flagsProperty && import_ts_morph5.Node.isPropertyAssignment(flagsProperty)) {
691
752
  const flagsInit = flagsProperty.getInitializer();
692
753
  if (flagsInit && import_ts_morph5.Node.isObjectLiteralExpression(flagsInit)) {
@@ -694,19 +755,19 @@ function addValidatedFlag(clause) {
694
755
  if (anyOfProperty && import_ts_morph5.Node.isPropertyAssignment(anyOfProperty)) {
695
756
  const anyOfInit = anyOfProperty.getInitializer();
696
757
  if (anyOfInit && import_ts_morph5.Node.isArrayLiteralExpression(anyOfInit)) {
697
- anyOfInit.addElement(`'${VALIDATED_FLAG_VALUE}'`);
758
+ anyOfInit.addElement(`'${VALIDATED_FLAG_VALUE2}'`);
698
759
  }
699
760
  } else {
700
761
  flagsInit.addPropertyAssignment({
701
762
  name: ANY_OF_PROPERTY_NAME,
702
- initializer: `['${VALIDATED_FLAG_VALUE}']`
763
+ initializer: `['${VALIDATED_FLAG_VALUE2}']`
703
764
  });
704
765
  }
705
766
  }
706
767
  } else {
707
768
  clause.addPropertyAssignment({
708
- name: FLAGS_PROPERTY_NAME,
709
- initializer: `{ ${ANY_OF_PROPERTY_NAME}: ['${VALIDATED_FLAG_VALUE}'] }`
769
+ name: FLAGS_PROPERTY_NAME2,
770
+ initializer: `{ ${ANY_OF_PROPERTY_NAME}: ['${VALIDATED_FLAG_VALUE2}'] }`
710
771
  });
711
772
  }
712
773
  }
@@ -872,23 +933,26 @@ var DEFINE_CONFIG_NAME4 = "defineConfig";
872
933
  var DEFINE_WORKQUEUES_NAME2 = "defineWorkqueues";
873
934
  var ACTIONS_PROPERTY_NAME4 = "actions";
874
935
  var ACTION_PROPERTY_NAME = "action";
875
- var FLAGS_PROPERTY_NAME2 = "flags";
936
+ var FLAGS_PROPERTY_NAME3 = "flags";
876
937
  var QUERY_PROPERTY_NAME2 = "query";
877
938
  var TYPE_PROPERTY_NAME6 = "type";
939
+ var ID_PROPERTY_NAME2 = "id";
878
940
  var CLAUSES_PROPERTY_NAME2 = "clauses";
879
941
  var ANY_OF_PROPERTY_NAME2 = "anyOf";
880
942
  var FLAG_ID = "pending-first-certificate-issuance";
881
943
  var REGISTER_TYPE = "REGISTER";
882
944
  var PRINT_CERTIFICATE_TYPE = "PRINT_CERTIFICATE";
883
- var FLAG_DEFINITION_INITIALIZER = `{
945
+ function flagDefinitionInitializer(eventType) {
946
+ return `{
884
947
  id: '${FLAG_ID}',
885
948
  label: {
886
- id: 'event.birth.flag.${FLAG_ID}',
949
+ id: 'event.${eventType}.flag.${FLAG_ID}',
887
950
  defaultMessage: 'Pending first certificate issuance',
888
951
  description: 'Flag label for first certificate issuance'
889
952
  },
890
953
  requiresAction: true
891
954
  }`;
955
+ }
892
956
  function isActionOfType(obj, typeName) {
893
957
  const typeProperty = obj.getProperty(TYPE_PROPERTY_NAME6);
894
958
  if (!typeProperty || !import_ts_morph6.Node.isPropertyAssignment(typeProperty)) return false;
@@ -902,18 +966,20 @@ function isActionOfType(obj, typeName) {
902
966
  }
903
967
  return false;
904
968
  }
905
- function isBirthConfig(configArg) {
906
- const typeProperty = configArg.getProperty(TYPE_PROPERTY_NAME6);
907
- if (!typeProperty || !import_ts_morph6.Node.isPropertyAssignment(typeProperty)) return false;
908
- const initializer = typeProperty.getInitializer();
909
- if (!initializer) return false;
910
- if (import_ts_morph6.Node.isStringLiteral(initializer)) {
911
- return initializer.getLiteralValue().toLowerCase() === "birth";
912
- }
913
- if (import_ts_morph6.Node.isPropertyAccessExpression(initializer)) {
914
- return initializer.getName().toLowerCase() === "birth";
969
+ function getEventType(configArg) {
970
+ for (const propName of [TYPE_PROPERTY_NAME6, ID_PROPERTY_NAME2]) {
971
+ const prop = configArg.getProperty(propName);
972
+ if (!prop || !import_ts_morph6.Node.isPropertyAssignment(prop)) continue;
973
+ const initializer = prop.getInitializer();
974
+ if (!initializer) continue;
975
+ if (import_ts_morph6.Node.isStringLiteral(initializer)) {
976
+ return initializer.getLiteralValue().toLowerCase();
977
+ }
978
+ if (import_ts_morph6.Node.isPropertyAccessExpression(initializer)) {
979
+ return initializer.getName().toLowerCase();
980
+ }
915
981
  }
916
- return false;
982
+ return null;
917
983
  }
918
984
  function hasElementWithId(array3, id) {
919
985
  for (const el of array3.getElements()) {
@@ -927,27 +993,28 @@ function hasElementWithId(array3, id) {
927
993
  }
928
994
  return false;
929
995
  }
930
- function ensureTopLevelFlagDefinition(configArg) {
931
- const flagsProp = configArg.getProperty(FLAGS_PROPERTY_NAME2);
996
+ function ensureTopLevelFlagDefinition(configArg, eventType) {
997
+ const initializer = flagDefinitionInitializer(eventType);
998
+ const flagsProp = configArg.getProperty(FLAGS_PROPERTY_NAME3);
932
999
  if (flagsProp && import_ts_morph6.Node.isPropertyAssignment(flagsProp)) {
933
1000
  const flagsInit = flagsProp.getInitializer();
934
1001
  if (flagsInit && import_ts_morph6.Node.isArrayLiteralExpression(flagsInit)) {
935
1002
  if (hasElementWithId(flagsInit, FLAG_ID)) return false;
936
- flagsInit.addElement(FLAG_DEFINITION_INITIALIZER);
1003
+ flagsInit.addElement(initializer);
937
1004
  return true;
938
1005
  }
939
1006
  return false;
940
1007
  }
941
1008
  configArg.addPropertyAssignment({
942
- name: FLAGS_PROPERTY_NAME2,
1009
+ name: FLAGS_PROPERTY_NAME3,
943
1010
  initializer: `[
944
- ${FLAG_DEFINITION_INITIALIZER}
1011
+ ${initializer}
945
1012
  ]`
946
1013
  });
947
1014
  return true;
948
1015
  }
949
1016
  function ensureActionOperationFlag(action, operation) {
950
- const flagsProp = action.getProperty(FLAGS_PROPERTY_NAME2);
1017
+ const flagsProp = action.getProperty(FLAGS_PROPERTY_NAME3);
951
1018
  const entry = `{ id: '${FLAG_ID}', operation: '${operation}' }`;
952
1019
  if (flagsProp && import_ts_morph6.Node.isPropertyAssignment(flagsProp)) {
953
1020
  const flagsInit = flagsProp.getInitializer();
@@ -959,16 +1026,18 @@ function ensureActionOperationFlag(action, operation) {
959
1026
  return false;
960
1027
  }
961
1028
  action.addPropertyAssignment({
962
- name: FLAGS_PROPERTY_NAME2,
1029
+ name: FLAGS_PROPERTY_NAME3,
963
1030
  initializer: `[${entry}]`
964
1031
  });
965
1032
  return true;
966
1033
  }
967
- function processBirthConfig(configArg, relPath) {
1034
+ function processConfig(configArg, eventType, relPath) {
968
1035
  let changes = 0;
969
- if (ensureTopLevelFlagDefinition(configArg)) {
1036
+ if (ensureTopLevelFlagDefinition(configArg, eventType)) {
970
1037
  changes++;
971
- console.log(` [${relPath}] Added '${FLAG_ID}' to top-level flags`);
1038
+ console.log(
1039
+ ` [${relPath}] (${eventType}) Added '${FLAG_ID}' to top-level flags`
1040
+ );
972
1041
  }
973
1042
  const actionsProp = configArg.getProperty(ACTIONS_PROPERTY_NAME4);
974
1043
  if (!actionsProp || !import_ts_morph6.Node.isPropertyAssignment(actionsProp)) return changes;
@@ -982,7 +1051,7 @@ function processBirthConfig(configArg, relPath) {
982
1051
  if (ensureActionOperationFlag(element, "add")) {
983
1052
  changes++;
984
1053
  console.log(
985
- ` [${relPath}] Added '${FLAG_ID}' add-flag to REGISTER action`
1054
+ ` [${relPath}] (${eventType}) Added '${FLAG_ID}' add-flag to REGISTER action`
986
1055
  );
987
1056
  }
988
1057
  }
@@ -990,7 +1059,7 @@ function processBirthConfig(configArg, relPath) {
990
1059
  if (ensureActionOperationFlag(element, "remove")) {
991
1060
  changes++;
992
1061
  console.log(
993
- ` [${relPath}] Added '${FLAG_ID}' remove-flag to PRINT_CERTIFICATE action`
1062
+ ` [${relPath}] (${eventType}) Added '${FLAG_ID}' remove-flag to PRINT_CERTIFICATE action`
994
1063
  );
995
1064
  }
996
1065
  }
@@ -1019,7 +1088,7 @@ function hasPrintCertificateAction(workqueue) {
1019
1088
  return false;
1020
1089
  }
1021
1090
  function addFlagToQueryClause(clause) {
1022
- const flagsProp = clause.getProperty(FLAGS_PROPERTY_NAME2);
1091
+ const flagsProp = clause.getProperty(FLAGS_PROPERTY_NAME3);
1023
1092
  if (flagsProp && import_ts_morph6.Node.isPropertyAssignment(flagsProp)) {
1024
1093
  const flagsInit = flagsProp.getInitializer();
1025
1094
  if (flagsInit && import_ts_morph6.Node.isObjectLiteralExpression(flagsInit)) {
@@ -1044,7 +1113,7 @@ function addFlagToQueryClause(clause) {
1044
1113
  }
1045
1114
  } else {
1046
1115
  clause.addPropertyAssignment({
1047
- name: FLAGS_PROPERTY_NAME2,
1116
+ name: FLAGS_PROPERTY_NAME3,
1048
1117
  initializer: `{ ${ANY_OF_PROPERTY_NAME2}: ['${FLAG_ID}'] }`
1049
1118
  });
1050
1119
  return true;
@@ -1088,8 +1157,9 @@ function processFile6(filePath, project) {
1088
1157
  if (args.length === 0) continue;
1089
1158
  const configArg = args[0];
1090
1159
  if (!import_ts_morph6.Node.isObjectLiteralExpression(configArg)) continue;
1091
- if (!isBirthConfig(configArg)) continue;
1092
- totalChanges += processBirthConfig(configArg, relPath);
1160
+ const eventType = getEventType(configArg);
1161
+ if (!eventType) continue;
1162
+ totalChanges += processConfig(configArg, eventType, relPath);
1093
1163
  }
1094
1164
  if (calleeName === DEFINE_WORKQUEUES_NAME2) {
1095
1165
  const args = call.getArguments();
@@ -1118,7 +1188,7 @@ function processFile6(filePath, project) {
1118
1188
  async function main6() {
1119
1189
  const srcDir = import_path6.default.join(process.cwd(), "src");
1120
1190
  console.log(
1121
- `Scanning for birth defineConfig and PRINT_CERTIFICATE workqueues in: ${srcDir}
1191
+ `Scanning for defineConfig and PRINT_CERTIFICATE workqueues in: ${srcDir}
1122
1192
  `
1123
1193
  );
1124
1194
  const project = new import_ts_morph6.Project({
@@ -1143,7 +1213,7 @@ async function main6() {
1143
1213
  }
1144
1214
  if (modifiedFiles.length === 0) {
1145
1215
  console.log(
1146
- "No birth configs or PRINT_CERTIFICATE workqueues found. Nothing to do."
1216
+ "No event configs or PRINT_CERTIFICATE workqueues found. Nothing to do."
1147
1217
  );
1148
1218
  return;
1149
1219
  }
@@ -2122,6 +2192,10 @@ var scopeByEvent = z3.preprocess(
2122
2192
  (val) => val === void 0 ? void 0 : [val].flat(),
2123
2193
  z3.array(z3.string()).optional()
2124
2194
  ).describe("Event type, e.g. birth, death");
2195
+ var userRole = z3.preprocess(
2196
+ (val) => val === void 0 ? void 0 : [val].flat(),
2197
+ z3.array(z3.string()).optional()
2198
+ ).describe("User role, e.g. admin, field agent");
2125
2199
  var scopeOptionsPlaceEvent = z3.object({
2126
2200
  event: scopeByEvent,
2127
2201
  placeOfEvent: JurisdictionFilter.optional()
@@ -2145,6 +2219,9 @@ var CustomActionScopeOptions = AllRecordScopeOptions.extend({
2145
2219
  var AccessLevelOptions = z3.object({
2146
2220
  accessLevel: JurisdictionFilter.optional()
2147
2221
  });
2222
+ var AllUserScopeOptions = AccessLevelOptions.extend({
2223
+ role: userRole.optional()
2224
+ });
2148
2225
  var WorkqueueOrDashboardOptions = z3.object({
2149
2226
  ids: z3.preprocess(
2150
2227
  (val) => val === void 0 ? void 0 : [val].flat(),
@@ -2154,6 +2231,7 @@ var WorkqueueOrDashboardOptions = z3.object({
2154
2231
  var AllScopeOptions = z3.object({
2155
2232
  ...AllRecordScopeOptions.shape,
2156
2233
  ...AccessLevelOptions.shape,
2234
+ ...AllUserScopeOptions.shape,
2157
2235
  ...WorkqueueOrDashboardOptions.shape
2158
2236
  });
2159
2237
  var ScopeOptionKey = AllScopeOptions.keyof();
@@ -2226,6 +2304,30 @@ var RecordScopeV2 = z3.discriminatedUnion("type", [
2226
2304
  ]).describe(
2227
2305
  "Scopes used to check user's permission to perform actions on a record."
2228
2306
  );
2307
+ var SystemScopeType = z3.enum([
2308
+ "organisation.read-locations",
2309
+ "user.read",
2310
+ "user.create",
2311
+ "user.edit",
2312
+ "user.search"
2313
+ ]);
2314
+ var UserScopeType = SystemScopeType.extract([
2315
+ "user.read",
2316
+ "user.create",
2317
+ "user.edit",
2318
+ "user.search"
2319
+ ]);
2320
+ var ScopesWithRoleOption = UserScopeType.extract(["user.edit", "user.create"]);
2321
+ var UserScopeV2 = z3.discriminatedUnion("type", [
2322
+ z3.object({
2323
+ type: ScopesWithRoleOption,
2324
+ options: AllUserScopeOptions.optional()
2325
+ }),
2326
+ z3.object({
2327
+ type: UserScopeType.extract(["user.read", "user.search"]),
2328
+ options: AccessLevelOptions.optional()
2329
+ })
2330
+ ]);
2229
2331
  var ResolvedRecordScopeV2 = z3.discriminatedUnion("type", [
2230
2332
  z3.object({
2231
2333
  type: ScopesWithPlaceEventOptions,
@@ -2246,16 +2348,14 @@ var ResolvedRecordScopeV2 = z3.discriminatedUnion("type", [
2246
2348
  }).optional()
2247
2349
  })
2248
2350
  ]).describe("Resolved scope with location/user IDs instead of filters.");
2249
- var SystemScopeType = z3.enum([
2250
- "organisation.read-locations",
2251
- "user.read",
2252
- "user.create",
2253
- "user.edit"
2254
- ]);
2255
2351
  var Scope2 = z3.discriminatedUnion("type", [
2256
2352
  z3.object({ type: PlainScopeType }),
2257
2353
  ...RecordScopeV2.options,
2258
- z3.object({ type: SystemScopeType, options: AccessLevelOptions.optional() }),
2354
+ ...UserScopeV2.options,
2355
+ z3.object({
2356
+ type: z3.literal("organisation.read-locations"),
2357
+ options: AccessLevelOptions.optional()
2358
+ }),
2259
2359
  z3.object({
2260
2360
  type: z3.literal("workqueue"),
2261
2361
  options: WorkqueueOrDashboardOptions
@@ -1 +1 @@
1
- {"version":3,"file":"make-built-in-validate-actions-custom.d.ts","sourceRoot":"","sources":["../../../src/migrations/v2.0/make-built-in-validate-actions-custom.ts"],"names":[],"mappings":"AA2WA,iBAAe,IAAI,kBAiDlB;AAED,OAAO,EAAE,IAAI,EAAE,CAAA"}
1
+ {"version":3,"file":"make-built-in-validate-actions-custom.d.ts","sourceRoot":"","sources":["../../../src/migrations/v2.0/make-built-in-validate-actions-custom.ts"],"names":[],"mappings":"AAodA,iBAAe,IAAI,kBAiDlB;AAED,OAAO,EAAE,IAAI,EAAE,CAAA"}
@@ -41,6 +41,10 @@ exports.main = main;
41
41
  * - Removes the `deduplication` property from those objects
42
42
  * - Replaces `review: { fields: <value> }` with `form: <value>`, or
43
43
  * `review: <expr>` with `form: <expr>.fields` when the value is not an inline object
44
+ * - Adds the top-level `validated` flag definition and `{ id: 'validated',
45
+ * operation: 'add' }` to the (now custom) validate action
46
+ * - Adds `{ id: 'validated', operation: 'remove' }` to every `REGISTER` action
47
+ * in the same config, closing the flag's lifecycle when registration happens
44
48
  * - Saves the modified files in-place
45
49
  */
46
50
  const ts_morph_1 = require("ts-morph");
@@ -74,6 +78,8 @@ function ensureImports(sourceFile) {
74
78
  const DEFINE_CONFIG_NAME = 'defineConfig';
75
79
  const ACTIONS_PROPERTY_NAME = 'actions';
76
80
  const TYPE_PROPERTY_NAME = 'type';
81
+ const ID_PROPERTY_NAME = 'id';
82
+ const FLAGS_PROPERTY_NAME = 'flags';
77
83
  const CUSTOM_ACTION_TYPE_PROPERTY_NAME = 'customActionType';
78
84
  const AUDIT_HISTORY_LABEL_PROPERTY_NAME = 'auditHistoryLabel';
79
85
  const DEDUPLICATION_PROPERTY_NAME = 'deduplication';
@@ -83,9 +89,12 @@ const FORM_PROPERTY_NAME = 'form';
83
89
  const ACTION_TYPE_ENUM_NAME = 'ActionType';
84
90
  const VALIDATE_MEMBER_NAME = 'VALIDATE';
85
91
  const CUSTOM_MEMBER_NAME = 'CUSTOM';
92
+ const REGISTER_MEMBER_NAME = 'REGISTER';
86
93
  const VALIDATE_STRING_LITERAL = 'VALIDATE';
87
94
  const CUSTOM_STRING_LITERAL = 'CUSTOM';
95
+ const REGISTER_STRING_LITERAL = 'REGISTER';
88
96
  const VALIDATE_DECLARATION_VALUE = 'VALIDATE_DECLARATION';
97
+ const VALIDATED_FLAG_VALUE = 'validated';
89
98
  function isValidateType(typeInitializer) {
90
99
  // Matches: ActionType.VALIDATE
91
100
  if (typeInitializer.getKind() === ts_morph_1.SyntaxKind.PropertyAccessExpression) {
@@ -230,6 +239,72 @@ function transformValidateActionToCustom(obj, eventType) {
230
239
  }
231
240
  return true;
232
241
  }
242
+ function isRegisterAction(action) {
243
+ const typeProperty = action.getProperty(TYPE_PROPERTY_NAME);
244
+ if (!typeProperty || !ts_morph_1.Node.isPropertyAssignment(typeProperty))
245
+ return false;
246
+ const typeInit = typeProperty.getInitializer();
247
+ if (!typeInit)
248
+ return false;
249
+ // Matches: ActionType.REGISTER
250
+ if (ts_morph_1.Node.isPropertyAccessExpression(typeInit)) {
251
+ const enumExpr = typeInit.getExpression();
252
+ return (ts_morph_1.Node.isIdentifier(enumExpr) &&
253
+ enumExpr.getText() === ACTION_TYPE_ENUM_NAME &&
254
+ typeInit.getName() === REGISTER_MEMBER_NAME);
255
+ }
256
+ // Matches: "REGISTER"
257
+ if (ts_morph_1.Node.isStringLiteral(typeInit)) {
258
+ return typeInit.getLiteralValue() === REGISTER_STRING_LITERAL;
259
+ }
260
+ return false;
261
+ }
262
+ /**
263
+ * Returns true if an array literal contains an object literal with `id: <id>`.
264
+ */
265
+ function arrayHasElementWithId(array, id) {
266
+ for (const el of array.getElements()) {
267
+ if (!ts_morph_1.Node.isObjectLiteralExpression(el))
268
+ continue;
269
+ const idProp = el.getProperty(ID_PROPERTY_NAME);
270
+ if (!idProp || !ts_morph_1.Node.isPropertyAssignment(idProp))
271
+ continue;
272
+ const idInit = idProp.getInitializer();
273
+ if (idInit &&
274
+ ts_morph_1.Node.isStringLiteral(idInit) &&
275
+ idInit.getLiteralValue() === id) {
276
+ return true;
277
+ }
278
+ }
279
+ return false;
280
+ }
281
+ /**
282
+ * Adds `{ id: 'validated', operation: 'remove' }` to a REGISTER action's
283
+ * `flags` array. Skips if any `validated` flag entry is already present —
284
+ * this keeps the codemod idempotent and avoids stomping on an intentional
285
+ * `add` op a maintainer may have authored manually.
286
+ *
287
+ * Returns true if a change was made.
288
+ */
289
+ function ensureRegisterRemovesValidatedFlag(action) {
290
+ const entry = `{ id: '${VALIDATED_FLAG_VALUE}', operation: 'remove' }`;
291
+ const flagsProp = action.getProperty(FLAGS_PROPERTY_NAME);
292
+ if (flagsProp && ts_morph_1.Node.isPropertyAssignment(flagsProp)) {
293
+ const flagsInit = flagsProp.getInitializer();
294
+ if (flagsInit && ts_morph_1.Node.isArrayLiteralExpression(flagsInit)) {
295
+ if (arrayHasElementWithId(flagsInit, VALIDATED_FLAG_VALUE))
296
+ return false;
297
+ flagsInit.addElement(entry);
298
+ return true;
299
+ }
300
+ return false;
301
+ }
302
+ action.addPropertyAssignment({
303
+ name: FLAGS_PROPERTY_NAME,
304
+ initializer: `[${entry}]`
305
+ });
306
+ return true;
307
+ }
233
308
  function processFile(filePath, project) {
234
309
  const sourceFile = project.getSourceFile(filePath);
235
310
  if (!sourceFile)
@@ -297,6 +372,20 @@ function processFile(filePath, project) {
297
372
  });
298
373
  console.log(` [${path_1.default.relative(process.cwd(), filePath)}] Added top-level 'flags' array to defineConfig`);
299
374
  }
375
+ // Close the `validated` flag's lifecycle: when the validate action adds the
376
+ // flag, the matching REGISTER action(s) must remove it on registration.
377
+ if (configHadValidateAction) {
378
+ for (const element of actionsInitializer.getElements()) {
379
+ if (!ts_morph_1.Node.isObjectLiteralExpression(element))
380
+ continue;
381
+ if (!isRegisterAction(element))
382
+ continue;
383
+ if (ensureRegisterRemovesValidatedFlag(element)) {
384
+ transformedCount++;
385
+ console.log(` [${path_1.default.relative(process.cwd(), filePath)}] Added '${VALIDATED_FLAG_VALUE}' remove-flag to REGISTER action`);
386
+ }
387
+ }
388
+ }
300
389
  if (fileNeedsImports) {
301
390
  ensureImports(sourceFile);
302
391
  }
@@ -338,6 +427,6 @@ function main() {
338
427
  yield sourceFile.save();
339
428
  console.log(` Saved: ${path_1.default.relative(process.cwd(), filePath)}`);
340
429
  }
341
- console.log(`\nDone. Transformed ${totalTransformed} VALIDATE action(s) to CUSTOM with customActionType: '${VALIDATE_DECLARATION_VALUE}'.`);
430
+ console.log(`\nDone. Applied ${totalTransformed} change(s): VALIDATE → CUSTOM (${VALIDATE_DECLARATION_VALUE}) and '${VALIDATED_FLAG_VALUE}' remove-flag on matching REGISTER actions.`);
342
431
  });
343
432
  }
@@ -110,6 +110,10 @@ var scopeByEvent = z2.preprocess(
110
110
  (val) => val === void 0 ? void 0 : [val].flat(),
111
111
  z2.array(z2.string()).optional()
112
112
  ).describe("Event type, e.g. birth, death");
113
+ var userRole = z2.preprocess(
114
+ (val) => val === void 0 ? void 0 : [val].flat(),
115
+ z2.array(z2.string()).optional()
116
+ ).describe("User role, e.g. admin, field agent");
113
117
  var scopeOptionsPlaceEvent = z2.object({
114
118
  event: scopeByEvent,
115
119
  placeOfEvent: JurisdictionFilter.optional()
@@ -133,6 +137,9 @@ var CustomActionScopeOptions = AllRecordScopeOptions.extend({
133
137
  var AccessLevelOptions = z2.object({
134
138
  accessLevel: JurisdictionFilter.optional()
135
139
  });
140
+ var AllUserScopeOptions = AccessLevelOptions.extend({
141
+ role: userRole.optional()
142
+ });
136
143
  var WorkqueueOrDashboardOptions = z2.object({
137
144
  ids: z2.preprocess(
138
145
  (val) => val === void 0 ? void 0 : [val].flat(),
@@ -142,6 +149,7 @@ var WorkqueueOrDashboardOptions = z2.object({
142
149
  var AllScopeOptions = z2.object({
143
150
  ...AllRecordScopeOptions.shape,
144
151
  ...AccessLevelOptions.shape,
152
+ ...AllUserScopeOptions.shape,
145
153
  ...WorkqueueOrDashboardOptions.shape
146
154
  });
147
155
  var ScopeOptionKey = AllScopeOptions.keyof();
@@ -214,6 +222,30 @@ var RecordScopeV2 = z2.discriminatedUnion("type", [
214
222
  ]).describe(
215
223
  "Scopes used to check user's permission to perform actions on a record."
216
224
  );
225
+ var SystemScopeType = z2.enum([
226
+ "organisation.read-locations",
227
+ "user.read",
228
+ "user.create",
229
+ "user.edit",
230
+ "user.search"
231
+ ]);
232
+ var UserScopeType = SystemScopeType.extract([
233
+ "user.read",
234
+ "user.create",
235
+ "user.edit",
236
+ "user.search"
237
+ ]);
238
+ var ScopesWithRoleOption = UserScopeType.extract(["user.edit", "user.create"]);
239
+ var UserScopeV2 = z2.discriminatedUnion("type", [
240
+ z2.object({
241
+ type: ScopesWithRoleOption,
242
+ options: AllUserScopeOptions.optional()
243
+ }),
244
+ z2.object({
245
+ type: UserScopeType.extract(["user.read", "user.search"]),
246
+ options: AccessLevelOptions.optional()
247
+ })
248
+ ]);
217
249
  var ResolvedRecordScopeV2 = z2.discriminatedUnion("type", [
218
250
  z2.object({
219
251
  type: ScopesWithPlaceEventOptions,
@@ -234,16 +266,14 @@ var ResolvedRecordScopeV2 = z2.discriminatedUnion("type", [
234
266
  }).optional()
235
267
  })
236
268
  ]).describe("Resolved scope with location/user IDs instead of filters.");
237
- var SystemScopeType = z2.enum([
238
- "organisation.read-locations",
239
- "user.read",
240
- "user.create",
241
- "user.edit"
242
- ]);
243
269
  var Scope = z2.discriminatedUnion("type", [
244
270
  z2.object({ type: PlainScopeType }),
245
271
  ...RecordScopeV2.options,
246
- z2.object({ type: SystemScopeType, options: AccessLevelOptions.optional() }),
272
+ ...UserScopeV2.options,
273
+ z2.object({
274
+ type: z2.literal("organisation.read-locations"),
275
+ options: AccessLevelOptions.optional()
276
+ }),
247
277
  z2.object({
248
278
  type: z2.literal("workqueue"),
249
279
  options: WorkqueueOrDashboardOptions