feat(MED-97): update cart flow for using benefits
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-account';
|
||||
import { MontonioOrderHandlerService } from '@/packages/billing/montonio/src';
|
||||
import { addToCart, deleteLineItem, retrieveCart } from '@lib/data/cart';
|
||||
import { addToCart, deleteLineItem } from '@lib/data/cart';
|
||||
import { getCartId } from '@lib/data/cookies';
|
||||
import { StoreCartLineItem, StoreProductVariant } from '@medusajs/types';
|
||||
import { z } from 'zod';
|
||||
@@ -87,9 +87,15 @@ export async function handleDeleteCartItem({ lineId }: { lineId: string }) {
|
||||
export async function handleNavigateToPayment({
|
||||
language,
|
||||
paymentSessionId,
|
||||
amount,
|
||||
currencyCode,
|
||||
cartId,
|
||||
}: {
|
||||
language: string;
|
||||
paymentSessionId: string;
|
||||
amount: number;
|
||||
currencyCode: string;
|
||||
cartId: string;
|
||||
}) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
@@ -97,26 +103,21 @@ export async function handleNavigateToPayment({
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
|
||||
const cart = await retrieveCart();
|
||||
if (!cart) {
|
||||
throw new Error('No cart found');
|
||||
}
|
||||
|
||||
const paymentLink =
|
||||
await new MontonioOrderHandlerService().getMontonioPaymentLink({
|
||||
notificationUrl: `${env().medusaBackendPublicUrl}/hooks/payment/montonio_montonio`,
|
||||
returnUrl: `${env().siteUrl}/home/cart/montonio-callback`,
|
||||
amount: cart.total,
|
||||
currency: cart.currency_code.toUpperCase(),
|
||||
amount,
|
||||
currency: currencyCode.toUpperCase(),
|
||||
description: `Order from Medreport`,
|
||||
locale: language,
|
||||
merchantReference: `${account.id}:${paymentSessionId}:${cart.id}`,
|
||||
merchantReference: `${account.id}:${paymentSessionId}:${cartId}`,
|
||||
});
|
||||
|
||||
const { error } = await supabase.schema('audit').from('cart_entries').insert({
|
||||
operation: 'NAVIGATE_TO_PAYMENT',
|
||||
account_id: account.id,
|
||||
cart_id: cart.id,
|
||||
cart_id: cartId,
|
||||
changed_by: user.id,
|
||||
});
|
||||
if (error) {
|
||||
|
||||
@@ -51,48 +51,6 @@ export async function createAnalysisOrder({
|
||||
return orderResult.data.id;
|
||||
}
|
||||
|
||||
export async function updateAnalysisOrder({
|
||||
orderId,
|
||||
orderStatus,
|
||||
}: {
|
||||
orderId: number;
|
||||
orderStatus: Tables<{ schema: 'medreport' }, 'analysis_orders'>['status'];
|
||||
}) {
|
||||
console.info(`Updating order id=${orderId} status=${orderStatus}`);
|
||||
await getSupabaseServerAdminClient()
|
||||
.schema('medreport')
|
||||
.from('analysis_orders')
|
||||
.update({
|
||||
status: orderStatus,
|
||||
})
|
||||
.eq('id', orderId)
|
||||
.throwOnError();
|
||||
}
|
||||
|
||||
export async function updateAnalysisOrderStatus({
|
||||
orderId,
|
||||
medusaOrderId,
|
||||
orderStatus,
|
||||
}: {
|
||||
orderId?: number;
|
||||
medusaOrderId?: string;
|
||||
orderStatus: Tables<{ schema: 'medreport' }, 'analysis_orders'>['status'];
|
||||
}) {
|
||||
const orderIdParam = orderId;
|
||||
const medusaOrderIdParam = medusaOrderId;
|
||||
if (!orderIdParam && !medusaOrderIdParam) {
|
||||
throw new Error('Either orderId or medusaOrderId must be provided');
|
||||
}
|
||||
await getSupabaseServerAdminClient()
|
||||
.schema('medreport')
|
||||
.rpc('update_analysis_order_status', {
|
||||
order_id: orderIdParam ?? -1,
|
||||
status_param: orderStatus,
|
||||
medusa_order_id_param: medusaOrderIdParam ?? '',
|
||||
})
|
||||
.throwOnError();
|
||||
}
|
||||
|
||||
export async function getAnalysisOrder({
|
||||
medusaOrderId,
|
||||
analysisOrderId,
|
||||
|
||||
Reference in New Issue
Block a user