Merge branch 'develop' into MED-97

This commit is contained in:
2025-09-26 17:01:24 +03:00
86 changed files with 11249 additions and 3151 deletions

View File

@@ -2,12 +2,19 @@
import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-account';
import { MontonioOrderHandlerService } from '@/packages/billing/montonio/src';
import { addToCart, deleteLineItem } from '@lib/data/cart';
import { addToCart, deleteLineItem, retrieveCart } from '@lib/data/cart';
import { getCartId } from '@lib/data/cookies';
import { StoreCartLineItem, StoreProductVariant } from '@medusajs/types';
import { isSameMinute } from 'date-fns';
import { z } from 'zod';
import { getSupabaseServerClient } from '@kit/supabase/server-client';
import {
cancelReservation,
getOrderedTtoServices,
} from '~/lib/services/reservation.service';
import { createCartEntriesLog } from './audit/cartEntries';
import { getAvailableAppointmentsForService } from './connected-online.service';
const env = () =>
z
@@ -35,53 +42,44 @@ export async function handleAddToCart({
selectedVariant: Pick<StoreProductVariant, 'id'>;
countryCode: string;
}) {
const supabase = getSupabaseServerClient();
const { account, user } = await loadCurrentUserAccount();
const { account } = await loadCurrentUserAccount();
if (!account) {
throw new Error('Account not found');
}
const quantity = 1;
const cart = await addToCart({
const { newCart, addedItem } = await addToCart({
variantId: selectedVariant.id,
quantity,
countryCode,
});
const { error } = await supabase.schema('audit').from('cart_entries').insert({
variant_id: selectedVariant.id,
await createCartEntriesLog({
variantId: selectedVariant.id,
operation: 'ADD_TO_CART',
account_id: account.id,
cart_id: cart.id,
changed_by: user.id,
accountId: account.id,
cartId: newCart.id,
});
if (error) {
throw new Error('Error logging cart entry: ' + error.message);
}
return cart;
return { cart: newCart, addedItem };
}
export async function handleDeleteCartItem({ lineId }: { lineId: string }) {
await deleteLineItem(lineId);
await cancelReservation(lineId);
const supabase = getSupabaseServerClient();
const cartId = await getCartId();
const { account, user } = await loadCurrentUserAccount();
const { account } = await loadCurrentUserAccount();
if (!account) {
throw new Error('Account not found');
}
const { error } = await supabase.schema('audit').from('cart_entries').insert({
variant_id: lineId,
await createCartEntriesLog({
variantId: lineId,
operation: 'REMOVE_FROM_CART',
account_id: account.id,
cart_id: cartId!,
changed_by: user.id,
accountId: account.id,
cartId: cartId!,
});
if (error) {
throw new Error('Error logging cart entry: ' + error.message);
}
}
export async function handleNavigateToPayment({
@@ -97,12 +95,43 @@ export async function handleNavigateToPayment({
currencyCode: string;
cartId: string;
}) {
const supabase = getSupabaseServerClient();
const { account, user } = await loadCurrentUserAccount();
const { account } = await loadCurrentUserAccount();
if (!account) {
throw new Error('Account not found');
}
const cart = await retrieveCart();
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({
notificationUrl: `${env().medusaBackendPublicUrl}/hooks/payment/montonio_montonio`,
@@ -114,17 +143,13 @@ export async function handleNavigateToPayment({
merchantReference: `${account.id}:${paymentSessionId}:${cartId}`,
});
const { error } = await supabase.schema('audit').from('cart_entries').insert({
await createCartEntriesLog({
operation: 'NAVIGATE_TO_PAYMENT',
account_id: account.id,
cart_id: cartId,
changed_by: user.id,
accountId: account.id,
cartId: cart.id,
});
if (error) {
throw new Error('Error logging cart entry: ' + error.message);
}
return paymentLink;
return { url: paymentLink };
}
export async function handleLineItemTimeout({
@@ -132,21 +157,16 @@ export async function handleLineItemTimeout({
}: {
lineItem: StoreCartLineItem;
}) {
const supabase = getSupabaseServerClient();
const { account, user } = await loadCurrentUserAccount();
const { account } = await loadCurrentUserAccount();
if (!account) {
throw new Error('Account not found');
}
await deleteLineItem(lineItem.id);
const { error } = await supabase.schema('audit').from('cart_entries').insert({
await createCartEntriesLog({
operation: 'LINE_ITEM_TIMEOUT',
account_id: account.id,
cart_id: lineItem.cart_id,
changed_by: user.id,
accountId: account.id,
cartId: lineItem.cart_id,
});
if (error) {
throw new Error('Error logging cart entry: ' + error.message);
}
}