feat(MED-131): remove private messages with unknown order ID from queue

This commit is contained in:
2025-08-04 16:29:50 +03:00
parent 0a4be89fc9
commit 84b629ab0b

View File

@@ -221,10 +221,19 @@ export async function readPrivateMessageResponse({
const messageResponse = privateMessageContent?.Saadetis?.Vastus; const messageResponse = privateMessageContent?.Saadetis?.Vastus;
if (!messageResponse) { if (!messageResponse) {
throw new Error(`Invalid data in private message response`); throw new Error(`Private message response has no results yet`);
}
console.info(`Private message content: ${JSON.stringify(privateMessageContent)}`);
let order: Tables<{ schema: 'medreport' }, 'analysis_orders'>;
try {
order = await getOrder({ medusaOrderId: messageResponse.ValisTellimuseId });
} catch (e) {
await deletePrivateMessage(privateMessage.messageId);
throw new Error(`Order not found by Medipost message ValisTellimuseId=${messageResponse.ValisTellimuseId}`);
} }
const status = await syncPrivateMessage({ messageResponse }); const status = await syncPrivateMessage({ messageResponse, order });
if (status === 'COMPLETED') { if (status === 'COMPLETED') {
await deletePrivateMessage(privateMessage.messageId); await deletePrivateMessage(privateMessage.messageId);
@@ -552,14 +561,13 @@ function getLatestMessage({
export async function syncPrivateMessage({ export async function syncPrivateMessage({
messageResponse, messageResponse,
order,
}: { }: {
messageResponse: MedipostOrderResponse['Saadetis']['Vastus']; messageResponse: MedipostOrderResponse['Saadetis']['Vastus'];
order: Tables<{ schema: 'medreport' }, 'analysis_orders'>;
}) { }) {
const supabase = getSupabaseServerAdminClient() const supabase = getSupabaseServerAdminClient()
const status = messageResponse.TellimuseOlek;
const order = await getOrder({ medusaOrderId: messageResponse.ValisTellimuseId });
const { data: analysisOrder, error: analysisOrderError } = await supabase const { data: analysisOrder, error: analysisOrderError } = await supabase
.schema('medreport') .schema('medreport')
.from('analysis_orders') .from('analysis_orders')
@@ -579,7 +587,7 @@ export async function syncPrivateMessage({
{ {
analysis_order_id: order.id, analysis_order_id: order.id,
order_number: messageResponse.TellimuseNumber, order_number: messageResponse.TellimuseNumber,
order_status: AnalysisOrderStatus[status], order_status: AnalysisOrderStatus[messageResponse.TellimuseOlek],
user_id: analysisOrder[0].user_id, user_id: analysisOrder[0].user_id,
}, },
{ onConflict: 'order_number', ignoreDuplicates: false }, { onConflict: 'order_number', ignoreDuplicates: false },
@@ -651,7 +659,7 @@ export async function syncPrivateMessage({
); );
} }
return AnalysisOrderStatus[status]; return AnalysisOrderStatus[messageResponse.TellimuseOlek];
} }
export async function sendOrderToMedipost({ export async function sendOrderToMedipost({