feat(MED-100): partner location metadata stored on order line instead of order
This commit is contained in:
@@ -73,11 +73,12 @@ const handleOrderToken = async (orderToken: string) => {
|
|||||||
}
|
}
|
||||||
const { productTypes } = await listProductTypes();
|
const { productTypes } = await listProductTypes();
|
||||||
const analysisPackagesType = productTypes.find(({ metadata }) => metadata?.handle === 'analysis-packages');
|
const analysisPackagesType = productTypes.find(({ metadata }) => metadata?.handle === 'analysis-packages');
|
||||||
const { order } = await placeOrder(cartId, { revalidateCacheTags: true });
|
const order = await placeOrder(cartId, { revalidateCacheTags: true });
|
||||||
|
const analysisPackageOrderItem = order.items?.find(({ product_type_id }) => product_type_id === analysisPackagesType?.id);
|
||||||
return {
|
return {
|
||||||
email: order.email,
|
email: order.email,
|
||||||
partnerLocationName: order.metadata?.partner_location_name as string ?? '',
|
partnerLocationName: analysisPackageOrderItem?.metadata?.partner_location_name as string ?? '',
|
||||||
analysisPackageName: order.items?.find(item => item.product_type_id === analysisPackagesType?.id)?.title ?? '',
|
analysisPackageName: analysisPackageOrderItem?.title ?? '',
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(`Failed to place order, message=${error}`);
|
throw new Error(`Failed to place order, message=${error}`);
|
||||||
@@ -105,8 +106,10 @@ export async function GET(request: Request) {
|
|||||||
|
|
||||||
const { email, partnerLocationName, analysisPackageName } = orderResult;
|
const { email, partnerLocationName, analysisPackageName } = orderResult;
|
||||||
const personName = account.name;
|
const personName = account.name;
|
||||||
if (email && analysisPackageName && partnerLocationName) {
|
if (email && analysisPackageName) {
|
||||||
await sendEmail({ email, analysisPackageName, personName, partnerLocationName, language });
|
await sendEmail({ email, analysisPackageName, personName, partnerLocationName, language });
|
||||||
|
} else {
|
||||||
|
console.error("Missing email or analysisPackageName", orderResult);
|
||||||
}
|
}
|
||||||
return Response.redirect(new URL('/home/order', baseUrl))
|
return Response.redirect(new URL('/home/order', baseUrl))
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { notFound } from 'next/navigation';
|
|||||||
import { retrieveOrder } from '~/medusa/lib/data/orders';
|
import { retrieveOrder } from '~/medusa/lib/data/orders';
|
||||||
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
|
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
|
||||||
import OrderCompleted from '@/app/home/(user)/_components/order/order-completed';
|
import OrderCompleted from '@/app/home/(user)/_components/order/order-completed';
|
||||||
|
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
params: Promise<{ orderId: string }>;
|
params: Promise<{ orderId: string }>;
|
||||||
@@ -16,7 +17,7 @@ export async function generateMetadata() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function OrderConfirmedPage(props: Props) {
|
async function OrderConfirmedPage(props: Props) {
|
||||||
const params = await props.params;
|
const params = await props.params;
|
||||||
const order = await retrieveOrder(params.orderId).catch(() => null);
|
const order = await retrieveOrder(params.orderId).catch(() => null);
|
||||||
|
|
||||||
@@ -26,3 +27,5 @@ export default async function OrderConfirmedPage(props: Props) {
|
|||||||
|
|
||||||
return <OrderCompleted order={order} />;
|
return <OrderCompleted order={order} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default withI18n(OrderConfirmedPage);
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { updateCart } from "@lib/data/cart"
|
import { updateLineItem } from "@lib/data/cart"
|
||||||
import { StoreCart } from "@medusajs/types"
|
import { StoreCart, StoreCartLineItem } from "@medusajs/types"
|
||||||
import { Form } from "@kit/ui/form";
|
import { Form } from "@kit/ui/form";
|
||||||
import { Trans } from '@kit/ui/trans';
|
import { Trans } from '@kit/ui/trans';
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
@@ -29,7 +29,7 @@ const MOCK_LOCATIONS: { id: string, name: string }[] = [
|
|||||||
{ id: "synlab-parnu-1", name: "SYNLAB - Pärnu" },
|
{ id: "synlab-parnu-1", name: "SYNLAB - Pärnu" },
|
||||||
]
|
]
|
||||||
|
|
||||||
export default function AnalysisLocation({ cart }: { cart: StoreCart }) {
|
export default function AnalysisLocation({ cart, analysisPackages }: { cart: StoreCart, analysisPackages: StoreCartLineItem[] }) {
|
||||||
const { t } = useTranslation('cart');
|
const { t } = useTranslation('cart');
|
||||||
|
|
||||||
const form = useForm<z.infer<typeof AnalysisLocationSchema>>({
|
const form = useForm<z.infer<typeof AnalysisLocationSchema>>({
|
||||||
@@ -40,12 +40,16 @@ export default function AnalysisLocation({ cart }: { cart: StoreCart }) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const onSubmit = async ({ locationId }: z.infer<typeof AnalysisLocationSchema>) => {
|
const onSubmit = async ({ locationId }: z.infer<typeof AnalysisLocationSchema>) => {
|
||||||
const promise = updateCart({
|
const promise = Promise.all(analysisPackages.map(async ({ id, quantity }) => {
|
||||||
metadata: {
|
await updateLineItem({
|
||||||
partner_location_name: MOCK_LOCATIONS.find(({ id }) => id === locationId)?.name ?? '',
|
lineId: id,
|
||||||
partner_location_id: locationId,
|
quantity,
|
||||||
},
|
metadata: {
|
||||||
});
|
partner_location_name: MOCK_LOCATIONS.find((location) => location.id === locationId)?.name ?? '',
|
||||||
|
partner_location_id: locationId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
toast.promise(promise, {
|
toast.promise(promise, {
|
||||||
success: t(`cart:items.analysisLocation.success`),
|
success: t(`cart:items.analysisLocation.success`),
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ export default function Cart({
|
|||||||
</h5>
|
</h5>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<AnalysisLocation cart={{ ...cart }} />
|
<AnalysisLocation cart={{ ...cart }} analysisPackages={analysisPackages} />
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ export default function OrderItem({ item, currencyCode }: {
|
|||||||
className="txt-medium-plus text-ui-fg-base"
|
className="txt-medium-plus text-ui-fg-base"
|
||||||
data-testid="product-name"
|
data-testid="product-name"
|
||||||
>
|
>
|
||||||
{item.product_title}
|
{item.product_title}{` (${item.metadata?.partner_location_name ?? "-"})`}
|
||||||
</span>
|
</span>
|
||||||
<LineItemOptions variant={item.variant} data-testid="product-variant" />
|
<LineItemOptions variant={item.variant} data-testid="product-variant" />
|
||||||
</TableCell>
|
</TableCell>
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import {
|
|||||||
} from "./cookies";
|
} from "./cookies";
|
||||||
import { getRegion } from "./regions";
|
import { getRegion } from "./regions";
|
||||||
import { sdk } from "@lib/config";
|
import { sdk } from "@lib/config";
|
||||||
|
import { retrieveOrder } from "./orders";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves a cart by its ID. If no ID is provided, it will use the cart ID from the cookies.
|
* Retrieves a cart by its ID. If no ID is provided, it will use the cart ID from the cookies.
|
||||||
@@ -161,9 +162,11 @@ export async function addToCart({
|
|||||||
export async function updateLineItem({
|
export async function updateLineItem({
|
||||||
lineId,
|
lineId,
|
||||||
quantity,
|
quantity,
|
||||||
|
metadata,
|
||||||
}: {
|
}: {
|
||||||
lineId: string;
|
lineId: string;
|
||||||
quantity: number;
|
quantity: number;
|
||||||
|
metadata?: Record<string, any>;
|
||||||
}) {
|
}) {
|
||||||
if (!lineId) {
|
if (!lineId) {
|
||||||
throw new Error("Missing lineItem ID when updating line item");
|
throw new Error("Missing lineItem ID when updating line item");
|
||||||
@@ -180,7 +183,7 @@ export async function updateLineItem({
|
|||||||
};
|
};
|
||||||
|
|
||||||
await sdk.store.cart
|
await sdk.store.cart
|
||||||
.updateLineItem(cartId, lineId, { quantity }, {}, headers)
|
.updateLineItem(cartId, lineId, { quantity, metadata }, {}, headers)
|
||||||
.then(async () => {
|
.then(async () => {
|
||||||
const cartCacheTag = await getCacheTag("carts");
|
const cartCacheTag = await getCacheTag("carts");
|
||||||
revalidateTag(cartCacheTag);
|
revalidateTag(cartCacheTag);
|
||||||
@@ -425,7 +428,7 @@ export async function placeOrder(cartId?: string, options: { revalidateCacheTags
|
|||||||
throw new Error("Cart is not an order");
|
throw new Error("Cart is not an order");
|
||||||
}
|
}
|
||||||
|
|
||||||
return cartRes;
|
return retrieveOrder(cartRes.order.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user