feat(MED-101): add audit log to cart item delete

This commit is contained in:
Karli
2025-08-18 13:27:13 +03:00
parent 43117985dd
commit ec866c7f29
2 changed files with 33 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import { z } from 'zod';
import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-account';
import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { addToCart, deleteLineItem, retrieveCart } from '@lib/data/cart';
import { getCartId } from '@lib/data/cookies';
import { StoreCartLineItem, StoreProductVariant } from '@medusajs/types';
import { MontonioOrderHandlerService } from '@/packages/billing/montonio/src';
import { requireUserInServerComponent } from '../server/require-user-in-server-component';
@@ -64,6 +65,36 @@ export async function handleAddToCart({
return cart;
}
export async function handleDeleteCartItem({
lineId,
}: {
lineId: string;
}) {
await deleteLineItem(lineId);
const supabase = getSupabaseServerClient();
const cartId = await getCartId();
const user = await requireUserInServerComponent();
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,
operation: 'REMOVE_FROM_CART',
account_id: account.id,
cart_id: cartId!,
changed_by: user.id,
});
if (error) {
throw new Error('Error logging cart entry: ' + error.message);
}
}
export async function handleNavigateToPayment({ language, paymentSessionId }: { language: string, paymentSessionId: string }) {
const supabase = getSupabaseServerClient();
const user = await requireUserInServerComponent();