@opencrvs/toolkit 2.0.0-rc.a8e70b1 → 2.0.0-rc.ab3230c

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.
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env node
2
+
3
+ /*
4
+ * This Source Code Form is subject to the terms of the Mozilla Public
5
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
6
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7
+ *
8
+ * OpenCRVS is also distributed under the terms of the Civil Registration
9
+ * & Healthcare Disclaimer located at http://opencrvs.org/license.
10
+ *
11
+ * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
12
+ */
13
+
14
+ const { execSync } = require('child_process')
15
+ const path = require('path')
16
+ const fs = require('fs')
17
+
18
+ const REPO_URL = 'https://github.com/opencrvs/opencrvs-countryconfig.git'
19
+
20
+ const projectName = process.argv[2]
21
+
22
+ if (!projectName) {
23
+ console.error(
24
+ 'Please specify a project name:\n\n npm create @opencrvs/countryconfig <project-name>\n'
25
+ )
26
+ process.exit(1)
27
+ }
28
+
29
+ const targetDir = path.resolve(process.cwd(), projectName)
30
+
31
+ if (fs.existsSync(targetDir)) {
32
+ console.error(`Error: Directory "${projectName}" already exists.`)
33
+ process.exit(1)
34
+ }
35
+
36
+ console.log(`\nScaffolding OpenCRVS country config in ./${projectName}...\n`)
37
+
38
+ try {
39
+ execSync(`git clone --depth 1 ${REPO_URL} ${projectName}`, {
40
+ stdio: 'inherit'
41
+ })
42
+ } catch (err) {
43
+ console.error('Failed to clone the repository:', err.message)
44
+ process.exit(1)
45
+ }
46
+
47
+ try {
48
+ fs.rmSync(path.join(targetDir, '.git'), { recursive: true, force: true })
49
+ } catch (err) {
50
+ console.error('Failed to remove .git directory:', err.message)
51
+ process.exit(1)
52
+ }
53
+
54
+ const pkgPath = path.join(targetDir, 'package.json')
55
+ if (fs.existsSync(pkgPath)) {
56
+ try {
57
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
58
+ pkg.name = projectName
59
+ fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
60
+ } catch (err) {
61
+ console.error('Failed to update package.json:', err.message)
62
+ process.exit(1)
63
+ }
64
+ } else {
65
+ console.warn(
66
+ `Warning: No package.json found in the cloned repository. Project name was not updated.`
67
+ )
68
+ }
69
+
70
+ console.log(`\nDone! To get started:\n`)
71
+ console.log(` cd ${projectName}`)
72
+ console.log(` git init`)
73
+ console.log(` npm install\n`)
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@opencrvs/create-countryconfig",
3
+ "version": "1.0.0",
4
+ "description": "Scaffold a new OpenCRVS country configuration",
5
+ "bin": {
6
+ "create-countryconfig": "./index.js"
7
+ },
8
+ "files": ["index.js"],
9
+ "keywords": ["opencrvs", "countryconfig", "create"],
10
+ "license": "MPL-2.0"
11
+ }
package/dist/cli.js CHANGED
@@ -178,6 +178,8 @@ function ensureImports(sourceFile) {
178
178
  var DEFINE_CONFIG_NAME2 = "defineConfig";
179
179
  var ACTIONS_PROPERTY_NAME2 = "actions";
180
180
  var TYPE_PROPERTY_NAME2 = "type";
181
+ var ID_PROPERTY_NAME = "id";
182
+ var FLAGS_PROPERTY_NAME = "flags";
181
183
  var CUSTOM_ACTION_TYPE_PROPERTY_NAME = "customActionType";
182
184
  var AUDIT_HISTORY_LABEL_PROPERTY_NAME = "auditHistoryLabel";
183
185
  var DEDUPLICATION_PROPERTY_NAME = "deduplication";
@@ -187,9 +189,12 @@ var FORM_PROPERTY_NAME = "form";
187
189
  var ACTION_TYPE_ENUM_NAME = "ActionType";
188
190
  var VALIDATE_MEMBER_NAME = "VALIDATE";
189
191
  var CUSTOM_MEMBER_NAME = "CUSTOM";
192
+ var REGISTER_MEMBER_NAME2 = "REGISTER";
190
193
  var VALIDATE_STRING_LITERAL = "VALIDATE";
191
194
  var CUSTOM_STRING_LITERAL = "CUSTOM";
195
+ var REGISTER_STRING_LITERAL2 = "REGISTER";
192
196
  var VALIDATE_DECLARATION_VALUE = "VALIDATE_DECLARATION";
197
+ var VALIDATED_FLAG_VALUE = "validated";
193
198
  function isValidateType(typeInitializer) {
194
199
  if (typeInitializer.getKind() === import_ts_morph2.SyntaxKind.PropertyAccessExpression) {
195
200
  const propAccess = typeInitializer.asKindOrThrow(
@@ -313,6 +318,50 @@ function transformValidateActionToCustom(obj, eventType) {
313
318
  }
314
319
  return true;
315
320
  }
321
+ function isRegisterAction(action) {
322
+ const typeProperty = action.getProperty(TYPE_PROPERTY_NAME2);
323
+ if (!typeProperty || !import_ts_morph2.Node.isPropertyAssignment(typeProperty)) return false;
324
+ const typeInit = typeProperty.getInitializer();
325
+ if (!typeInit) return false;
326
+ if (import_ts_morph2.Node.isPropertyAccessExpression(typeInit)) {
327
+ const enumExpr = typeInit.getExpression();
328
+ return import_ts_morph2.Node.isIdentifier(enumExpr) && enumExpr.getText() === ACTION_TYPE_ENUM_NAME && typeInit.getName() === REGISTER_MEMBER_NAME2;
329
+ }
330
+ if (import_ts_morph2.Node.isStringLiteral(typeInit)) {
331
+ return typeInit.getLiteralValue() === REGISTER_STRING_LITERAL2;
332
+ }
333
+ return false;
334
+ }
335
+ function arrayHasElementWithId(array3, id) {
336
+ for (const el of array3.getElements()) {
337
+ if (!import_ts_morph2.Node.isObjectLiteralExpression(el)) continue;
338
+ const idProp = el.getProperty(ID_PROPERTY_NAME);
339
+ if (!idProp || !import_ts_morph2.Node.isPropertyAssignment(idProp)) continue;
340
+ const idInit = idProp.getInitializer();
341
+ if (idInit && import_ts_morph2.Node.isStringLiteral(idInit) && idInit.getLiteralValue() === id) {
342
+ return true;
343
+ }
344
+ }
345
+ return false;
346
+ }
347
+ function ensureRegisterRemovesValidatedFlag(action) {
348
+ const entry = `{ id: '${VALIDATED_FLAG_VALUE}', operation: 'remove' }`;
349
+ const flagsProp = action.getProperty(FLAGS_PROPERTY_NAME);
350
+ if (flagsProp && import_ts_morph2.Node.isPropertyAssignment(flagsProp)) {
351
+ const flagsInit = flagsProp.getInitializer();
352
+ if (flagsInit && import_ts_morph2.Node.isArrayLiteralExpression(flagsInit)) {
353
+ if (arrayHasElementWithId(flagsInit, VALIDATED_FLAG_VALUE)) return false;
354
+ flagsInit.addElement(entry);
355
+ return true;
356
+ }
357
+ return false;
358
+ }
359
+ action.addPropertyAssignment({
360
+ name: FLAGS_PROPERTY_NAME,
361
+ initializer: `[${entry}]`
362
+ });
363
+ return true;
364
+ }
316
365
  function processFile2(filePath, project) {
317
366
  const sourceFile = project.getSourceFile(filePath);
318
367
  if (!sourceFile) return 0;
@@ -378,6 +427,18 @@ function processFile2(filePath, project) {
378
427
  ` [${import_path2.default.relative(process.cwd(), filePath)}] Added top-level 'flags' array to defineConfig`
379
428
  );
380
429
  }
430
+ if (configHadValidateAction) {
431
+ for (const element of actionsInitializer.getElements()) {
432
+ if (!import_ts_morph2.Node.isObjectLiteralExpression(element)) continue;
433
+ if (!isRegisterAction(element)) continue;
434
+ if (ensureRegisterRemovesValidatedFlag(element)) {
435
+ transformedCount++;
436
+ console.log(
437
+ ` [${import_path2.default.relative(process.cwd(), filePath)}] Added '${VALIDATED_FLAG_VALUE}' remove-flag to REGISTER action`
438
+ );
439
+ }
440
+ }
441
+ }
381
442
  if (fileNeedsImports) {
382
443
  ensureImports(sourceFile);
383
444
  }
@@ -424,7 +485,7 @@ Saving ${modifiedFiles.length} modified file(s)...`);
424
485
  }
425
486
  console.log(
426
487
  `
427
- Done. Transformed ${totalTransformed} VALIDATE action(s) to CUSTOM with customActionType: '${VALIDATE_DECLARATION_VALUE}'.`
488
+ Done. Applied ${totalTransformed} change(s): VALIDATE \u2192 CUSTOM (${VALIDATE_DECLARATION_VALUE}) and '${VALIDATED_FLAG_VALUE}' remove-flag on matching REGISTER actions.`
428
489
  );
429
490
  }
430
491
 
@@ -667,14 +728,14 @@ var QUERY_PROPERTY_NAME = "query";
667
728
  var TYPE_PROPERTY_NAME5 = "type";
668
729
  var CLAUSES_PROPERTY_NAME = "clauses";
669
730
  var STATUS_PROPERTY_NAME = "status";
670
- var FLAGS_PROPERTY_NAME = "flags";
731
+ var FLAGS_PROPERTY_NAME2 = "flags";
671
732
  var TERMS_PROPERTY_NAME = "terms";
672
733
  var TERM_PROPERTY_NAME = "term";
673
734
  var ANY_OF_PROPERTY_NAME = "anyOf";
674
735
  var VALIDATED_STATUS_VALUE = "VALIDATED";
675
- var VALIDATED_FLAG_VALUE = "validated";
736
+ var VALIDATED_FLAG_VALUE2 = "validated";
676
737
  function addValidatedFlag(clause) {
677
- const flagsProperty = clause.getProperty(FLAGS_PROPERTY_NAME);
738
+ const flagsProperty = clause.getProperty(FLAGS_PROPERTY_NAME2);
678
739
  if (flagsProperty && import_ts_morph5.Node.isPropertyAssignment(flagsProperty)) {
679
740
  const flagsInit = flagsProperty.getInitializer();
680
741
  if (flagsInit && import_ts_morph5.Node.isObjectLiteralExpression(flagsInit)) {
@@ -682,19 +743,19 @@ function addValidatedFlag(clause) {
682
743
  if (anyOfProperty && import_ts_morph5.Node.isPropertyAssignment(anyOfProperty)) {
683
744
  const anyOfInit = anyOfProperty.getInitializer();
684
745
  if (anyOfInit && import_ts_morph5.Node.isArrayLiteralExpression(anyOfInit)) {
685
- anyOfInit.addElement(`'${VALIDATED_FLAG_VALUE}'`);
746
+ anyOfInit.addElement(`'${VALIDATED_FLAG_VALUE2}'`);
686
747
  }
687
748
  } else {
688
749
  flagsInit.addPropertyAssignment({
689
750
  name: ANY_OF_PROPERTY_NAME,
690
- initializer: `['${VALIDATED_FLAG_VALUE}']`
751
+ initializer: `['${VALIDATED_FLAG_VALUE2}']`
691
752
  });
692
753
  }
693
754
  }
694
755
  } else {
695
756
  clause.addPropertyAssignment({
696
- name: FLAGS_PROPERTY_NAME,
697
- initializer: `{ ${ANY_OF_PROPERTY_NAME}: ['${VALIDATED_FLAG_VALUE}'] }`
757
+ name: FLAGS_PROPERTY_NAME2,
758
+ initializer: `{ ${ANY_OF_PROPERTY_NAME}: ['${VALIDATED_FLAG_VALUE2}'] }`
698
759
  });
699
760
  }
700
761
  }
@@ -860,23 +921,26 @@ var DEFINE_CONFIG_NAME4 = "defineConfig";
860
921
  var DEFINE_WORKQUEUES_NAME2 = "defineWorkqueues";
861
922
  var ACTIONS_PROPERTY_NAME4 = "actions";
862
923
  var ACTION_PROPERTY_NAME = "action";
863
- var FLAGS_PROPERTY_NAME2 = "flags";
924
+ var FLAGS_PROPERTY_NAME3 = "flags";
864
925
  var QUERY_PROPERTY_NAME2 = "query";
865
926
  var TYPE_PROPERTY_NAME6 = "type";
927
+ var ID_PROPERTY_NAME2 = "id";
866
928
  var CLAUSES_PROPERTY_NAME2 = "clauses";
867
929
  var ANY_OF_PROPERTY_NAME2 = "anyOf";
868
930
  var FLAG_ID = "pending-first-certificate-issuance";
869
931
  var REGISTER_TYPE = "REGISTER";
870
932
  var PRINT_CERTIFICATE_TYPE = "PRINT_CERTIFICATE";
871
- var FLAG_DEFINITION_INITIALIZER = `{
933
+ function flagDefinitionInitializer(eventType) {
934
+ return `{
872
935
  id: '${FLAG_ID}',
873
936
  label: {
874
- id: 'event.birth.flag.${FLAG_ID}',
937
+ id: 'event.${eventType}.flag.${FLAG_ID}',
875
938
  defaultMessage: 'Pending first certificate issuance',
876
939
  description: 'Flag label for first certificate issuance'
877
940
  },
878
941
  requiresAction: true
879
942
  }`;
943
+ }
880
944
  function isActionOfType(obj, typeName) {
881
945
  const typeProperty = obj.getProperty(TYPE_PROPERTY_NAME6);
882
946
  if (!typeProperty || !import_ts_morph6.Node.isPropertyAssignment(typeProperty)) return false;
@@ -890,18 +954,20 @@ function isActionOfType(obj, typeName) {
890
954
  }
891
955
  return false;
892
956
  }
893
- function isBirthConfig(configArg) {
894
- const typeProperty = configArg.getProperty(TYPE_PROPERTY_NAME6);
895
- if (!typeProperty || !import_ts_morph6.Node.isPropertyAssignment(typeProperty)) return false;
896
- const initializer = typeProperty.getInitializer();
897
- if (!initializer) return false;
898
- if (import_ts_morph6.Node.isStringLiteral(initializer)) {
899
- return initializer.getLiteralValue().toLowerCase() === "birth";
900
- }
901
- if (import_ts_morph6.Node.isPropertyAccessExpression(initializer)) {
902
- return initializer.getName().toLowerCase() === "birth";
957
+ function getEventType(configArg) {
958
+ for (const propName of [TYPE_PROPERTY_NAME6, ID_PROPERTY_NAME2]) {
959
+ const prop = configArg.getProperty(propName);
960
+ if (!prop || !import_ts_morph6.Node.isPropertyAssignment(prop)) continue;
961
+ const initializer = prop.getInitializer();
962
+ if (!initializer) continue;
963
+ if (import_ts_morph6.Node.isStringLiteral(initializer)) {
964
+ return initializer.getLiteralValue().toLowerCase();
965
+ }
966
+ if (import_ts_morph6.Node.isPropertyAccessExpression(initializer)) {
967
+ return initializer.getName().toLowerCase();
968
+ }
903
969
  }
904
- return false;
970
+ return null;
905
971
  }
906
972
  function hasElementWithId(array3, id) {
907
973
  for (const el of array3.getElements()) {
@@ -915,27 +981,28 @@ function hasElementWithId(array3, id) {
915
981
  }
916
982
  return false;
917
983
  }
918
- function ensureTopLevelFlagDefinition(configArg) {
919
- const flagsProp = configArg.getProperty(FLAGS_PROPERTY_NAME2);
984
+ function ensureTopLevelFlagDefinition(configArg, eventType) {
985
+ const initializer = flagDefinitionInitializer(eventType);
986
+ const flagsProp = configArg.getProperty(FLAGS_PROPERTY_NAME3);
920
987
  if (flagsProp && import_ts_morph6.Node.isPropertyAssignment(flagsProp)) {
921
988
  const flagsInit = flagsProp.getInitializer();
922
989
  if (flagsInit && import_ts_morph6.Node.isArrayLiteralExpression(flagsInit)) {
923
990
  if (hasElementWithId(flagsInit, FLAG_ID)) return false;
924
- flagsInit.addElement(FLAG_DEFINITION_INITIALIZER);
991
+ flagsInit.addElement(initializer);
925
992
  return true;
926
993
  }
927
994
  return false;
928
995
  }
929
996
  configArg.addPropertyAssignment({
930
- name: FLAGS_PROPERTY_NAME2,
997
+ name: FLAGS_PROPERTY_NAME3,
931
998
  initializer: `[
932
- ${FLAG_DEFINITION_INITIALIZER}
999
+ ${initializer}
933
1000
  ]`
934
1001
  });
935
1002
  return true;
936
1003
  }
937
1004
  function ensureActionOperationFlag(action, operation) {
938
- const flagsProp = action.getProperty(FLAGS_PROPERTY_NAME2);
1005
+ const flagsProp = action.getProperty(FLAGS_PROPERTY_NAME3);
939
1006
  const entry = `{ id: '${FLAG_ID}', operation: '${operation}' }`;
940
1007
  if (flagsProp && import_ts_morph6.Node.isPropertyAssignment(flagsProp)) {
941
1008
  const flagsInit = flagsProp.getInitializer();
@@ -947,16 +1014,18 @@ function ensureActionOperationFlag(action, operation) {
947
1014
  return false;
948
1015
  }
949
1016
  action.addPropertyAssignment({
950
- name: FLAGS_PROPERTY_NAME2,
1017
+ name: FLAGS_PROPERTY_NAME3,
951
1018
  initializer: `[${entry}]`
952
1019
  });
953
1020
  return true;
954
1021
  }
955
- function processBirthConfig(configArg, relPath) {
1022
+ function processConfig(configArg, eventType, relPath) {
956
1023
  let changes = 0;
957
- if (ensureTopLevelFlagDefinition(configArg)) {
1024
+ if (ensureTopLevelFlagDefinition(configArg, eventType)) {
958
1025
  changes++;
959
- console.log(` [${relPath}] Added '${FLAG_ID}' to top-level flags`);
1026
+ console.log(
1027
+ ` [${relPath}] (${eventType}) Added '${FLAG_ID}' to top-level flags`
1028
+ );
960
1029
  }
961
1030
  const actionsProp = configArg.getProperty(ACTIONS_PROPERTY_NAME4);
962
1031
  if (!actionsProp || !import_ts_morph6.Node.isPropertyAssignment(actionsProp)) return changes;
@@ -970,7 +1039,7 @@ function processBirthConfig(configArg, relPath) {
970
1039
  if (ensureActionOperationFlag(element, "add")) {
971
1040
  changes++;
972
1041
  console.log(
973
- ` [${relPath}] Added '${FLAG_ID}' add-flag to REGISTER action`
1042
+ ` [${relPath}] (${eventType}) Added '${FLAG_ID}' add-flag to REGISTER action`
974
1043
  );
975
1044
  }
976
1045
  }
@@ -978,7 +1047,7 @@ function processBirthConfig(configArg, relPath) {
978
1047
  if (ensureActionOperationFlag(element, "remove")) {
979
1048
  changes++;
980
1049
  console.log(
981
- ` [${relPath}] Added '${FLAG_ID}' remove-flag to PRINT_CERTIFICATE action`
1050
+ ` [${relPath}] (${eventType}) Added '${FLAG_ID}' remove-flag to PRINT_CERTIFICATE action`
982
1051
  );
983
1052
  }
984
1053
  }
@@ -1007,7 +1076,7 @@ function hasPrintCertificateAction(workqueue) {
1007
1076
  return false;
1008
1077
  }
1009
1078
  function addFlagToQueryClause(clause) {
1010
- const flagsProp = clause.getProperty(FLAGS_PROPERTY_NAME2);
1079
+ const flagsProp = clause.getProperty(FLAGS_PROPERTY_NAME3);
1011
1080
  if (flagsProp && import_ts_morph6.Node.isPropertyAssignment(flagsProp)) {
1012
1081
  const flagsInit = flagsProp.getInitializer();
1013
1082
  if (flagsInit && import_ts_morph6.Node.isObjectLiteralExpression(flagsInit)) {
@@ -1032,7 +1101,7 @@ function addFlagToQueryClause(clause) {
1032
1101
  }
1033
1102
  } else {
1034
1103
  clause.addPropertyAssignment({
1035
- name: FLAGS_PROPERTY_NAME2,
1104
+ name: FLAGS_PROPERTY_NAME3,
1036
1105
  initializer: `{ ${ANY_OF_PROPERTY_NAME2}: ['${FLAG_ID}'] }`
1037
1106
  });
1038
1107
  return true;
@@ -1076,8 +1145,9 @@ function processFile6(filePath, project) {
1076
1145
  if (args2.length === 0) continue;
1077
1146
  const configArg = args2[0];
1078
1147
  if (!import_ts_morph6.Node.isObjectLiteralExpression(configArg)) continue;
1079
- if (!isBirthConfig(configArg)) continue;
1080
- totalChanges += processBirthConfig(configArg, relPath);
1148
+ const eventType = getEventType(configArg);
1149
+ if (!eventType) continue;
1150
+ totalChanges += processConfig(configArg, eventType, relPath);
1081
1151
  }
1082
1152
  if (calleeName === DEFINE_WORKQUEUES_NAME2) {
1083
1153
  const args2 = call.getArguments();
@@ -1106,7 +1176,7 @@ function processFile6(filePath, project) {
1106
1176
  async function main6() {
1107
1177
  const srcDir = import_path6.default.join(process.cwd(), "src");
1108
1178
  console.log(
1109
- `Scanning for birth defineConfig and PRINT_CERTIFICATE workqueues in: ${srcDir}
1179
+ `Scanning for defineConfig and PRINT_CERTIFICATE workqueues in: ${srcDir}
1110
1180
  `
1111
1181
  );
1112
1182
  const project = new import_ts_morph6.Project({
@@ -1131,7 +1201,7 @@ async function main6() {
1131
1201
  }
1132
1202
  if (modifiedFiles.length === 0) {
1133
1203
  console.log(
1134
- "No birth configs or PRINT_CERTIFICATE workqueues found. Nothing to do."
1204
+ "No event configs or PRINT_CERTIFICATE workqueues found. Nothing to do."
1135
1205
  );
1136
1206
  return;
1137
1207
  }
@@ -73,5 +73,26 @@ export declare function canAccessOtherUserWithScopes({ scopes, userToAccess, use
73
73
  * One of the scopes must allow access for the event to be accessible.
74
74
  */
75
75
  export declare function userCanAccessEventWithScopes(event: Partial<EventIndexWithAdministrativeHierarchy>, scopes: RecordScopeV2[], user: UserContext | SystemContext, customActionType?: string): boolean;
76
+ export declare function getAdministrativeAreaHierarchy(administrativeAreaId: string | undefined | null, administrativeAreas: Map<UUID, AdministrativeArea>): {
77
+ id: string & z.core.$brand<"UUID">;
78
+ name: string;
79
+ parentId: (string & z.core.$brand<"UUID">) | null;
80
+ validUntil: string | null;
81
+ externalId?: string | null | undefined;
82
+ }[];
83
+ /**
84
+ * Resolves a location or administrative area UUID into a root-first hierarchy of UUIDs.
85
+ *
86
+ * If the ID refers to a `Location` (e.g. CRVS office), the hierarchy includes
87
+ * the administrative area ancestors followed by the location itself.
88
+ * If the ID refers to an `AdministrativeArea`, returns the area's ancestor chain (root-first).
89
+ *
90
+ * Uses `getAdministrativeAreaHierarchy` which returns leaf-first order,
91
+ * so the result is reversed to match the root-first convention used by the server.
92
+ */
93
+ export declare function getLocationHierarchy(selectedId: UUID, context: {
94
+ administrativeAreas: Map<UUID, AdministrativeArea>;
95
+ locations: Map<UUID, Location>;
96
+ }): UUID[];
76
97
  export {};
77
98
  //# sourceMappingURL=locations.d.ts.map
@@ -0,0 +1,19 @@
1
+ import { AdministrativeArea, Location } from './locations';
2
+ export declare const V2_DEFAULT_MOCK_ADMINISTRATIVE_AREAS: AdministrativeArea[];
3
+ export declare const V2_DEFAULT_MOCK_LOCATIONS: Location[];
4
+ export declare const V2_DEFAULT_MOCK_LOCATIONS_MAP: Map<string & import("zod").$brand<"UUID">, {
5
+ id: string & import("zod").$brand<"UUID">;
6
+ name: string;
7
+ administrativeAreaId: (string & import("zod").$brand<"UUID">) | null;
8
+ validUntil: string | null;
9
+ locationType: string | null;
10
+ externalId?: string | null | undefined;
11
+ }>;
12
+ export declare const V2_DEFAULT_MOCK_ADMINISTRATIVE_AREAS_MAP: Map<string & import("zod").$brand<"UUID">, {
13
+ id: string & import("zod").$brand<"UUID">;
14
+ name: string;
15
+ parentId: (string & import("zod").$brand<"UUID">) | null;
16
+ validUntil: string | null;
17
+ externalId?: string | null | undefined;
18
+ }>;
19
+ //# sourceMappingURL=mocks.test.utils.d.ts.map
@@ -1,8 +1,9 @@
1
1
  import { DisplayableAction } from './ActionType';
2
- import { EncodedScope } from '../scopes';
2
+ import { EncodedScope, UserScopeV2 } from '../scopes';
3
3
  import { EventIndexWithAdministrativeHierarchy } from './locations';
4
4
  import { UserContext } from '../users/User';
5
5
  import { EventIndex } from './EventIndex';
6
+ import { UUID } from 'src/uuid';
6
7
  export declare const ACTION_SCOPE_MAP: {
7
8
  READ: "record.read"[];
8
9
  CREATE: "record.create"[];
@@ -53,4 +54,26 @@ export declare function isActionInScope({ scopes, action, event, currentUser }:
53
54
  event: EventIndexWithAdministrativeHierarchy;
54
55
  currentUser: UserContext;
55
56
  }): boolean;
57
+ /**
58
+ *
59
+ *
60
+ * @param allRoles - All possible roles in the system
61
+ * @param userLocation - The location and role information of the user whose role options are being determined
62
+ * @param acceptedScopes - The scopes of the user requesting the role update, used to determine which roles they can assign.
63
+ * @param userRequesting - The user context of the user requesting the role update, used to check location-based access to the target user's location.
64
+ *
65
+ * @returns An array of roles that the requesting user is allowed to assign to the target user based on their scopes and location
66
+ *
67
+ * 1) Used to determine which roles should be available in the dropdown when a user is editing another user's role in the user management interface.
68
+ * 2) Used to check if payload includes valid role.
69
+ */
70
+ export declare function getAvailableRolesForUserUpdatePayload({ allRoles, userLocation, acceptedScopes, userRequesting }: {
71
+ allRoles: string[];
72
+ userRequesting: UserContext;
73
+ acceptedScopes: UserScopeV2[];
74
+ userLocation: {
75
+ administrativeHierarchy: UUID[];
76
+ primaryOfficeId: UUID;
77
+ };
78
+ }): string[];
56
79
  //# sourceMappingURL=scopes.d.ts.map
@@ -302,12 +302,14 @@ __export(events_exports, {
302
302
  getActionConfig: () => getActionConfig,
303
303
  getActionReview: () => getActionReview,
304
304
  getActionReviewFields: () => getActionReviewFields,
305
+ getAdministrativeAreaHierarchy: () => getAdministrativeAreaHierarchy,
305
306
  getAllUniqueFields: () => getAllUniqueFields,
306
307
  getAnnotationFromDrafts: () => getAnnotationFromDrafts,
307
308
  getAssignedUserFromActions: () => getAssignedUserFromActions,
308
309
  getAssignedUserSignatureFromActions: () => getAssignedUserSignatureFromActions,
309
310
  getAssignmentStatus: () => getAssignmentStatus,
310
311
  getAvailableActionsForEvent: () => getAvailableActionsForEvent,
312
+ getAvailableRolesForUserUpdatePayload: () => getAvailableRolesForUserUpdatePayload,
311
313
  getCompleteActionAnnotation: () => getCompleteActionAnnotation,
312
314
  getCompleteActionDeclaration: () => getCompleteActionDeclaration,
313
315
  getCurrentEventState: () => getCurrentEventState,
@@ -318,6 +320,7 @@ __export(events_exports, {
318
320
  getDeclarationPages: () => getDeclarationPages,
319
321
  getEventConfigById: () => getEventConfigById,
320
322
  getFilePathsFromEvent: () => getFilePathsFromEvent,
323
+ getLocationHierarchy: () => getLocationHierarchy,
321
324
  getMixedPath: () => getMixedPath,
322
325
  getPendingAction: () => getPendingAction,
323
326
  getPrintCertificatePages: () => getPrintCertificatePages,
@@ -10001,6 +10004,39 @@ function userCanAccessEventWithScopes(event2, scopes, user2, customActionType) {
10001
10004
  (scope) => canAccessEventWithScope(event2, scope, user2, customActionType)
10002
10005
  );
10003
10006
  }
10007
+ function getAdministrativeAreaHierarchy(administrativeAreaId, administrativeAreas) {
10008
+ const collectedLocations = [];
10009
+ const parsedAdministrativeAreaId = administrativeAreaId && UUID.safeParse(administrativeAreaId).data;
10010
+ let current = parsedAdministrativeAreaId ? administrativeAreas.get(parsedAdministrativeAreaId) : null;
10011
+ while (current) {
10012
+ collectedLocations.push(current);
10013
+ if (!current.parentId) {
10014
+ break;
10015
+ }
10016
+ const parentId = current.parentId;
10017
+ current = administrativeAreas.get(parentId);
10018
+ }
10019
+ return collectedLocations;
10020
+ }
10021
+ function getLocationHierarchy(selectedId, context) {
10022
+ const { administrativeAreas, locations } = context;
10023
+ const loc = locations.get(selectedId);
10024
+ if (loc) {
10025
+ if (loc.administrativeAreaId) {
10026
+ const hierarchy2 = getAdministrativeAreaHierarchy(
10027
+ loc.administrativeAreaId,
10028
+ administrativeAreas
10029
+ );
10030
+ return [...hierarchy2.reverse().map((area) => area.id), loc.id];
10031
+ }
10032
+ return [loc.id];
10033
+ }
10034
+ const hierarchy = getAdministrativeAreaHierarchy(
10035
+ selectedId,
10036
+ administrativeAreas
10037
+ );
10038
+ return hierarchy.reverse().map((area) => area.id);
10039
+ }
10004
10040
 
10005
10041
  // ../commons/src/events/scopes.ts
10006
10042
  var ACTION_SCOPE_MAP = {
@@ -10075,6 +10111,25 @@ function isActionInScope({
10075
10111
  );
10076
10112
  return canAccess;
10077
10113
  }
10114
+ function getAvailableRolesForUserUpdatePayload({
10115
+ allRoles,
10116
+ userLocation,
10117
+ acceptedScopes,
10118
+ userRequesting
10119
+ }) {
10120
+ const availableRoles = allRoles.filter((role) => {
10121
+ const canAccess = canAccessOtherUserWithScopes({
10122
+ userToAccess: {
10123
+ ...userLocation,
10124
+ role
10125
+ },
10126
+ user: userRequesting,
10127
+ scopes: acceptedScopes
10128
+ });
10129
+ return canAccess;
10130
+ });
10131
+ return availableRoles;
10132
+ }
10078
10133
 
10079
10134
  // ../commons/src/events/serializers/user/deserializer.ts
10080
10135
  function userDeserializer(serializedUserField, user2) {
@@ -1 +1 @@
1
- {"version":3,"file":"add-birth-certificate-issuance-flag.d.ts","sourceRoot":"","sources":["../../../src/migrations/v2.0/add-birth-certificate-issuance-flag.ts"],"names":[],"mappings":"AA2aA,iBAAe,IAAI,kBAiDlB;AAED,OAAO,EAAE,IAAI,EAAE,CAAA"}
1
+ {"version":3,"file":"add-birth-certificate-issuance-flag.d.ts","sourceRoot":"","sources":["../../../src/migrations/v2.0/add-birth-certificate-issuance-flag.ts"],"names":[],"mappings":"AAycA,iBAAe,IAAI,kBAiDlB;AAED,OAAO,EAAE,IAAI,EAAE,CAAA"}
@@ -24,15 +24,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.main = main;
26
26
  /**
27
- * Codemod: Add `pending-first-certificate-issuance` flag to birth event configs and workqueues.
27
+ * Codemod: Add `pending-first-certificate-issuance` flag to event configs and workqueues.
28
28
  *
29
29
  * Usage:
30
30
  * ts-node -r tsconfig-paths/register src/migrations/v2.0/add-birth-certificate-issuance-flag.ts
31
31
  *
32
32
  * What it does:
33
- * defineConfig (birth type only):
33
+ * defineConfig (every event type — birth, death, marriage, custom, etc.):
34
34
  * - Ensures the top-level `flags` array contains the `pending-first-certificate-issuance`
35
- * flag definition
35
+ * flag definition. The flag's i18n `label.id` is event-specific, e.g.
36
+ * `event.birth.flag.pending-first-certificate-issuance`,
37
+ * `event.death.flag.pending-first-certificate-issuance`.
36
38
  * - Ensures every `REGISTER` action has
37
39
  * `flags: [{ id: 'pending-first-certificate-issuance', operation: 'add' }]`
38
40
  * - Ensures every `PRINT_CERTIFICATE` action has
@@ -55,20 +57,27 @@ const ACTION_PROPERTY_NAME = 'action';
55
57
  const FLAGS_PROPERTY_NAME = 'flags';
56
58
  const QUERY_PROPERTY_NAME = 'query';
57
59
  const TYPE_PROPERTY_NAME = 'type';
60
+ const ID_PROPERTY_NAME = 'id';
58
61
  const CLAUSES_PROPERTY_NAME = 'clauses';
59
62
  const ANY_OF_PROPERTY_NAME = 'anyOf';
60
63
  const FLAG_ID = 'pending-first-certificate-issuance';
61
64
  const REGISTER_TYPE = 'REGISTER';
62
65
  const PRINT_CERTIFICATE_TYPE = 'PRINT_CERTIFICATE';
63
- const FLAG_DEFINITION_INITIALIZER = `{
66
+ /**
67
+ * Builds the flag-definition object literal for a given event type so the i18n
68
+ * `label.id` is namespaced per event (e.g. `event.death.flag.…`).
69
+ */
70
+ function flagDefinitionInitializer(eventType) {
71
+ return `{
64
72
  id: '${FLAG_ID}',
65
73
  label: {
66
- id: 'event.birth.flag.${FLAG_ID}',
74
+ id: 'event.${eventType}.flag.${FLAG_ID}',
67
75
  defaultMessage: 'Pending first certificate issuance',
68
76
  description: 'Flag label for first certificate issuance'
69
77
  },
70
78
  requiresAction: true
71
79
  }`;
80
+ }
72
81
  // ─── Generic helpers ──────────────────────────────────────────────────────────
73
82
  /**
74
83
  * Returns true if the object's `type` property matches the given type name.
@@ -90,23 +99,36 @@ function isActionOfType(obj, typeName) {
90
99
  return false;
91
100
  }
92
101
  /**
93
- * Returns true when the `defineConfig` root config object describes a birth event.
94
- * Handles both `'birth'` string literal and `EventType.BIRTH` enum access.
102
+ * Extracts the event type from a `defineConfig` root config object, using the
103
+ * (lower-cased) value of either the `type` or `id` property. Property name has
104
+ * shifted across core/country-config versions, so both are tried.
105
+ *
106
+ * Handles all known shapes:
107
+ * - `type: 'birth'` / `id: 'birth'`
108
+ * - `type: EventType.BIRTH` / `id: Event.Birth`
109
+ *
110
+ * For `PropertyAccessExpression` (enum access), the *member name* is returned
111
+ * lower-cased — this works for the conventional `EventType.BIRTH = 'birth'` and
112
+ * `Event.Birth = 'birth'` mappings used in core and country configs.
113
+ *
114
+ * Returns null when no event type can be determined.
95
115
  */
96
- function isBirthConfig(configArg) {
97
- const typeProperty = configArg.getProperty(TYPE_PROPERTY_NAME);
98
- if (!typeProperty || !ts_morph_1.Node.isPropertyAssignment(typeProperty))
99
- return false;
100
- const initializer = typeProperty.getInitializer();
101
- if (!initializer)
102
- return false;
103
- if (ts_morph_1.Node.isStringLiteral(initializer)) {
104
- return initializer.getLiteralValue().toLowerCase() === 'birth';
105
- }
106
- if (ts_morph_1.Node.isPropertyAccessExpression(initializer)) {
107
- return initializer.getName().toLowerCase() === 'birth';
116
+ function getEventType(configArg) {
117
+ for (const propName of [TYPE_PROPERTY_NAME, ID_PROPERTY_NAME]) {
118
+ const prop = configArg.getProperty(propName);
119
+ if (!prop || !ts_morph_1.Node.isPropertyAssignment(prop))
120
+ continue;
121
+ const initializer = prop.getInitializer();
122
+ if (!initializer)
123
+ continue;
124
+ if (ts_morph_1.Node.isStringLiteral(initializer)) {
125
+ return initializer.getLiteralValue().toLowerCase();
126
+ }
127
+ if (ts_morph_1.Node.isPropertyAccessExpression(initializer)) {
128
+ return initializer.getName().toLowerCase();
129
+ }
108
130
  }
109
- return false;
131
+ return null;
110
132
  }
111
133
  /**
112
134
  * Returns true if an array literal contains an object literal with `id: <id>`.
@@ -129,17 +151,19 @@ function hasElementWithId(array, id) {
129
151
  }
130
152
  // ─── defineConfig helpers ─────────────────────────────────────────────────────
131
153
  /**
132
- * Ensures the top-level `flags` array in a birth `defineConfig` contains the
133
- * `pending-first-certificate-issuance` flag definition.
154
+ * Ensures the top-level `flags` array in a `defineConfig` contains the
155
+ * `pending-first-certificate-issuance` flag definition. The flag's i18n
156
+ * `label.id` is namespaced per event type (e.g. `event.death.flag.…`).
134
157
  */
135
- function ensureTopLevelFlagDefinition(configArg) {
158
+ function ensureTopLevelFlagDefinition(configArg, eventType) {
159
+ const initializer = flagDefinitionInitializer(eventType);
136
160
  const flagsProp = configArg.getProperty(FLAGS_PROPERTY_NAME);
137
161
  if (flagsProp && ts_morph_1.Node.isPropertyAssignment(flagsProp)) {
138
162
  const flagsInit = flagsProp.getInitializer();
139
163
  if (flagsInit && ts_morph_1.Node.isArrayLiteralExpression(flagsInit)) {
140
164
  if (hasElementWithId(flagsInit, FLAG_ID))
141
165
  return false;
142
- flagsInit.addElement(FLAG_DEFINITION_INITIALIZER);
166
+ flagsInit.addElement(initializer);
143
167
  return true;
144
168
  }
145
169
  return false;
@@ -147,7 +171,7 @@ function ensureTopLevelFlagDefinition(configArg) {
147
171
  // No `flags` property at all — create it
148
172
  configArg.addPropertyAssignment({
149
173
  name: FLAGS_PROPERTY_NAME,
150
- initializer: `[\n ${FLAG_DEFINITION_INITIALIZER}\n]`
174
+ initializer: `[\n ${initializer}\n]`
151
175
  });
152
176
  return true;
153
177
  }
@@ -174,15 +198,15 @@ function ensureActionOperationFlag(action, operation) {
174
198
  return true;
175
199
  }
176
200
  /**
177
- * Processes a birth defineConfig object: ensures top-level flag definition,
201
+ * Processes a `defineConfig` object: ensures top-level flag definition,
178
202
  * REGISTER action flags, and PRINT_CERTIFICATE action flags.
179
203
  * Returns the number of changes made.
180
204
  */
181
- function processBirthConfig(configArg, relPath) {
205
+ function processConfig(configArg, eventType, relPath) {
182
206
  let changes = 0;
183
- if (ensureTopLevelFlagDefinition(configArg)) {
207
+ if (ensureTopLevelFlagDefinition(configArg, eventType)) {
184
208
  changes++;
185
- console.log(` [${relPath}] Added '${FLAG_ID}' to top-level flags`);
209
+ console.log(` [${relPath}] (${eventType}) Added '${FLAG_ID}' to top-level flags`);
186
210
  }
187
211
  const actionsProp = configArg.getProperty(ACTIONS_PROPERTY_NAME);
188
212
  if (!actionsProp || !ts_morph_1.Node.isPropertyAssignment(actionsProp))
@@ -197,13 +221,13 @@ function processBirthConfig(configArg, relPath) {
197
221
  if (isActionOfType(element, REGISTER_TYPE)) {
198
222
  if (ensureActionOperationFlag(element, 'add')) {
199
223
  changes++;
200
- console.log(` [${relPath}] Added '${FLAG_ID}' add-flag to REGISTER action`);
224
+ console.log(` [${relPath}] (${eventType}) Added '${FLAG_ID}' add-flag to REGISTER action`);
201
225
  }
202
226
  }
203
227
  if (isActionOfType(element, PRINT_CERTIFICATE_TYPE)) {
204
228
  if (ensureActionOperationFlag(element, 'remove')) {
205
229
  changes++;
206
- console.log(` [${relPath}] Added '${FLAG_ID}' remove-flag to PRINT_CERTIFICATE action`);
230
+ console.log(` [${relPath}] (${eventType}) Added '${FLAG_ID}' remove-flag to PRINT_CERTIFICATE action`);
207
231
  }
208
232
  }
209
233
  }
@@ -332,9 +356,10 @@ function processFile(filePath, project) {
332
356
  const configArg = args[0];
333
357
  if (!ts_morph_1.Node.isObjectLiteralExpression(configArg))
334
358
  continue;
335
- if (!isBirthConfig(configArg))
359
+ const eventType = getEventType(configArg);
360
+ if (!eventType)
336
361
  continue;
337
- totalChanges += processBirthConfig(configArg, relPath);
362
+ totalChanges += processConfig(configArg, eventType, relPath);
338
363
  }
339
364
  // ── defineWorkqueues ──────────────────────────────────────────────────────
340
365
  if (calleeName === DEFINE_WORKQUEUES_NAME) {
@@ -369,7 +394,7 @@ function processFile(filePath, project) {
369
394
  function main() {
370
395
  return __awaiter(this, void 0, void 0, function* () {
371
396
  const srcDir = path_1.default.join(process.cwd(), 'src');
372
- console.log(`Scanning for birth defineConfig and PRINT_CERTIFICATE workqueues in: ${srcDir}\n`);
397
+ console.log(`Scanning for defineConfig and PRINT_CERTIFICATE workqueues in: ${srcDir}\n`);
373
398
  const project = new ts_morph_1.Project({
374
399
  tsConfigFilePath: path_1.default.resolve(srcDir, '../tsconfig.json'),
375
400
  skipAddingFilesFromTsConfig: false
@@ -390,7 +415,7 @@ function main() {
390
415
  }
391
416
  }
392
417
  if (modifiedFiles.length === 0) {
393
- console.log('No birth configs or PRINT_CERTIFICATE workqueues found. Nothing to do.');
418
+ console.log('No event configs or PRINT_CERTIFICATE workqueues found. Nothing to do.');
394
419
  return;
395
420
  }
396
421
  console.log(`\nSaving ${modifiedFiles.length} modified file(s)...`);
@@ -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
  }
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencrvs/toolkit",
3
- "version": "2.0.0-rc.a8e70b1",
3
+ "version": "2.0.0-rc.ab3230c",
4
4
  "description": "OpenCRVS toolkit for building country configurations",
5
5
  "license": "MPL-2.0",
6
6
  "bin": {