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

@@ -27,7 +27,7 @@ export async function getLatestMessage({
);
}
export async function createMedipostActionLog({
export async function upsertMedipostActionLog({
action,
xml,
hasAnalysisResults = false,
@@ -40,8 +40,7 @@ export async function createMedipostActionLog({
action:
| 'send_order_to_medipost'
| 'sync_analysis_results_from_medipost'
| 'send_fake_analysis_results_to_medipost'
| 'send_analysis_results_to_medipost';
| 'send_fake_analysis_results_to_medipost';
xml: string;
hasAnalysisResults?: boolean;
medusaOrderId?: string | null;
@@ -50,19 +49,34 @@ export async function createMedipostActionLog({
medipostExternalOrderId?: string | null;
medipostPrivateMessageId?: string | null;
}) {
await getSupabaseServerAdminClient()
const { data } = await getSupabaseServerAdminClient()
.schema('medreport')
.from('medipost_actions')
.insert({
action,
xml,
has_analysis_results: hasAnalysisResults,
medusa_order_id: medusaOrderId,
response_xml: responseXml,
has_error: hasError,
medipost_external_order_id: medipostExternalOrderId,
medipost_private_message_id: medipostPrivateMessageId,
})
.upsert(
{
action,
xml,
has_analysis_results: hasAnalysisResults,
medusa_order_id: medusaOrderId,
response_xml: responseXml,
has_error: hasError,
medipost_external_order_id: medipostExternalOrderId,
medipost_private_message_id: medipostPrivateMessageId,
},
{
onConflict: 'medipost_private_message_id',
ignoreDuplicates: false
}
)
.select('id')
.throwOnError();
const medipostActionId = data?.[0]?.id;
if (!medipostActionId) {
throw new Error(
`Failed to insert or update medipost action (private message id: ${medipostPrivateMessageId})`
);
}
return { medipostActionId };
}