acme/checkout !42
Apply discount codes at checkout
src/checkout/discount.ts
+18
@@ -0,0 +1,18 @@ 5+export async function findCode(code: string): Promise<Code | null> { 6+ const found = await db.discountCodes.find(code); 7+ if (!found) return null; 8+ // Expired codes don't apply. 9+ if (found.expiresAt < found.createdAt) return null;
Sealmerge
@sealmerge
Expired codes still accepted
findCode() compares expiresAt with the
creation date instead of now, so a code never expires.
Evidence: src/checkout/discount.ts:9 compares code.expiresAt with code.createdAt
Suggested change
9- if (found.expiresAt < found.createdAt) return null; 9+ if (found.expiresAt < new Date()) return null;
10+ return found; 11+} 12+ 13+export const applyDiscount = (total: number, code: Code) => percentOff(total, code.percent);