MED-177: add booking confirmation
This commit is contained in:
@@ -9,8 +9,13 @@ import type { StoreCart, StoreOrder } from '@medusajs/types';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
|
||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||
import { bookAppointment } from '~/lib/services/connected-online.service';
|
||||
import {
|
||||
bookAppointment,
|
||||
getConfirmedService,
|
||||
} from '~/lib/services/connected-online.service';
|
||||
import { sendOrderToMedipost } from '~/lib/services/medipost/medipostPrivateMessage.service';
|
||||
import { handleNavigateToPayment } from '~/lib/services/medusaCart.service';
|
||||
import { getOrderedAnalysisIds } from '~/lib/services/medusaOrder.service';
|
||||
@@ -141,6 +146,7 @@ export const initiatePayment = async ({
|
||||
};
|
||||
|
||||
export async function handlePlaceOrder({ cart }: { cart: StoreCart }) {
|
||||
const logger = await getLogger();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found in context');
|
||||
@@ -160,7 +166,7 @@ export async function handlePlaceOrder({ cart }: { cart: StoreCart }) {
|
||||
const existingAnalysisOrder = await getAnalysisOrder({
|
||||
medusaOrderId: medusaOrder.id,
|
||||
});
|
||||
console.info(
|
||||
logger.info(
|
||||
`Analysis order already exists for medusaOrderId=${medusaOrder.id}, orderId=${existingAnalysisOrder.id}`,
|
||||
);
|
||||
return { success: true, orderId: existingAnalysisOrder.id };
|
||||
@@ -185,7 +191,8 @@ export async function handlePlaceOrder({ cart }: { cart: StoreCart }) {
|
||||
let bookServiceResults: {
|
||||
success: boolean;
|
||||
reason?: FailureReason;
|
||||
serviceId?: number;
|
||||
bookingCode: string | null;
|
||||
clinicKey: string | null;
|
||||
}[] = [];
|
||||
if (orderedTtoServices?.length) {
|
||||
const bookingPromises = orderedTtoServices.map((service) =>
|
||||
@@ -199,7 +206,6 @@ export async function handlePlaceOrder({ cart }: { cart: StoreCart }) {
|
||||
);
|
||||
bookServiceResults = await Promise.all(bookingPromises);
|
||||
}
|
||||
// TODO: SEND EMAIL
|
||||
|
||||
if (email) {
|
||||
if (analysisPackageOrder) {
|
||||
@@ -209,22 +215,35 @@ export async function handlePlaceOrder({ cart }: { cart: StoreCart }) {
|
||||
analysisPackageOrder,
|
||||
});
|
||||
} else {
|
||||
console.info(`Order has no analysis package, skipping email.`);
|
||||
logger.info(`Order has no analysis package, skipping email.`);
|
||||
}
|
||||
|
||||
if (analysisItemsOrder) {
|
||||
// @TODO send email for separate analyses
|
||||
console.warn(
|
||||
logger.warn(
|
||||
`Order has analysis items, but no email template exists yet`,
|
||||
);
|
||||
} else {
|
||||
console.info(`Order has no analysis items, skipping email.`);
|
||||
logger.info(`Order has no analysis items, skipping email.`);
|
||||
}
|
||||
} else {
|
||||
console.error('Missing email to send order result email', orderResult);
|
||||
logger.error('Missing email to send order result email', orderResult);
|
||||
}
|
||||
|
||||
if (env().isEnabledDispatchOnMontonioCallback) {
|
||||
if (bookServiceResults?.length) {
|
||||
bookServiceResults.forEach(async ({ bookingCode, clinicKey }) => {
|
||||
if (!bookingCode || !clinicKey) {
|
||||
logger.info('A booking is missing either bookingCode or clinicKey', {
|
||||
bookingCode,
|
||||
clinicKey,
|
||||
});
|
||||
return;
|
||||
}
|
||||
await getConfirmedService(bookingCode, clinicKey);
|
||||
});
|
||||
}
|
||||
|
||||
if (env().isEnabledDispatchOnMontonioCallback && orderContainsSynlabItems) {
|
||||
await sendOrderToMedipost({ medusaOrderId, orderedAnalysisElements });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user