dev #21

Merged
Seazn merged 35 commits from dev into main 2026-05-21 17:34:44 +00:00
Showing only changes of commit 1261d54c95 - Show all commits

View File

@ -0,0 +1,52 @@
export const CAPSULES_PER_PACK = 10;
export const MIN_ABO_PACKS = 6;
export const MAX_ABO_PACKS = 10000;
export const COFFEE_SELECTIONS_STORAGE_KEY = 'coffeeSelections';
export const COFFEE_SELECTIONS_UNIT_STORAGE_KEY = 'coffeeSelectionsUnit';
export const COFFEE_SELECTIONS_UNIT = 'packs-v1';
export function packsToCapsules(packs: number) {
return Math.max(0, Math.floor(Number(packs) || 0)) * CAPSULES_PER_PACK;
}
export function normalizePackCount(value: unknown) {
const parsed = Number(value);
if (!Number.isFinite(parsed)) return 0;
return Math.max(0, Math.floor(parsed));
}
export function getOrderPackError(totalPacks: number) {
if (totalPacks < MIN_ABO_PACKS) {
return `Order must contain at least ${MIN_ABO_PACKS} packs (${packsToCapsules(MIN_ABO_PACKS)} capsules).`;
}
if (totalPacks > MAX_ABO_PACKS) {
return `Order cannot contain more than ${MAX_ABO_PACKS.toLocaleString('en-US')} packs (${packsToCapsules(MAX_ABO_PACKS).toLocaleString('en-US')} capsules).`;
}
return null;
}
export function getRemainingMinPacks(totalPacks: number) {
return Math.max(0, MIN_ABO_PACKS - totalPacks);
}
export function normalizeStoredSelections(
rawSelections: Record<string, unknown>,
unit: string | null,
) {
const entries = Object.entries(rawSelections || {});
return entries.reduce<Record<string, number>>((acc, [coffeeId, value]) => {
const normalizedValue = normalizePackCount(value);
if (normalizedValue <= 0) return acc;
const packCount = unit === COFFEE_SELECTIONS_UNIT
? normalizedValue
: (normalizedValue % CAPSULES_PER_PACK === 0 ? normalizedValue / CAPSULES_PER_PACK : normalizedValue);
if (packCount <= 0) return acc;
acc[coffeeId] = Math.min(packCount, MAX_ABO_PACKS);
return acc;
}, {});
}