Files
alla-allaos-fullstack/src/features/crm/shared/project-party.ts
phaichayon 5be6c54272 taks-d.2.1
2026-06-17 14:46:52 +07:00

79 lines
1.7 KiB
TypeScript

export const PROJECT_PARTY_OPTION_CATEGORY = 'crm_project_party_role';
const LEGACY_PROJECT_PARTY_ROLE_CODE_MAP = {
owner: 'customer',
billing: 'billing_customer',
consultant: 'consultant',
contractor: 'contractor'
} as const;
export const PROJECT_PARTY_CODE_LABELS: Record<string, string> = {
billing_customer: 'Billing Customer',
customer: 'Customer',
consultant: 'Consultant',
contractor: 'Contractor',
end_customer: 'End Customer',
other: 'Other'
};
export interface ProjectPartyRoleOptionLike {
id: string;
code: string;
label: string;
}
export interface ProjectPartyResolvedRole {
id: string;
code: string;
label: string;
}
export function normalizeProjectPartyRoleCode(role: string | null | undefined) {
if (!role) {
return null;
}
return LEGACY_PROJECT_PARTY_ROLE_CODE_MAP[role as keyof typeof LEGACY_PROJECT_PARTY_ROLE_CODE_MAP] ?? role;
}
export function resolveProjectPartyRole(
options: ProjectPartyRoleOptionLike[],
role: string | null | undefined
): ProjectPartyResolvedRole | null {
if (!role) {
return null;
}
const direct = options.find((option) => option.id === role);
if (direct) {
return {
id: direct.id,
code: direct.code,
label: direct.label
};
}
const normalizedCode = normalizeProjectPartyRoleCode(role);
if (!normalizedCode) {
return null;
}
const byCode = options.find((option) => option.code === normalizedCode);
if (!byCode) {
return null;
}
return {
id: byCode.id,
code: byCode.code,
label: byCode.label
};
}
export function buildProjectPartyKey(customerId: string, roleCode: string) {
return `${customerId}::${roleCode}`;
}