MED-177: add booking confirmation
This commit is contained in:
@@ -69,11 +69,12 @@ export default function OrderBlock({
|
||||
title="orders:table.ttoService"
|
||||
type="ttoService"
|
||||
order={{
|
||||
status: medusaOrderStatus.toUpperCase(),
|
||||
status: ttoReservation?.status,
|
||||
medusaOrderId,
|
||||
location: ttoLocation?.name,
|
||||
bookingCode: ttoReservation?.booking_code,
|
||||
clinicId: ttoReservation?.clinic_id,
|
||||
medusaLineItemId: ttoReservation?.medusa_cart_line_item_id,
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { useState } from 'react';
|
||||
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { redirect, useRouter } from 'next/navigation';
|
||||
|
||||
import ConfirmationModal from '@/packages/shared/src/components/confirmation-modal';
|
||||
import { StoreOrderLineItem } from '@medusajs/types';
|
||||
@@ -40,6 +40,11 @@ export default function OrderItemsTable({
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const [isConfirmOpen, setIsConfirmOpen] = useState(false);
|
||||
const isCancelOrderAllowed =
|
||||
order?.bookingCode &&
|
||||
order?.clinicId &&
|
||||
order?.medusaLineItemId &&
|
||||
order?.status === 'CONFIRMED';
|
||||
|
||||
if (!items || items.length === 0) {
|
||||
return null;
|
||||
@@ -109,7 +114,7 @@ export default function OrderItemsTable({
|
||||
<Button size="sm" onClick={openDetailedView}>
|
||||
<Trans i18nKey="analysis-results:view" />
|
||||
</Button>
|
||||
{isTtoservice && order.bookingCode && (
|
||||
{isCancelOrderAllowed && (
|
||||
<Button
|
||||
size="sm"
|
||||
className="bg-warning/90 hover:bg-warning mt-2 w-full"
|
||||
@@ -122,12 +127,18 @@ export default function OrderItemsTable({
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
{order?.bookingCode && order?.clinicId && (
|
||||
{isCancelOrderAllowed && (
|
||||
<ConfirmationModal
|
||||
isOpen={isConfirmOpen}
|
||||
onClose={() => setIsConfirmOpen(false)}
|
||||
onConfirm={async () => {
|
||||
await cancelTtoBooking(order.bookingCode!, order.clinicId!);
|
||||
cancelTtoBooking(
|
||||
order.bookingCode!,
|
||||
order.clinicId!,
|
||||
order.medusaLineItemId!,
|
||||
).then(() => {
|
||||
redirect(pathsConfig.app.myOrders);
|
||||
});
|
||||
}}
|
||||
titleKey="orders:confirmBookingCancel.title"
|
||||
descriptionKey="orders:confirmBookingCancel.description"
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
|
||||
@@ -65,18 +65,17 @@ async function loadAccountMembers(
|
||||
|
||||
const members = data ?? [];
|
||||
|
||||
return members
|
||||
.sort((prev, next) => {
|
||||
if (prev.primary_owner_user_id === prev.user_id) {
|
||||
return -1;
|
||||
}
|
||||
return members.sort((prev, next) => {
|
||||
if (prev.primary_owner_user_id === prev.user_id) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (prev.role_hierarchy_level < next.role_hierarchy_level) {
|
||||
return -1;
|
||||
}
|
||||
if (prev.role_hierarchy_level < next.role_hierarchy_level) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
});
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
|
||||
export async function loadAccountMembersBenefitsUsage(
|
||||
|
||||
Reference in New Issue
Block a user