Move medipostPrivateMessage.service to separate classes, improve logging
This commit is contained in:
@@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user