@opencrvs/toolkit 1.8.1-rc.98d21b5 → 1.8.1-rc.9f4c5c5

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.
@@ -1,9 +1,9 @@
1
1
  import { ConditionalParameters, JSONSchema } from './conditionals';
2
- import { EventState, ActionUpdate } from '../events/ActionDocument';
2
+ import { ActionUpdate, EventState } from '../events/ActionDocument';
3
+ import { FieldConditional } from '../events/Conditional';
3
4
  import { FieldConfig } from '../events/FieldConfig';
4
5
  import { FieldUpdateValue } from '../events/FieldValue';
5
6
  import { TranslationConfig } from '../events/TranslationConfig';
6
- import { FieldConditional } from '../events/Conditional';
7
7
  export declare function validate(schema: JSONSchema, data: ConditionalParameters): boolean;
8
8
  export declare function isConditionMet(conditional: JSONSchema, values: Record<string, unknown>): boolean;
9
9
  export declare function areConditionsMet(conditions: FieldConditional[], values: Record<string, unknown>): boolean;
@@ -77,4 +77,5 @@ export declare function runFieldValidations({ field, values }: {
77
77
  }[];
78
78
  };
79
79
  export declare function getValidatorsForField(fieldId: FieldConfig['id'], validations: NonNullable<FieldConfig['validation']>): NonNullable<FieldConfig['validation']>;
80
+ export declare function areCertificateConditionsMet(conditions: FieldConditional[], values: Record<string, unknown>): boolean;
80
81
  //# sourceMappingURL=validate.d.ts.map
@@ -45,9 +45,63 @@ declare function eventFn(fieldId: EventFieldId): {
45
45
  declare const event: typeof eventFn & {
46
46
  /**
47
47
  * Checks if the event contains a specific action type.
48
+ * Can be used directly as a conditional or chained with additional methods.
48
49
  * @param action - The action type to check for.
49
50
  */
50
- hasAction: (action: ActionType) => import("../conditionals/conditionals").JSONSchema;
51
+ hasAction: (action: ActionType) => {
52
+ /**
53
+ * Creates a conditional that checks if the event contains a specific action type
54
+ * with a minimum count of occurrences.
55
+ *
56
+ * @param minCount - The minimum number of actions required.
57
+ */
58
+ minCount: (minCount: number) => import("../conditionals/conditionals").JSONSchema;
59
+ /**
60
+ * Builds a conditional that sets a maximum count for the number of actions.
61
+ * This is useful for limiting the number of actions of a specific type in a single event.
62
+ */
63
+ maxCount: (maxCount: number) => import("../conditionals/conditionals").JSONSchema;
64
+ /**
65
+ * Adds additional field constraints to the action matching.
66
+ *
67
+ * @param fields - Object containing additional fields to match on the action.
68
+ */
69
+ withFields: (fields: Record<string, unknown>) => {
70
+ /**
71
+ * Creates a conditional that checks if the event contains a specific action type
72
+ * with a minimum count of occurrences.
73
+ *
74
+ * @param minCount - The minimum number of actions required.
75
+ */
76
+ minCount: (minCount: number) => import("../conditionals/conditionals").JSONSchema;
77
+ /**
78
+ * Builds a conditional that sets a maximum count for the number of actions.
79
+ * This is useful for limiting the number of actions of a specific type in a single event.
80
+ */
81
+ maxCount: (maxCount: number) => import("../conditionals/conditionals").JSONSchema;
82
+ };
83
+ /**
84
+ * Adds template ID constraint to the action matching.
85
+ * This is a convenience method that adds content.templateId to the fields.
86
+ *
87
+ * @param id - The template ID to match against.
88
+ */
89
+ withTemplate: (id: string) => {
90
+ /**
91
+ * Creates a conditional that checks if the event contains a specific action type
92
+ * with a minimum count of occurrences.
93
+ *
94
+ * @param minCount - The minimum number of actions required.
95
+ */
96
+ minCount: (minCount: number) => import("../conditionals/conditionals").JSONSchema;
97
+ /**
98
+ * Builds a conditional that sets a maximum count for the number of actions.
99
+ * This is useful for limiting the number of actions of a specific type in a single event.
100
+ */
101
+ maxCount: (maxCount: number) => import("../conditionals/conditionals").JSONSchema;
102
+ };
103
+ __nominal__type: "JSONSchema";
104
+ };
51
105
  field(field: WorkqueueColumnKeys): WorkqueueColumnValue;
52
106
  };
53
107
  export { event };
@@ -198,6 +198,7 @@ __export(events_exports, {
198
198
  annotationActions: () => annotationActions,
199
199
  applyDeclarationToEventIndex: () => applyDeclarationToEventIndex,
200
200
  applyDraftsToEventIndex: () => applyDraftsToEventIndex,
201
+ areCertificateConditionsMet: () => areCertificateConditionsMet,
201
202
  areConditionsMet: () => areConditionsMet,
202
203
  compositeFieldTypes: () => compositeFieldTypes,
203
204
  createEmptyDraft: () => createEmptyDraft,
@@ -1223,7 +1224,8 @@ var CertificateConfig = import_zod11.z.object({
1223
1224
  delayed: import_zod11.z.number()
1224
1225
  }),
1225
1226
  svgUrl: import_zod11.z.string(),
1226
- fonts: import_zod11.z.record(FontFamily).optional()
1227
+ fonts: import_zod11.z.record(FontFamily).optional(),
1228
+ conditionals: import_zod11.z.array(ShowConditional).optional()
1227
1229
  });
1228
1230
  var CertificateTemplateConfig = CertificateConfig.extend({
1229
1231
  hash: import_zod11.z.string().optional(),
@@ -2045,7 +2047,7 @@ var ResolvedUser = import_zod19.z.object({
2045
2047
  });
2046
2048
 
2047
2049
  // ../commons/src/conditionals/validate.ts
2048
- var import_ajv = __toESM(require("ajv"));
2050
+ var import__ = __toESM(require("ajv/dist/2019"));
2049
2051
  var import_ajv_formats = __toESM(require("ajv-formats"));
2050
2052
  var import_date_fns = require("date-fns");
2051
2053
 
@@ -2259,9 +2261,11 @@ var isNonInteractiveFieldType = (field2) => {
2259
2261
  };
2260
2262
 
2261
2263
  // ../commons/src/conditionals/validate.ts
2262
- var ajv = new import_ajv.default({
2264
+ var ajv = new import__.default({
2263
2265
  $data: true,
2264
- allowUnionTypes: true
2266
+ allowUnionTypes: true,
2267
+ strict: false
2268
+ // Allow minContains and other newer features
2265
2269
  });
2266
2270
  (0, import_ajv_formats.default)(ajv);
2267
2271
  ajv.addKeyword({
@@ -2499,6 +2503,11 @@ function getValidatorsForField(fieldId, validations) {
2499
2503
  };
2500
2504
  }).filter((x) => x !== null);
2501
2505
  }
2506
+ function areCertificateConditionsMet(conditions, values) {
2507
+ return conditions.every((condition) => {
2508
+ return ajv.validate(condition.conditional, values);
2509
+ });
2510
+ }
2502
2511
 
2503
2512
  // ../commons/src/utils.ts
2504
2513
  function getOrThrow(x, message) {
@@ -3222,32 +3231,107 @@ function eventFn(fieldId) {
3222
3231
  var event = Object.assign(eventFn, {
3223
3232
  /**
3224
3233
  * Checks if the event contains a specific action type.
3234
+ * Can be used directly as a conditional or chained with additional methods.
3225
3235
  * @param action - The action type to check for.
3226
3236
  */
3227
- hasAction: (action) => defineConditional({
3228
- type: "object",
3229
- properties: {
3230
- $event: {
3237
+ hasAction: (action) => {
3238
+ const basicConditional = defineConditional({
3239
+ type: "object",
3240
+ properties: {
3241
+ $event: {
3242
+ type: "object",
3243
+ properties: {
3244
+ actions: {
3245
+ type: "array",
3246
+ contains: {
3247
+ type: "object",
3248
+ properties: {
3249
+ type: {
3250
+ const: action
3251
+ }
3252
+ },
3253
+ required: ["type"]
3254
+ }
3255
+ }
3256
+ },
3257
+ required: ["actions"]
3258
+ }
3259
+ },
3260
+ required: ["$event"]
3261
+ });
3262
+ const buildActionConstraints = (additionalFields) => {
3263
+ const actionProperties = {
3264
+ type: { const: action }
3265
+ };
3266
+ const requiredFields = ["type"];
3267
+ if (additionalFields) {
3268
+ Object.entries(additionalFields).forEach(([key, value]) => {
3269
+ actionProperties[key] = { const: value };
3270
+ requiredFields.push(key);
3271
+ });
3272
+ }
3273
+ return { actionProperties, requiredFields };
3274
+ };
3275
+ const createCountConditional = (countType, count, additionalFields) => {
3276
+ const { actionProperties, requiredFields } = buildActionConstraints(additionalFields);
3277
+ return defineConditional({
3231
3278
  type: "object",
3232
3279
  properties: {
3233
- actions: {
3234
- type: "array",
3235
- contains: {
3236
- type: "object",
3237
- properties: {
3238
- type: {
3239
- const: action
3240
- }
3241
- },
3242
- required: ["type"]
3243
- }
3280
+ $event: {
3281
+ type: "object",
3282
+ properties: {
3283
+ actions: {
3284
+ type: "array",
3285
+ contains: {
3286
+ type: "object",
3287
+ properties: actionProperties,
3288
+ required: requiredFields
3289
+ },
3290
+ [countType]: count
3291
+ }
3292
+ },
3293
+ required: ["actions"]
3244
3294
  }
3245
3295
  },
3246
- required: ["actions"]
3247
- }
3248
- },
3249
- required: ["$event"]
3250
- }),
3296
+ required: ["$event"]
3297
+ });
3298
+ };
3299
+ const withMinMax = (additionalFields) => {
3300
+ return {
3301
+ /**
3302
+ * Creates a conditional that checks if the event contains a specific action type
3303
+ * with a minimum count of occurrences.
3304
+ *
3305
+ * @param minCount - The minimum number of actions required.
3306
+ */
3307
+ minCount: (minCount) => createCountConditional("minContains", minCount, additionalFields),
3308
+ /**
3309
+ * Builds a conditional that sets a maximum count for the number of actions.
3310
+ * This is useful for limiting the number of actions of a specific type in a single event.
3311
+ */
3312
+ maxCount: (maxCount) => createCountConditional("maxContains", maxCount, additionalFields)
3313
+ };
3314
+ };
3315
+ const chainableMethods = {
3316
+ /**
3317
+ * Adds additional field constraints to the action matching.
3318
+ *
3319
+ * @param fields - Object containing additional fields to match on the action.
3320
+ */
3321
+ withFields: (fields) => withMinMax(fields),
3322
+ /**
3323
+ * Adds template ID constraint to the action matching.
3324
+ * This is a convenience method that adds content.templateId to the fields.
3325
+ *
3326
+ * @param id - The template ID to match against.
3327
+ */
3328
+ withTemplate: (id) => withMinMax({
3329
+ content: { templateId: id }
3330
+ }),
3331
+ ...withMinMax()
3332
+ };
3333
+ return { ...basicConditional, ...chainableMethods };
3334
+ },
3251
3335
  field(field2) {
3252
3336
  return {
3253
3337
  $event: field2
@@ -6850,7 +6934,11 @@ function generateActionDocument({
6850
6934
  case ActionType.NOTIFY:
6851
6935
  return { ...actionBase, type: action };
6852
6936
  case ActionType.PRINT_CERTIFICATE:
6853
- return { ...actionBase, type: action };
6937
+ return {
6938
+ ...actionBase,
6939
+ type: action,
6940
+ content: defaults.content
6941
+ };
6854
6942
  case ActionType.REQUEST_CORRECTION:
6855
6943
  return { ...actionBase, type: action };
6856
6944
  case ActionType.APPROVE_CORRECTION:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencrvs/toolkit",
3
- "version": "1.8.1-rc.98d21b5",
3
+ "version": "1.8.1-rc.9f4c5c5",
4
4
  "description": "OpenCRVS toolkit for building country configurations",
5
5
  "license": "MPL-2.0",
6
6
  "exports": {
package/tsconfig.json CHANGED
@@ -19,7 +19,7 @@
19
19
  "@opencrvs/commons/events": ["../commons/src/events/index.ts"]
20
20
  }
21
21
  },
22
- "references": [{ "path": "../commons" }],
22
+ "references": [{ "path": "../commons/tsconfig-commonjs.json" }],
23
23
  "include": ["src/**/*.ts"],
24
24
  "exclude": ["**/node_modules/**", "dist"]
25
25
  }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=conditionals.test.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=validate-address.test.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=validate.test.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=utils.test.d.ts.map