From fae1ce04c21c13049f723b30f3d95bc76aa6af2c Mon Sep 17 00:00:00 2001 From: Andymick Date: Sat, 18 Jul 2026 19:54:53 +0100 Subject: [PATCH] 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. --- .claude/settings.local.json | 3 ++- src/app/api/checkout/route.ts | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 3afad14..8a5ac49 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -64,7 +64,8 @@ "Bash(git status *)", "Bash(git revert *)", "Bash(git commit -m 'Fix price display issues and layout in design review page *)", - "Bash(git commit -m 'Fix: Calculate effectivePrice in design API endpoints *)" + "Bash(git commit -m 'Fix: Calculate effectivePrice in design API endpoints *)", + "Bash(git commit -m 'Fix: Handle designJson as string in checkout API *)" ] } } diff --git a/src/app/api/checkout/route.ts b/src/app/api/checkout/route.ts index 5b811cf..f3b845e 100644 --- a/src/app/api/checkout/route.ts +++ b/src/app/api/checkout/route.ts @@ -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,