Fix: Handle designJson as string in checkout API
The designJson field can be either a parsed object or a JSON string depending on how it was stored. The checkout API was assuming it was always an object and casting it, which caused 'Cannot read properties of undefined' error when trying to access .length on front/back arrays. Now properly parses the string if needed and safely accesses array properties with optional chaining.
This commit is contained in:
@@ -78,8 +78,8 @@ export async function POST(req: Request) {
|
||||
|
||||
const activePromotions = await fetchActivePromotions();
|
||||
const pricedItems = items.map((i) => {
|
||||
const design = i.designJson as { front: unknown[]; back: unknown[] };
|
||||
const isPersonalised = design.front.length > 0 || design.back.length > 0;
|
||||
const design = typeof i.designJson === 'string' ? JSON.parse(i.designJson) : i.designJson;
|
||||
const isPersonalised = (design.front?.length ?? 0) > 0 || (design.back?.length ?? 0) > 0;
|
||||
return {
|
||||
...i,
|
||||
unitPrice: getEffectivePrice(productsById.get(i.productId)!, activePromotions, new Date(), isPersonalised).price,
|
||||
|
||||
Reference in New Issue
Block a user