translations, remove random empty lines, refactor
This commit is contained in:
40
lib/services/audit/cartEntries.ts
Normal file
40
lib/services/audit/cartEntries.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
export const createCartEntriesLog = async ({
|
||||
operation,
|
||||
accountId,
|
||||
cartId,
|
||||
variantId,
|
||||
comment,
|
||||
}: {
|
||||
operation: string;
|
||||
accountId: string;
|
||||
cartId: string;
|
||||
variantId?: string;
|
||||
comment?: string;
|
||||
}) => {
|
||||
try {
|
||||
const supabase = getSupabaseServerClient();
|
||||
|
||||
const {
|
||||
data: { user },
|
||||
error: userError,
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
if (userError || !user) {
|
||||
console.error('No authenticated user found; skipping audit insert');
|
||||
return;
|
||||
}
|
||||
|
||||
return supabase.schema('audit').from('cart_entries').insert({
|
||||
operation,
|
||||
account_id: accountId,
|
||||
cart_id: cartId,
|
||||
changed_by: user.id,
|
||||
variant_id: variantId,
|
||||
comment,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to insert doctor page view log', error);
|
||||
}
|
||||
};
|
||||
@@ -5,25 +5,13 @@ import { MontonioOrderHandlerService } from '@/packages/billing/montonio/src';
|
||||
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, getOrderedTtoServices } from '~/lib/services/reservation.service';
|
||||
|
||||
|
||||
|
||||
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
|
||||
.object({
|
||||
@@ -50,8 +38,7 @@ 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');
|
||||
}
|
||||
@@ -63,16 +50,12 @@ export async function handleAddToCart({
|
||||
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: newCart.id,
|
||||
changed_by: user.id,
|
||||
accountId: account.id,
|
||||
cartId: newCart.id,
|
||||
});
|
||||
if (error) {
|
||||
throw new Error('Error logging cart entry: ' + error.message);
|
||||
}
|
||||
|
||||
return { cart: newCart, addedItem };
|
||||
}
|
||||
@@ -81,23 +64,18 @@ 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({
|
||||
@@ -107,8 +85,7 @@ export async function handleNavigateToPayment({
|
||||
language: string;
|
||||
paymentSessionId: string;
|
||||
}) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
@@ -117,30 +94,33 @@ export async function handleNavigateToPayment({
|
||||
if (!cart) {
|
||||
throw new Error('No cart found');
|
||||
}
|
||||
const orderedTtoServices = await getOrderedTtoServices({ cart });
|
||||
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 (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 (unavailableLineItemIds.length) {
|
||||
return { unavailableLineItemIds }
|
||||
if (!isAvailable) {
|
||||
unavailableLineItemIds.push(ttoService.medusa_cart_line_item_id!);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (unavailableLineItemIds.length) {
|
||||
return { unavailableLineItemIds };
|
||||
}
|
||||
}
|
||||
|
||||
const paymentLink =
|
||||
await new MontonioOrderHandlerService().getMontonioPaymentLink({
|
||||
@@ -152,17 +132,12 @@ export async function handleNavigateToPayment({
|
||||
locale: language,
|
||||
merchantReference: `${account.id}:${paymentSessionId}:${cart.id}`,
|
||||
});
|
||||
|
||||
const { error } = await supabase.schema('audit').from('cart_entries').insert({
|
||||
operation: 'NAVIGATE_TO_PAYMENT',
|
||||
account_id: account.id,
|
||||
cart_id: cart.id,
|
||||
changed_by: user.id,
|
||||
|
||||
await createCartEntriesLog({
|
||||
operation: 'NAVIGATE_TO_PAYMENT',
|
||||
accountId: account.id,
|
||||
cartId: cart.id,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
throw new Error('Error logging cart entry: ' + error.message);
|
||||
}
|
||||
|
||||
return { url: paymentLink };
|
||||
}
|
||||
@@ -172,21 +147,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);
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import { EnrichedCartItem } from '../../app/home/(user)/_components/cart/types';
|
||||
import { loadCurrentUserAccount } from '../../app/home/(user)/_lib/server/load-user-account';
|
||||
import { createCartEntriesLog } from './audit/cartEntries';
|
||||
import { handleDeleteCartItem } from './medusaCart.service';
|
||||
|
||||
type Locations = Tables<{ schema: 'medreport' }, 'connected_online_locations'>;
|
||||
@@ -305,29 +306,21 @@ export async function updateReservationTime(
|
||||
.throwOnError();
|
||||
|
||||
logger.info(`Successfully updated reservation ${reservationData}`);
|
||||
await supabase
|
||||
.schema('audit')
|
||||
.from('cart_entries')
|
||||
.insert({
|
||||
operation: 'CHANGE_RESERVATION',
|
||||
account_id: account.id,
|
||||
cart_id: cartId,
|
||||
changed_by: user.id,
|
||||
comment: `${reservationData}`,
|
||||
});
|
||||
await createCartEntriesLog({
|
||||
operation: 'CHANGE_RESERVATION',
|
||||
accountId: account.id,
|
||||
cartId: cartId,
|
||||
comment: `${reservationData}`,
|
||||
});
|
||||
revalidatePath('/home/cart', 'layout');
|
||||
} catch (e) {
|
||||
logger.error(`Failed to update reservation ${reservationData}`);
|
||||
await supabase
|
||||
.schema('audit')
|
||||
.from('cart_entries')
|
||||
.insert({
|
||||
operation: 'CHANGE_RESERVATION',
|
||||
account_id: account.id,
|
||||
cart_id: cartId,
|
||||
changed_by: user.id,
|
||||
comment: `${e}`,
|
||||
});
|
||||
await createCartEntriesLog({
|
||||
operation: 'CHANGE_RESERVATION',
|
||||
accountId: account.id,
|
||||
cartId: cartId,
|
||||
comment: `${e}`,
|
||||
});
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user