feat(MED-131): route sometimes redirects back to login instead
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { redirect } from 'next/navigation';
|
||||||
import { MontonioOrderToken } from "@/app/home/(user)/_components/cart/types";
|
import { MontonioOrderToken } from "@/app/home/(user)/_components/cart/types";
|
||||||
import { loadCurrentUserAccount } from "@/app/home/(user)/_lib/server/load-user-account";
|
import { loadCurrentUserAccount } from "@/app/home/(user)/_lib/server/load-user-account";
|
||||||
import { listProductTypes } from "@lib/data/products";
|
import { listProductTypes } from "@lib/data/products";
|
||||||
@@ -99,27 +100,40 @@ const handleOrderToken = async (orderToken: string) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
interface PageProps {
|
||||||
|
params: {
|
||||||
|
montonioId?: string;
|
||||||
|
};
|
||||||
|
searchParams: {
|
||||||
|
'order-token'?: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function MontonioCallbackPage({ searchParams }: PageProps) {
|
||||||
const { language } = await createI18nServerInstance();
|
const { language } = await createI18nServerInstance();
|
||||||
const baseUrl = new URL(env().siteUrl);
|
|
||||||
try {
|
try {
|
||||||
const orderToken = new URL(request.url).searchParams.get('order-token');
|
const orderToken = searchParams['order-token'];
|
||||||
if (!orderToken) {
|
if (!orderToken) {
|
||||||
throw new Error("Order token is missing");
|
console.error("Order token is missing");
|
||||||
|
redirect('/home/cart/montonio-callback/error');
|
||||||
}
|
}
|
||||||
|
|
||||||
const account = await loadCurrentUserAccount();
|
const account = await loadCurrentUserAccount();
|
||||||
if (!account) {
|
if (!account) {
|
||||||
throw new Error("Account not found in context");
|
console.error("Account not found in context");
|
||||||
|
redirect('/home/cart/montonio-callback/error');
|
||||||
}
|
}
|
||||||
|
|
||||||
const orderResult = await handleOrderToken(orderToken);
|
const orderResult = await handleOrderToken(orderToken);
|
||||||
if (!orderResult) {
|
if (!orderResult) {
|
||||||
throw new Error("Order result is missing");
|
console.error("Order result is missing");
|
||||||
|
redirect('/home/cart/montonio-callback/error');
|
||||||
}
|
}
|
||||||
|
|
||||||
const { medusaOrderId, email, partnerLocationName, analysisPackageName, orderedAnalysisElements } = orderResult;
|
const { medusaOrderId, email, partnerLocationName, analysisPackageName, orderedAnalysisElements } = orderResult;
|
||||||
const personName = account.name;
|
const personName = account.name;
|
||||||
|
|
||||||
if (email && analysisPackageName) {
|
if (email && analysisPackageName) {
|
||||||
try {
|
try {
|
||||||
await sendEmail({ email, analysisPackageName, personName, partnerLocationName, language });
|
await sendEmail({ email, analysisPackageName, personName, partnerLocationName, language });
|
||||||
@@ -130,10 +144,13 @@ export async function GET(request: Request) {
|
|||||||
// @TODO send email for separate analyses
|
// @TODO send email for separate analyses
|
||||||
console.error("Missing email or analysisPackageName", orderResult);
|
console.error("Missing email or analysisPackageName", orderResult);
|
||||||
}
|
}
|
||||||
sendOrderToMedipost({ medusaOrderId, orderedAnalysisElements })
|
|
||||||
return Response.redirect(new URL('/home/order', baseUrl))
|
// Send order to Medipost (no await to avoid blocking the redirect)
|
||||||
|
sendOrderToMedipost({ medusaOrderId, orderedAnalysisElements });
|
||||||
|
|
||||||
|
redirect('/home/order');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to place order", error);
|
console.error("Failed to place order", error);
|
||||||
return Response.redirect(new URL('/home/cart/montonio-callback/error', baseUrl));
|
redirect('/home/cart/montonio-callback/error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1 +0,0 @@
|
|||||||
export { GET } from "./[montonioId]/route";
|
|
||||||
Reference in New Issue
Block a user