/** * The sentence a clinician should read when a meeting for this context is ALREADY LIVE. * * `POST /R4/video-session` answers `409 { message, code: 'CONTEXT_TAKEN' }` when a session already * exists for the context being created. Relaying that `message` verbatim put *"Request failed: A live * session already exists for this context"* on screen - developer wording, for a situation with a * plain meaning and an obvious next action. The refusal itself is correct and the server is the * boundary; what was wrong is only what the person was told. * * KEYED ON THE CODE, NEVER THE SENTENCE. The server's wording is free to change and this copy must * not follow it; `code` is the part meant to be read by a program. Matching on the message would also * put us one rephrasing away from silently falling back to the raw string again. * * Returns `undefined` for anything else, so the caller keeps reporting every other failure exactly as * before. An unmapped refusal must still say something: a denial that renders as silence is the * defect this codebase is most careful about, and trading a badly-worded message for no message would * be the worse bug. * * Deliberately tolerant about the error's shape, mirroring `isBillingLimitRefusal`: the HTTP layer * wraps failures differently by path, and `HttpError` carries the parsed `body` alongside its flattened * message precisely so a caller can read the machine-readable half. */ const CONTEXT_TAKEN = 'CONTEXT_TAKEN'; /** * TWO SENTENCES, BECAUSE THE SAME REFUSAL MEANS TWO DIFFERENT THINGS. `POST /R4/video-session` is the * create route for BOTH a case conference (`collabteam#`) and a patient's video visit (`encounter#`), * and it answers the same 409 for each. Telling a clinician who pressed "Start video" on a visit that * "a case conference is already running for this team" would be a confident, wrong sentence about the * wrong thing - the failure mode this whole change exists to remove, reintroduced one layer up. */ export const CONTEXT_TAKEN_NOTICE = 'A case conference is already running for this team. Rejoin it rather than starting another.'; export const VISIT_CONTEXT_TAKEN_NOTICE = 'A video visit is already running for this patient. Rejoin it rather than starting another.'; export const contextTakenNotice = (error: unknown, context?: string): string | undefined => { const candidate = error as | { status?: number; statusCode?: number; body?: { code?: string }; response?: { status?: number } } | undefined; const status = candidate?.status ?? candidate?.statusCode ?? candidate?.response?.status; if (status !== 409) return undefined; if (candidate?.body?.code !== CONTEXT_TAKEN) return undefined; // An unknown or absent context reads as the conference wording only when it says so; anything // else - including a context we have not seen - falls back to the visit wording, because that is // the surface a patient may be sitting in front of. return context?.startsWith('collabteam#') ? CONTEXT_TAKEN_NOTICE : VISIT_CONTEXT_TAKEN_NOTICE; };