/** * Pickup point delivery on the ship page (PMSG-347): which services deliver to a locker or * shop, and which place a booking sends. */ import type { Order, OrderPickupPoint, Quote } from '../../../types'; /** Quote tag for services that deliver to a pickup point (a locker or shop). */ export const SHOP_DELIVERY_TAG = 'shopdelivery'; /** Whether the service delivers to a pickup point the booking must name (destination.useAddress.shopId). */ export function isPickupQuote(quote: Quote | null | undefined): boolean { return !!quote && (quote.tags ?? []).includes(SHOP_DELIVERY_TAG); } /** The provider whose lockers or shops a pickup service delivers to, e.g. INPOST or MYHRMS. */ export function pickupProvider(quote: Quote | null | undefined): string { return (quote?.service?.dropOffProviderGroup ?? '').trim().toUpperCase(); } /** Whether places can be looked up: any carrier, if the quote has a valid provider (PickupPoints::is_provider). */ export function hasPickupLookup(quote: Quote | null | undefined): boolean { return isPickupQuote(quote) && /^[A-Z0-9][A-Z0-9_-]{1,29}$/.test(pickupProvider(quote)); } /** Locker or shop, from the quote's locker tag. */ export function pickupKind(quote: Quote | null | undefined): 'locker' | 'shop' { return (quote?.tags ?? []).includes('locker') ? 'locker' : 'shop'; } /** The place saved on the order: the customer's choice at checkout, or the merchant's pick. */ export function orderPickupPoint(order: Order | null | undefined): OrderPickupPoint | null { return order?.shipping_method?.checkout?.pickupPoint ?? null; } /** * The place this booking delivers to: the order's place when the service is a pickup service of * the same provider; null otherwise (home delivery, or a place of another provider, which the * merchant must replace). */ export function bookingPickupPoint(order: Order | null | undefined, quote: Quote | null | undefined): OrderPickupPoint | null { if (!isPickupQuote(quote)) return null; const point = orderPickupPoint(order); return point && point.provider.toUpperCase() === pickupProvider(quote) ? point : null; } /** * The order with another place to collect from, as the merchant picked it on the ship page. The * booking uses it, and once paid the order keeps it (markOrderAsPaid) so the customer's emails * show where to collect. */ export function withPickupPoint(order: Order, point: OrderPickupPoint): Order { const method = order.shipping_method ?? { id: null, title: null }; const checkout = method.checkout ?? { serviceSlug: '', courierSlug: '', label: '', quotedNet: null, quotedGross: null, quotedAt: null, customerPaid: 0, customerTax: 0, }; return { ...order, shipping_method: { ...method, checkout: { ...checkout, pickupPoint: point } } }; } /** "Name, address, town, postcode", leaving out empty parts. */ export function pickupPointText(point: Pick): string { return [point.name, point.address1, point.address2, point.postcode] .map((part) => (part ?? '').trim()) .filter(Boolean) .join(', '); }