@aglyn/plugins-commerce 1.15.2 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +13 -13
- package/src/lib/components/cart.js +2 -1
- package/src/lib/components/cart.js.map +1 -1
- package/src/lib/components/console/payments-settings-card.component.js +6 -1
- package/src/lib/components/console/payments-settings-card.component.js.map +1 -1
- package/src/lib/components/console/pos/pos-api.d.ts +5 -0
- package/src/lib/components/console/pos/pos-api.js.map +1 -1
- package/src/lib/components/console/pos/pos-tender-dialogs.component.d.ts +2 -0
- package/src/lib/components/console/pos/pos-tender-dialogs.component.js +13 -6
- package/src/lib/components/console/pos/pos-tender-dialogs.component.js.map +1 -1
- package/src/lib/components/console/pos/pos-tender-panel.component.js +7 -4
- package/src/lib/components/console/pos/pos-tender-panel.component.js.map +1 -1
- package/src/lib/components/console/pos-cellular-control.component.d.ts +5 -0
- package/src/lib/components/console/pos-cellular-control.component.js +29 -15
- package/src/lib/components/console/pos-cellular-control.component.js.map +1 -1
- package/src/lib/components/console/product-add-ons-fields.component.d.ts +13 -0
- package/src/lib/components/console/product-add-ons-fields.component.js +96 -3
- package/src/lib/components/console/product-add-ons-fields.component.js.map +1 -1
- package/src/lib/components/console/product-builder-fields.component.d.ts +39 -0
- package/src/lib/components/console/product-builder-fields.component.js +195 -0
- package/src/lib/components/console/product-builder-fields.component.js.map +1 -0
- package/src/lib/components/console/product-editor-dialog.component.js +35 -2
- package/src/lib/components/console/product-editor-dialog.component.js.map +1 -1
- package/src/lib/components/console/quote-editor-dialog.component.js +43 -6
- package/src/lib/components/console/quote-editor-dialog.component.js.map +1 -1
- package/src/lib/components/console/quote-messages.component.d.ts +21 -0
- package/src/lib/components/console/quote-messages.component.js +185 -0
- package/src/lib/components/console/quote-messages.component.js.map +1 -0
- package/src/lib/components/console/quote-pay-link.component.js +1 -1
- package/src/lib/components/console/quote-pay-link.component.js.map +1 -1
- package/src/lib/components/console/quote-proofs.component.js +30 -3
- package/src/lib/components/console/quote-proofs.component.js.map +1 -1
- package/src/lib/components/console/quote-settings-card.component.d.ts +7 -3
- package/src/lib/components/console/quote-settings-card.component.js +154 -24
- package/src/lib/components/console/quote-settings-card.component.js.map +1 -1
- package/src/lib/components/product-builder-context.d.ts +22 -0
- package/src/lib/components/product-builder-context.js +21 -0
- package/src/lib/components/product-builder-context.js.map +1 -0
- package/src/lib/components/product-builder-mockup.d.ts +42 -0
- package/src/lib/components/product-builder-mockup.js +120 -0
- package/src/lib/components/product-builder-mockup.js.map +1 -0
- package/src/lib/components/product-builder-sample.d.ts +63 -0
- package/src/lib/components/product-builder-sample.js +96 -0
- package/src/lib/components/product-builder-sample.js.map +1 -0
- package/src/lib/components/product-choice-cards.d.ts +51 -0
- package/src/lib/components/product-choice-cards.js +311 -0
- package/src/lib/components/product-choice-cards.js.map +1 -0
- package/src/lib/components/product-detail-panels.d.ts +42 -0
- package/src/lib/components/product-detail-panels.js +185 -0
- package/src/lib/components/product-detail-panels.js.map +1 -0
- package/src/lib/components/product-detail.d.ts +20 -12
- package/src/lib/components/product-detail.js +136 -221
- package/src/lib/components/product-detail.js.map +1 -1
- package/src/lib/components/product-grid.d.ts +7 -0
- package/src/lib/components/product-grid.js +13 -9
- package/src/lib/components/product-grid.js.map +1 -1
- package/src/lib/components/product-name-lines.d.ts +37 -0
- package/src/lib/components/product-name-lines.js +36 -0
- package/src/lib/components/product-name-lines.js.map +1 -0
- package/src/lib/components/product-options-field-body.d.ts +9 -8
- package/src/lib/components/product-options-field-body.js +152 -3
- package/src/lib/components/product-options-field-body.js.map +1 -1
- package/src/lib/components/product-options-field.js +1 -1
- package/src/lib/components/product-options-field.js.map +1 -1
- package/src/lib/components/product-photo-fit.d.ts +49 -0
- package/src/lib/components/product-photo-fit.js +58 -0
- package/src/lib/components/product-photo-fit.js.map +1 -0
- package/src/lib/components/product-restock-notify.d.ts +12 -0
- package/src/lib/components/product-restock-notify.js +86 -0
- package/src/lib/components/product-restock-notify.js.map +1 -0
- package/src/lib/components/quote-messages-panel.d.ts +21 -0
- package/src/lib/components/quote-messages-panel.js +191 -0
- package/src/lib/components/quote-messages-panel.js.map +1 -0
- package/src/lib/components/quote-page-body.js +15 -1
- package/src/lib/components/quote-page-body.js.map +1 -1
- package/src/lib/components/quote-proof-panel.d.ts +6 -3
- package/src/lib/components/quote-proof-panel.js +139 -14
- package/src/lib/components/quote-proof-panel.js.map +1 -1
- package/src/lib/components/related-products.d.ts +7 -0
- package/src/lib/components/related-products.js +10 -7
- package/src/lib/components/related-products.js.map +1 -1
- package/src/lib/constants/quote-page.d.ts +8 -0
- package/src/lib/constants/quote-page.js +11 -0
- package/src/lib/constants/quote-page.js.map +1 -1
- package/src/lib/model/commerce-cart.d.ts +6 -1
- package/src/lib/model/commerce-cart.js +5 -1
- package/src/lib/model/commerce-cart.js.map +1 -1
- package/src/lib/model/commerce-invoices.js +5 -1
- package/src/lib/model/commerce-invoices.js.map +1 -1
- package/src/lib/model/commerce-pos-ops.d.ts +3 -0
- package/src/lib/model/commerce-pos-ops.js +3 -0
- package/src/lib/model/commerce-pos-ops.js.map +1 -1
- package/src/lib/model/commerce-pos.d.ts +8 -0
- package/src/lib/model/commerce-pos.js.map +1 -1
- package/src/lib/model/commerce-quote-messages.d.ts +106 -0
- package/src/lib/model/commerce-quote-messages.js +146 -0
- package/src/lib/model/commerce-quote-messages.js.map +1 -0
- package/src/lib/model/commerce-quote-pay.d.ts +31 -2
- package/src/lib/model/commerce-quote-pay.js +31 -7
- package/src/lib/model/commerce-quote-pay.js.map +1 -1
- package/src/lib/model/commerce-quote-proofs.d.ts +47 -8
- package/src/lib/model/commerce-quote-proofs.js +73 -18
- package/src/lib/model/commerce-quote-proofs.js.map +1 -1
- package/src/lib/model/commerce-quote-request.d.ts +14 -4
- package/src/lib/model/commerce-quote-request.js +25 -2
- package/src/lib/model/commerce-quote-request.js.map +1 -1
- package/src/lib/model/commerce-quotes.d.ts +56 -3
- package/src/lib/model/commerce-quotes.js +47 -2
- package/src/lib/model/commerce-quotes.js.map +1 -1
- package/src/lib/model/commerce.d.ts +12 -1
- package/src/lib/model/commerce.js.map +1 -1
- package/src/lib/model/index.d.ts +1 -0
- package/src/lib/model/index.js +1 -0
- package/src/lib/model/index.js.map +1 -1
- package/src/lib/model/pos-cellular.d.ts +36 -4
- package/src/lib/model/pos-cellular.js +39 -4
- package/src/lib/model/pos-cellular.js.map +1 -1
- package/src/lib/model/product-card-rules.d.ts +41 -0
- package/src/lib/model/product-card-rules.js +90 -0
- package/src/lib/model/product-card-rules.js.map +1 -0
- package/src/lib/model/product-modifiers.d.ts +11 -0
- package/src/lib/model/product-modifiers.js +10 -0
- package/src/lib/model/product-modifiers.js.map +1 -1
- package/src/lib/model/product-write.d.ts +3 -0
- package/src/lib/server/billing-webhook.js +6 -2
- package/src/lib/server/billing-webhook.js.map +1 -1
- package/src/lib/server/cancel-order.js +15 -0
- package/src/lib/server/cancel-order.js.map +1 -1
- package/src/lib/server/channel-order-updates.js +15 -1
- package/src/lib/server/channel-order-updates.js.map +1 -1
- package/src/lib/server/channel-orders.js +20 -0
- package/src/lib/server/channel-orders.js.map +1 -1
- package/src/lib/server/checkout.js +1 -1
- package/src/lib/server/checkout.js.map +1 -1
- package/src/lib/server/local-fulfillment.js +1 -1
- package/src/lib/server/local-fulfillment.js.map +1 -1
- package/src/lib/server/pos-cellular-billing.d.ts +5 -4
- package/src/lib/server/pos-cellular-billing.js +24 -12
- package/src/lib/server/pos-cellular-billing.js.map +1 -1
- package/src/lib/server/pos-cellular.d.ts +3 -1
- package/src/lib/server/pos-cellular.js +55 -33
- package/src/lib/server/pos-cellular.js.map +1 -1
- package/src/lib/server/pos-offline-sync.d.ts +3 -3
- package/src/lib/server/pos-offline-sync.js +17 -4
- package/src/lib/server/pos-offline-sync.js.map +1 -1
- package/src/lib/server/pos-order.js +17 -6
- package/src/lib/server/pos-order.js.map +1 -1
- package/src/lib/server/pos-payment.js +27 -11
- package/src/lib/server/pos-payment.js.map +1 -1
- package/src/lib/server/pos-reader-splash.js +10 -5
- package/src/lib/server/pos-reader-splash.js.map +1 -1
- package/src/lib/server/pos-readers.js +42 -12
- package/src/lib/server/pos-readers.js.map +1 -1
- package/src/lib/server/pos-return.js +19 -3
- package/src/lib/server/pos-return.js.map +1 -1
- package/src/lib/server/pos-stripe.d.ts +14 -0
- package/src/lib/server/pos-stripe.js +6 -2
- package/src/lib/server/pos-stripe.js.map +1 -1
- package/src/lib/server/pos-terminal-connection-token.js +23 -9
- package/src/lib/server/pos-terminal-connection-token.js.map +1 -1
- package/src/lib/server/pos-terminal.d.ts +80 -14
- package/src/lib/server/pos-terminal.js +393 -123
- package/src/lib/server/pos-terminal.js.map +1 -1
- package/src/lib/server/product-drafts.d.ts +1 -1
- package/src/lib/server/product-drafts.js +4 -3
- package/src/lib/server/product-drafts.js.map +1 -1
- package/src/lib/server/product-writer.js +5 -4
- package/src/lib/server/product-writer.js.map +1 -1
- package/src/lib/server/product.d.ts +11 -0
- package/src/lib/server/product.js +18 -3
- package/src/lib/server/product.js.map +1 -1
- package/src/lib/server/products-write.js +4 -3
- package/src/lib/server/products-write.js.map +1 -1
- package/src/lib/server/quote-email.d.ts +26 -0
- package/src/lib/server/quote-email.js +73 -7
- package/src/lib/server/quote-email.js.map +1 -1
- package/src/lib/server/quote-messages.d.ts +72 -0
- package/src/lib/server/quote-messages.js +356 -0
- package/src/lib/server/quote-messages.js.map +1 -0
- package/src/lib/server/quote-pay.d.ts +42 -0
- package/src/lib/server/quote-pay.js +81 -4
- package/src/lib/server/quote-pay.js.map +1 -1
- package/src/lib/server/quote-proofs.d.ts +1 -1
- package/src/lib/server/quote-proofs.js +63 -19
- package/src/lib/server/quote-proofs.js.map +1 -1
- package/src/lib/server/quote.d.ts +12 -0
- package/src/lib/server/quote.js +69 -16
- package/src/lib/server/quote.js.map +1 -1
- package/src/lib/server/refund.js +3 -1
- package/src/lib/server/refund.js.map +1 -1
- package/src/lib/server/reserve-stock.js +9 -0
- package/src/lib/server/reserve-stock.js.map +1 -1
- package/src/lib/server/returns.js +15 -1
- package/src/lib/server/returns.js.map +1 -1
- package/src/lib/server/reviews.js +15 -0
- package/src/lib/server/reviews.js.map +1 -1
- package/src/lib/server/sold-out-drop.d.ts +58 -0
- package/src/lib/server/sold-out-drop.js +81 -0
- package/src/lib/server/sold-out-drop.js.map +1 -0
- package/src/lib/server/stock-levels.js +9 -0
- package/src/lib/server/stock-levels.js.map +1 -1
- package/src/lib/server/stripe-refund.d.ts +7 -0
- package/src/lib/server/stripe-refund.js +26 -7
- package/src/lib/server/stripe-refund.js.map +1 -1
- package/src/lib/server.d.ts +15 -0
- package/src/lib/server.js +30 -1
- package/src/lib/server.js.map +1 -1
- package/src/lib/tenant-emails.js +85 -2
- package/src/lib/tenant-emails.js.map +1 -1
- package/src/lib/testing/fake-seller-payment-account.d.ts +29 -0
- package/src/lib/testing/fake-seller-payment-account.js +40 -0
- package/src/lib/testing/fake-seller-payment-account.js.map +1 -0
- package/src/lib/transfer/product-transfer.js +6 -5
- package/src/lib/transfer/product-transfer.js.map +1 -1
- package/src/lib/transfer/products.server.js +4 -3
- package/src/lib/transfer/products.server.js.map +1 -1
- package/src/lib/utils/quote-email-content.d.ts +6 -0
- package/src/lib/utils/quote-email-content.js +6 -3
- package/src/lib/utils/quote-email-content.js.map +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/server/local-fulfillment.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { resolveSiteTimeZone } from '@aglyn/aglyn/app-utils/collection-entry-date'\nimport { normalizePhone } from '@aglyn/aglyn/foundation/definitions/contact.types'\nimport { pluginSmsMessaging } from '@aglyn/aglyn/plugin-manager/plugin-sms-messaging'\nimport { pluginShippingRateQuoter } from '@aglyn/aglyn/plugin-manager/plugin-shipping-rates'\nimport type { PluginApiHandler } from '@aglyn/aglyn/server'\nimport * as Aglyn from '@aglyn/aglyn/server'\nimport { consumeRateLimit, firebaseAdmin, getOrgForHost } from '@aglyn/tenant-data-admin'\nimport { isDocumentId } from '@aglyn/tenant-data-admin/server/document-id'\nimport * as CommerceModel from '../model'\nimport { readCartId } from './cart-cookie'\n\n/**\n * Buy online, pick up in store, and local delivery at checkout (AGL-3624).\n *\n * The buyer chooses at the CART, before payment — the only place the store\n * still controls what is offered: a Stripe Checkout Session prices shipping\n * from the options it is created with and asks nothing back. So the cart\n * declares one of three ways, and this module turns the declaration into the\n * session it needs, or refuses it in words the cart can show:\n *\n * - `pickup` at one location: no shipping charged, no address collected,\n * the stock reserved at that location, and the order routed there.\n * - `local_delivery`: the store's OWN driver (not a marketplace courier —\n * that is the delivery-apps plugin, which never reads this). The postal\n * code — or, for a distance zone, the address — picks a zone, the zone\n * prices the fee and enforces its minimum, and the buyer books a window.\n * The session carries the fee as its one shipping option and collects an\n * address only in the store's country.\n * - `shipping`: everything as it was.\n *\n * A DECLARATION IS A REQUEST, NEVER A PRICE. The cart sends a location id, a\n * postal code and a window; the fee, the minimum and whether the window is\n * still open are all decided here from the store's own settings, on every\n * attempt.\n *\n * DISTANCE ZONES need both ends on a map, and the repository has no geocoder\n * of its own: the destination is placed by the shipping plugin's address\n * check (core's `core.shipping-rate-quoter` seam) when its provider returns a\n * position. Where none does, a distance zone matches nothing and the buyer is\n * told the store does not deliver there; postal-code zones need nothing.\n */\n\n/** What the cart declares. */\nexport interface LocalFulfillmentRequest {\n method: CommerceModel.OrderFulfillmentMethod\n locationId?: string\n postalCode?: string\n windowStartMs?: number\n /** The street address, asked only when the store has distance zones. */\n address?: { line1?: string; line2?: string; city?: string; state?: string; postalCode?: string }\n /**\n * The mobile number the buyer gave AT THE CART for texts about this pickup\n * or delivery (AGL-3624), in E.164. The only phone an online order's texts\n * go to: asked for that purpose, never taken from an address.\n */\n textPhone?: string\n}\n\n/** Reads the cart's declaration; `null` when it made none (a shipped order). */\nexport function readLocalFulfillmentRequest(value: unknown): LocalFulfillmentRequest | null {\n if (!value || typeof value !== 'object') return null\n const raw = value as Record<string, unknown>\n const method = raw['method']\n if (method !== 'pickup' && method !== 'local_delivery') return null\n const text = (input: unknown, max: number) => String(input ?? '').trim().slice(0, max)\n const request: LocalFulfillmentRequest = { method }\n const phone = normalizePhone(text(raw['textPhone'], 32))\n if (phone) request.textPhone = phone\n if (method === 'pickup') {\n request.locationId = text(raw['locationId'], 120)\n return request\n }\n const postalCode = text(raw['postalCode'], 16)\n if (postalCode) request.postalCode = postalCode\n const start = Math.round(Number(raw['windowStartMs']))\n if (Number.isFinite(start) && start > 0) request.windowStartMs = start\n const address = raw['address']\n if (address && typeof address === 'object') {\n const parts = address as Record<string, unknown>\n request.address = {\n line1: text(parts['line1'], 120),\n line2: text(parts['line2'], 120),\n city: text(parts['city'], 80),\n state: text(parts['state'], 40),\n postalCode: text(parts['postalCode'], 16) || postalCode,\n }\n }\n return request\n}\n\n/** The store's pickup locations and delivery settings, read once per attempt. */\nexport interface LocalFulfillmentStore {\n pickupLocations: CommerceModel.PickupLocationOption[]\n delivery: CommerceModel.LocalDeliverySettings\n timeZone: string\n}\n\n/** Reads what checkout and the cart need. Never throws: a failed read offers nothing. */\nexport async function readLocalFulfillmentStore(input: {\n hostRef: any\n /** `settings/store` as already read, to save a read. */\n storeSettings?: Record<string, unknown> | null\n org?: { timeZone?: string } | null\n host?: { timeZone?: string } | null\n}): Promise<LocalFulfillmentStore> {\n const { hostRef } = input\n const [locationsSnapshot, settings, host] = await Promise.all([\n hostRef\n .collection('locations')\n .limit(CommerceModel.PICKUP_LOCATIONS_MAX)\n .get()\n .catch(() => null),\n input.storeSettings !== undefined\n ? Promise.resolve(input.storeSettings)\n : hostRef\n .collection('settings')\n .doc('store')\n .get()\n .then((snapshot: any) => (snapshot?.data?.() ?? null) as Record<string, unknown> | null)\n .catch(() => null),\n input.host !== undefined\n ? Promise.resolve(input.host)\n : hostRef\n .get()\n .then((snapshot: any) => (snapshot?.data?.() ?? null) as { timeZone?: string } | null)\n .catch(() => null),\n ])\n const locations = (locationsSnapshot?.docs ?? []).map((snapshot: any) => ({\n ...(snapshot.data() as CommerceModel.InventoryLocation),\n id: String(snapshot.id),\n }))\n return {\n pickupLocations: CommerceModel.pickupLocationOptions(locations),\n delivery: CommerceModel.normalizeLocalDeliverySettings(settings?.['localDelivery']),\n timeZone: resolveSiteTimeZone(input.org ?? null, host ?? null),\n }\n}\n\n/** How long checkout waits for an address to be placed on a map. */\nexport const LOCAL_DELIVERY_GEOCODE_TIMEOUT_MS = 3_000\n\n/**\n * Places an address on the map through the shipping plugin's address check,\n * or answers `null`: no plugin, not available on this site, no position in\n * the answer, or slower than the timeout. Never throws.\n */\nexport async function placeDeliveryAddress(\n hostId: string,\n address: { line1?: string; line2?: string; city?: string; state?: string; postalCode?: string; country: string },\n): Promise<CommerceModel.LocalDeliveryCoordinates | null> {\n if (!address.line1 || !address.postalCode) return null\n const quoter = pluginShippingRateQuoter()\n if (!quoter?.validateAddress) return null\n let timer: ReturnType<typeof setTimeout> | undefined\n try {\n const placed = await Promise.race([\n (async () => {\n if (!(await quoter.available(hostId))) return null\n const check = await quoter.validateAddress?.(hostId, {\n line1: address.line1,\n ...(address.line2 ? { line2: address.line2 } : {}),\n ...(address.city ? { city: address.city } : {}),\n ...(address.state ? { state: address.state } : {}),\n postalCode: address.postalCode,\n country: address.country,\n } as never)\n return check?.verdict === 'invalid' ? null : (check?.coordinates ?? null)\n })().catch(() => null),\n new Promise<null>((resolve) => {\n timer = setTimeout(() => resolve(null), LOCAL_DELIVERY_GEOCODE_TIMEOUT_MS)\n }),\n ])\n return placed ?? null\n } finally {\n if (timer) clearTimeout(timer)\n }\n}\n\n/** The shipping option id a local delivery fee is declared under. */\nexport const LOCAL_DELIVERY_RATE_ID = 'local-delivery'\n\nexport type LocalFulfillmentPlan =\n | { kind: 'shipping' }\n | {\n kind: 'refusal'\n status: 400 | 409\n error: string\n /** What the cart should re-ask: the options changed under the buyer. */\n changed?: 'pickup' | 'delivery'\n }\n | {\n kind: 'pickup'\n location: CommerceModel.PickupLocationOption\n metadata: Record<string, string>\n }\n | {\n kind: 'local_delivery'\n zone: CommerceModel.LocalDeliveryZone\n window: CommerceModel.LocalDeliveryWindow\n feeCents: number\n country: string\n locationId?: string\n option: CommerceModel.ResolvedShippingRate\n metadata: Record<string, string>\n }\n\nconst money = (cents: number) => CommerceModel.formatOrderMoney(cents, 'USD')\n\n/**\n * Turns the cart's declaration into what the session needs, or a refusal.\n * Taken BEFORE any reservation or Stripe call, so a refusal costs nothing\n * and the buyer's same button works once they choose again.\n */\nexport async function planLocalFulfillment(input: {\n hostId: string\n request: LocalFulfillmentRequest | null\n /** The goods, pre-discount — what a minimum and a free-over threshold read. */\n itemsCents: number\n hasPhysicalLine: boolean\n store: LocalFulfillmentStore\n nowMs?: number\n}): Promise<LocalFulfillmentPlan> {\n const { request, store } = input\n // A cart of downloads is not collected or driven anywhere.\n if (!request || request.method === 'shipping' || !input.hasPhysicalLine) return { kind: 'shipping' }\n const nowMs = input.nowMs ?? Date.now()\n\n if (request.method === 'pickup') {\n const location = store.pickupLocations.find((entry) => entry.id === request.locationId)\n if (!location) {\n return {\n kind: 'refusal',\n status: 409,\n error: 'That pickup location is no longer available. Choose another.',\n changed: 'pickup',\n }\n }\n return {\n kind: 'pickup',\n location,\n metadata: {\n fulfillment: 'pickup',\n pickupLocationId: location.id,\n ...(request.textPhone ? { orderTextPhone: request.textPhone } : {}),\n },\n }\n }\n\n const delivery = store.delivery\n if (!CommerceModel.localDeliveryOffered(delivery)) {\n return {\n kind: 'refusal',\n status: 409,\n error: 'Local delivery is not available right now. Choose shipping or pickup.',\n changed: 'delivery',\n }\n }\n const country = String(delivery.country)\n const postalCode = CommerceModel.normalizePostcode(\n request.postalCode ?? request.address?.postalCode,\n country,\n )\n if (!postalCode) {\n return { kind: 'refusal', status: 400, error: 'Enter your postal code for delivery.' }\n }\n let zone = CommerceModel.matchLocalDeliveryZone(delivery, { country, postalCode })\n if (!zone && CommerceModel.localDeliveryHasRadiusZones(delivery)) {\n if (!request.address?.line1) {\n return { kind: 'refusal', status: 400, error: 'Enter your street address for delivery.' }\n }\n const coordinates = await placeDeliveryAddress(input.hostId, {\n ...request.address,\n postalCode,\n country,\n })\n zone = CommerceModel.matchLocalDeliveryZone(delivery, { country, postalCode, coordinates })\n }\n if (!zone) {\n return {\n kind: 'refusal',\n status: 409,\n error: `We don’t deliver to ${postalCode}. Choose shipping or pickup instead.`,\n }\n }\n const shortfall = CommerceModel.localDeliveryMinimumShortfall(zone, input.itemsCents)\n if (shortfall > 0) {\n return {\n kind: 'refusal',\n status: 409,\n error:\n `Delivery to ${postalCode} needs an order of at least ${money(zone.minimumCents ?? 0)}. ` +\n `Add ${money(shortfall)} more, or choose shipping or pickup.`,\n }\n }\n const window = CommerceModel.findLocalDeliveryWindow(delivery, request.windowStartMs, {\n nowMs,\n timeZone: store.timeZone,\n })\n if (!window) {\n return {\n kind: 'refusal',\n status: 409,\n error: 'That delivery time is no longer available. Choose another.',\n changed: 'delivery',\n }\n }\n const feeCents = CommerceModel.localDeliveryFeeCents(zone, input.itemsCents)\n const label = CommerceModel.formatLocalDeliveryWindow(window, store.timeZone)\n return {\n kind: 'local_delivery',\n zone,\n window,\n feeCents,\n country,\n ...(delivery.locationId ? { locationId: delivery.locationId } : {}),\n option: {\n rateId: LOCAL_DELIVERY_RATE_ID,\n name: `Local delivery — ${label}`.slice(0, 100),\n amountCents: feeCents,\n },\n metadata: {\n fulfillment: 'local_delivery',\n deliveryZoneId: zone.id,\n deliveryWindowStartMs: String(window.startMs),\n deliveryWindowEndMs: String(window.endMs),\n deliveryPostalCode: postalCode,\n deliveryFeeCents: String(feeCents),\n ...(delivery.locationId ? { deliveryLocationId: delivery.locationId } : {}),\n ...(request.textPhone ? { orderTextPhone: request.textPhone } : {}),\n },\n }\n}\n\n/** Sets a plan's metadata on a session's form body. */\nexport function appendLocalFulfillmentMetadata(params: URLSearchParams, metadata: Record<string, string>): void {\n for (const [key, value] of Object.entries(metadata)) {\n params.set(`metadata[${key}]`, String(value).slice(0, 480))\n }\n}\n\n/** The location a plan takes its stock from, if any. */\nexport function localFulfillmentLocationId(plan: LocalFulfillmentPlan): string | undefined {\n if (plan.kind === 'pickup') return plan.location.id\n if (plan.kind === 'local_delivery') return plan.locationId\n return undefined\n}\n\n/** A stock refusal for a plan that reserves at one location (AGL-3624). */\nexport function localStockRefusalMessage(plan: LocalFulfillmentPlan, productName: string): string {\n const what = productName ? `\"${productName}\"` : 'An item in your cart'\n if (plan.kind === 'pickup') {\n return `${what} is not in stock at ${plan.location.name}. Choose another location, or have it shipped.`\n }\n return `${what} is not in stock for local delivery. Choose pickup at another location, or have it shipped.`\n}\n\n// ---------------------------------------------------------------------------\n// The order, as the webhook records it\n// ---------------------------------------------------------------------------\n\n/** What a paid session's declaration becomes on its order. */\nexport interface OrderLocalFulfillment {\n fields: {\n fulfillmentMethod: CommerceModel.OrderFulfillmentMethod\n pickup?: CommerceModel.OrderPickup\n localDelivery?: CommerceModel.OrderLocalDelivery\n locationId?: string\n /** The number the buyer asked to be texted at, at the cart. */\n customerPhone?: string\n } & CommerceModel.OrderLocalFulfillmentListFields\n /** The line the receipt adds, so the buyer knows where and when. */\n summary: string\n /** The address entered at payment is outside the zone the fee was charged for. */\n outsideZone: boolean\n}\n\n/**\n * Reads a paid session's `fulfillment` metadata into order fields, or `null`\n * for a shipped order. The location and zone are re-read so the order keeps\n * their names as they were when the buyer paid.\n */\nexport async function orderLocalFulfillmentFromSession(input: {\n hostId: string\n hostRef: any\n metadata: Record<string, unknown> | null | undefined\n /** The address the buyer entered at payment, when the session collected one. */\n shippingAddress?: { postalCode?: string | null; country?: string | null } | null\n createdAtMs: number\n}): Promise<OrderLocalFulfillment | null> {\n const metadata = input.metadata ?? {}\n const method = metadata['fulfillment']\n if (method !== 'pickup' && method !== 'local_delivery') return null\n const textPhone = normalizePhone(String(metadata['orderTextPhone'] ?? ''))\n const phone = textPhone ? { customerPhone: textPhone } : {}\n const org = await getOrgForHost(input.hostId).catch(() => null)\n const store = await readLocalFulfillmentStore({\n hostRef: input.hostRef,\n org: (org?.org ?? null) as { timeZone?: string } | null,\n })\n\n if (method === 'pickup') {\n const locationId = String(metadata['pickupLocationId'] ?? '')\n let location = store.pickupLocations.find((entry) => entry.id === locationId)\n // A location renamed or switched off after the buyer paid: the order\n // still goes there, under the name the location doc now has.\n if (!location && locationId) {\n const snapshot = await input.hostRef\n .collection('locations')\n .doc(locationId)\n .get()\n .catch(() => null)\n const data = (snapshot?.exists ? snapshot.data() : null) as CommerceModel.InventoryLocation | null\n location = {\n id: locationId,\n name: String(data?.name || 'the store'),\n ...(data\n ? { address: CommerceModel.formatPostalAddressLine(data.postalAddress, data.address) || undefined }\n : {}),\n }\n }\n if (!location) return null\n const pickup: CommerceModel.OrderPickup = {\n locationId: location.id,\n locationName: location.name,\n ...(location.address ? { address: location.address } : {}),\n ...(location.instructions ? { instructions: location.instructions } : {}),\n ...(location.hours ? { hours: location.hours } : {}),\n status: 'preparing',\n updatedAtMs: input.createdAtMs,\n }\n const order = { fulfillmentMethod: 'pickup' as const, pickup, createdAtMs: input.createdAtMs }\n const list = CommerceModel.orderLocalFulfillmentListFields(order) as CommerceModel.OrderLocalFulfillmentListFields\n const ready = CommerceModel.pickupReadyWithinLabel(location.readyWithinMinutes)\n return {\n fields: { fulfillmentMethod: 'pickup', pickup, locationId: location.id, ...phone, ...list },\n summary: [\n `Pickup at ${location.name}${location.address ? `, ${location.address}` : ''}.`,\n `We’ll let you know when it’s ready${ready ? ` (${ready.toLowerCase()})` : ''}.`,\n location.instructions ?? '',\n ]\n .filter(Boolean)\n .join(' '),\n outsideZone: false,\n }\n }\n\n const zoneId = String(metadata['deliveryZoneId'] ?? '')\n const zone = (store.delivery.zones ?? []).find((entry) => entry.id === zoneId)\n const windowStartMs = Number(metadata['deliveryWindowStartMs']) || 0\n const windowEndMs = Number(metadata['deliveryWindowEndMs']) || windowStartMs\n const declared = String(metadata['deliveryPostalCode'] ?? '')\n const country = store.delivery.country ?? String(input.shippingAddress?.country ?? '')\n const entered = CommerceModel.normalizePostcode(input.shippingAddress?.postalCode, country)\n // The fee was charged for the code the buyer declared. The address they\n // then typed at payment must be in a zone too — a zone of ANY fee, since\n // the store delivers there — or the store is told.\n const outsideZone = Boolean(\n entered &&\n entered !== declared &&\n !CommerceModel.matchLocalDeliveryZone(store.delivery, { country, postalCode: entered }),\n )\n const windowLabel = windowStartMs\n ? CommerceModel.formatLocalDeliveryWindow({ startMs: windowStartMs, endMs: windowEndMs }, store.timeZone)\n : ''\n const locationId = String(metadata['deliveryLocationId'] ?? '') || undefined\n const localDelivery: CommerceModel.OrderLocalDelivery = {\n zoneId,\n zoneName: zone?.name ?? 'Local delivery',\n feeCents: Math.max(0, Math.round(Number(metadata['deliveryFeeCents']) || 0)),\n windowStartMs,\n windowEndMs,\n ...(windowLabel ? { windowLabel } : {}),\n ...(declared ? { postalCode: declared } : {}),\n ...(locationId ? { locationId } : {}),\n status: 'scheduled',\n ...(outsideZone ? { addressOutsideZone: true } : {}),\n updatedAtMs: input.createdAtMs,\n }\n const order = { fulfillmentMethod: 'local_delivery' as const, localDelivery, createdAtMs: input.createdAtMs }\n const list = CommerceModel.orderLocalFulfillmentListFields(order) as CommerceModel.OrderLocalFulfillmentListFields\n return {\n fields: {\n fulfillmentMethod: 'local_delivery',\n localDelivery,\n ...(locationId ? { locationId } : {}),\n ...phone,\n ...list,\n },\n summary: [\n `Local delivery${windowLabel ? `: ${windowLabel}` : ''}.`,\n store.delivery.instructions ?? '',\n ]\n .filter(Boolean)\n .join(' '),\n outsideZone,\n }\n}\n\n// ---------------------------------------------------------------------------\n// The cart's question: what can this basket be?\n// ---------------------------------------------------------------------------\n\n/** Distance zones place an address through a provider: a visitor may ask this often. */\nconst PLACE_LIMIT = { limit: 20, windowMs: 60 * 60 * 1000 }\n\n/**\n * `POST commerce/local-fulfillment-options` (AGL-3624): what the cart offers\n * besides shipping — the pickup locations and, with a postal code, the\n * delivery zone's fee, minimum and the windows open to book.\n *\n * Public, because a shopper asks it; it returns only what a storefront would\n * print anyway (location names, addresses, hours, fees). It reads the\n * visitor's own cart through the cart cookie and offers nothing for a\n * basket with nothing physical in it.\n */\nexport const localFulfillmentOptionsHandler: PluginApiHandler = async (req, res) => {\n if (req.method !== 'POST' && req.method !== 'GET') {\n return res.status(405).json({ error: 'Method not allowed' })\n }\n let body: Record<string, any>\n try {\n body =\n req.method === 'POST'\n ? typeof req.body === 'string'\n ? JSON.parse(req.body || '{}')\n : (req.body ?? {})\n : ((req.query ?? {}) as Record<string, any>)\n } catch {\n return res.status(400).json({ error: 'Invalid request' })\n }\n const hostId = String(body.hostId ?? '')\n if (!isDocumentId(hostId)) return res.status(400).json({ error: 'Missing or invalid hostId' })\n const nothing = { pickup: [], delivery: null }\n try {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = firestore.collection('hosts').doc(hostId)\n const ownerOrg = await getOrgForHost(hostId).catch(() => null)\n if (!ownerOrg?.org || !Aglyn.checkEntitlement(ownerOrg.org as any, 'commerce')) {\n return res.status(200).json(nothing)\n }\n // The visitor's own basket decides whether there is anything to collect\n // — or, from a product's Buy button, that one product (AGL-3624).\n const buyNowProductId = typeof body.productId === 'string' ? body.productId : ''\n let cart: Pick<CommerceModel.HostCart, 'lines'>\n if (buyNowProductId) {\n if (!isDocumentId(buyNowProductId)) return res.status(400).json({ error: 'Invalid productId' })\n cart = {\n lines: [\n {\n productId: buyNowProductId,\n ...(typeof body.variantId === 'string' && body.variantId ? { variantId: body.variantId } : {}),\n // The add-ons chosen on the page (AGL-4100), priced below.\n ...(Array.isArray(body.modifiers)\n ? { modifiers: CommerceModel.modifierSelectionFrom(body.modifiers) }\n : {}),\n quantity: Math.min(99, Math.max(1, Math.round(Number(body.quantity) || 1))),\n },\n ],\n }\n } else {\n const cartId = readCartId(req.cookies, hostId)\n const cartSnapshot = cartId ? await hostRef.collection('carts').doc(cartId).get().catch(() => null) : null\n cart = (cartSnapshot?.data?.() as CommerceModel.HostCart | undefined) ?? { lines: [] }\n }\n const productIds = [...new Set((cart.lines ?? []).map((line) => line.productId))].slice(0, 50)\n const products = await Promise.all(\n productIds.map((id) =>\n hostRef\n .collection('products')\n .doc(id)\n .get()\n .catch(() => null),\n ),\n )\n let itemsCents = 0\n let hasPhysicalLine = false\n for (const line of cart.lines ?? []) {\n const snapshot = products[productIds.indexOf(line.productId)]\n if (!snapshot?.exists) continue\n const product = CommerceModel.liftLegacyProduct(snapshot.data() as any)\n const variant = line.variantId\n ? product.variants.find((item) => item.id === line.variantId)\n : product.variants[0]\n // With its add-ons (AGL-4100), as checkout prices the line.\n const chosen = CommerceModel.resolveWebsiteLineModifiers(product, line.modifiers, variant?.id)\n itemsCents +=\n (Math.round(Number(variant?.priceUsd ?? 0) * 100) + (chosen.ok ? chosen.extraCents : 0)) *\n (Number(line.quantity) || 0)\n if ((product.type ?? 'physical') === 'physical') hasPhysicalLine = true\n }\n if (!hasPhysicalLine) return res.status(200).json(nothing)\n\n const store = await readLocalFulfillmentStore({\n hostRef,\n org: ownerOrg.org as { timeZone?: string },\n })\n const nowMs = Date.now()\n let delivery: Record<string, unknown> | null = null\n if (CommerceModel.localDeliveryOffered(store.delivery)) {\n const settings = store.delivery\n const country = String(settings.country)\n const windows = CommerceModel.upcomingLocalDeliveryWindows(settings, { nowMs, timeZone: store.timeZone }).map(\n (window) => ({\n ...window,\n label: CommerceModel.formatLocalDeliveryWindow(window, store.timeZone),\n }),\n )\n const radius = CommerceModel.localDeliveryHasRadiusZones(settings)\n delivery = {\n country,\n ...(settings.instructions ? { instructions: settings.instructions } : {}),\n needsAddress: radius,\n windows,\n }\n const request = readLocalFulfillmentRequest({\n method: 'local_delivery',\n postalCode: body.postalCode,\n address: body.address,\n })\n const postalCode = CommerceModel.normalizePostcode(request?.postalCode ?? request?.address?.postalCode, country)\n if (postalCode) {\n let zone = CommerceModel.matchLocalDeliveryZone(settings, { country, postalCode })\n if (!zone && radius && request?.address?.line1) {\n const visitor = String(req.socket?.remoteAddress ?? '') || 'no-address'\n const allowed = await consumeRateLimit(`local-delivery:place:${hostId}:${visitor}`, PLACE_LIMIT)\n .then((result) => result.allowed)\n .catch(() => false)\n const coordinates = allowed\n ? await placeDeliveryAddress(hostId, { ...request.address, postalCode, country })\n : null\n zone = CommerceModel.matchLocalDeliveryZone(settings, { country, postalCode, coordinates })\n }\n delivery['quote'] = zone\n ? {\n zoneId: zone.id,\n zoneName: zone.name,\n feeCents: CommerceModel.localDeliveryFeeCents(zone, itemsCents),\n ...(zone.minimumCents ? { minimumCents: zone.minimumCents } : {}),\n ...(zone.freeOverCents ? { freeOverCents: zone.freeOverCents } : {}),\n shortfallCents: CommerceModel.localDeliveryMinimumShortfall(zone, itemsCents),\n }\n : { unavailable: `We don’t deliver to ${postalCode}.` }\n }\n }\n // Whether the cart may offer texts about the pickup or delivery: the\n // platform can send them and the store has not turned them off.\n const toggles = (await hostRef\n .collection('settings')\n .doc('store')\n .get()\n .then((snapshot: any) => snapshot?.get?.('buyerNotifications'))\n .catch(() => null)) as unknown\n const texts = Boolean(\n pluginSmsMessaging()?.isConfigured() && CommerceModel.buyerNotificationEnabled(toggles, 'texts'),\n )\n return res.status(200).json({ pickup: store.pickupLocations, delivery, texts })\n } catch (error) {\n console.error('[local-fulfillment] options failed', hostId, error)\n return res.status(200).json(nothing)\n }\n}\n"],"names":["resolveSiteTimeZone","normalizePhone","pluginSmsMessaging","pluginShippingRateQuoter","Aglyn","consumeRateLimit","firebaseAdmin","getOrgForHost","isDocumentId","CommerceModel","readCartId","readLocalFulfillmentRequest","value","raw","method","text","input","max","String","trim","slice","request","phone","textPhone","locationId","postalCode","start","Math","round","Number","isFinite","windowStartMs","address","parts","line1","line2","city","state","readLocalFulfillmentStore","hostRef","locationsSnapshot","settings","host","Promise","all","collection","limit","PICKUP_LOCATIONS_MAX","get","catch","storeSettings","undefined","resolve","doc","then","snapshot","data","locations","docs","map","id","pickupLocations","pickupLocationOptions","delivery","normalizeLocalDeliverySettings","timeZone","org","LOCAL_DELIVERY_GEOCODE_TIMEOUT_MS","placeDeliveryAddress","hostId","quoter","validateAddress","timer","placed","race","available","check","country","verdict","coordinates","setTimeout","clearTimeout","LOCAL_DELIVERY_RATE_ID","money","cents","formatOrderMoney","planLocalFulfillment","store","hasPhysicalLine","kind","nowMs","Date","now","location","find","entry","status","error","changed","metadata","fulfillment","pickupLocationId","orderTextPhone","localDeliveryOffered","normalizePostcode","zone","matchLocalDeliveryZone","localDeliveryHasRadiusZones","shortfall","localDeliveryMinimumShortfall","itemsCents","minimumCents","window","findLocalDeliveryWindow","feeCents","localDeliveryFeeCents","label","formatLocalDeliveryWindow","option","rateId","name","amountCents","deliveryZoneId","deliveryWindowStartMs","startMs","deliveryWindowEndMs","endMs","deliveryPostalCode","deliveryFeeCents","deliveryLocationId","appendLocalFulfillmentMetadata","params","key","Object","entries","set","localFulfillmentLocationId","plan","localStockRefusalMessage","productName","what","orderLocalFulfillmentFromSession","customerPhone","exists","formatPostalAddressLine","postalAddress","pickup","locationName","instructions","hours","updatedAtMs","createdAtMs","order","fulfillmentMethod","list","orderLocalFulfillmentListFields","ready","pickupReadyWithinLabel","readyWithinMinutes","fields","summary","toLowerCase","filter","Boolean","join","outsideZone","zoneId","zones","windowEndMs","declared","shippingAddress","entered","windowLabel","localDelivery","zoneName","addressOutsideZone","PLACE_LIMIT","windowMs","localFulfillmentOptionsHandler","req","res","body","json","JSON","parse","query","nothing","cart","firestore","app","ownerOrg","checkEntitlement","buyNowProductId","productId","lines","variantId","Array","isArray","modifiers","modifierSelectionFrom","quantity","min","cartSnapshot","cartId","cookies","productIds","Set","line","products","product","indexOf","liftLegacyProduct","variant","variants","item","chosen","resolveWebsiteLineModifiers","priceUsd","ok","extraCents","type","windows","upcomingLocalDeliveryWindows","radius","needsAddress","visitor","socket","remoteAddress","allowed","result","freeOverCents","shortfallCents","unavailable","toggles","texts","isConfigured","buyerNotificationEnabled","console"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,mBAAmB,QAAQ,+CAA8C;AAClF,SAASC,cAAc,QAAQ,oDAAmD;AAClF,SAASC,kBAAkB,QAAQ,mDAAkD;AACrF,SAASC,wBAAwB,QAAQ,oDAAmD;AAE5F,YAAYC,WAAW,sBAAqB;AAC5C,SAASC,gBAAgB,EAAEC,aAAa,EAAEC,aAAa,QAAQ,2BAA0B;AACzF,SAASC,YAAY,QAAQ,8CAA6C;AAC1E,YAAYC,mBAAmB,oBAAU;AACzC,SAASC,UAAU,QAAQ,mBAAe;AAiD1C,8EAA8E,GAC9E,OAAO,SAASC,4BAA4BC,KAAc;IACxD,IAAI,CAACA,SAAS,OAAOA,UAAU,UAAU,OAAO;IAChD,MAAMC,MAAMD;IACZ,MAAME,SAASD,GAAG,CAAC,SAAS;IAC5B,IAAIC,WAAW,YAAYA,WAAW,kBAAkB,OAAO;IAC/D,MAAMC,OAAO,CAACC,OAAgBC,MAAgBC,OAAOF,gBAAAA,QAAS,IAAIG,IAAI,GAAGC,KAAK,CAAC,GAAGH;IAClF,MAAMI,UAAmC;QAAEP;IAAO;IAClD,MAAMQ,QAAQrB,eAAec,KAAKF,GAAG,CAAC,YAAY,EAAE;IACpD,IAAIS,OAAOD,QAAQE,SAAS,GAAGD;IAC/B,IAAIR,WAAW,UAAU;QACvBO,QAAQG,UAAU,GAAGT,KAAKF,GAAG,CAAC,aAAa,EAAE;QAC7C,OAAOQ;IACT;IACA,MAAMI,aAAaV,KAAKF,GAAG,CAAC,aAAa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C5D,OAAOmF,IAAIpF,MAAM,CAAC,KAAKsF,IAAI,CAACI;IAC9B;AACF,EAAC"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/server/local-fulfillment.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { resolveSiteTimeZone } from '@aglyn/aglyn/app-utils/collection-entry-date'\nimport { normalizePhone } from '@aglyn/aglyn/foundation/definitions/contact.types'\nimport { pluginSmsMessaging } from '@aglyn/aglyn/plugin-manager/plugin-sms-messaging'\nimport { pluginShippingRateQuoter } from '@aglyn/aglyn/plugin-manager/plugin-shipping-rates'\nimport type { PluginApiHandler } from '@aglyn/aglyn/server'\nimport * as Aglyn from '@aglyn/aglyn/server'\nimport { consumeRateLimit, firebaseAdmin, getOrgForHost } from '@aglyn/tenant-data-admin'\nimport { isDocumentId } from '@aglyn/tenant-data-admin/server/document-id'\nimport * as CommerceModel from '../model'\nimport { readCartId } from './cart-cookie'\n\n/**\n * Buy online, pick up in store, and local delivery at checkout (AGL-3624).\n *\n * The buyer chooses at the CART, before payment — the only place the store\n * still controls what is offered: a Stripe Checkout Session prices shipping\n * from the options it is created with and asks nothing back. So the cart\n * declares one of three ways, and this module turns the declaration into the\n * session it needs, or refuses it in words the cart can show:\n *\n * - `pickup` at one location: no shipping charged, no address collected,\n * the stock reserved at that location, and the order routed there.\n * - `local_delivery`: the store's OWN driver (not a marketplace courier —\n * that is the delivery-apps plugin, which never reads this). The postal\n * code — or, for a distance zone, the address — picks a zone, the zone\n * prices the fee and enforces its minimum, and the buyer books a window.\n * The session carries the fee as its one shipping option and collects an\n * address only in the store's country.\n * - `shipping`: everything as it was.\n *\n * A DECLARATION IS A REQUEST, NEVER A PRICE. The cart sends a location id, a\n * postal code and a window; the fee, the minimum and whether the window is\n * still open are all decided here from the store's own settings, on every\n * attempt.\n *\n * DISTANCE ZONES need both ends on a map, and the repository has no geocoder\n * of its own: the destination is placed by the shipping plugin's address\n * check (core's `core.shipping-rate-quoter` seam) when its provider returns a\n * position. Where none does, a distance zone matches nothing and the buyer is\n * told the store does not deliver there; postal-code zones need nothing.\n */\n\n/** What the cart declares. */\nexport interface LocalFulfillmentRequest {\n method: CommerceModel.OrderFulfillmentMethod\n locationId?: string\n postalCode?: string\n windowStartMs?: number\n /** The street address, asked only when the store has distance zones. */\n address?: { line1?: string; line2?: string; city?: string; state?: string; postalCode?: string }\n /**\n * The mobile number the buyer gave AT THE CART for texts about this pickup\n * or delivery (AGL-3624), in E.164. The only phone an online order's texts\n * go to: asked for that purpose, never taken from an address.\n */\n textPhone?: string\n}\n\n/** Reads the cart's declaration; `null` when it made none (a shipped order). */\nexport function readLocalFulfillmentRequest(value: unknown): LocalFulfillmentRequest | null {\n if (!value || typeof value !== 'object') return null\n const raw = value as Record<string, unknown>\n const method = raw['method']\n if (method !== 'pickup' && method !== 'local_delivery') return null\n const text = (input: unknown, max: number) => String(input ?? '').trim().slice(0, max)\n const request: LocalFulfillmentRequest = { method }\n const phone = normalizePhone(text(raw['textPhone'], 32))\n if (phone) request.textPhone = phone\n if (method === 'pickup') {\n request.locationId = text(raw['locationId'], 120)\n return request\n }\n const postalCode = text(raw['postalCode'], 16)\n if (postalCode) request.postalCode = postalCode\n const start = Math.round(Number(raw['windowStartMs']))\n if (Number.isFinite(start) && start > 0) request.windowStartMs = start\n const address = raw['address']\n if (address && typeof address === 'object') {\n const parts = address as Record<string, unknown>\n request.address = {\n line1: text(parts['line1'], 120),\n line2: text(parts['line2'], 120),\n city: text(parts['city'], 80),\n state: text(parts['state'], 40),\n postalCode: text(parts['postalCode'], 16) || postalCode,\n }\n }\n return request\n}\n\n/** The store's pickup locations and delivery settings, read once per attempt. */\nexport interface LocalFulfillmentStore {\n pickupLocations: CommerceModel.PickupLocationOption[]\n delivery: CommerceModel.LocalDeliverySettings\n timeZone: string\n}\n\n/** Reads what checkout and the cart need. Never throws: a failed read offers nothing. */\nexport async function readLocalFulfillmentStore(input: {\n hostRef: any\n /** `settings/store` as already read, to save a read. */\n storeSettings?: Record<string, unknown> | null\n org?: { timeZone?: string } | null\n host?: { timeZone?: string } | null\n}): Promise<LocalFulfillmentStore> {\n const { hostRef } = input\n const [locationsSnapshot, settings, host] = await Promise.all([\n hostRef\n .collection('locations')\n .limit(CommerceModel.PICKUP_LOCATIONS_MAX)\n .get()\n .catch(() => null),\n input.storeSettings !== undefined\n ? Promise.resolve(input.storeSettings)\n : hostRef\n .collection('settings')\n .doc('store')\n .get()\n .then((snapshot: any) => (snapshot?.data?.() ?? null) as Record<string, unknown> | null)\n .catch(() => null),\n input.host !== undefined\n ? Promise.resolve(input.host)\n : hostRef\n .get()\n .then((snapshot: any) => (snapshot?.data?.() ?? null) as { timeZone?: string } | null)\n .catch(() => null),\n ])\n const locations = (locationsSnapshot?.docs ?? []).map((snapshot: any) => ({\n ...(snapshot.data() as CommerceModel.InventoryLocation),\n id: String(snapshot.id),\n }))\n return {\n pickupLocations: CommerceModel.pickupLocationOptions(locations),\n delivery: CommerceModel.normalizeLocalDeliverySettings(settings?.['localDelivery']),\n timeZone: resolveSiteTimeZone(input.org ?? null, host ?? null),\n }\n}\n\n/** How long checkout waits for an address to be placed on a map. */\nexport const LOCAL_DELIVERY_GEOCODE_TIMEOUT_MS = 3_000\n\n/**\n * Places an address on the map through the shipping plugin's address check,\n * or answers `null`: no plugin, not available on this site, no position in\n * the answer, or slower than the timeout. Never throws.\n */\nexport async function placeDeliveryAddress(\n hostId: string,\n address: { line1?: string; line2?: string; city?: string; state?: string; postalCode?: string; country: string },\n): Promise<CommerceModel.LocalDeliveryCoordinates | null> {\n if (!address.line1 || !address.postalCode) return null\n const quoter = pluginShippingRateQuoter()\n if (!quoter?.validateAddress) return null\n let timer: ReturnType<typeof setTimeout> | undefined\n try {\n const placed = await Promise.race([\n (async () => {\n if (!(await quoter.available(hostId))) return null\n const check = await quoter.validateAddress?.(hostId, {\n line1: address.line1,\n ...(address.line2 ? { line2: address.line2 } : {}),\n ...(address.city ? { city: address.city } : {}),\n ...(address.state ? { state: address.state } : {}),\n postalCode: address.postalCode,\n country: address.country,\n } as never)\n return check?.verdict === 'invalid' ? null : (check?.coordinates ?? null)\n })().catch(() => null),\n new Promise<null>((resolve) => {\n timer = setTimeout(() => resolve(null), LOCAL_DELIVERY_GEOCODE_TIMEOUT_MS)\n }),\n ])\n return placed ?? null\n } finally {\n if (timer) clearTimeout(timer)\n }\n}\n\n/** The shipping option id a local delivery fee is declared under. */\nexport const LOCAL_DELIVERY_RATE_ID = 'local-delivery'\n\nexport type LocalFulfillmentPlan =\n | { kind: 'shipping' }\n | {\n kind: 'refusal'\n status: 400 | 409\n error: string\n /** What the cart should re-ask: the options changed under the buyer. */\n changed?: 'pickup' | 'delivery'\n }\n | {\n kind: 'pickup'\n location: CommerceModel.PickupLocationOption\n metadata: Record<string, string>\n }\n | {\n kind: 'local_delivery'\n zone: CommerceModel.LocalDeliveryZone\n window: CommerceModel.LocalDeliveryWindow\n feeCents: number\n country: string\n locationId?: string\n option: CommerceModel.ResolvedShippingRate\n metadata: Record<string, string>\n }\n\nconst money = (cents: number) => CommerceModel.formatOrderMoney(cents, 'USD')\n\n/**\n * Turns the cart's declaration into what the session needs, or a refusal.\n * Taken BEFORE any reservation or Stripe call, so a refusal costs nothing\n * and the buyer's same button works once they choose again.\n */\nexport async function planLocalFulfillment(input: {\n hostId: string\n request: LocalFulfillmentRequest | null\n /** The goods, pre-discount — what a minimum and a free-over threshold read. */\n itemsCents: number\n hasPhysicalLine: boolean\n store: LocalFulfillmentStore\n nowMs?: number\n}): Promise<LocalFulfillmentPlan> {\n const { request, store } = input\n // A cart of downloads is not collected or driven anywhere.\n if (!request || request.method === 'shipping' || !input.hasPhysicalLine) return { kind: 'shipping' }\n const nowMs = input.nowMs ?? Date.now()\n\n if (request.method === 'pickup') {\n const location = store.pickupLocations.find((entry) => entry.id === request.locationId)\n if (!location) {\n return {\n kind: 'refusal',\n status: 409,\n error: 'That pickup location is no longer available. Choose another.',\n changed: 'pickup',\n }\n }\n return {\n kind: 'pickup',\n location,\n metadata: {\n fulfillment: 'pickup',\n pickupLocationId: location.id,\n ...(request.textPhone ? { orderTextPhone: request.textPhone } : {}),\n },\n }\n }\n\n const delivery = store.delivery\n if (!CommerceModel.localDeliveryOffered(delivery)) {\n return {\n kind: 'refusal',\n status: 409,\n error: 'Local delivery is not available right now. Choose shipping or pickup.',\n changed: 'delivery',\n }\n }\n const country = String(delivery.country)\n const postalCode = CommerceModel.normalizePostcode(\n request.postalCode ?? request.address?.postalCode,\n country,\n )\n if (!postalCode) {\n return { kind: 'refusal', status: 400, error: 'Enter your postal code for delivery.' }\n }\n let zone = CommerceModel.matchLocalDeliveryZone(delivery, { country, postalCode })\n if (!zone && CommerceModel.localDeliveryHasRadiusZones(delivery)) {\n if (!request.address?.line1) {\n return { kind: 'refusal', status: 400, error: 'Enter your street address for delivery.' }\n }\n const coordinates = await placeDeliveryAddress(input.hostId, {\n ...request.address,\n postalCode,\n country,\n })\n zone = CommerceModel.matchLocalDeliveryZone(delivery, { country, postalCode, coordinates })\n }\n if (!zone) {\n return {\n kind: 'refusal',\n status: 409,\n error: `We don’t deliver to ${postalCode}. Choose shipping or pickup instead.`,\n }\n }\n const shortfall = CommerceModel.localDeliveryMinimumShortfall(zone, input.itemsCents)\n if (shortfall > 0) {\n return {\n kind: 'refusal',\n status: 409,\n error:\n `Delivery to ${postalCode} needs an order of at least ${money(zone.minimumCents ?? 0)}. ` +\n `Add ${money(shortfall)} more, or choose shipping or pickup.`,\n }\n }\n const window = CommerceModel.findLocalDeliveryWindow(delivery, request.windowStartMs, {\n nowMs,\n timeZone: store.timeZone,\n })\n if (!window) {\n return {\n kind: 'refusal',\n status: 409,\n error: 'That delivery time is no longer available. Choose another.',\n changed: 'delivery',\n }\n }\n const feeCents = CommerceModel.localDeliveryFeeCents(zone, input.itemsCents)\n const label = CommerceModel.formatLocalDeliveryWindow(window, store.timeZone)\n return {\n kind: 'local_delivery',\n zone,\n window,\n feeCents,\n country,\n ...(delivery.locationId ? { locationId: delivery.locationId } : {}),\n option: {\n rateId: LOCAL_DELIVERY_RATE_ID,\n name: `Local delivery — ${label}`.slice(0, 100),\n amountCents: feeCents,\n },\n metadata: {\n fulfillment: 'local_delivery',\n deliveryZoneId: zone.id,\n deliveryWindowStartMs: String(window.startMs),\n deliveryWindowEndMs: String(window.endMs),\n deliveryPostalCode: postalCode,\n deliveryFeeCents: String(feeCents),\n ...(delivery.locationId ? { deliveryLocationId: delivery.locationId } : {}),\n ...(request.textPhone ? { orderTextPhone: request.textPhone } : {}),\n },\n }\n}\n\n/** Sets a plan's metadata on a session's form body. */\nexport function appendLocalFulfillmentMetadata(params: URLSearchParams, metadata: Record<string, string>): void {\n for (const [key, value] of Object.entries(metadata)) {\n params.set(`metadata[${key}]`, String(value).slice(0, 480))\n }\n}\n\n/** The location a plan takes its stock from, if any. */\nexport function localFulfillmentLocationId(plan: LocalFulfillmentPlan): string | undefined {\n if (plan.kind === 'pickup') return plan.location.id\n if (plan.kind === 'local_delivery') return plan.locationId\n return undefined\n}\n\n/** A stock refusal for a plan that reserves at one location (AGL-3624). */\nexport function localStockRefusalMessage(plan: LocalFulfillmentPlan, productName: string): string {\n const what = productName ? `\"${productName}\"` : 'An item in your cart'\n if (plan.kind === 'pickup') {\n return `${what} is not in stock at ${plan.location.name}. Choose another location, or have it shipped.`\n }\n return `${what} is not in stock for local delivery. Choose pickup at another location, or have it shipped.`\n}\n\n// ---------------------------------------------------------------------------\n// The order, as the webhook records it\n// ---------------------------------------------------------------------------\n\n/** What a paid session's declaration becomes on its order. */\nexport interface OrderLocalFulfillment {\n fields: {\n fulfillmentMethod: CommerceModel.OrderFulfillmentMethod\n pickup?: CommerceModel.OrderPickup\n localDelivery?: CommerceModel.OrderLocalDelivery\n locationId?: string\n /** The number the buyer asked to be texted at, at the cart. */\n customerPhone?: string\n } & CommerceModel.OrderLocalFulfillmentListFields\n /** The line the receipt adds, so the buyer knows where and when. */\n summary: string\n /** The address entered at payment is outside the zone the fee was charged for. */\n outsideZone: boolean\n}\n\n/**\n * Reads a paid session's `fulfillment` metadata into order fields, or `null`\n * for a shipped order. The location and zone are re-read so the order keeps\n * their names as they were when the buyer paid.\n */\nexport async function orderLocalFulfillmentFromSession(input: {\n hostId: string\n hostRef: any\n metadata: Record<string, unknown> | null | undefined\n /** The address the buyer entered at payment, when the session collected one. */\n shippingAddress?: { postalCode?: string | null; country?: string | null } | null\n createdAtMs: number\n}): Promise<OrderLocalFulfillment | null> {\n const metadata = input.metadata ?? {}\n const method = metadata['fulfillment']\n if (method !== 'pickup' && method !== 'local_delivery') return null\n const textPhone = normalizePhone(String(metadata['orderTextPhone'] ?? ''))\n const phone = textPhone ? { customerPhone: textPhone } : {}\n const org = await getOrgForHost(input.hostId).catch(() => null)\n const store = await readLocalFulfillmentStore({\n hostRef: input.hostRef,\n org: (org?.org ?? null) as { timeZone?: string } | null,\n })\n\n if (method === 'pickup') {\n const locationId = String(metadata['pickupLocationId'] ?? '')\n let location = store.pickupLocations.find((entry) => entry.id === locationId)\n // A location renamed or switched off after the buyer paid: the order\n // still goes there, under the name the location doc now has.\n if (!location && locationId) {\n const snapshot = await input.hostRef\n .collection('locations')\n .doc(locationId)\n .get()\n .catch(() => null)\n const data = (snapshot?.exists ? snapshot.data() : null) as CommerceModel.InventoryLocation | null\n location = {\n id: locationId,\n name: String(data?.name || 'the store'),\n ...(data\n ? { address: CommerceModel.formatPostalAddressLine(data.postalAddress, data.address) || undefined }\n : {}),\n }\n }\n if (!location) return null\n const pickup: CommerceModel.OrderPickup = {\n locationId: location.id,\n locationName: location.name,\n ...(location.address ? { address: location.address } : {}),\n ...(location.instructions ? { instructions: location.instructions } : {}),\n ...(location.hours ? { hours: location.hours } : {}),\n status: 'preparing',\n updatedAtMs: input.createdAtMs,\n }\n const order = { fulfillmentMethod: 'pickup' as const, pickup, createdAtMs: input.createdAtMs }\n const list = CommerceModel.orderLocalFulfillmentListFields(order) as CommerceModel.OrderLocalFulfillmentListFields\n const ready = CommerceModel.pickupReadyWithinLabel(location.readyWithinMinutes)\n return {\n fields: { fulfillmentMethod: 'pickup', pickup, locationId: location.id, ...phone, ...list },\n summary: [\n `Pickup at ${location.name}${location.address ? `, ${location.address}` : ''}.`,\n `We’ll let you know when it’s ready${ready ? ` (${ready.toLowerCase()})` : ''}.`,\n location.instructions ?? '',\n ]\n .filter(Boolean)\n .join(' '),\n outsideZone: false,\n }\n }\n\n const zoneId = String(metadata['deliveryZoneId'] ?? '')\n const zone = (store.delivery.zones ?? []).find((entry) => entry.id === zoneId)\n const windowStartMs = Number(metadata['deliveryWindowStartMs']) || 0\n const windowEndMs = Number(metadata['deliveryWindowEndMs']) || windowStartMs\n const declared = String(metadata['deliveryPostalCode'] ?? '')\n const country = store.delivery.country ?? String(input.shippingAddress?.country ?? '')\n const entered = CommerceModel.normalizePostcode(input.shippingAddress?.postalCode, country)\n // The fee was charged for the code the buyer declared. The address they\n // then typed at payment must be in a zone too — a zone of ANY fee, since\n // the store delivers there — or the store is told.\n const outsideZone = Boolean(\n entered &&\n entered !== declared &&\n !CommerceModel.matchLocalDeliveryZone(store.delivery, { country, postalCode: entered }),\n )\n const windowLabel = windowStartMs\n ? CommerceModel.formatLocalDeliveryWindow({ startMs: windowStartMs, endMs: windowEndMs }, store.timeZone)\n : ''\n const locationId = String(metadata['deliveryLocationId'] ?? '') || undefined\n const localDelivery: CommerceModel.OrderLocalDelivery = {\n zoneId,\n zoneName: zone?.name ?? 'Local delivery',\n feeCents: Math.max(0, Math.round(Number(metadata['deliveryFeeCents']) || 0)),\n windowStartMs,\n windowEndMs,\n ...(windowLabel ? { windowLabel } : {}),\n ...(declared ? { postalCode: declared } : {}),\n ...(locationId ? { locationId } : {}),\n status: 'scheduled',\n ...(outsideZone ? { addressOutsideZone: true } : {}),\n updatedAtMs: input.createdAtMs,\n }\n const order = { fulfillmentMethod: 'local_delivery' as const, localDelivery, createdAtMs: input.createdAtMs }\n const list = CommerceModel.orderLocalFulfillmentListFields(order) as CommerceModel.OrderLocalFulfillmentListFields\n return {\n fields: {\n fulfillmentMethod: 'local_delivery',\n localDelivery,\n ...(locationId ? { locationId } : {}),\n ...phone,\n ...list,\n },\n summary: [\n `Local delivery${windowLabel ? `: ${windowLabel}` : ''}.`,\n store.delivery.instructions ?? '',\n ]\n .filter(Boolean)\n .join(' '),\n outsideZone,\n }\n}\n\n// ---------------------------------------------------------------------------\n// The cart's question: what can this basket be?\n// ---------------------------------------------------------------------------\n\n/** Distance zones place an address through a provider: a visitor may ask this often. */\nconst PLACE_LIMIT = { limit: 20, windowMs: 60 * 60 * 1000 }\n\n/**\n * `POST commerce/local-fulfillment-options` (AGL-3624): what the cart offers\n * besides shipping — the pickup locations and, with a postal code, the\n * delivery zone's fee, minimum and the windows open to book.\n *\n * Public, because a shopper asks it; it returns only what a storefront would\n * print anyway (location names, addresses, hours, fees). It reads the\n * visitor's own cart through the cart cookie and offers nothing for a\n * basket with nothing physical in it.\n */\nexport const localFulfillmentOptionsHandler: PluginApiHandler = async (req, res) => {\n if (req.method !== 'POST' && req.method !== 'GET') {\n return res.status(405).json({ error: 'Method not allowed' })\n }\n let body: Record<string, any>\n try {\n body =\n req.method === 'POST'\n ? typeof req.body === 'string'\n ? JSON.parse(req.body || '{}')\n : (req.body ?? {})\n : ((req.query ?? {}) as Record<string, any>)\n } catch {\n return res.status(400).json({ error: 'Invalid request' })\n }\n const hostId = String(body.hostId ?? '')\n if (!isDocumentId(hostId)) return res.status(400).json({ error: 'Missing or invalid hostId' })\n const nothing = { pickup: [], delivery: null }\n try {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = firestore.collection('hosts').doc(hostId)\n const ownerOrg = await getOrgForHost(hostId).catch(() => null)\n if (!ownerOrg?.org || !Aglyn.checkEntitlement(ownerOrg.org as any, 'commerce')) {\n return res.status(200).json(nothing)\n }\n // The visitor's own basket decides whether there is anything to collect\n // — or, from a product's Buy button, that one product (AGL-3624).\n const buyNowProductId = typeof body.productId === 'string' ? body.productId : ''\n let cart: Pick<CommerceModel.HostCart, 'lines'>\n if (buyNowProductId) {\n if (!isDocumentId(buyNowProductId)) return res.status(400).json({ error: 'Invalid productId' })\n cart = {\n lines: [\n {\n productId: buyNowProductId,\n ...(typeof body.variantId === 'string' && body.variantId ? { variantId: body.variantId } : {}),\n // The add-ons chosen on the page (AGL-4100), priced below.\n ...(Array.isArray(body.modifiers)\n ? { modifiers: CommerceModel.modifierSelectionFrom(body.modifiers) }\n : {}),\n quantity: Math.min(CommerceModel.CART_MAX_QUANTITY, Math.max(1, Math.round(Number(body.quantity) || 1))),\n },\n ],\n }\n } else {\n const cartId = readCartId(req.cookies, hostId)\n const cartSnapshot = cartId ? await hostRef.collection('carts').doc(cartId).get().catch(() => null) : null\n cart = (cartSnapshot?.data?.() as CommerceModel.HostCart | undefined) ?? { lines: [] }\n }\n const productIds = [...new Set((cart.lines ?? []).map((line) => line.productId))].slice(0, 50)\n const products = await Promise.all(\n productIds.map((id) =>\n hostRef\n .collection('products')\n .doc(id)\n .get()\n .catch(() => null),\n ),\n )\n let itemsCents = 0\n let hasPhysicalLine = false\n for (const line of cart.lines ?? []) {\n const snapshot = products[productIds.indexOf(line.productId)]\n if (!snapshot?.exists) continue\n const product = CommerceModel.liftLegacyProduct(snapshot.data() as any)\n const variant = line.variantId\n ? product.variants.find((item) => item.id === line.variantId)\n : product.variants[0]\n // With its add-ons (AGL-4100), as checkout prices the line.\n const chosen = CommerceModel.resolveWebsiteLineModifiers(product, line.modifiers, variant?.id)\n itemsCents +=\n (Math.round(Number(variant?.priceUsd ?? 0) * 100) + (chosen.ok ? chosen.extraCents : 0)) *\n (Number(line.quantity) || 0)\n if ((product.type ?? 'physical') === 'physical') hasPhysicalLine = true\n }\n if (!hasPhysicalLine) return res.status(200).json(nothing)\n\n const store = await readLocalFulfillmentStore({\n hostRef,\n org: ownerOrg.org as { timeZone?: string },\n })\n const nowMs = Date.now()\n let delivery: Record<string, unknown> | null = null\n if (CommerceModel.localDeliveryOffered(store.delivery)) {\n const settings = store.delivery\n const country = String(settings.country)\n const windows = CommerceModel.upcomingLocalDeliveryWindows(settings, { nowMs, timeZone: store.timeZone }).map(\n (window) => ({\n ...window,\n label: CommerceModel.formatLocalDeliveryWindow(window, store.timeZone),\n }),\n )\n const radius = CommerceModel.localDeliveryHasRadiusZones(settings)\n delivery = {\n country,\n ...(settings.instructions ? { instructions: settings.instructions } : {}),\n needsAddress: radius,\n windows,\n }\n const request = readLocalFulfillmentRequest({\n method: 'local_delivery',\n postalCode: body.postalCode,\n address: body.address,\n })\n const postalCode = CommerceModel.normalizePostcode(request?.postalCode ?? request?.address?.postalCode, country)\n if (postalCode) {\n let zone = CommerceModel.matchLocalDeliveryZone(settings, { country, postalCode })\n if (!zone && radius && request?.address?.line1) {\n const visitor = String(req.socket?.remoteAddress ?? '') || 'no-address'\n const allowed = await consumeRateLimit(`local-delivery:place:${hostId}:${visitor}`, PLACE_LIMIT)\n .then((result) => result.allowed)\n .catch(() => false)\n const coordinates = allowed\n ? await placeDeliveryAddress(hostId, { ...request.address, postalCode, country })\n : null\n zone = CommerceModel.matchLocalDeliveryZone(settings, { country, postalCode, coordinates })\n }\n delivery['quote'] = zone\n ? {\n zoneId: zone.id,\n zoneName: zone.name,\n feeCents: CommerceModel.localDeliveryFeeCents(zone, itemsCents),\n ...(zone.minimumCents ? { minimumCents: zone.minimumCents } : {}),\n ...(zone.freeOverCents ? { freeOverCents: zone.freeOverCents } : {}),\n shortfallCents: CommerceModel.localDeliveryMinimumShortfall(zone, itemsCents),\n }\n : { unavailable: `We don’t deliver to ${postalCode}.` }\n }\n }\n // Whether the cart may offer texts about the pickup or delivery: the\n // platform can send them and the store has not turned them off.\n const toggles = (await hostRef\n .collection('settings')\n .doc('store')\n .get()\n .then((snapshot: any) => snapshot?.get?.('buyerNotifications'))\n .catch(() => null)) as unknown\n const texts = Boolean(\n pluginSmsMessaging()?.isConfigured() && CommerceModel.buyerNotificationEnabled(toggles, 'texts'),\n )\n return res.status(200).json({ pickup: store.pickupLocations, delivery, texts })\n } catch (error) {\n console.error('[local-fulfillment] options failed', hostId, error)\n return res.status(200).json(nothing)\n }\n}\n"],"names":["resolveSiteTimeZone","normalizePhone","pluginSmsMessaging","pluginShippingRateQuoter","Aglyn","consumeRateLimit","firebaseAdmin","getOrgForHost","isDocumentId","CommerceModel","readCartId","readLocalFulfillmentRequest","value","raw","method","text","input","max","String","trim","slice","request","phone","textPhone","locationId","postalCode","start","Math","round","Number","isFinite","windowStartMs","address","parts","line1","line2","city","state","readLocalFulfillmentStore","hostRef","locationsSnapshot","settings","host","Promise","all","collection","limit","PICKUP_LOCATIONS_MAX","get","catch","storeSettings","undefined","resolve","doc","then","snapshot","data","locations","docs","map","id","pickupLocations","pickupLocationOptions","delivery","normalizeLocalDeliverySettings","timeZone","org","LOCAL_DELIVERY_GEOCODE_TIMEOUT_MS","placeDeliveryAddress","hostId","quoter","validateAddress","timer","placed","race","available","check","country","verdict","coordinates","setTimeout","clearTimeout","LOCAL_DELIVERY_RATE_ID","money","cents","formatOrderMoney","planLocalFulfillment","store","hasPhysicalLine","kind","nowMs","Date","now","location","find","entry","status","error","changed","metadata","fulfillment","pickupLocationId","orderTextPhone","localDeliveryOffered","normalizePostcode","zone","matchLocalDeliveryZone","localDeliveryHasRadiusZones","shortfall","localDeliveryMinimumShortfall","itemsCents","minimumCents","window","findLocalDeliveryWindow","feeCents","localDeliveryFeeCents","label","formatLocalDeliveryWindow","option","rateId","name","amountCents","deliveryZoneId","deliveryWindowStartMs","startMs","deliveryWindowEndMs","endMs","deliveryPostalCode","deliveryFeeCents","deliveryLocationId","appendLocalFulfillmentMetadata","params","key","Object","entries","set","localFulfillmentLocationId","plan","localStockRefusalMessage","productName","what","orderLocalFulfillmentFromSession","customerPhone","exists","formatPostalAddressLine","postalAddress","pickup","locationName","instructions","hours","updatedAtMs","createdAtMs","order","fulfillmentMethod","list","orderLocalFulfillmentListFields","ready","pickupReadyWithinLabel","readyWithinMinutes","fields","summary","toLowerCase","filter","Boolean","join","outsideZone","zoneId","zones","windowEndMs","declared","shippingAddress","entered","windowLabel","localDelivery","zoneName","addressOutsideZone","PLACE_LIMIT","windowMs","localFulfillmentOptionsHandler","req","res","body","json","JSON","parse","query","nothing","cart","firestore","app","ownerOrg","checkEntitlement","buyNowProductId","productId","lines","variantId","Array","isArray","modifiers","modifierSelectionFrom","quantity","min","CART_MAX_QUANTITY","cartSnapshot","cartId","cookies","productIds","Set","line","products","product","indexOf","liftLegacyProduct","variant","variants","item","chosen","resolveWebsiteLineModifiers","priceUsd","ok","extraCents","type","windows","upcomingLocalDeliveryWindows","radius","needsAddress","visitor","socket","remoteAddress","allowed","result","freeOverCents","shortfallCents","unavailable","toggles","texts","isConfigured","buyerNotificationEnabled","console"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,mBAAmB,QAAQ,+CAA8C;AAClF,SAASC,cAAc,QAAQ,oDAAmD;AAClF,SAASC,kBAAkB,QAAQ,mDAAkD;AACrF,SAASC,wBAAwB,QAAQ,oDAAmD;AAE5F,YAAYC,WAAW,sBAAqB;AAC5C,SAASC,gBAAgB,EAAEC,aAAa,EAAEC,aAAa,QAAQ,2BAA0B;AACzF,SAASC,YAAY,QAAQ,8CAA6C;AAC1E,YAAYC,mBAAmB,oBAAU;AACzC,SAASC,UAAU,QAAQ,mBAAe;AAiD1C,8EAA8E,GAC9E,OAAO,SAASC,4BAA4BC,KAAc;IACxD,IAAI,CAACA,SAAS,OAAOA,UAAU,UAAU,OAAO;IAChD,MAAMC,MAAM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,UAAW,MAAMnM,QACpBM,UAAU,CAAC,YACXQ,GAAG,CAAC,SACJL,GAAG,GACHM,IAAI,CAAC,CAACC;gBAAkBA;mBAAAA,6BAAAA,gBAAAA,SAAUP,GAAG,qBAAbO,mBAAAA,UAAgB;WACxCN,KAAK,CAAC,IAAM;QACf,MAAM0L,QAAQxE,QACZjK,EAAAA,sBAAAA,yCAAAA,oBAAsB0O,YAAY,OAAMnO,cAAcoO,wBAAwB,CAACH,SAAS;QAE1F,OAAOtD,IAAIpF,MAAM,CAAC,KAAKsF,IAAI,CAAC;YAAEpC,QAAQ3D,MAAM1B,eAAe;YAAEE;YAAU4K;QAAM;IAC/E,EAAE,OAAO1I,OAAO;QACd6I,QAAQ7I,KAAK,CAAC,sCAAsC5B,QAAQ4B;QAC5D,OAAOmF,IAAIpF,MAAM,CAAC,KAAKsF,IAAI,CAACI;IAC9B;AACF,EAAC"}
|
|
@@ -31,7 +31,7 @@ export interface PosCellularBillingDeps {
|
|
|
31
31
|
readBilling?: (orgId: string) => Promise<{
|
|
32
32
|
stripeCustomerId?: unknown;
|
|
33
33
|
}>;
|
|
34
|
-
turnOff?: (configurationId: string) => Promise<boolean>;
|
|
34
|
+
turnOff?: (configurationId: string, stripeAccount?: string | null) => Promise<boolean>;
|
|
35
35
|
alert?: typeof raiseOperatorAlert;
|
|
36
36
|
/** Stripe's word on a charge's invoice now, or null when it cannot be asked. */
|
|
37
37
|
invoiceStatus?: InvoiceStatus;
|
|
@@ -63,15 +63,16 @@ export declare function runPosCellularBilling(gate: PluginJobHostGate, deps?: Po
|
|
|
63
63
|
* goes:
|
|
64
64
|
*
|
|
65
65
|
* 1. CHARGE IT. The month the workspace is deleted in is charged now, for
|
|
66
|
-
* each S710 that had 4G in it
|
|
66
|
+
* each S710 on the platform that had 4G in it (one on a store's own
|
|
67
|
+
* Stripe account is Stripe's to bill), as the monthly run would have charged it a
|
|
67
68
|
* month later; and last month too, if the run had not charged it yet.
|
|
68
69
|
* Through the run's own claim and key, so the two never charge one month
|
|
69
70
|
* twice. A workspace that is locked is not charged (its card may be the
|
|
70
71
|
* stolen one the lock is about): staff are alerted instead. Any failure
|
|
71
72
|
* alerts staff and goes on.
|
|
72
73
|
* 2. TURN IT OFF. Every store of the workspace with 4G on has cellular turned
|
|
73
|
-
* off at Stripe,
|
|
74
|
-
*
|
|
74
|
+
* off at Stripe, on the account it is on, so nobody keeps paying for
|
|
75
|
+
* readers of a workspace that is gone, and their periods are closed.
|
|
75
76
|
*
|
|
76
77
|
* Never fails the erasure: a refusal is a must alert. A dry run only counts.
|
|
77
78
|
*/
|
|
@@ -20,7 +20,7 @@ import { firebaseAdmin, raiseOperatorAlert, readOrgBilling } from "@aglyn/tenant
|
|
|
20
20
|
import { chargeOrgUsageInvoice, USAGE_INVOICE_MIN_CHARGE_CENTS } from "@aglyn/tenant-data-admin/server/usage-invoice";
|
|
21
21
|
import { BUNDLE_ID } from "../constants/bundle-common.js";
|
|
22
22
|
import { COMMERCE_POS_CELLULAR_CHARGE_FAILED, COMMERCE_POS_CELLULAR_TURN_OFF_FAILED, COMMERCE_POS_CELLULAR_TURNED_OFF_UNPAID } from "../constants/operator-alerts.js";
|
|
23
|
-
import { closePosCellularPeriod, parsePosCellularConfig, posCellularChargeCents, posCellularGraceEndsAtMs, posCellularMonthBounds, posCellularMonthKey, posCellularMonthLabel, posCellularOverdue, posCellularPreviousMonthKey, posCellularReadersByWorkspace, posCellularWarningDue, POS_CELLULAR_CHARGE_KIND, POS_CELLULAR_CHARGES_COLLECTION, POS_CELLULAR_READERS_COLLECTION, POS_CELLULAR_SETTLED_STATES } from "../model/pos-cellular.js";
|
|
23
|
+
import { closePosCellularPeriod, parsePosCellularConfig, posCellularBilledToPlatform, posCellularChargeCents, posCellularGraceEndsAtMs, posCellularMonthBounds, posCellularMonthKey, posCellularMonthLabel, posCellularOverdue, posCellularPreviousMonthKey, posCellularReadersByWorkspace, posCellularWarningDue, POS_CELLULAR_CHARGE_KIND, POS_CELLULAR_CHARGES_COLLECTION, POS_CELLULAR_READERS_COLLECTION, POS_CELLULAR_SETTLED_STATES } from "../model/pos-cellular.js";
|
|
24
24
|
import { posCellularInvoiceStatus, settleSuspendedPosCellularCharge, turnOffPosCellularConfiguration } from "./pos-cellular.js";
|
|
25
25
|
/*==========================================
|
|
26
26
|
* THE MONTHLY 4G CHARGE (AGL-4038).
|
|
@@ -69,6 +69,14 @@ import { posCellularInvoiceStatus, settleSuspendedPosCellularCharge, turnOffPosC
|
|
|
69
69
|
* (and last month too, if the run had not charged it yet), so the two never
|
|
70
70
|
* charge one month twice. A failure there alerts staff and never holds the
|
|
71
71
|
* erasure.
|
|
72
|
+
*
|
|
73
|
+
* A STORE ON ITS OWN STRIPE ACCOUNT (a reader record with
|
|
74
|
+
* `stripeConfigurationAccount`) is billed for its 4G by Stripe, on that
|
|
75
|
+
* account, so none of the charging above counts it
|
|
76
|
+
* (`posCellularReadersByWorkspace`), and no charge here can warn about it or
|
|
77
|
+
* turn its 4G off: an unpaid month suspends only the workspace's stores on
|
|
78
|
+
* the platform. A gone store and an erased workspace still turn it off at
|
|
79
|
+
* Stripe, on the account it is on.
|
|
72
80
|
*=========================================*/ const LABEL = '[pos-cellular] billing';
|
|
73
81
|
/** A charge claimed this long ago and not settled was abandoned by its run. */ const CLAIM_STALE_MS = 30 * 60 * 1000;
|
|
74
82
|
/** How many times a charge that never reached Stripe is tried before staff hear of it. */ const ALERT_AFTER_ATTEMPTS = 3;
|
|
@@ -131,7 +139,8 @@ function chargesRef() {
|
|
|
131
139
|
let off = 0;
|
|
132
140
|
let failed = 0;
|
|
133
141
|
for (const [configurationId, record] of configurations){
|
|
134
|
-
|
|
142
|
+
var _record_stripeConfigurationAccount;
|
|
143
|
+
const done = await deps.turnOff(configurationId, (_record_stripeConfigurationAccount = record.stripeConfigurationAccount) != null ? _record_stripeConfigurationAccount : null).catch(()=>false);
|
|
135
144
|
if (done) {
|
|
136
145
|
off += 1;
|
|
137
146
|
continue;
|
|
@@ -470,10 +479,11 @@ async function settleMonth(orgId, month, patch, now) {
|
|
|
470
479
|
const charge = await claimWarning(id, now);
|
|
471
480
|
if (!charge) return false;
|
|
472
481
|
const told = await (async ()=>{
|
|
473
|
-
// The stores that have 4G now, which lose it on that
|
|
482
|
+
// The stores that have 4G on the platform now, which lose it on that
|
|
483
|
+
// day. A store on its own Stripe account keeps it whatever happens here.
|
|
474
484
|
const snapshot = await recordsRef().where('orgId', '==', charge.orgId).where('enabled', '==', true).limit(500).get();
|
|
475
485
|
const hostIds = [
|
|
476
|
-
...new Set(snapshot.docs.map((doc)=>doc.data().hostId))
|
|
486
|
+
...new Set(snapshot.docs.map((doc)=>doc.data()).filter(posCellularBilledToPlatform).map((record)=>record.hostId))
|
|
477
487
|
];
|
|
478
488
|
return context.warn({
|
|
479
489
|
orgId: charge.orgId,
|
|
@@ -512,10 +522,11 @@ async function settleMonth(orgId, month, patch, now) {
|
|
|
512
522
|
});
|
|
513
523
|
}
|
|
514
524
|
/**
|
|
515
|
-
* 4G off at Stripe for every store of the workspace that still has it
|
|
516
|
-
* store's settings saying why, its readers' periods closed,
|
|
517
|
-
* told once.
|
|
518
|
-
*
|
|
525
|
+
* 4G off at Stripe for every store of the workspace that still has it on the
|
|
526
|
+
* platform, each store's settings saying why, its readers' periods closed,
|
|
527
|
+
* and the workspace told once. A store on its own Stripe account is never
|
|
528
|
+
* touched: its 4G is not part of the charge. Safe to run again: a store
|
|
529
|
+
* already off and a notice already sent are left alone.
|
|
519
530
|
*/ async function finishSuspension(id, charge, context) {
|
|
520
531
|
var _charge_suspendedHostIds, _charge_suspendedHostIds1;
|
|
521
532
|
const { now } = context;
|
|
@@ -523,7 +534,7 @@ async function settleMonth(orgId, month, patch, now) {
|
|
|
523
534
|
const records = snapshot.docs.map((doc)=>({
|
|
524
535
|
id: String(doc.id),
|
|
525
536
|
record: doc.data()
|
|
526
|
-
}));
|
|
537
|
+
})).filter(({ record })=>posCellularBilledToPlatform(record));
|
|
527
538
|
const hostIds = new Set((_charge_suspendedHostIds = charge.suspendedHostIds) != null ? _charge_suspendedHostIds : []);
|
|
528
539
|
if (records.length) {
|
|
529
540
|
await turnOffConfigurations(records.map(({ record })=>record), {
|
|
@@ -581,15 +592,16 @@ async function settleMonth(orgId, month, patch, now) {
|
|
|
581
592
|
* goes:
|
|
582
593
|
*
|
|
583
594
|
* 1. CHARGE IT. The month the workspace is deleted in is charged now, for
|
|
584
|
-
* each S710 that had 4G in it
|
|
595
|
+
* each S710 on the platform that had 4G in it (one on a store's own
|
|
596
|
+
* Stripe account is Stripe's to bill), as the monthly run would have charged it a
|
|
585
597
|
* month later; and last month too, if the run had not charged it yet.
|
|
586
598
|
* Through the run's own claim and key, so the two never charge one month
|
|
587
599
|
* twice. A workspace that is locked is not charged (its card may be the
|
|
588
600
|
* stolen one the lock is about): staff are alerted instead. Any failure
|
|
589
601
|
* alerts staff and goes on.
|
|
590
602
|
* 2. TURN IT OFF. Every store of the workspace with 4G on has cellular turned
|
|
591
|
-
* off at Stripe,
|
|
592
|
-
*
|
|
603
|
+
* off at Stripe, on the account it is on, so nobody keeps paying for
|
|
604
|
+
* readers of a workspace that is gone, and their periods are closed.
|
|
593
605
|
*
|
|
594
606
|
* Never fails the erasure: a refusal is a must alert. A dry run only counts.
|
|
595
607
|
*/ export function createPosCellularOrgEraser(deps = {}) {
|