feature: Phase 2- splitting the treatment schema into TreatmentDetail and LabCase.
This commit is contained in:
@@ -71,17 +71,18 @@ function mapAppointment(record: AppointmentRecord): TreatmentAppointment {
|
||||
};
|
||||
}
|
||||
|
||||
function mapCaseFromApi(c: PastTreatmentCase): TreatmentCaseDraft {
|
||||
function mapDetailFromApi(d: PastTreatmentCase): TreatmentCaseDraft {
|
||||
return {
|
||||
clientId: c.clientId,
|
||||
id: c.id,
|
||||
treatmentType: c.treatmentType,
|
||||
teeth: c.teeth,
|
||||
comment: c.notes ?? '',
|
||||
attachmentMetas: c.attachmentMetas ?? [],
|
||||
sendToOrganizationIds: c.sendToOrganizationIds ?? [],
|
||||
sends: c.sends ?? [],
|
||||
sentAt: c.sentAt ?? null,
|
||||
clientId: d.clientId,
|
||||
id: d.id,
|
||||
treatmentType: d.treatmentType,
|
||||
teeth: d.teeth,
|
||||
comment: d.notes ?? '',
|
||||
attachmentMetas: d.attachmentMetas ?? [],
|
||||
labCaseId: d.labCaseId ?? null,
|
||||
sendToOrganizationIds: d.destinationOrganizationId ? [d.destinationOrganizationId] : [],
|
||||
sends: d.sends ?? [],
|
||||
sentAt: d.sentAt ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -110,16 +111,19 @@ function casesToPreviewTreatment(
|
||||
title: meta.title,
|
||||
treatmentAt: meta.treatmentAt,
|
||||
status: meta.status,
|
||||
cases: cases.map((c, idx) => ({
|
||||
details: cases.map((c, idx) => ({
|
||||
id: c.id ?? c.clientId ?? `draft-${idx + 1}`,
|
||||
clientId: c.clientId,
|
||||
treatmentType: c.treatmentType,
|
||||
teeth: c.teeth,
|
||||
notes: c.comment || null,
|
||||
attachmentMetas: c.attachmentMetas,
|
||||
sendToOrganizationIds: c.sendToOrganizationIds,
|
||||
labCaseId: c.labCaseId ?? null,
|
||||
destinationOrganizationId: c.sendToOrganizationIds[0] ?? null,
|
||||
sends: c.sends ?? [],
|
||||
sentAt: c.sentAt ?? null,
|
||||
})),
|
||||
labCases: [],
|
||||
documents: [],
|
||||
};
|
||||
}
|
||||
@@ -305,8 +309,8 @@ export function TreatmentWorkspace({ userId, currentOrganization }: TreatmentWor
|
||||
const response = await treatmentsApi.getDraft(appointmentId);
|
||||
if (cancelled) return;
|
||||
|
||||
if (response.data?.cases?.length) {
|
||||
const mapped = response.data.cases.map(mapCaseFromApi);
|
||||
if (response.data?.details?.length) {
|
||||
const mapped = response.data.details.map(mapDetailFromApi);
|
||||
setCases(mapped);
|
||||
setActiveCaseId((prev) => {
|
||||
const stillExists = mapped.some((c) => c.clientId === prev);
|
||||
@@ -387,7 +391,7 @@ export function TreatmentWorkspace({ userId, currentOrganization }: TreatmentWor
|
||||
if (!selectedAppointment) throw new Error('No appointment selected');
|
||||
|
||||
const response = await treatmentsApi.saveDraft(selectedAppointment.id, {
|
||||
cases: cases.map(({ clientId, id, treatmentType, teeth, comment, attachmentMetas }) => ({
|
||||
details: cases.map(({ clientId, id, treatmentType, teeth, comment, attachmentMetas }) => ({
|
||||
clientId,
|
||||
id,
|
||||
treatmentType,
|
||||
@@ -396,7 +400,7 @@ export function TreatmentWorkspace({ userId, currentOrganization }: TreatmentWor
|
||||
attachmentIds: attachmentMetas.map((a) => a.id),
|
||||
})),
|
||||
});
|
||||
const mapped = response.data.cases.map(mapCaseFromApi);
|
||||
const mapped = response.data.details.map(mapDetailFromApi);
|
||||
setCases(mapped);
|
||||
setActiveCaseId((prev) => {
|
||||
const stillExists = mapped.some((c) => c.clientId === prev);
|
||||
@@ -422,28 +426,57 @@ export function TreatmentWorkspace({ userId, currentOrganization }: TreatmentWor
|
||||
const handleSendCase = useCallback(
|
||||
async (treatmentCase: TreatmentCaseDraft) => {
|
||||
if (!canEditTreatmentForDay || !selectedAppointment) return;
|
||||
const targets = treatmentCase.sendToOrganizationIds.filter((id) =>
|
||||
const destinationOrgId = treatmentCase.sendToOrganizationIds.find((id) =>
|
||||
orgs.some((o) => o.id === id && o.active),
|
||||
);
|
||||
if (targets.length === 0) {
|
||||
if (!destinationOrgId) {
|
||||
showError(t('errorChooseOrg'));
|
||||
return;
|
||||
}
|
||||
setSendBusyId(treatmentCase.clientId);
|
||||
try {
|
||||
const saved = await persistDraft();
|
||||
const serverCase = saved.cases.find((c) => c.clientId === treatmentCase.clientId);
|
||||
if (!serverCase?.id) throw new Error(t('errorCaseMustSave'));
|
||||
const serverDetail = saved.details.find((c) => c.clientId === treatmentCase.clientId);
|
||||
if (!serverDetail?.id) throw new Error(t('errorCaseMustSave'));
|
||||
|
||||
const labCaseClientId = treatmentCase.labCaseId
|
||||
? saved.labCases.find((lc) => lc.id === treatmentCase.labCaseId)?.clientId
|
||||
: `lab-${treatmentCase.clientId}`;
|
||||
|
||||
const existingLabCase = saved.labCases.find(
|
||||
(lc) =>
|
||||
lc.treatmentDetailIds.includes(serverDetail.id) &&
|
||||
!lc.sentAt,
|
||||
);
|
||||
|
||||
const withLabCases = await treatmentsApi.saveLabCases(selectedAppointment.id, {
|
||||
labCases: [
|
||||
{
|
||||
clientId: existingLabCase?.clientId ?? labCaseClientId ?? `lab-${treatmentCase.clientId}`,
|
||||
id: existingLabCase?.id ?? treatmentCase.labCaseId ?? undefined,
|
||||
destinationOrganizationId: destinationOrgId,
|
||||
treatmentDetailIds: [serverDetail.id],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const labCase = withLabCases.data.labCases.find((lc) =>
|
||||
lc.treatmentDetailIds.includes(serverDetail.id),
|
||||
);
|
||||
if (!labCase?.id) throw new Error(t('errorSendCase'));
|
||||
|
||||
const response = await treatmentsApi.sendLabCase(labCase.id);
|
||||
|
||||
const response = await treatmentsApi.sendCase(serverCase.id, { organizationIds: targets });
|
||||
setCases((prev) => {
|
||||
const next = prev.map((c) =>
|
||||
c.clientId === treatmentCase.clientId
|
||||
? {
|
||||
...c,
|
||||
id: response.data.id,
|
||||
labCaseId: response.data.id,
|
||||
sentAt: response.data.sentAt,
|
||||
sendToOrganizationIds: response.data.sendToOrganizationIds,
|
||||
sendToOrganizationIds: response.data.destinationOrganizationId
|
||||
? [response.data.destinationOrganizationId]
|
||||
: [],
|
||||
sends: response.data.sends,
|
||||
}
|
||||
: c,
|
||||
@@ -452,7 +485,7 @@ export function TreatmentWorkspace({ userId, currentOrganization }: TreatmentWor
|
||||
return next;
|
||||
});
|
||||
setRecentOrganizationIds((prev) => {
|
||||
const next = [...targets.filter((id) => !prev.includes(id)), ...prev];
|
||||
const next = [destinationOrgId, ...prev.filter((id) => id !== destinationOrgId)];
|
||||
return next.slice(0, 10);
|
||||
});
|
||||
showSuccess(t('successCaseSent'));
|
||||
|
||||
Reference in New Issue
Block a user