Skip to content

Commit

Permalink
(fix) add validation rule for individual conditions in a condition group
Browse files Browse the repository at this point in the history
  • Loading branch information
usamaidrsk committed Nov 29, 2024
1 parent f15bd99 commit 539517b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/config-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '@openmrs/esm-framework';
import { Type, validators } from '@openmrs/esm-framework';

/**
* Represents the possible states of a billing line item
Expand Down Expand Up @@ -27,14 +27,23 @@ export type ConditionGroup = string;
* Validates that condition groups don't contain conflicting states (e.g., INVOICED and NOT_INVOICED)
*/
const validateConditionGroup = (conditions: string) => {
const allowedConditions = Object.values(BillingCondition);
const conditionsArray = conditions.split(',');
const conflictPairs = [
['INVOICED', 'NOT_INVOICED'],
['FULLY_INVOICED', 'PARTIALLY_INVOICED'],
['PAID', 'NOT_PAID'],
['OVERDUE', 'NOT_OVERDUE'],
];

const conditionsArray = conditions.split(',');
for (const condition of conditionsArray) {
const validateCondition = validators.oneOf(allowedConditions);
const result = validateCondition(condition);
if (result !== undefined) {
return result;
}
}

for (const pair of conflictPairs) {
if (conditionsArray.includes(pair[0]) && conditionsArray.includes(pair[1])) {
return `Condition group contains conflicting states: ${pair[0]} and ${pair[1]}`;
Expand Down

0 comments on commit 539517b

Please sign in to comment.