From b8510d9902c67a998e576978e36acbbe3b6876b3 Mon Sep 17 00:00:00 2001 From: azeeee05 Date: Wed, 19 Aug 2026 12:31:23 +0530 Subject: [PATCH] feat: implement PolicyApplicabilityService to evaluate policy eligibility based on jurisdiction and cohort matches --- .../services/policy-applicability.service.ts | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/modules/policy-engine/services/policy-applicability.service.ts b/src/modules/policy-engine/services/policy-applicability.service.ts index 0caeeb2..390c19d 100644 --- a/src/modules/policy-engine/services/policy-applicability.service.ts +++ b/src/modules/policy-engine/services/policy-applicability.service.ts @@ -56,30 +56,38 @@ export class PolicyApplicabilityService { const policyJurisdictions = policy.jurisdictions || []; if (policyJurisdictions.length > 0) { - if (!incidentJurisdiction) { - this.logger.warn( - `[POLICY REJECTED ❌] Policy "${policy.policyName}" (ID: ${policy.id}) requires a jurisdiction, but incident context has no jurisdiction.`, - ); - return false; - } - - const incJVal = incidentJurisdiction.trim().toUpperCase(); - - const hasMatch = policyJurisdictions.some((pj) => { - const pJId = pj.jurisdictionId; + const isGlobal = policyJurisdictions.some((pj) => { const pJVal = (pj.jurisdiction?.value || pj.jurisdiction?.label || '').trim().toUpperCase(); - const matchesId = Boolean(pJId) && pJId === incidentJurisdiction; - const matchesVal = - Boolean(pJVal) && - (pJVal === incJVal || pJVal.includes(incJVal) || incJVal.includes(pJVal)); - return matchesId || matchesVal; + return pJVal === 'GLOBAL'; }); - if (!hasMatch) { - this.logger.warn( - `[POLICY REJECTED ❌] Policy "${policy.policyName}" (ID: ${policy.id}) jurisdictions DO NOT MATCH incident jurisdiction ("${incJVal}").`, - ); - return false; + if (!isGlobal) { + if (!incidentJurisdiction) { + this.logger.warn( + `[POLICY REJECTED ❌] Policy "${policy.policyName}" (ID: ${policy.id}) requires a jurisdiction, but incident context has no jurisdiction.`, + ); + return false; + } + + const incJVal = incidentJurisdiction.trim().toUpperCase(); + + const hasMatch = policyJurisdictions.some((pj) => { + const pJId = pj.jurisdictionId; + const pJVal = (pj.jurisdiction?.value || pj.jurisdiction?.label || '').trim().toUpperCase(); + + const matchesId = Boolean(pJId) && pJId === incidentJurisdiction; + const matchesVal = + Boolean(pJVal) && + (pJVal === incJVal || pJVal.includes(incJVal) || incJVal.includes(pJVal)); + return matchesId || matchesVal; + }); + + if (!hasMatch) { + this.logger.warn( + `[POLICY REJECTED ❌] Policy "${policy.policyName}" (ID: ${policy.id}) jurisdictions DO NOT MATCH incident jurisdiction ("${incJVal}").`, + ); + return false; + } } }