MED-177: add booking confirmation

This commit is contained in:
Danel Kungla
2025-10-01 18:55:53 +03:00
parent 0c5d38f274
commit f73bbf54ad
13 changed files with 215 additions and 2052 deletions

View File

@@ -5,7 +5,10 @@ import { StoreProductVariant } from '@medusajs/types';
import logRequestResult from '~/lib/services/audit.service';
import { handleAddToCart } from '~/lib/services/medusaCart.service';
import { createInitialReservation } from '~/lib/services/reservation.service';
import {
cancelReservation,
createInitialReservation,
} from '~/lib/services/reservation.service';
import { RequestStatus } from '~/lib/types/audit';
import { ConnectedOnlineMethodName } from '~/lib/types/connected-online';
import { ExternalApi } from '~/lib/types/external';
@@ -46,11 +49,16 @@ export async function createInitialReservationAction(
}
}
export async function cancelTtoBooking(bookingCode: string, clinicId: number) {
export async function cancelTtoBooking(
bookingCode: string,
clinicId: number,
medusaLineItemId: string,
) {
try {
const response = await fetch(
await fetch(
`${process.env.CONNECTED_ONLINE_URL}/${ConnectedOnlineMethodName.ConfirmedCancel}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
@@ -60,7 +68,14 @@ export async function cancelTtoBooking(bookingCode: string, clinicId: number) {
},
);
return null;
await cancelReservation(medusaLineItemId);
await logRequestResult(
ExternalApi.ConnectedOnline,
ConnectedOnlineMethodName.ConfirmedCancel,
RequestStatus.Success,
medusaLineItemId,
);
} catch (error) {
await logRequestResult(
ExternalApi.ConnectedOnline,

View File

@@ -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 });
}