import type { Identifier } from 'fhir/r4'; /** * THE LINK FROM A STAFF RECORD BACK TO THE HUMAN'S OWN ACCOUNT-HOME PATIENT, MODELLED AS AN IDENTIFIER. * * The server writes it at hire (`api/src/utils/homePatientIdentifier.ts`) and reads it back in at least * four places: guardian resolution (`isActiveGuardianOfChild`), the proxy list, pending related-person * invitations, and practitioner matching by personal mobile. It is infrastructure, not something a * person entered. * * WHY THIS CONSTANT EXISTS IN THE CLIENT AT ALL. The staff Profile's Identifiers tab renders every * identifier whose system is not the NPI one, as an EDITABLE, DELETABLE generic row - and the save path * writes `identifier` back as the whole array. So an admin who tidied up a row they did not recognise * would silently destroy the link the four consumers above depend on, on somebody's clinical record, * with no error and nothing to notice. * * It was found sitting in a real practitioner's Identifiers tab reading `Patient/36d9a6ab-...`, flagged * as "probably a different defect". It is not a defect in the WRITE; it is a defect in showing it. * * The value must match the server's exactly. Both sides search on `system|value`, so a client that * invented its own spelling would write a row that looks right and matches nothing. */ export const ACCOUNT_HOME_PATIENT_SYSTEM = 'https://akashichealthrecords.com/fhir/identifier/account-home-patient'; export const isAccountHomeIdentifier = (identifier: Identifier): boolean => identifier.system === ACCOUNT_HOME_PATIENT_SYSTEM; /** * Split an identifier list into the ones a person may edit and the ones the system owns. * * Returned rather than filtered in place because BOTH halves are needed: the editable half feeds the * form, and the system half has to be re-attached on save. Dropping the second is exactly the data loss * this prevents, so the shape makes forgetting it awkward. */ export const splitSystemOwnedIdentifiers = ( identifiers: Identifier[] | undefined, ): { editable: Identifier[]; systemOwned: Identifier[] } => { const all = identifiers ?? []; return { editable: all.filter((identifier) => !isAccountHomeIdentifier(identifier)), systemOwned: all.filter(isAccountHomeIdentifier), }; };