feat(MED-168): keep medipost response data unique, no need for duplicate rows

This commit is contained in:
2025-09-18 16:41:05 +03:00
parent a243bd4769
commit d99016aa5e
5 changed files with 75 additions and 27 deletions

View File

@@ -18,16 +18,32 @@ export async function getExistingAnalysisResponseElements({
return data as AnalysisResponseElement[];
}
export async function createAnalysisResponseElement({
export async function upsertAnalysisResponseElement({
element,
}: {
element: Omit<AnalysisResponseElement, 'created_at' | 'updated_at' | 'id'>;
}) {
await getSupabaseServerAdminClient()
const { data } = await getSupabaseServerAdminClient()
.schema('medreport')
.from('analysis_response_elements')
.insert(element)
.upsert(
element,
{
onConflict: 'analysis_response_id,analysis_element_original_id',
ignoreDuplicates: false
}
)
.select('id')
.throwOnError();
const analysisResponseElementId = data?.[0]?.id;
if (!analysisResponseElementId) {
throw new Error(
`Failed to insert or update analysis response element (response id: ${element.analysis_response_id}, element id: ${element.analysis_element_original_id})`
);
}
return { analysisResponseElementId };
}
export async function upsertAnalysisResponse({