Move medipostPrivateMessage.service to separate classes, improve logging

This commit is contained in:
2025-11-12 08:51:48 +02:00
parent 0878b5d1bd
commit 8f32fdf08d
12 changed files with 670 additions and 390 deletions

View File

@@ -29,6 +29,19 @@ export async function getLatestMessage({
);
}
export async function getMedipostActionLog({
medipostPrivateMessageId,
}: {
medipostPrivateMessageId: string;
}) {
const { data: existingRecord } = await getSupabaseServerAdminClient()
.schema('medreport').from('medipost_actions')
.select('id')
.eq('medipost_private_message_id', medipostPrivateMessageId)
.single();
return existingRecord;
}
export async function upsertMedipostActionLog({
action,
xml,
@@ -51,6 +64,10 @@ export async function upsertMedipostActionLog({
medipostExternalOrderId?: string | null;
medipostPrivateMessageId?: string | null;
}) {
if (typeof medipostPrivateMessageId !== 'string') {
throw new Error('medipostPrivateMessageId is required');
}
const recordData = {
action,
xml,
@@ -62,18 +79,19 @@ export async function upsertMedipostActionLog({
medipost_private_message_id: medipostPrivateMessageId,
};
const query = getSupabaseServerAdminClient()
const existingActionLog = await getMedipostActionLog({ medipostPrivateMessageId });
if (existingActionLog) {
console.info(`Medipost action log already exists for private message id: ${medipostPrivateMessageId}`);
return { medipostActionId: existingActionLog.id };
}
console.info(`Inserting medipost action log for private message id: ${medipostPrivateMessageId}`);
const { data } = await getSupabaseServerAdminClient()
.schema('medreport')
.from('medipost_actions');
const { data } = medipostPrivateMessageId
? await query
.upsert(recordData, {
onConflict: 'medipost_private_message_id',
ignoreDuplicates: false,
})
.select('id')
.throwOnError()
: await query.insert(recordData).select('id').throwOnError();
.from('medipost_actions')
.insert(recordData)
.select('id')
.throwOnError();
const medipostActionId = data?.[0]?.id;
if (!medipostActionId) {
@@ -84,3 +102,46 @@ export async function upsertMedipostActionLog({
return { medipostActionId };
}
export async function createMedipostActionLogForError({
privateMessageXml,
medipostPrivateMessageId,
medusaOrderId,
medipostExternalOrderId,
}: {
privateMessageXml: string;
medipostPrivateMessageId: string;
medusaOrderId?: string;
medipostExternalOrderId: string;
}) {
await upsertMedipostActionLog({
action: 'sync_analysis_results_from_medipost',
xml: privateMessageXml,
hasAnalysisResults: false,
medipostPrivateMessageId,
medusaOrderId,
medipostExternalOrderId,
hasError: true,
});
}
export async function createMedipostActionLogForSuccess({
privateMessageXml,
medipostPrivateMessageId,
medusaOrderId,
medipostExternalOrderId,
}: {
privateMessageXml: string;
medipostPrivateMessageId: string;
medusaOrderId: string;
medipostExternalOrderId: string;
}) {
await upsertMedipostActionLog({
action: 'sync_analysis_results_from_medipost',
xml: privateMessageXml,
hasAnalysisResults: true,
medipostPrivateMessageId: medipostPrivateMessageId,
medusaOrderId,
medipostExternalOrderId,
});
}