feat(MED-85): create logs of sending order to medipost success/error
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
'use server'
|
||||
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
|
||||
import { RequestStatus } from '@/lib/types/audit';
|
||||
import { ConnectedOnlineMethodName } from '@/lib/types/connected-online';
|
||||
import { ExternalApi } from '@/lib/types/external';
|
||||
import { MedipostAction } from '@/lib/types/medipost';
|
||||
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||
|
||||
export default async function logRequestResult(
|
||||
/* personalCode: string, */ requestApi: keyof typeof ExternalApi,
|
||||
@@ -16,19 +15,7 @@ export default async function logRequestResult(
|
||||
serviceId?: number,
|
||||
serviceProviderId?: number,
|
||||
) {
|
||||
const supabaseServiceUser = createClient(
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
||||
process.env.SUPABASE_SERVICE_ROLE_KEY!,
|
||||
{
|
||||
auth: {
|
||||
persistSession: false,
|
||||
autoRefreshToken: false,
|
||||
detectSessionInUrl: false,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const { error } = await supabaseServiceUser
|
||||
const { error } = await getSupabaseServerClient()
|
||||
.schema('audit')
|
||||
.from('request_entries')
|
||||
.insert({
|
||||
@@ -46,3 +33,29 @@ export default async function logRequestResult(
|
||||
throw new Error('Failed to insert log entry, error: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
export async function logMedipostDispatch({
|
||||
medusaOrderId,
|
||||
isSuccess,
|
||||
isMedipostError,
|
||||
errorMessage,
|
||||
}: {
|
||||
medusaOrderId: string;
|
||||
isSuccess: boolean;
|
||||
isMedipostError: boolean;
|
||||
errorMessage?: string;
|
||||
}) {
|
||||
const { error } = await getSupabaseServerClient()
|
||||
.schema('audit')
|
||||
.from('medipost_dispatch')
|
||||
.insert({
|
||||
medusa_order_id: medusaOrderId,
|
||||
is_success: isSuccess,
|
||||
is_medipost_error: isMedipostError,
|
||||
error_message: errorMessage,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
throw new Error('Failed to insert log entry, error: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,8 @@ import { StoreOrder } from '@medusajs/types';
|
||||
import { listProducts } from '@lib/data/products';
|
||||
import { listRegions } from '@lib/data/regions';
|
||||
import { getAnalysisElementMedusaProductIds } from '@/utils/medusa-product';
|
||||
import { MedipostValidationError } from './medipost/MedipostValidationError';
|
||||
import { logMedipostDispatch } from './audit.service';
|
||||
|
||||
const BASE_URL = process.env.MEDIPOST_URL!;
|
||||
const USER = process.env.MEDIPOST_USER!;
|
||||
@@ -63,14 +65,14 @@ export async function validateMedipostResponse(response: string, { canHaveEmptyC
|
||||
if (canHaveEmptyCode) {
|
||||
if (code && code !== 0) {
|
||||
console.error("Bad response", response);
|
||||
throw new Error(`Medipost response is invalid`);
|
||||
throw new MedipostValidationError(response);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof code !== 'number' || (code !== 0 && !canHaveEmptyCode)) {
|
||||
console.error("Bad response", response);
|
||||
throw new Error(`Medipost response is invalid`);
|
||||
throw new MedipostValidationError(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -704,7 +706,22 @@ export async function sendOrderToMedipost({
|
||||
comment: '',
|
||||
});
|
||||
|
||||
try {
|
||||
await sendPrivateMessage(orderXml);
|
||||
} catch (e) {
|
||||
await logMedipostDispatch({
|
||||
medusaOrderId,
|
||||
isSuccess: false,
|
||||
isMedipostError: e instanceof MedipostValidationError,
|
||||
errorMessage: e instanceof MedipostValidationError ? e.response : undefined,
|
||||
});
|
||||
throw e;
|
||||
}
|
||||
await logMedipostDispatch({
|
||||
medusaOrderId,
|
||||
isSuccess: true,
|
||||
isMedipostError: false,
|
||||
});
|
||||
await updateOrderStatus({ medusaOrderId, orderStatus: 'PROCESSING' });
|
||||
}
|
||||
|
||||
|
||||
9
lib/services/medipost/MedipostValidationError.ts
Normal file
9
lib/services/medipost/MedipostValidationError.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export class MedipostValidationError extends Error {
|
||||
response: string;
|
||||
|
||||
constructor(response: string) {
|
||||
super('Invalid Medipost response');
|
||||
this.name = 'MedipostValidationError';
|
||||
this.response = response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user