Refactor subscribeAbo to enforce supported package sizes using getOrderPackError

This commit is contained in:
seaznCode 2026-05-05 22:10:56 +02:00
parent 1261d54c95
commit 646c399ae9

View File

@ -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