forked from Yara724/api
Merge pull request 'Fixed visibility rules to match the business rules' (#112) from s.yahyaee/yara724-api:main into main
Reviewed-on: Yara724/api#112
This commit is contained in:
@@ -13,6 +13,23 @@ export function requireActorClientKey(actor: { clientKey?: string }): string {
|
|||||||
|
|
||||||
export function blameCaseTouchesClient(doc: any, clientKey: string): boolean {
|
export function blameCaseTouchesClient(doc: any, clientKey: string): boolean {
|
||||||
const id = String(clientKey);
|
const id = String(clientKey);
|
||||||
|
|
||||||
|
// CAR_BODY: single party, always the first party's clientId
|
||||||
|
if (doc?.type === "CAR_BODY") {
|
||||||
|
const firstParty = (doc?.parties ?? []).find(
|
||||||
|
(p: any) => p?.role === "FIRST",
|
||||||
|
);
|
||||||
|
return String(firstParty?.person?.clientId ?? "") === id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// THIRD_PARTY: only the guilty party's insurer sees this file
|
||||||
|
const guiltyClientId = resolveGuiltyPartyClientId(doc);
|
||||||
|
if (guiltyClientId) {
|
||||||
|
return guiltyClientId === id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback when guilt cannot be resolved yet — show to any party's insurer
|
||||||
|
// This should be rare since guilt is always known upfront in your system
|
||||||
return (doc?.parties ?? []).some(
|
return (doc?.parties ?? []).some(
|
||||||
(p: any) => String(p?.person?.clientId ?? "") === id,
|
(p: any) => String(p?.person?.clientId ?? "") === id,
|
||||||
);
|
);
|
||||||
@@ -27,14 +44,11 @@ export function claimCaseTouchesClient(doc: any, clientKey: string): boolean {
|
|||||||
return String(tenantId ?? "") === id;
|
return String(tenantId ?? "") === id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Field expert–initiated cases may not yet have party clientId filled; the
|
|
||||||
* initiating expert is always scoped to their insurer via JWT.
|
|
||||||
*/
|
|
||||||
export function blameCaseAccessibleToExpert(
|
export function blameCaseAccessibleToExpert(
|
||||||
doc: any,
|
doc: any,
|
||||||
actor: { sub: string; clientKey?: string },
|
actor: { sub: string; clientKey?: string },
|
||||||
): boolean {
|
): boolean {
|
||||||
|
// Expert-initiated cases: only the initiating expert sees them
|
||||||
if (
|
if (
|
||||||
doc.expertInitiated &&
|
doc.expertInitiated &&
|
||||||
doc.initiatedByFieldExpertId &&
|
doc.initiatedByFieldExpertId &&
|
||||||
@@ -42,8 +56,11 @@ export function blameCaseAccessibleToExpert(
|
|||||||
) {
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ck = actor?.clientKey;
|
const ck = actor?.clientKey;
|
||||||
if (!ck) return false;
|
if (!ck) return false;
|
||||||
|
|
||||||
|
// Uses the updated blameCaseTouchesClient — guilty party's clientId only
|
||||||
return blameCaseTouchesClient(doc, ck);
|
return blameCaseTouchesClient(doc, ck);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,3 +86,37 @@ export function assertClaimCaseForTenant(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resolveGuiltyPartyClientId(doc: any): string | null {
|
||||||
|
const parties: any[] = doc?.parties ?? [];
|
||||||
|
|
||||||
|
// Phase 1: expert decision exists — use it
|
||||||
|
const guiltyUserId = doc?.expert?.decision?.guiltyPartyId
|
||||||
|
? String(doc.expert.decision.guiltyPartyId)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (guiltyUserId) {
|
||||||
|
const guiltyParty = parties.find(
|
||||||
|
(p) => String(p?.person?.userId ?? "") === guiltyUserId,
|
||||||
|
);
|
||||||
|
return String(guiltyParty?.person?.clientId ?? "") || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Phase 2: no expert decision yet — use party self-statements
|
||||||
|
// The party who admitsGuilt=true is the guilty one
|
||||||
|
const guiltyByStatement = parties.find(
|
||||||
|
(p) => p?.statement?.admitsGuilt === true,
|
||||||
|
);
|
||||||
|
if (guiltyByStatement) {
|
||||||
|
return String(guiltyByStatement?.person?.clientId ?? "") || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Phase 3: fallback — use blameStatus field if set
|
||||||
|
// AGREED means first party admitted guilt (convention in your system)
|
||||||
|
if (doc?.blameStatus === "AGREED") {
|
||||||
|
const firstParty = parties.find((p) => p?.role === "FIRST");
|
||||||
|
return String(firstParty?.person?.clientId ?? "") || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user