'use server'; 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 { StoreCartLineItem, StoreProductVariant } from '@medusajs/types'; import { MontonioOrderHandlerService } from '@/packages/billing/montonio/src'; import { headers } from 'next/headers'; import { requireUserInServerComponent } from '../server/require-user-in-server-component'; export async function handleAddToCart({ selectedVariant, countryCode, }: { selectedVariant: StoreProductVariant countryCode: string }) { const supabase = getSupabaseServerClient(); const user = await requireUserInServerComponent(); const account = await loadCurrentUserAccount() if (!account) { throw new Error('Account not found'); } const quantity = 1; const cart = await addToCart({ variantId: selectedVariant.id, quantity, countryCode, }); const { error } = await supabase .schema('audit') .from('cart_entries') .insert({ variant_id: selectedVariant.id, operation: 'ADD_TO_CART', account_id: account.id, cart_id: cart.id, changed_by: user.id, }); if (error) { throw new Error('Error logging cart entry: ' + error.message); } return cart; } export async function handleNavigateToPayment({ language }: { language: string }) { const supabase = getSupabaseServerClient(); const user = await requireUserInServerComponent(); 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 headersList = await headers(); const host = "webhook.site:3000"; const proto = "http"; // const host = headersList.get('host'); // const proto = headersList.get('x-forwarded-proto') ?? 'http'; const publicUrl = `${proto}://${host}`; const paymentLink = await new MontonioOrderHandlerService().getMontonioPaymentLink({ notificationUrl: `${publicUrl}/api/billing/webhook`, returnUrl: `${publicUrl}/home/cart/montonio-callback`, amount: cart.total, currency: cart.currency_code.toUpperCase(), description: `Order from Medreport`, locale: language, merchantReference: `${account.id}:${Date.now()}`, }); 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, }); if (error) { throw new Error('Error logging cart entry: ' + error.message); } return paymentLink; } export async function handleLineItemTimeout({ lineItem, }: { lineItem: StoreCartLineItem }) { const supabase = getSupabaseServerClient(); const user = await requireUserInServerComponent(); const account = await loadCurrentUserAccount() if (!account) { throw new Error('Account not found'); } if (lineItem.updated_at) { const updatedAt = new Date(lineItem.updated_at); const now = new Date(); const diff = now.getTime() - updatedAt.getTime(); } await deleteLineItem(lineItem.id); const { error } = await supabase .schema('audit') .from('cart_entries') .insert({ operation: 'LINE_ITEM_TIMEOUT', account_id: account.id, cart_id: lineItem.cart_id, changed_by: user.id, }); if (error) { throw new Error('Error logging cart entry: ' + error.message); } }