feat(MED-131): revert to use server action for montonio token for revalidateTag
This commit is contained in:
130
app/home/(user)/(dashboard)/cart/montonio-callback/actions.ts
Normal file
130
app/home/(user)/(dashboard)/cart/montonio-callback/actions.ts
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import jwt from 'jsonwebtoken';
|
||||||
|
import { z } from "zod";
|
||||||
|
import { MontonioOrderToken } from "@/app/home/(user)/_components/cart/types";
|
||||||
|
import { loadCurrentUserAccount } from "@/app/home/(user)/_lib/server/load-user-account";
|
||||||
|
import { listProductTypes } from "@lib/data/products";
|
||||||
|
import { placeOrder, retrieveCart } from "@lib/data/cart";
|
||||||
|
import { createI18nServerInstance } from "~/lib/i18n/i18n.server";
|
||||||
|
import { createOrder } from '~/lib/services/order.service';
|
||||||
|
import { getOrderedAnalysisElementsIds, sendOrderToMedipost } from '~/lib/services/medipost.service';
|
||||||
|
|
||||||
|
const ANALYSIS_PACKAGES_TYPE_HANDLE = 'analysis-packages';
|
||||||
|
const MONTONIO_PAID_STATUS = 'PAID';
|
||||||
|
|
||||||
|
const env = () => z
|
||||||
|
.object({
|
||||||
|
emailSender: z
|
||||||
|
.string({
|
||||||
|
required_error: 'EMAIL_SENDER is required',
|
||||||
|
})
|
||||||
|
.min(1),
|
||||||
|
siteUrl: z
|
||||||
|
.string({
|
||||||
|
required_error: 'NEXT_PUBLIC_SITE_URL is required',
|
||||||
|
})
|
||||||
|
.min(1),
|
||||||
|
})
|
||||||
|
.parse({
|
||||||
|
emailSender: process.env.EMAIL_SENDER,
|
||||||
|
siteUrl: process.env.NEXT_PUBLIC_SITE_URL!,
|
||||||
|
});
|
||||||
|
|
||||||
|
const sendEmail = async ({ email, analysisPackageName, personName, partnerLocationName, language }: { email: string, analysisPackageName: string, personName: string, partnerLocationName: string, language: string }) => {
|
||||||
|
try {
|
||||||
|
const { renderSynlabAnalysisPackageEmail } = await import('@kit/email-templates');
|
||||||
|
const { getMailer } = await import('@kit/mailers');
|
||||||
|
|
||||||
|
const mailer = await getMailer();
|
||||||
|
|
||||||
|
const { html, subject } = await renderSynlabAnalysisPackageEmail({
|
||||||
|
analysisPackageName,
|
||||||
|
personName,
|
||||||
|
partnerLocationName,
|
||||||
|
language,
|
||||||
|
});
|
||||||
|
|
||||||
|
await mailer
|
||||||
|
.sendEmail({
|
||||||
|
from: env().emailSender,
|
||||||
|
to: email,
|
||||||
|
subject,
|
||||||
|
html,
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
throw new Error(`Failed to send email, message=${error}`);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Failed to send email, message=${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function processMontonioCallback(orderToken: string) {
|
||||||
|
const { language } = await createI18nServerInstance();
|
||||||
|
|
||||||
|
const secretKey = process.env.MONTONIO_SECRET_KEY as string;
|
||||||
|
|
||||||
|
const decoded = jwt.verify(orderToken, secretKey, {
|
||||||
|
algorithms: ['HS256'],
|
||||||
|
}) as MontonioOrderToken;
|
||||||
|
|
||||||
|
if (decoded.paymentStatus !== MONTONIO_PAID_STATUS) {
|
||||||
|
throw new Error("Payment not successful");
|
||||||
|
}
|
||||||
|
|
||||||
|
const account = await loadCurrentUserAccount();
|
||||||
|
if (!account) {
|
||||||
|
throw new Error("Account not found in context");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const [,, cartId] = decoded.merchantReferenceDisplay.split(':');
|
||||||
|
if (!cartId) {
|
||||||
|
throw new Error("Cart ID not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
const cart = await retrieveCart(cartId);
|
||||||
|
if (!cart) {
|
||||||
|
throw new Error("Cart not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
const medusaOrder = await placeOrder(cartId, { revalidateCacheTags: false });
|
||||||
|
const orderedAnalysisElements = await getOrderedAnalysisElementsIds({ medusaOrder });
|
||||||
|
await createOrder({ medusaOrder, orderedAnalysisElements });
|
||||||
|
|
||||||
|
const { productTypes } = await listProductTypes();
|
||||||
|
const analysisPackagesType = productTypes.find(({ metadata }) => metadata?.handle === ANALYSIS_PACKAGES_TYPE_HANDLE);
|
||||||
|
const analysisPackageOrderItem = medusaOrder.items?.find(({ product_type_id }) => product_type_id === analysisPackagesType?.id);
|
||||||
|
|
||||||
|
const orderResult = {
|
||||||
|
medusaOrderId: medusaOrder.id,
|
||||||
|
email: medusaOrder.email,
|
||||||
|
partnerLocationName: analysisPackageOrderItem?.metadata?.partner_location_name as string ?? '',
|
||||||
|
analysisPackageName: analysisPackageOrderItem?.title ?? '',
|
||||||
|
orderedAnalysisElements,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { medusaOrderId, email, partnerLocationName, analysisPackageName } = orderResult;
|
||||||
|
const personName = account.name;
|
||||||
|
|
||||||
|
if (email && analysisPackageName) {
|
||||||
|
try {
|
||||||
|
await sendEmail({ email, analysisPackageName, personName, partnerLocationName, language });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to send email", error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// @TODO send email for separate analyses
|
||||||
|
console.error("Missing email or analysisPackageName", orderResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send order to Medipost (no await to avoid blocking)
|
||||||
|
sendOrderToMedipost({ medusaOrderId, orderedAnalysisElements });
|
||||||
|
|
||||||
|
return { success: true };
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to place order", error);
|
||||||
|
throw new Error(`Failed to place order, message=${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import { processMontonioCallback } from './actions';
|
||||||
|
|
||||||
|
export default function MontonioCallbackClient({ orderToken, error }: {
|
||||||
|
orderToken?: string;
|
||||||
|
error?: string;
|
||||||
|
}) {
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const [isProcessing, setIsProcessing] = useState(false);
|
||||||
|
const [hasProcessed, setHasProcessed] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (error) {
|
||||||
|
console.error(error);
|
||||||
|
router.push('/home/cart/montonio-callback/error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!orderToken || hasProcessed || isProcessing) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const processOrder = async () => {
|
||||||
|
setIsProcessing(true);
|
||||||
|
setHasProcessed(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await processMontonioCallback(orderToken);
|
||||||
|
router.push('/home/order');
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to place order", error);
|
||||||
|
router.push('/home/cart/montonio-callback/error');
|
||||||
|
} finally {
|
||||||
|
setIsProcessing(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
processOrder();
|
||||||
|
}, [orderToken, error, router, hasProcessed, isProcessing]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex mt-10 justify-center min-h-screen">
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary mx-auto mb-4"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,153 +1,16 @@
|
|||||||
import jwt from 'jsonwebtoken';
|
import MontonioCallbackClient from './client-component';
|
||||||
import { z } from "zod";
|
|
||||||
import { redirect } from 'next/navigation';
|
|
||||||
import { MontonioOrderToken } from "@/app/home/(user)/_components/cart/types";
|
|
||||||
import { loadCurrentUserAccount } from "@/app/home/(user)/_lib/server/load-user-account";
|
|
||||||
import { listProductTypes } from "@lib/data/products";
|
|
||||||
import { placeOrder, retrieveCart } from "@lib/data/cart";
|
|
||||||
import { createI18nServerInstance } from "~/lib/i18n/i18n.server";
|
|
||||||
import { createOrder } from '~/lib/services/order.service';
|
|
||||||
import { getOrderedAnalysisElementsIds, sendOrderToMedipost } from '~/lib/services/medipost.service';
|
|
||||||
|
|
||||||
const ANALYSIS_PACKAGES_TYPE_HANDLE = 'analysis-packages';
|
export default async function MontonioCallbackPage({ searchParams }: {
|
||||||
const MONTONIO_PAID_STATUS = 'PAID';
|
searchParams: Promise<{
|
||||||
|
|
||||||
const env = () => z
|
|
||||||
.object({
|
|
||||||
emailSender: z
|
|
||||||
.string({
|
|
||||||
required_error: 'EMAIL_SENDER is required',
|
|
||||||
})
|
|
||||||
.min(1),
|
|
||||||
siteUrl: z
|
|
||||||
.string({
|
|
||||||
required_error: 'NEXT_PUBLIC_SITE_URL is required',
|
|
||||||
})
|
|
||||||
.min(1),
|
|
||||||
})
|
|
||||||
.parse({
|
|
||||||
emailSender: process.env.EMAIL_SENDER,
|
|
||||||
siteUrl: process.env.NEXT_PUBLIC_SITE_URL!,
|
|
||||||
});
|
|
||||||
|
|
||||||
const sendEmail = async ({ email, analysisPackageName, personName, partnerLocationName, language }: { email: string, analysisPackageName: string, personName: string, partnerLocationName: string, language: string }) => {
|
|
||||||
try {
|
|
||||||
const { renderSynlabAnalysisPackageEmail } = await import('@kit/email-templates');
|
|
||||||
const { getMailer } = await import('@kit/mailers');
|
|
||||||
|
|
||||||
const mailer = await getMailer();
|
|
||||||
|
|
||||||
const { html, subject } = await renderSynlabAnalysisPackageEmail({
|
|
||||||
analysisPackageName,
|
|
||||||
personName,
|
|
||||||
partnerLocationName,
|
|
||||||
language,
|
|
||||||
});
|
|
||||||
|
|
||||||
await mailer
|
|
||||||
.sendEmail({
|
|
||||||
from: env().emailSender,
|
|
||||||
to: email,
|
|
||||||
subject,
|
|
||||||
html,
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
throw new Error(`Failed to send email, message=${error}`);
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(`Failed to send email, message=${error}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleOrderToken = async (orderToken: string) => {
|
|
||||||
const secretKey = process.env.MONTONIO_SECRET_KEY as string;
|
|
||||||
|
|
||||||
const decoded = jwt.verify(orderToken, secretKey, {
|
|
||||||
algorithms: ['HS256'],
|
|
||||||
}) as MontonioOrderToken;
|
|
||||||
if (decoded.paymentStatus !== MONTONIO_PAID_STATUS) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const [,, cartId] = decoded.merchantReferenceDisplay.split(':');
|
|
||||||
if (!cartId) {
|
|
||||||
throw new Error("Cart ID not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
const cart = await retrieveCart(cartId);
|
|
||||||
if (!cart) {
|
|
||||||
throw new Error("Cart not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
const medusaOrder = await placeOrder(cartId, { revalidateCacheTags: true });
|
|
||||||
const orderedAnalysisElements = await getOrderedAnalysisElementsIds({ medusaOrder });
|
|
||||||
await createOrder({ medusaOrder, orderedAnalysisElements });
|
|
||||||
|
|
||||||
const { productTypes } = await listProductTypes();
|
|
||||||
const analysisPackagesType = productTypes.find(({ metadata }) => metadata?.handle === ANALYSIS_PACKAGES_TYPE_HANDLE);
|
|
||||||
const analysisPackageOrderItem = medusaOrder.items?.find(({ product_type_id }) => product_type_id === analysisPackagesType?.id);
|
|
||||||
return {
|
|
||||||
medusaOrderId: medusaOrder.id,
|
|
||||||
email: medusaOrder.email,
|
|
||||||
partnerLocationName: analysisPackageOrderItem?.metadata?.partner_location_name as string ?? '',
|
|
||||||
analysisPackageName: analysisPackageOrderItem?.title ?? '',
|
|
||||||
orderedAnalysisElements,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to place order", error);
|
|
||||||
throw new Error(`Failed to place order, message=${error}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface PageProps {
|
|
||||||
searchParams: {
|
|
||||||
'order-token'?: string;
|
'order-token'?: string;
|
||||||
};
|
}>;
|
||||||
}
|
}) {
|
||||||
|
const orderToken = (await searchParams)['order-token'];
|
||||||
export default async function MontonioCallbackPage({ searchParams }: PageProps) {
|
|
||||||
const { language } = await createI18nServerInstance();
|
|
||||||
|
|
||||||
try {
|
console.log('orderToken', orderToken);
|
||||||
const orderToken = searchParams['order-token'];
|
if (!orderToken) {
|
||||||
if (!orderToken) {
|
return <MontonioCallbackClient error="Order token is missing" />;
|
||||||
console.error("Order token is missing");
|
|
||||||
redirect('/home/cart/montonio-callback/error');
|
|
||||||
}
|
|
||||||
|
|
||||||
const account = await loadCurrentUserAccount();
|
|
||||||
if (!account) {
|
|
||||||
console.error("Account not found in context");
|
|
||||||
redirect('/home/cart/montonio-callback/error');
|
|
||||||
}
|
|
||||||
|
|
||||||
const orderResult = await handleOrderToken(orderToken);
|
|
||||||
if (!orderResult) {
|
|
||||||
console.error("Order result is missing");
|
|
||||||
redirect('/home/cart/montonio-callback/error');
|
|
||||||
}
|
|
||||||
|
|
||||||
const { medusaOrderId, email, partnerLocationName, analysisPackageName, orderedAnalysisElements } = orderResult;
|
|
||||||
const personName = account.name;
|
|
||||||
|
|
||||||
if (email && analysisPackageName) {
|
|
||||||
try {
|
|
||||||
await sendEmail({ email, analysisPackageName, personName, partnerLocationName, language });
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to send email", error);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// @TODO send email for separate analyses
|
|
||||||
console.error("Missing email or analysisPackageName", orderResult);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send order to Medipost (no await to avoid blocking the redirect)
|
|
||||||
sendOrderToMedipost({ medusaOrderId, orderedAnalysisElements });
|
|
||||||
|
|
||||||
redirect('/home/order');
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to place order", error);
|
|
||||||
redirect('/home/cart/montonio-callback/error');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return <MontonioCallbackClient orderToken={orderToken} />;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user