From 646c399ae90fa8b11ee769652bfd13e9290cbc6f Mon Sep 17 00:00:00 2001 From: seaznCode Date: Tue, 5 May 2026 22:10:56 +0200 Subject: [PATCH] Refactor subscribeAbo to enforce supported package sizes using getOrderPackError --- src/app/coffee-abonnements/summary/hooks/subscribeAbo.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/app/coffee-abonnements/summary/hooks/subscribeAbo.ts b/src/app/coffee-abonnements/summary/hooks/subscribeAbo.ts index a4bfda4..c327570 100644 --- a/src/app/coffee-abonnements/summary/hooks/subscribeAbo.ts +++ b/src/app/coffee-abonnements/summary/hooks/subscribeAbo.ts @@ -1,4 +1,5 @@ import { authFetch } from '../../../utils/authFetch' +import { getOrderPackError } from '../../lib/orderRules' export type SubscribeAboItem = { coffeeId: string | number; quantity?: number } export type SubscribeAboInput = { @@ -123,11 +124,11 @@ export async function subscribeAbo(input: SubscribeAboInput) { coffeeId: i.coffeeId, quantity: i.quantity != null ? i.quantity : 1, })) - // NEW: enforce supported package sizes const sumPacks = body.items.reduce((s: number, it: any) => s + Number(it.quantity || 0), 0) - if (sumPacks !== 6 && sumPacks !== 12) { - console.warn('[subscribeAbo] Invalid pack total:', sumPacks, 'expected 6 or 12') - throw new Error('Order must contain exactly 6 packs (60 capsules) or 12 packs (120 capsules).') + const orderPackError = getOrderPackError(sumPacks) + if (orderPackError) { + console.warn('[subscribeAbo] Invalid pack total:', sumPacks, orderPackError) + throw new Error(orderPackError) } } else { body.coffeeId = input.coffeeId