add cart functionality for tto services

This commit is contained in:
Helena
2025-09-19 16:23:19 +03:00
parent 3c272505d6
commit b59148630a
26 changed files with 921 additions and 221 deletions

View File

@@ -6,8 +6,23 @@ import { addToCart, deleteLineItem, retrieveCart } from '@lib/data/cart';
import { getCartId } from '@lib/data/cookies';
import { StoreCartLineItem, StoreProductVariant } from '@medusajs/types';
import { z } from 'zod';
import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { cancelReservation } from './connected-online.service';
import { cancelReservation, getOrderedTtoServices } from '~/lib/services/reservation.service';
import { isSameMinute } from 'date-fns';
import { getAvailableAppointmentsForService } from './connected-online.service';
const env = () =>
z
@@ -102,6 +117,30 @@ export async function handleNavigateToPayment({
if (!cart) {
throw new Error('No cart found');
}
const orderedTtoServices = await getOrderedTtoServices({ cart });
if (orderedTtoServices?.length) {
const unavailableLineItemIds: string[] = []
for (const ttoService of orderedTtoServices) {
const availabilities = await getAvailableAppointmentsForService(
ttoService.service_id,
ttoService.provider.key,
ttoService.location_sync_id,
new Date(ttoService.start_time),
1,
);
const isAvailable = availabilities?.T_Booking?.length ? availabilities.T_Booking.find((timeSlot) => isSameMinute(ttoService.start_time, timeSlot.StartTime)) : false
if (!isAvailable) {
unavailableLineItemIds.push(ttoService.medusa_cart_line_item_id!)
}
}
if (unavailableLineItemIds.length) {
return { unavailableLineItemIds }
}
}
const paymentLink =
await new MontonioOrderHandlerService().getMontonioPaymentLink({
@@ -120,11 +159,12 @@ export async function handleNavigateToPayment({
cart_id: cart.id,
changed_by: user.id,
});
if (error) {
throw new Error('Error logging cart entry: ' + error.message);
}
return paymentLink;
return { url: paymentLink };
}
export async function handleLineItemTimeout({
@@ -149,4 +189,4 @@ export async function handleLineItemTimeout({
if (error) {
throw new Error('Error logging cart entry: ' + error.message);
}
}
}