@opencrvs/toolkit 2.0.0-rc.d451f09 → 2.0.0-rc.d603f53

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.
@@ -1105,8 +1105,7 @@ var ActionCreationMetadata = z15.object({
1105
1105
  ),
1106
1106
  createdByUserType: z15.enum(["user", "system"]).nullish().describe("Whether the user is a normal user or a system."),
1107
1107
  acceptedAt: z15.iso.datetime().describe("Timestamp when the action request was accepted."),
1108
- createdByRole: z15.string().optional().describe("Role of the user at the time of action request creation."),
1109
- createdBySignature: z15.string().nullish().describe("Signature of the user who created the action request.")
1108
+ createdByRole: z15.string().optional().describe("Role of the user at the time of action request creation.")
1110
1109
  });
1111
1110
  var RegistrationCreationMetadata = ActionCreationMetadata.extend({
1112
1111
  registrationNumber: z15.string().describe(
@@ -1134,9 +1133,6 @@ var EventMetadata = z15.object({
1134
1133
  createdAtLocation: UUID.nullish().describe(
1135
1134
  "Location of the user who created the event."
1136
1135
  ),
1137
- createdBySignature: DocumentPath.nullish().describe(
1138
- "Signature of the user who created the event."
1139
- ),
1140
1136
  updatedAtLocation: UUID.nullish().describe(
1141
1137
  "Location of the user who last changed the status."
1142
1138
  ),
package/dist/cli.js CHANGED
@@ -1883,9 +1883,9 @@ function updateRouteHandlers(sourceFiles, group, srcDir, relCwd) {
1883
1883
  }
1884
1884
  return replacedCount;
1885
1885
  }
1886
- function resolveConfigChain(configObj, path23) {
1886
+ function resolveConfigChain(configObj, path24) {
1887
1887
  let current = configObj;
1888
- for (const key of path23) {
1888
+ for (const key of path24) {
1889
1889
  if (!import_ts_morph10.Node.isObjectLiteralExpression(current)) return null;
1890
1890
  const prop = current.getProperty(key);
1891
1891
  if (!prop || !import_ts_morph10.Node.isPropertyAssignment(prop)) return null;
@@ -2120,6 +2120,10 @@ Done. Converted ${convertedCount} config file(s), replaced ${totalReplaced} rout
2120
2120
  var import_ts_morph11 = require("ts-morph");
2121
2121
  var import_path11 = __toESM(require("path"));
2122
2122
 
2123
+ // ../commons/src/scopes.deprecated.do-not-use.ts
2124
+ var z4 = __toESM(require("zod/v4"));
2125
+ var import_lodash = require("lodash");
2126
+
2123
2127
  // ../commons/src/scopes.ts
2124
2128
  var z3 = __toESM(require("zod/v4"));
2125
2129
  var qs = __toESM(require("qs"));
@@ -2393,7 +2397,6 @@ var DEFAULT_SCOPE_OPTIONS = {
2393
2397
  };
2394
2398
 
2395
2399
  // ../commons/src/scopes.deprecated.do-not-use.ts
2396
- var z4 = __toESM(require("zod/v4"));
2397
2400
  var rawConfigurableScopeRegex = /^([a-zA-Z][a-zA-Z0-9.-]*(?:\.[a-zA-Z0-9.-]+)*)\[((?:\w+=[\w.-]+(?:\|[\w.-]+)*)(?:,[\w]+=[\w.-]+(?:\|[\w.-]+)*)*)\]$/;
2398
2401
  var rawConfigurableScope = z4.string().regex(rawConfigurableScopeRegex);
2399
2402
  var SearchScope = z4.object({
@@ -2627,6 +2630,74 @@ var legacyScopeToV2Scope = (v1Scope) => {
2627
2630
  }
2628
2631
  throw new Error(`Unsupported V1 scope type: ${v1Scope}`);
2629
2632
  };
2633
+ var MERGEABLE_USER_SCOPE_TYPES = ["user.create", "user.edit"];
2634
+ function isMergeableUserScopeType(type) {
2635
+ return MERGEABLE_USER_SCOPE_TYPES.includes(type);
2636
+ }
2637
+ function decodeLegacyScope(legacy) {
2638
+ const encoded = legacyScopeToV2Scope(legacy);
2639
+ const scope = decodeScope(encoded);
2640
+ if (!scope) {
2641
+ throw new Error(
2642
+ `Could not decode migrated scope from legacy '${legacy}' (encoded: '${encoded}').`
2643
+ );
2644
+ }
2645
+ return { legacy, scope };
2646
+ }
2647
+ function groupByType(entries) {
2648
+ return entries.reduce(
2649
+ (groups, entry) => ({
2650
+ ...groups,
2651
+ [entry.scope.type]: [...groups[entry.scope.type] ?? [], entry]
2652
+ }),
2653
+ {}
2654
+ );
2655
+ }
2656
+ function deduplicateEntries(entries) {
2657
+ return entries.filter(
2658
+ (entry, index) => entries.findIndex((e) => (0, import_lodash.isEqual)(e.scope, entry.scope)) === index
2659
+ );
2660
+ }
2661
+ function mergeEntries(type, entries) {
2662
+ const optionsList = entries.map(
2663
+ (e) => "options" in e.scope && e.scope.options ? e.scope.options : {}
2664
+ );
2665
+ const allKeys = optionsList.flatMap(
2666
+ (opts) => Object.keys(opts).filter((k) => opts[k] !== void 0)
2667
+ );
2668
+ const conflictKey = allKeys.find((key, i) => allKeys.indexOf(key) !== i);
2669
+ if (conflictKey) {
2670
+ const conflicting = entries.filter(
2671
+ (e) => conflictKey in ("options" in e.scope && e.scope.options || {})
2672
+ ).map((e) => JSON.stringify(e.legacy));
2673
+ throw new Error(
2674
+ `Cannot auto-merge two '${type}' scopes that both define '${conflictKey}'. Conflicting legacy scopes: [${conflicting.join(", ")}]. Resolve manually in roles.ts and re-run.`
2675
+ );
2676
+ }
2677
+ const mergedOptions = Object.assign({}, ...optionsList);
2678
+ const merged = Object.keys(mergedOptions).length > 0 ? { type, options: mergedOptions } : { type };
2679
+ const parsed = Scope2.safeParse(merged);
2680
+ if (!parsed.success) {
2681
+ throw new Error(
2682
+ `Merged '${type}' scope failed schema validation: ${parsed.error.message}. Legacy scopes: [${entries.map((e) => JSON.stringify(e.legacy)).join(", ")}].`
2683
+ );
2684
+ }
2685
+ return parsed.data;
2686
+ }
2687
+ function migrateLegacyScopesArrayToV2Scopes(legacyScopes) {
2688
+ const groups = groupByType(legacyScopes.map(decodeLegacyScope));
2689
+ return Object.entries(groups).flatMap(([type, entries]) => {
2690
+ const deduped = deduplicateEntries(entries);
2691
+ if (deduped.length <= 1) return deduped.map((e) => e.scope);
2692
+ if (!isMergeableUserScopeType(type)) {
2693
+ console.warn(
2694
+ `Multiple non-mergeable '${type}' scopes detected after dedup \u2014 kept all as-is. Legacy scopes: [${deduped.map((e) => JSON.stringify(e.legacy)).join(", ")}]. Review whether the duplication is intentional.`
2695
+ );
2696
+ return deduped.map((e) => e.scope);
2697
+ }
2698
+ return [mergeEntries(type, deduped)];
2699
+ });
2700
+ }
2630
2701
 
2631
2702
  // src/migrations/v2.0/migrate-scopes.ts
2632
2703
  var ROLES_FILE_RELATIVE_PATH = "src/data-seeding/roles/roles.ts";
@@ -2664,57 +2735,65 @@ function resolveLegacyScopeFromElement(element) {
2664
2735
  }
2665
2736
  function migrateScopesArrays(rolesFilePath, project) {
2666
2737
  const sourceFile = project.getSourceFile(rolesFilePath);
2667
- if (!sourceFile) return { migratedCount: 0, removedCount: 0 };
2738
+ if (!sourceFile) {
2739
+ return { migratedCount: 0, removedCount: 0, mergedCount: 0 };
2740
+ }
2668
2741
  let migratedCount = 0;
2669
2742
  let removedCount = 0;
2743
+ let mergedCount = 0;
2670
2744
  const scopesProperties = sourceFile.getDescendantsOfKind(import_ts_morph11.SyntaxKind.PropertyAssignment).filter((prop) => prop.getName() === SCOPES_PROPERTY_NAME);
2745
+ const relPath = import_path11.default.relative(process.cwd(), rolesFilePath);
2671
2746
  for (const scopesProp of scopesProperties) {
2672
2747
  const initializer = scopesProp.getInitializer();
2673
2748
  if (!initializer || !import_ts_morph11.Node.isArrayLiteralExpression(initializer)) continue;
2674
- const elements = initializer.getElements();
2675
- for (let index = elements.length - 1; index >= 0; index--) {
2676
- const element = elements[index];
2677
- if (!import_ts_morph11.Node.isExpression(element)) continue;
2749
+ const rawTexts = [];
2750
+ const legacyStrings = [];
2751
+ let anyChange = false;
2752
+ for (const element of initializer.getElements()) {
2678
2753
  const scopeKey = resolveScopeKeyFromElementAccess(element);
2679
2754
  if (scopeKey && !(scopeKey in SCOPES)) {
2680
2755
  console.warn(
2681
- ` [${import_path11.default.relative(process.cwd(), rolesFilePath)}] Removing unknown scope reference: SCOPES.${scopeKey}`
2756
+ ` [${relPath}] Removing unknown scope reference: SCOPES.${scopeKey}`
2682
2757
  );
2683
- initializer.removeElement(index);
2684
2758
  removedCount++;
2759
+ anyChange = true;
2685
2760
  continue;
2686
2761
  }
2687
2762
  const legacyScope = resolveLegacyScopeFromElement(element);
2688
2763
  if (!legacyScope) {
2689
2764
  console.warn(
2690
- ` [${import_path11.default.relative(process.cwd(), rolesFilePath)}] Skipping non-literal scope expression: ${element.getText()}`
2765
+ ` [${relPath}] Preserving non-literal scope expression verbatim: ${element.getText()}`
2691
2766
  );
2767
+ rawTexts.push(element.getText());
2692
2768
  continue;
2693
2769
  }
2694
- try {
2695
- const migratedScope = legacyScopeToV2Scope(legacyScope);
2696
- const scope = decodeScope(migratedScope);
2697
- if (!scope) {
2698
- console.warn(
2699
- ` [${import_path11.default.relative(process.cwd(), rolesFilePath)}] Could not decode migrated scope '${migratedScope}'.`
2700
- );
2701
- continue;
2702
- }
2703
- element.replaceWithText(JSON.stringify(scope));
2704
- migratedCount++;
2705
- } catch (error) {
2706
- console.warn(
2707
- ` [${import_path11.default.relative(process.cwd(), rolesFilePath)}] Could not migrate scope '${legacyScope}': ${error}`
2708
- );
2709
- }
2770
+ legacyStrings.push(legacyScope);
2771
+ anyChange = true;
2772
+ }
2773
+ if (!anyChange) continue;
2774
+ let mergedScopes;
2775
+ try {
2776
+ mergedScopes = migrateLegacyScopesArrayToV2Scopes(legacyStrings);
2777
+ } catch (error) {
2778
+ throw new Error(
2779
+ `[${relPath}] Failed to migrate scopes for role: ${error instanceof Error ? error.message : String(error)}`,
2780
+ { cause: error }
2781
+ );
2710
2782
  }
2783
+ migratedCount += legacyStrings.length;
2784
+ mergedCount += legacyStrings.length - mergedScopes.length;
2785
+ const outputElements = [
2786
+ ...rawTexts,
2787
+ ...mergedScopes.map((s) => JSON.stringify(s))
2788
+ ];
2789
+ scopesProp.setInitializer(`[${outputElements.join(", ")}]`);
2711
2790
  }
2712
2791
  if (removedCount > 0) {
2713
2792
  console.log(
2714
- ` Removed ${removedCount} unknown SCOPES reference(s) from '${import_path11.default.relative(process.cwd(), rolesFilePath)}'.`
2793
+ ` Removed ${removedCount} unknown SCOPES reference(s) from '${relPath}'.`
2715
2794
  );
2716
2795
  }
2717
- return { migratedCount, removedCount };
2796
+ return { migratedCount, removedCount, mergedCount };
2718
2797
  }
2719
2798
  function ensureScopesHelpersImport(rolesFilePath, project) {
2720
2799
  const sourceFile = project.getSourceFile(rolesFilePath);
@@ -2849,7 +2928,7 @@ async function main11() {
2849
2928
  return;
2850
2929
  }
2851
2930
  const importChanged = ensureScopesHelpersImport(rolesFilePath, project);
2852
- const { migratedCount, removedCount } = migrateScopesArrays(
2931
+ const { migratedCount, removedCount, mergedCount } = migrateScopesArrays(
2853
2932
  rolesFilePath,
2854
2933
  project
2855
2934
  );
@@ -2868,7 +2947,7 @@ async function main11() {
2868
2947
  }
2869
2948
  await sourceFile.save();
2870
2949
  console.log(
2871
- `Done. Migrated ${migratedCount} legacy scope(s), removed ${removedCount} unknown SCOPES reference(s), wrapped ${wrappedScopesCount} scope array(s) with defineScopes()${wrappedRoles ? ", wrapped roles with defineRoles()" : ""}${removedRoleType ? ", and removed Role type definition" : ""}${importChanged ? ". Updated toolkit/scopes imports." : "."}`
2950
+ `Done. Migrated ${migratedCount} legacy scope(s), merged ${mergedCount} AND-paired scope(s) into single v2 objects (privilege-escalation fix, see issue #12489), removed ${removedCount} unknown SCOPES reference(s), wrapped ${wrappedScopesCount} scope array(s) with defineScopes()${wrappedRoles ? ", wrapped roles with defineRoles()" : ""}${removedRoleType ? ", and removed Role type definition" : ""}${importChanged ? ". Updated toolkit/scopes imports." : "."}`
2872
2951
  );
2873
2952
  }
2874
2953
 
@@ -4752,9 +4831,9 @@ var import_path24 = require("path");
4752
4831
  var TEMP_REMOTE2 = "opencrvs-upgrade-v19-v20-codemod-merge-infra";
4753
4832
  var INFRASTRUCTURE_DIR = "infrastructure";
4754
4833
  var BASE_BRANCH = "release-v1.9";
4755
- function gitShowBytes(ref, path23) {
4834
+ function gitShowBytes(ref, path24) {
4756
4835
  try {
4757
- const buf = (0, import_child_process2.execFileSync)("git", ["show", `${ref}:${path23}`], {
4836
+ const buf = (0, import_child_process2.execFileSync)("git", ["show", `${ref}:${path24}`], {
4758
4837
  cwd: process.cwd(),
4759
4838
  stdio: ["ignore", "pipe", "pipe"]
4760
4839
  });
@@ -4930,6 +5009,94 @@ async function main26({
4930
5009
  );
4931
5010
  }
4932
5011
 
5012
+ // src/migrations/v2.0/fix-accept-requested-registration-function-type.ts
5013
+ var import_ts_morph22 = require("ts-morph");
5014
+ var import_path26 = __toESM(require("path"));
5015
+ var TARGET_FUNCTION_NAME = "acceptRequestedRegistration";
5016
+ var ACTION_PARAMETER_NAME = "action";
5017
+ var OLD_TYPE = "ActionInput";
5018
+ var NEW_TYPE = "Extract<ActionInput, { type?: 'REGISTER' }>";
5019
+ function isAcceptRequestedRegistrationParameter(parameter) {
5020
+ const functionDeclaration = parameter.getFirstAncestorByKind(
5021
+ import_ts_morph22.SyntaxKind.FunctionDeclaration
5022
+ );
5023
+ if (functionDeclaration?.getName() === TARGET_FUNCTION_NAME) {
5024
+ return true;
5025
+ }
5026
+ const arrowFunction = parameter.getFirstAncestorByKind(
5027
+ import_ts_morph22.SyntaxKind.ArrowFunction
5028
+ );
5029
+ const variableDeclaration = arrowFunction?.getFirstAncestorByKind(
5030
+ import_ts_morph22.SyntaxKind.VariableDeclaration
5031
+ );
5032
+ return variableDeclaration?.getName() === TARGET_FUNCTION_NAME;
5033
+ }
5034
+ function processFile14(filePath, project) {
5035
+ const sourceFile = project.getSourceFile(filePath);
5036
+ if (!sourceFile) return 0;
5037
+ let updatedCount = 0;
5038
+ for (const parameter of sourceFile.getDescendantsOfKind(
5039
+ import_ts_morph22.SyntaxKind.Parameter
5040
+ )) {
5041
+ if (parameter.getName() !== ACTION_PARAMETER_NAME) {
5042
+ continue;
5043
+ }
5044
+ if (!isAcceptRequestedRegistrationParameter(parameter)) {
5045
+ continue;
5046
+ }
5047
+ const typeNode = parameter.getTypeNode();
5048
+ if (!typeNode || typeNode.getText() !== OLD_TYPE) {
5049
+ continue;
5050
+ }
5051
+ parameter.setType(NEW_TYPE);
5052
+ updatedCount++;
5053
+ console.log(
5054
+ ` [${import_path26.default.relative(process.cwd(), filePath)}] Updated '${ACTION_PARAMETER_NAME}' in ${TARGET_FUNCTION_NAME} to ${NEW_TYPE}`
5055
+ );
5056
+ }
5057
+ return updatedCount;
5058
+ }
5059
+ async function main27() {
5060
+ const srcDir = import_path26.default.join(process.cwd(), "src");
5061
+ console.log(`Scanning for ${TARGET_FUNCTION_NAME} in: ${srcDir}
5062
+ `);
5063
+ const project = new import_ts_morph22.Project({
5064
+ tsConfigFilePath: import_path26.default.resolve(srcDir, "../tsconfig.json"),
5065
+ skipAddingFilesFromTsConfig: false
5066
+ });
5067
+ const sourceFiles = project.getSourceFiles().filter((sf) => {
5068
+ const filePath = sf.getFilePath();
5069
+ return filePath.includes("/src/") && !filePath.includes("/node_modules/");
5070
+ });
5071
+ let totalUpdated = 0;
5072
+ const modifiedFiles = [];
5073
+ for (const sourceFile of sourceFiles) {
5074
+ const filePath = sourceFile.getFilePath();
5075
+ const updated = processFile14(filePath, project);
5076
+ if (updated > 0) {
5077
+ totalUpdated += updated;
5078
+ modifiedFiles.push(filePath);
5079
+ }
5080
+ }
5081
+ if (modifiedFiles.length === 0) {
5082
+ console.log(
5083
+ `No '${ACTION_PARAMETER_NAME}: ${OLD_TYPE}' parameter in ${TARGET_FUNCTION_NAME} found. Nothing to do.`
5084
+ );
5085
+ return;
5086
+ }
5087
+ console.log(`
5088
+ Saving ${modifiedFiles.length} modified file(s)...`);
5089
+ for (const filePath of modifiedFiles) {
5090
+ const sourceFile = project.getSourceFileOrThrow(filePath);
5091
+ await sourceFile.save();
5092
+ console.log(` Saved: ${import_path26.default.relative(process.cwd(), filePath)}`);
5093
+ }
5094
+ console.log(
5095
+ `
5096
+ Done. Updated ${totalUpdated} parameter type annotation(s) in ${TARGET_FUNCTION_NAME}.`
5097
+ );
5098
+ }
5099
+
4933
5100
  // src/migrations/v2.0/index.ts
4934
5101
  var NON_SWARM_INFRA_SUBDIRS = ["postgres", "metabase"];
4935
5102
  async function runUpgrade(dockerSwarm) {
@@ -4956,6 +5123,7 @@ async function runUpgrade(dockerSwarm) {
4956
5123
  await main22();
4957
5124
  await main23();
4958
5125
  await main24();
5126
+ await main27();
4959
5127
  await main17();
4960
5128
  if (dockerSwarm) {
4961
5129
  await main25();
@@ -4990,7 +5158,7 @@ Options:
4990
5158
  'infrastructure/' directory is deleted (default).
4991
5159
  -h, --help Show this message.
4992
5160
  `.trim();
4993
- function main27() {
5161
+ function main28() {
4994
5162
  const command = args[0];
4995
5163
  if (!command || command === "--help" || command === "-h") {
4996
5164
  console.log(USAGE);
@@ -5065,4 +5233,4 @@ function handleCheckTranslations() {
5065
5233
  console.warn("This command is not implemented yet!");
5066
5234
  process.exit(1);
5067
5235
  }
5068
- main27();
5236
+ main28();