import { resolveMissingBirthTimezoneOffset } from "./birth-profile-timezone.ts"; import { resolveReportBirthClock } from "./personal-report-route-core.ts"; import { PersonalReportWorkerError } from "./personal-report-worker-core.ts"; import { ConsultationSubjectError, loadSubjectBirth } from "./subject-birth.ts"; export async function loadReportWorkerProfile(client: unknown, userId: string, subjectId: string): Promise { try { const resolved = await loadSubjectBirth(client, userId, subjectId); if (!resolved.chartable) throw new PersonalReportWorkerError("profile_incomplete", false); let row = resolved.row; if (resolved.role === "self") { // Case linkage is worker metadata, not a second source of birth data. const metadataClient = client as { from: (table: string) => { select: (columns: string) => { eq: (column: string, value: string) => { maybeSingle: () => PromiseLike<{ data: { rectification_case_id?: unknown } | null; error: unknown }>; } }; } }; const { data, error } = await metadataClient.from("profiles") .select("rectification_case_id").eq("id", userId).maybeSingle(); if (error) throw new PersonalReportWorkerError("profile_incomplete", true); row = { ...row, rectification_case_id: data?.rectification_case_id ?? null }; } return await resolveMissingBirthTimezoneOffset(row, { useActiveDate: true }); } catch (error) { if (error instanceof ConsultationSubjectError) { // Missing/deleted/foreign subjects are terminal and release the existing // reservation. A temporary database outage may retry, never as self. throw new PersonalReportWorkerError("profile_incomplete", error.code === "subject_unavailable"); } throw error; } } export function reportWorkerUsesCandidateRange( report: { chartProfileId: string | null }, profile: Record, ): boolean { return (!report.chartProfileId || report.chartProfileId === "self") && resolveReportBirthClock(profile)?.status !== "confirmed"; }