feat: implement PolicyApplicabilityService to evaluate policy eligibility based on jurisdiction and cohort matches

This commit is contained in:
azeeee05
2026-08-19 12:31:23 +05:30
parent 6c1a80587e
commit b8510d9902
@@ -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;
}
}
}