diff --git a/before/dispatch-note.ts b/before/dispatch-note.ts index 4590dfa..46dc45f 100644 --- a/before/dispatch-note.ts +++ b/before/dispatch-note.ts @@ -5,6 +5,8 @@ export function dispatchNote(method: MethodId, destination: Destination): string return `RECOJO EN TIENDA · ${STORES[destination.store ?? '']}`; } else if (method === 'courier') { return `DOMICILIO · ${(destination.address ?? '').trim()}, ${(destination.district ?? '').trim()}`; + } else if (method === 'locker') { + return `CASILLERO · ${destination.locker}`; } throw new Error('Forma de entrega desconocida'); } diff --git a/before/shipping-cost.ts b/before/shipping-cost.ts index 481076d..07fb8cf 100644 --- a/before/shipping-cost.ts +++ b/before/shipping-cost.ts @@ -17,6 +17,8 @@ export function shippingQuote( if (parcel.subtotalCents >= FREE_SHIPPING_FROM_CENTS) return { costCents: 0, businessDays: BUSINESS_DAYS[zone] }; const extraKg = Math.max(0, Math.ceil(parcel.weightGrams / 1000) - 1); return { costCents: BASE_CENTS[zone] + extraKg * EXTRA_KG_CENTS, businessDays: BUSINESS_DAYS[zone] }; + } else if (method === 'locker') { + return { costCents: 500, businessDays: 2 }; } throw new Error('Forma de entrega desconocida'); } diff --git a/before/types.ts b/before/types.ts index a21781a..de9463e 100644 --- a/before/types.ts +++ b/before/types.ts @@ -1,11 +1,12 @@ // La versión de partida: las formas de entrega son un valor en una unión, y // cada archivo pregunta por él con un if. -export type MethodId = 'pickup' | 'courier'; +export type MethodId = 'pickup' | 'courier' | 'locker'; export const METHOD_LABELS: Readonly> = { pickup: 'Recojo en tienda', courier: 'Envío a domicilio', + locker: 'Casillero', }; export type Parcel = { weightGrams: number; subtotalCents: number }; @@ -26,5 +27,7 @@ export const ZONE_OF_DISTRICT: Readonly> = { 'villa el salvador': 2, }; +export const LOCKER_CODE = /^LK-\d{4}$/; + export const zoneOf = (district: string | undefined): 1 | 2 | undefined => ZONE_OF_DISTRICT[(district ?? '').trim().toLowerCase()]; diff --git a/before/validate-destination.ts b/before/validate-destination.ts index 7c75cb6..7958daf 100644 --- a/before/validate-destination.ts +++ b/before/validate-destination.ts @@ -1,4 +1,4 @@ -import { STORES, zoneOf, type Destination, type MethodId } from './types.ts'; +import { LOCKER_CODE, STORES, zoneOf, type Destination, type MethodId } from './types.ts'; export function validateDestination(method: MethodId, destination: Destination): string[] { if (method === 'pickup') { @@ -11,6 +11,8 @@ export function validateDestination(method: MethodId, destination: Destination): } if ((destination.address ?? '').trim().length < 5) problems.push('address debe ser la dirección de entrega'); return problems; + } else if (method === 'locker') { + return LOCKER_CODE.test(destination.locker ?? '') ? [] : ['locker debe ser un código de casillero, como LK-0420']; } throw new Error('Forma de entrega desconocida'); }