@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/returns.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 { holdsCommerceSiteKey } from './commerce-site-keys'\nimport { commerceSitePermissionRefusal } from '../model/plugin-permissions'\nimport * as Aglyn from '@aglyn/aglyn/server'\nimport { createResourceUid } from '@aglyn/aglyn/app-utils/create-resource-uid'\nimport type { PluginApiHandler, PluginApiRequest, PluginApiResponse } from '@aglyn/aglyn/server'\nimport {\n findUserByUidAcrossPools,\n firebaseAdmin,\n getOrgForHost,\n hostSendingIdentity,\n meterHostEmail,\n notifyHostManagers,\n renderHostEmailWithTokens,\n} from '@aglyn/tenant-data-admin'\nimport { hostConsoleUrl } from '@aglyn/tenant-data-admin/server/host-console-link'\nimport { isEmailConfigured, sendEmail } from '@aglyn/shared-util-email'\nimport * as CommerceModel from '../model'\nimport {\n RETURN_APPROVED_EVENT,\n RETURN_DECLINED_EVENT,\n RETURN_RECEIVED_EVENT,\n RETURN_REFUNDED_EVENT,\n RETURN_REQUESTED_EVENT,\n} from '../model/order-events'\nimport { raiseOrderEvent, stageOrderEvent } from './order-events'\nimport { orderStatusUrl, verifyOrderStatusToken } from './order-status-token'\nimport { readActiveMemberSession } from './membership'\nimport { resolveTrackedRestockLines } from './restock-flag'\nimport { refundHandler } from './refund'\n\n/**\n * Returns (RMA, AGL-3611): the buyer's door, the merchant's door, and the\n * writes behind both. The rules — what is returnable, the window, the state\n * machine, what a refund is worth — are the model's (`commerce-returns.ts`);\n * this is where they are re-asked under the write.\n *\n * EVERY WRITE IS A TRANSACTION that reads the return, the order and, when it\n * opens one, every return already on the order — so two requests for the last\n * unit cannot both be granted, and a stale console cannot approve a return a\n * colleague declined a moment ago.\n *\n * THE MONEY MOVES THROUGH THE ORDER'S REFUND ROUTE, never here. A return's\n * refund calls `refund.ts` with the merchant's own credentials and a key\n * derived from the return, so its gates (an admin of the whole workspace,\n * the over-refund cap, the open-dispute block, Stripe's idempotency) are the\n * ones every refund passes, and asking twice refunds once.\n *\n * STOCK COMES BACK WHEN THE PARCEL DOES. Receiving a return puts the units\n * the merchant chose back on the shelf, at the location they chose, with an\n * inventory adjustment row — the products hub's history shows it beside the\n * sale. A refund then answers the order's restock question with what the\n * return already did, so nobody is asked to restock the same units twice.\n */\n\ntype Firestore = FirebaseFirestore.Firestore\n\nconst hostRefFor = (firestore: Firestore, hostId: string) => firestore.collection('hosts').doc(hostId)\n\nconst isId = (value: string) => Boolean(value) && value.length <= 200 && !/^__.*__$/.test(value) && !value.includes('/')\n\n/** The store's settings document and what the return emails need from the host. */\nasync function readStore(firestore: Firestore, hostId: string) {\n const hostRef = hostRefFor(firestore, hostId)\n const [host, store] = await Promise.all([\n hostRef.get().catch(() => null),\n hostRef.collection('settings').doc('store').get().catch(() => null),\n ])\n const settings = (store?.data() ?? {}) as Record<string, unknown>\n return {\n host: (host?.data() ?? {}) as Record<string, unknown>,\n returnSettings: CommerceModel.readReturnSettings(settings['returns']),\n currency: String(settings['currency'] ?? 'USD').toUpperCase(),\n }\n}\n\nfunction money(cents: number, currency: string): string {\n try {\n return new Intl.NumberFormat('en-US', { style: 'currency', currency }).format(cents / 100)\n } catch {\n return `$${(cents / 100).toFixed(2)}`\n }\n}\n\n/** The return as an event carries it. */\nfunction returnEventView(id: string, entry: CommerceModel.HostReturn) {\n return {\n id,\n status: entry.status,\n lines: entry.lines.map((line) => ({ ...line })),\n refundCents: entry.refundCents ?? null,\n restock: entry.restock ? entry.restock.lines.map((line) => ({ ...line })) : null,\n }\n}\n\n/** \"1× House Blend — Arrived damaged\", one per line. */\nfunction returnItemsText(order: CommerceModel.HostOrder, entry: Pick<CommerceModel.HostReturn, 'lines'>): string {\n return entry.lines\n .map(\n (line) =>\n `${line.quantity}× ${order.lineItems?.[line.lineItemId]?.name ?? `line ${line.lineItemId}`} — ${\n CommerceModel.RETURN_REASON_LABELS[line.reason] ?? line.reason\n }`,\n )\n .join('\\n')\n}\n\nexport type OpenReturnOutcome =\n | { outcome: 'created'; returnId: string; entry: CommerceModel.HostReturn; order: CommerceModel.HostOrder }\n | { outcome: 'refused'; message: string }\n | { outcome: 'no_such_order' }\n\n/**\n * Opens a return. A buyer's starts `requested`; a merchant's starts\n * `approved`, since the merchant asking is the approval.\n */\nexport async function openReturn(request: {\n hostId: string\n orderId: string\n lines: ReadonlyArray<{ lineItemId: unknown; quantity: unknown; reason: unknown }>\n note?: string\n by: 'buyer' | 'merchant'\n now?: number\n}): Promise<OpenReturnOutcome> {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = hostRefFor(firestore, request.hostId)\n const orderRef = hostRef.collection('orders').doc(request.orderId)\n const { returnSettings } = await readStore(firestore, request.hostId)\n const now = request.now ?? Date.now()\n const note = String(request.note ?? '').trim().slice(0, CommerceModel.RETURN_NOTE_MAX)\n return firestore.runTransaction(async (transaction): Promise<OpenReturnOutcome> => {\n const orderSnapshot = await transaction.get(orderRef)\n if (!orderSnapshot.exists) return { outcome: 'no_such_order' }\n const existing = await transaction.get(\n hostRef.collection('returns').where('orderId', '==', request.orderId),\n )\n const order = CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never)\n const verdict = CommerceModel.validateReturnRequest({\n order,\n existing: existing.docs.map((entry) => entry.data() as CommerceModel.HostReturn),\n settings: returnSettings,\n lines: request.lines,\n by: request.by,\n now,\n })\n if (!('lines' in verdict)) {\n return { outcome: 'refused', message: CommerceModel.describeReturnRequestProblem(verdict) }\n }\n const status: CommerceModel.ReturnStatus = request.by === 'merchant' ? 'approved' : 'requested'\n const entry: CommerceModel.HostReturn = {\n orderId: request.orderId,\n orderNumber: CommerceModel.returnOrderNumber(order, request.orderId),\n customerEmail: order.customerEmail ?? null,\n customerName: order.customerName ?? null,\n lines: verdict.lines.map((line) => ({\n ...line,\n name: String(order.lineItems?.[line.lineItemId]?.name ?? '').slice(0, 200),\n })),\n status,\n requestedBy: request.by,\n ...(note ? (request.by === 'buyer' ? { customerNote: note } : { merchantNote: note }) : {}),\n timeline: [\n {\n atMs: now,\n event: status,\n detail: `${request.by === 'buyer' ? 'Requested by the buyer' : 'Opened by the store'}: ${CommerceModel.describeReturnLines(order, verdict.lines)}`,\n },\n ],\n createdAtMs: now,\n updatedAtMs: now,\n }\n const returnRef = hostRef.collection('returns').doc(createResourceUid())\n transaction.create(returnRef, entry)\n const orderPatch = {\n timeline: CommerceModel.appendOrderEvent(\n order,\n 'return-requested',\n CommerceModel.describeReturnLines(order, verdict.lines),\n now,\n ),\n }\n transaction.update(orderRef, orderPatch)\n stageOrderEvent(transaction, RETURN_REQUESTED_EVENT, {\n hostId: request.hostId,\n orderId: request.orderId,\n key: `return:${returnRef.id}`,\n order: { ...orderSnapshot.data(), ...orderPatch },\n extra: { return: returnEventView(returnRef.id, entry) },\n })\n // The store opening a return is its approval.\n if (status === 'approved') {\n stageOrderEvent(transaction, RETURN_APPROVED_EVENT, {\n hostId: request.hostId,\n orderId: request.orderId,\n key: `return-approved:${returnRef.id}`,\n order: { ...orderSnapshot.data(), ...orderPatch },\n extra: { return: returnEventView(returnRef.id, entry) },\n })\n }\n return { outcome: 'created', returnId: returnRef.id, entry, order }\n })\n}\n\nexport type ReturnAction = 'approve' | 'decline' | 'close' | 'note' | 'attach-label'\n\nexport type ChangeReturnOutcome =\n | { outcome: 'changed'; entry: CommerceModel.HostReturn; order: CommerceModel.HostOrder | null }\n | { outcome: 'already'; entry: CommerceModel.HostReturn }\n | { outcome: 'refused'; message: string }\n | { outcome: 'no_such_return' }\n\nconst ACTION_TARGET: Partial<Record<ReturnAction, CommerceModel.ReturnStatus>> = {\n approve: 'approved',\n decline: 'declined',\n close: 'closed',\n}\n\n/**\n * Approve, decline, close, a note, or a return label — the writes that move\n * no stock and no money. The transition is re-asked under the write.\n */\nexport async function changeReturn(request: {\n hostId: string\n returnId: string\n action: ReturnAction\n merchantNote?: string\n label?: { carrier: string; trackingNumber: string; labelUrl: string; trackingUrl?: string }\n now?: number\n}): Promise<ChangeReturnOutcome> {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = hostRefFor(firestore, request.hostId)\n const returnRef = hostRef.collection('returns').doc(request.returnId)\n const now = request.now ?? Date.now()\n const note = request.merchantNote === undefined ? undefined : String(request.merchantNote).trim().slice(0, CommerceModel.RETURN_NOTE_MAX)\n return firestore.runTransaction(async (transaction): Promise<ChangeReturnOutcome> => {\n const snapshot = await transaction.get(returnRef)\n if (!snapshot.exists) return { outcome: 'no_such_return' }\n const entry = snapshot.data() as CommerceModel.HostReturn\n const orderSnapshot = await transaction.get(hostRef.collection('orders').doc(entry.orderId))\n const order = orderSnapshot.exists ? CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never) : null\n const target = ACTION_TARGET[request.action]\n let patch: Partial<CommerceModel.HostReturn>\n if (target) {\n if (entry.status === target) return { outcome: 'already', entry }\n if (!CommerceModel.canTransitionReturn(entry.status, target)) {\n return {\n outcome: 'refused',\n message: `A return that is ${CommerceModel.RETURN_STATUS_LABELS[entry.status].toLowerCase()} cannot be ${CommerceModel.RETURN_STATUS_LABELS[target].toLowerCase()}`,\n }\n }\n patch = {\n status: target,\n ...(note !== undefined && note ? { merchantNote: note } : {}),\n timeline: CommerceModel.appendReturnEvent(entry, target, note || undefined, now),\n }\n } else if (request.action === 'attach-label') {\n const label = request.label\n if (!label?.labelUrl || !/^https:\\/\\//i.test(label.labelUrl)) {\n return { outcome: 'refused', message: 'A return label needs an https link' }\n }\n if (!CommerceModel.returnIsOpen(entry) || entry.status === 'refunded') {\n return { outcome: 'refused', message: 'This return is finished; a label can no longer be attached' }\n }\n const carrier = String(label.carrier ?? '').trim().slice(0, 40)\n const trackingNumber = String(label.trackingNumber ?? '').trim().slice(0, 60)\n const trackingUrl =\n (label.trackingUrl && /^https:\\/\\//i.test(label.trackingUrl) ? label.trackingUrl : null) ??\n CommerceModel.trackingUrlFor(carrier, trackingNumber)\n patch = {\n returnLabel: {\n carrier,\n trackingNumber,\n labelUrl: String(label.labelUrl).slice(0, 2048),\n ...(trackingUrl ? { trackingUrl } : {}),\n attachedAtMs: now,\n },\n timeline: CommerceModel.appendReturnEvent(entry, 'label', `${carrier} ${trackingNumber}`.trim() || 'Return label attached', now),\n }\n } else {\n patch = {\n merchantNote: note ?? '',\n timeline: CommerceModel.appendReturnEvent(entry, 'note', note || 'Note cleared', now),\n }\n }\n transaction.update(returnRef, { ...patch, updatedAtMs: now })\n const changed = { ...entry, ...patch, updatedAtMs: now }\n const event = target === 'approved' ? RETURN_APPROVED_EVENT : target === 'declined' ? RETURN_DECLINED_EVENT : null\n if (event && orderSnapshot.exists) {\n stageOrderEvent(transaction, event, {\n hostId: request.hostId,\n orderId: entry.orderId,\n key: `return-${target}:${request.returnId}`,\n order: orderSnapshot.data() ?? {},\n extra: { return: returnEventView(request.returnId, changed) },\n })\n }\n return { outcome: 'changed', entry: changed, order }\n })\n}\n\n/**\n * The parcel arrived: the return is `received`, and the units the merchant\n * chose go back on the shelf, at the location they chose. A unit the sale\n * never took off a tracked count (an untracked variant, a deleted product)\n * has nothing to go back to and is left out, as a cancellation leaves it.\n */\nexport async function receiveReturn(request: {\n hostId: string\n returnId: string\n restock: ReadonlyArray<{ lineItemId: unknown; quantity: unknown }>\n locationId?: string\n now?: number\n}): Promise<ChangeReturnOutcome & { units?: number }> {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = hostRefFor(firestore, request.hostId)\n const returnRef = hostRef.collection('returns').doc(request.returnId)\n const now = request.now ?? Date.now()\n const locationId = String(request.locationId ?? '').trim().slice(0, 100)\n return firestore.runTransaction(async (transaction) => {\n const snapshot = await transaction.get(returnRef)\n if (!snapshot.exists) return { outcome: 'no_such_return' as const }\n const entry = snapshot.data() as CommerceModel.HostReturn\n if (entry.status === 'received' || entry.restock) return { outcome: 'already' as const, entry }\n if (!CommerceModel.canTransitionReturn(entry.status, 'received')) {\n return {\n outcome: 'refused' as const,\n message: `A return that is ${CommerceModel.RETURN_STATUS_LABELS[entry.status].toLowerCase()} cannot be received`,\n }\n }\n // What may go back: at most what the return covers, per line.\n const covered = new Map(entry.lines.map((line) => [line.lineItemId, line.quantity]))\n const wanted = new Map<number, number>()\n for (const line of request.restock) {\n const lineItemId = Number(line?.lineItemId)\n const quantity = Number(line?.quantity)\n if (!covered.has(lineItemId)) {\n return { outcome: 'refused' as const, message: `Line ${lineItemId} is not part of this return` }\n }\n if (!Number.isInteger(quantity) || quantity < 0) {\n return { outcome: 'refused' as const, message: 'Restock quantities must be whole numbers' }\n }\n const total = (wanted.get(lineItemId) ?? 0) + quantity\n if (total > (covered.get(lineItemId) ?? 0)) {\n return { outcome: 'refused' as const, message: `Only ${covered.get(lineItemId)} of line ${lineItemId} came back` }\n }\n if (quantity > 0) wanted.set(lineItemId, total)\n }\n if (locationId && wanted.size > 0) {\n const location = await transaction.get(hostRef.collection('locations').doc(locationId))\n if (!location.exists) return { outcome: 'refused' as const, message: 'That stock location no longer exists' }\n }\n const orderRef = hostRef.collection('orders').doc(entry.orderId)\n const orderSnapshot = await transaction.get(orderRef)\n const order = orderSnapshot.exists ? CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never) : null\n // Every read before any write, as a transaction requires.\n const tracked =\n order && wanted.size > 0\n ? await resolveTrackedRestockLines(hostRef, order, (ref) => transaction.get(ref))\n : { lines: [], products: new Map<string, CommerceModel.HostProduct>() }\n const byProduct = new Map<string, Array<{ variantId: string; quantity: number }>>()\n for (const line of tracked.lines) {\n const quantity = Math.min(line.quantity, wanted.get(line.lineIndex ?? -1) ?? 0)\n if (quantity <= 0) continue\n byProduct.set(line.productId, [...(byProduct.get(line.productId) ?? []), { variantId: line.variantId, quantity }])\n }\n let units = 0\n for (const [productId, lines] of byProduct) {\n const product = tracked.products.get(productId)\n if (!product) continue\n let variants = product.variants\n for (const line of lines) {\n variants = CommerceModel.adjustVariantInventory({ variants }, line.variantId, line.quantity, locationId || undefined)\n }\n transaction.update(hostRef.collection('products').doc(productId), {\n variants,\n ...CommerceModel.productStockFields({ ...product, variants }),\n updatedAtMs: now,\n })\n for (const line of lines) {\n units += line.quantity\n transaction.create(hostRef.collection('inventoryAdjustments').doc(createResourceUid()), {\n productId,\n variantId: line.variantId,\n delta: line.quantity,\n reason: 'restock',\n orderId: entry.orderId,\n ...(locationId ? { locationId } : {}),\n atMs: now,\n } satisfies CommerceModel.InventoryAdjustment)\n }\n }\n const restock: CommerceModel.ReturnRestock = {\n lines: [...wanted.entries()].map(([lineItemId, quantity]) => ({ lineItemId, quantity })),\n ...(locationId ? { locationId } : {}),\n atMs: now,\n }\n const patch = {\n status: 'received' as const,\n restock,\n timeline: CommerceModel.appendReturnEvent(\n entry,\n 'received',\n units > 0 ? `${units} ${units === 1 ? 'unit' : 'units'} back in stock` : 'Nothing restocked',\n now,\n ),\n updatedAtMs: now,\n }\n transaction.update(returnRef, patch)\n const received = { ...entry, ...patch }\n if (order) {\n const orderPatch = {\n timeline: CommerceModel.appendOrderEvent(order, 'return-received', units > 0 ? `${units} restocked` : undefined, now),\n }\n transaction.update(orderRef, orderPatch)\n stageOrderEvent(transaction, RETURN_RECEIVED_EVENT, {\n hostId: request.hostId,\n orderId: entry.orderId,\n key: `return-received:${request.returnId}`,\n order: { ...orderSnapshot.data(), ...orderPatch },\n extra: { return: returnEventView(request.returnId, received) },\n })\n }\n return { outcome: 'changed' as const, entry: received, order, units }\n })\n}\n\n/**\n * Records a refund that went through the order's refund route against the\n * return, and answers the order's restock question with what the return\n * already did, so the merchant is not asked to restock the same units again.\n */\nexport async function markReturnRefunded(request: {\n hostId: string\n returnId: string\n refundCents: number\n uid: string\n now?: number\n}): Promise<ChangeReturnOutcome> {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = hostRefFor(firestore, request.hostId)\n const returnRef = hostRef.collection('returns').doc(request.returnId)\n const now = request.now ?? Date.now()\n return firestore.runTransaction(async (transaction): Promise<ChangeReturnOutcome> => {\n const snapshot = await transaction.get(returnRef)\n if (!snapshot.exists) return { outcome: 'no_such_return' }\n const entry = snapshot.data() as CommerceModel.HostReturn\n if (entry.status === 'refunded' || entry.refundedAtMs) return { outcome: 'already', entry }\n const orderRef = hostRef.collection('orders').doc(entry.orderId)\n const orderSnapshot = await transaction.get(orderRef)\n const order = orderSnapshot.exists ? CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never) : null\n const patch = {\n status: 'refunded' as const,\n refundedAtMs: now,\n refundCents: Math.max(0, Math.round(request.refundCents)),\n timeline: CommerceModel.appendReturnEvent(entry, 'refunded', undefined, now),\n updatedAtMs: now,\n }\n transaction.update(returnRef, patch)\n const check = order?.restockCheck\n if (order && check && !check.resolution && entry.restock) {\n const restocked = entry.restock.lines.some((line) => line.quantity > 0)\n transaction.update(orderRef, {\n restockCheck: {\n ...check,\n resolution: restocked ? 'restocked' : 'dismissed',\n resolvedAtMs: now,\n ...(request.uid ? { resolvedBy: request.uid } : {}),\n } satisfies CommerceModel.OrderRestockCheck,\n timeline: CommerceModel.appendOrderEvent(\n order,\n 'restock-check',\n restocked ? 'answered by the return — restocked' : 'answered by the return — no restock',\n now,\n ),\n })\n }\n return { outcome: 'changed', entry: { ...entry, ...patch }, order }\n })\n}\n\n/** Calls the order's own refund route in-process, with the caller's credentials. */\nasync function callRefundRoute(\n req: PluginApiRequest,\n body: Record<string, unknown>,\n idempotencyKey: string,\n): Promise<{ status: number; body: Record<string, any> }> {\n let status = 200\n let payload: Record<string, any> = {}\n const res = {\n status(code: number) {\n status = code\n return res\n },\n json(value: unknown) {\n payload = (value ?? {}) as Record<string, any>\n return res\n },\n send(value: unknown) {\n payload = { error: String(value ?? '') }\n return res\n },\n setHeader() {\n return res\n },\n end() {\n return res\n },\n } as unknown as PluginApiResponse\n await refundHandler(\n {\n ...req,\n method: 'POST',\n headers: { authorization: String(req.headers.authorization ?? ''), 'idempotency-key': idempotencyKey },\n body,\n } as PluginApiRequest,\n res,\n )\n return { status, body: payload }\n}\n\n// ---------------------------------------------------------------------------\n// Emails\n// ---------------------------------------------------------------------------\n\ntype ReturnEmailKey = 'return-requested' | 'return-approved' | 'return-declined' | 'return-refunded'\n\nasync function sendReturnEmail(\n hostId: string,\n key: ReturnEmailKey,\n to: string,\n tokens: Record<string, string>,\n fallback: { subject: string; text: string },\n): Promise<void> {\n if (!to || !isEmailConfigured()) return\n try {\n const firestore = firebaseAdmin.app().firestore()\n const designed = await renderHostEmailWithTokens(firestore, hostId, key, tokens)\n const org = await getOrgForHost(hostId).catch(() => null)\n await sendEmail({\n to,\n subject: designed?.subject ?? fallback.subject,\n text: designed?.text || fallback.text,\n ...(designed?.html ? { html: designed.html } : {}),\n fromName: Aglyn.resolveBrandingProfile(org?.org as never).fromName,\n sendingIdentity: await hostSendingIdentity(hostId),\n audience: 'tenant',\n context: key,\n // Owed to the recipient by their own order (AGL-3356).\n owedFor: 'order',\n })\n await meterHostEmail(hostId)\n } catch (error) {\n // A return that is recorded is not undone because its email failed.\n console.error(`[returns] ${key} not sent`, hostId, error)\n }\n}\n\n/** Tells the store a buyer asked, in the console and by email to the owner. */\nasync function tellStoreOfRequest(hostId: string, returnId: string, entry: CommerceModel.HostReturn, order: CommerceModel.HostOrder) {\n const items = returnItemsText(order, entry)\n await notifyHostManagers(\n hostId,\n {\n type: 'content.order',\n title: `Return requested for order ${entry.orderNumber}`,\n body: `${entry.customerEmail ?? 'A buyer'} wants to return ${CommerceModel.describeReturnLines(order, entry.lines)} from order ${entry.orderNumber} on {site}.`,\n link: `/${hostId}/products/returns?return=${encodeURIComponent(returnId)}`,\n },\n { skipOwnerEmail: true },\n ).catch(() => undefined)\n const ownerUid = (await getOrgForHost(hostId).catch(() => null))?.org?.ownerUid\n const owner = ownerUid ? (await findUserByUidAcrossPools(ownerUid).catch(() => null))?.record : null\n if (!owner?.email) return\n await sendReturnEmail(\n hostId,\n 'return-requested',\n owner.email,\n {\n 'order.number': entry.orderNumber,\n 'return.items': items,\n 'buyer.email': String(entry.customerEmail ?? ''),\n 'return.customerNote': String(entry.customerNote ?? ''),\n 'return.url': await hostConsoleUrl(\n hostId,\n `/${hostId}/products/returns?return=${encodeURIComponent(returnId)}`,\n ),\n },\n {\n subject: `Return requested for order ${entry.orderNumber}`,\n text:\n `${entry.customerEmail ?? 'A buyer'} wants to return items from order ${entry.orderNumber}.\\n\\n${items}` +\n (entry.customerNote ? `\\n\\n\"${entry.customerNote}\"` : '') +\n `\\n\\nApprove or decline it under Products → Returns.`,\n },\n )\n}\n\n/** Tells the buyer what happened to their return. */\nasync function tellBuyer(\n hostId: string,\n key: Exclude<ReturnEmailKey, 'return-requested'>,\n entry: CommerceModel.HostReturn,\n order: CommerceModel.HostOrder | null,\n): Promise<void> {\n const to = String(entry.customerEmail ?? order?.customerEmail ?? '').trim()\n if (!to || !order) return\n const firestore = firebaseAdmin.app().firestore()\n const store = await readStore(firestore, hostId)\n const statusUrl = orderStatusUrl(store.host as never, hostId, entry.orderId) ?? ''\n const items = returnItemsText(order, entry)\n const label = entry.returnLabel\n ? `Print your return label: ${entry.returnLabel.labelUrl}` +\n (entry.returnLabel.trackingNumber ? ` (${[entry.returnLabel.carrier, entry.returnLabel.trackingNumber].filter(Boolean).join(' ')})` : '')\n : ''\n const refundTotal = money(entry.refundCents ?? 0, store.currency)\n const tokens: Record<string, string> = {\n 'order.number': entry.orderNumber,\n 'return.items': items,\n 'return.note': String(entry.merchantNote ?? ''),\n 'return.label': label,\n 'return.refundTotal': refundTotal,\n 'order.statusUrl': statusUrl,\n }\n const fallback =\n key === 'return-approved'\n ? {\n subject: `Your return for order ${entry.orderNumber} is approved`,\n text: `Your return is approved:\\n\\n${items}` + (entry.merchantNote ? `\\n\\n${entry.merchantNote}` : '') + (label ? `\\n\\n${label}` : ''),\n }\n : key === 'return-declined'\n ? {\n subject: `About your return for order ${entry.orderNumber}`,\n text: `We could not accept the return of:\\n\\n${items}` + (entry.merchantNote ? `\\n\\n${entry.merchantNote}` : ''),\n }\n : {\n subject: `Your refund for order ${entry.orderNumber}`,\n text: `We received your return and refunded ${refundTotal}.\\n\\n${items}`,\n }\n if (statusUrl) fallback.text += `\\n\\nView your order: ${statusUrl}`\n await sendReturnEmail(hostId, key, to, tokens, fallback)\n}\n\n// ---------------------------------------------------------------------------\n// The buyer's door (tenant)\n// ---------------------------------------------------------------------------\n\n/**\n * Whether this request may act for the buyer of this order: the signed-in\n * site member whose email the order carries, or the holder of the order's\n * signed status link. Neither reveals whether the order exists to anyone\n * else — both refusals read the same.\n */\nasync function buyerMayActFor(\n req: PluginApiRequest,\n hostId: string,\n orderId: string,\n token: string,\n order: CommerceModel.HostOrder,\n): Promise<boolean> {\n if (token && verifyOrderStatusToken(hostId, orderId, token)) return true\n const session = await readActiveMemberSession(req, hostId)\n if (session.status !== 'active') return false\n const memberEmail = String(session.member.get('email') ?? '').trim().toLowerCase()\n return Boolean(memberEmail) && memberEmail === String(order.customerEmail ?? '').trim().toLowerCase()\n}\n\n/**\n * `GET /api/commerce/return-request?hostId&orderId&t` — what the buyer may\n * send back and until when, and the returns already open.\n * `POST` with `{ hostId, orderId, t?, lines: [{ lineItemId, quantity, reason }], note? }`\n * opens a return.\n */\nexport const returnRequestHandler: PluginApiHandler = async (req, res) => {\n if (req.method !== 'GET' && req.method !== 'POST') {\n return res.status(405).json({ error: 'Method not allowed' })\n }\n const input = (req.method === 'GET' ? req.query : typeof req.body === 'string' ? JSON.parse(req.body) : req.body) ?? {}\n const hostId = String(input.hostId ?? '')\n const orderId = String(input.orderId ?? input.o ?? '')\n const token = String(input.t ?? input.token ?? '').slice(0, 100)\n if (!isId(hostId) || !isId(orderId)) return res.status(400).json({ error: 'Missing hostId or orderId' })\n try {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = hostRefFor(firestore, hostId)\n const orderSnapshot = await hostRef.collection('orders').doc(orderId).get()\n const order = orderSnapshot.exists ? CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never) : null\n if (!order || !(await buyerMayActFor(req, hostId, orderId, token, order))) {\n return res.status(404).json({ error: 'We could not find that order' })\n }\n if (req.method === 'GET') {\n const [{ returnSettings }, existing] = await Promise.all([\n readStore(firestore, hostId),\n hostRef.collection('returns').where('orderId', '==', orderId).get(),\n ])\n const returns = existing.docs.map((entry) => ({ id: entry.id, ...(entry.data() as CommerceModel.HostReturn) }))\n const endsAt = CommerceModel.returnWindowEndsAtMs(order, returnSettings)\n return res.status(200).json({\n enabled: returnSettings.enabled,\n windowEndsAtMs: endsAt,\n windowOpen: endsAt === null || Date.now() <= endsAt,\n lines: CommerceModel.returnableLines(order, returns, returnSettings),\n reasons: CommerceModel.RETURN_REASONS.map((reason) => ({ value: reason, label: CommerceModel.RETURN_REASON_LABELS[reason] })),\n returns: returns.map((entry) => ({\n id: entry.id,\n status: entry.status,\n lines: entry.lines,\n createdAtMs: entry.createdAtMs,\n ...(entry.returnLabel ? { returnLabel: { labelUrl: entry.returnLabel.labelUrl, carrier: entry.returnLabel.carrier, trackingNumber: entry.returnLabel.trackingNumber } } : {}),\n ...(entry.status === 'declined' && entry.merchantNote ? { merchantNote: entry.merchantNote } : {}),\n })),\n })\n }\n const outcome = await openReturn({\n hostId,\n orderId,\n lines: Array.isArray(input.lines) ? (input.lines as unknown[]).slice(0, 100) as never : [],\n note: String(input.note ?? ''),\n by: 'buyer',\n })\n if (outcome.outcome === 'no_such_order') return res.status(404).json({ error: 'We could not find that order' })\n if (outcome.outcome === 'refused') return res.status(409).json({ error: outcome.message })\n await tellStoreOfRequest(hostId, outcome.returnId, outcome.entry, outcome.order)\n return res.status(200).json({ ok: true, returnId: outcome.returnId, status: outcome.entry.status })\n } catch (error) {\n console.error('return request failed', error)\n return res.status(500).json({ error: 'Your return could not be requested. Please try again.' })\n }\n}\n\n// ---------------------------------------------------------------------------\n// The merchant's door (console)\n// ---------------------------------------------------------------------------\n\nconst MERCHANT_ACTIONS = ['create', 'approve', 'decline', 'close', 'note', 'attach-label', 'receive', 'refund'] as const\ntype MerchantAction = (typeof MERCHANT_ACTIONS)[number]\n\n/**\n * `POST /api/commerce/returns` with `{ hostId, action, … }`. Admins and\n * editors run the return; the refund goes through the order's refund route,\n * which asks for an admin of the whole workspace on its own.\n */\nexport const returnsHandler: PluginApiHandler = async (req, res) => {\n if (req.method !== 'POST') return res.status(405).json({ error: 'Method not allowed' })\n const authorization = String(req.headers.authorization ?? '')\n const idToken = authorization.startsWith('Bearer ') ? authorization.slice('Bearer '.length) : undefined\n if (!idToken) return res.status(401).json({ error: 'Unauthenticated' })\n const body = (typeof req.body === 'string' ? JSON.parse(req.body) : req.body) ?? {}\n const hostId = String(body.hostId ?? '')\n const action = String(body.action ?? '') as MerchantAction\n const returnId = String(body.returnId ?? '')\n if (!isId(hostId)) return res.status(400).json({ error: 'Missing hostId' })\n if (!MERCHANT_ACTIONS.includes(action)) return res.status(400).json({ error: 'Unknown action' })\n if (action !== 'create' && !isId(returnId)) return res.status(400).json({ error: 'Missing returnId' })\n try {\n const decoded = await firebaseAdmin.app().auth().verifyIdToken(idToken)\n const firestore = firebaseAdmin.app().firestore()\n const hostSnapshot = await hostRefFor(firestore, hostId).get()\n if (!hostSnapshot.exists) return res.status(404).json({ error: 'Unknown site' })\n // \"Manage orders\" (`commerce.orders`, AGL-4047).\n if (!holdsCommerceSiteKey(hostSnapshot, decoded.uid, 'commerce.orders')) {\n return res.status(403).json({ error: commerceSitePermissionRefusal('commerce.orders') })\n }\n\n if (action === 'create') {\n const orderId = String(body.orderId ?? '')\n if (!isId(orderId)) return res.status(400).json({ error: 'Missing orderId' })\n const outcome = await openReturn({\n hostId,\n orderId,\n lines: Array.isArray(body.lines) ? (body.lines as unknown[]).slice(0, 100) as never : [],\n note: String(body.note ?? ''),\n by: 'merchant',\n })\n if (outcome.outcome === 'no_such_order') return res.status(404).json({ error: 'Unknown order' })\n if (outcome.outcome === 'refused') return res.status(409).json({ error: outcome.message })\n if (body.notify !== false) await tellBuyer(hostId, 'return-approved', outcome.entry, outcome.order)\n return res.status(200).json({ ok: true, returnId: outcome.returnId, status: outcome.entry.status })\n }\n\n if (action === 'receive') {\n const outcome = await receiveReturn({\n hostId,\n returnId,\n restock: Array.isArray(body.restock) ? (body.restock as unknown[]).slice(0, 100) as never : [],\n locationId: body.locationId ? String(body.locationId) : undefined,\n })\n if (outcome.outcome === 'no_such_return') return res.status(404).json({ error: 'Unknown return' })\n if (outcome.outcome === 'refused') return res.status(409).json({ error: outcome.message })\n return res.status(200).json({\n ok: true,\n ...(outcome.outcome === 'already' ? { already: true } : { units: outcome.units ?? 0 }),\n status: outcome.entry.status,\n })\n }\n\n if (action === 'refund') {\n const returnSnapshot = await hostRefFor(firestore, hostId).collection('returns').doc(returnId).get()\n if (!returnSnapshot.exists) return res.status(404).json({ error: 'Unknown return' })\n const entry = returnSnapshot.data() as CommerceModel.HostReturn\n if (entry.status === 'refunded' || entry.refundedAtMs) {\n return res.status(200).json({ ok: true, already: true, status: entry.status })\n }\n if (!CommerceModel.canTransitionReturn(entry.status, 'refunded')) {\n return res.status(409).json({ error: `A return that is ${CommerceModel.RETURN_STATUS_LABELS[entry.status].toLowerCase()} cannot be refunded` })\n }\n const orderSnapshot = await hostRefFor(firestore, hostId).collection('orders').doc(entry.orderId).get()\n if (!orderSnapshot.exists) return res.status(404).json({ error: 'The order for this return is gone' })\n const order = CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never)\n const suggested = CommerceModel.returnRefundCents(order, entry.lines)\n const amountCents = body.amountCents == null ? suggested : Math.round(Number(body.amountCents))\n if (!Number.isFinite(amountCents) || amountCents <= 0) {\n return res.status(400).json({ error: 'The refund amount must be above zero' })\n }\n const wholeLines = CommerceModel.returnWholeLineIds(order, entry.lines).filter(\n (index) => !CommerceModel.orderLineRefunded(order, index),\n )\n const namedWorth = CommerceModel.orderLineRefundCents(order, wholeLines)\n // Lines are named only when the amount covers them in full, which is\n // what the refund route requires before it withdraws a line's\n // entitlements; a smaller amount refunds by amount alone.\n const refund = await callRefundRoute(\n req,\n {\n hostId,\n orderId: entry.orderId,\n amountCents,\n ...(wholeLines.length > 0 && amountCents >= namedWorth ? { lineItemIds: wholeLines } : {}),\n },\n // One refund per return: asking again replays rather than pays twice.\n `return:${hostId}:${returnId}`,\n )\n if (refund.status !== 200) {\n return res.status(refund.status).json({ error: refund.body?.error ?? 'Refund failed' })\n }\n const marked = await markReturnRefunded({ hostId, returnId, refundCents: amountCents, uid: decoded.uid })\n if (marked.outcome === 'changed') {\n await raiseOrderEvent(RETURN_REFUNDED_EVENT, {\n hostId,\n orderId: entry.orderId,\n key: `return-refunded:${returnId}`,\n extra: { return: returnEventView(returnId, marked.entry) },\n })\n await tellBuyer(hostId, 'return-refunded', marked.entry, marked.order)\n }\n return res.status(200).json({ ok: true, refundCents: amountCents, status: 'refunded' })\n }\n\n const outcome = await changeReturn({\n hostId,\n returnId,\n action: action as ReturnAction,\n ...(body.merchantNote !== undefined ? { merchantNote: String(body.merchantNote) } : {}),\n ...(action === 'attach-label' ? { label: body.label } : {}),\n })\n if (outcome.outcome === 'no_such_return') return res.status(404).json({ error: 'Unknown return' })\n if (outcome.outcome === 'refused') return res.status(409).json({ error: outcome.message })\n if (outcome.outcome === 'changed' && body.notify !== false) {\n if (action === 'approve') await tellBuyer(hostId, 'return-approved', outcome.entry, outcome.order)\n if (action === 'decline') await tellBuyer(hostId, 'return-declined', outcome.entry, outcome.order)\n // A label attached after the approval reaches the buyer the same way,\n // in the approval email, which carries it.\n if (action === 'attach-label' && outcome.entry.status === 'approved') {\n await tellBuyer(hostId, 'return-approved', outcome.entry, outcome.order)\n }\n }\n return res.status(200).json({\n ok: true,\n ...(outcome.outcome === 'already' ? { already: true } : {}),\n status: outcome.entry.status,\n })\n } catch (error) {\n console.error('returns action failed', action, error)\n // Every write here is a transaction or keyed (the refund), so asking\n // again lands once.\n return res.status(500).json({ error: 'The return could not be updated. Retrying is safe.' })\n }\n}\n"],"names":["holdsCommerceSiteKey","commerceSitePermissionRefusal","Aglyn","createResourceUid","findUserByUidAcrossPools","firebaseAdmin","getOrgForHost","hostSendingIdentity","meterHostEmail","notifyHostManagers","renderHostEmailWithTokens","hostConsoleUrl","isEmailConfigured","sendEmail","CommerceModel","RETURN_APPROVED_EVENT","RETURN_DECLINED_EVENT","RETURN_RECEIVED_EVENT","RETURN_REFUNDED_EVENT","RETURN_REQUESTED_EVENT","raiseOrderEvent","stageOrderEvent","orderStatusUrl","verifyOrderStatusToken","readActiveMemberSession","resolveTrackedRestockLines","refundHandler","hostRefFor","firestore","hostId","collection","doc","isId","value","Boolean","length","test","includes","readStore","settings","hostRef","host","store","Promise","all","get","catch","data","returnSettings","readReturnSettings","currency","String","toUpperCase","money","cents","Intl","NumberFormat","style","format","toFixed","returnEventView","id","entry","status","lines","map","line","refundCents","restock","returnItemsText","order","quantity","lineItems","lineItemId","name","RETURN_REASON_LABELS","reason","join","openReturn","request","app","orderRef","orderId","now","Date","note","trim","slice","RETURN_NOTE_MAX","runTransaction","transaction","orderSnapshot","exists","outcome","existing","where","liftLegacyOrder","verdict","validateReturnRequest","docs","by","message","describeReturnRequestProblem","orderNumber","returnOrderNumber","customerEmail","customerName","requestedBy","customerNote","merchantNote","timeline","atMs","event","detail","describeReturnLines","createdAtMs","updatedAtMs","returnRef","create","orderPatch","appendOrderEvent","update","key","extra","return","returnId","ACTION_TARGET","approve","decline","close","changeReturn","undefined","snapshot","target","action","patch","canTransitionReturn","RETURN_STATUS_LABELS","toLowerCase","appendReturnEvent","label","labelUrl","returnIsOpen","carrier","trackingNumber","trackingUrl","trackingUrlFor","returnLabel","attachedAtMs","changed","receiveReturn","locationId","covered","Map","wanted","Number","has","isInteger","total","set","size","location","tracked","ref","products","byProduct","Math","min","lineIndex","productId","variantId","units","product","variants","adjustVariantInventory","productStockFields","delta","entries","received","markReturnRefunded","refundedAtMs","max","round","check","restockCheck","resolution","restocked","some","resolvedAtMs","uid","resolvedBy","callRefundRoute","req","body","idempotencyKey","payload","res","code","json","send","error","setHeader","end","method","headers","authorization","sendReturnEmail","to","tokens","fallback","designed","org","subject","text","html","fromName","resolveBrandingProfile","sendingIdentity","audience","context","owedFor","console","tellStoreOfRequest","items","type","title","link","encodeURIComponent","skipOwnerEmail","ownerUid","owner","record","email","tellBuyer","statusUrl","filter","refundTotal","buyerMayActFor","token","session","memberEmail","member","returnRequestHandler","input","query","JSON","parse","o","t","returns","endsAt","returnWindowEndsAtMs","enabled","windowEndsAtMs","windowOpen","returnableLines","reasons","RETURN_REASONS","Array","isArray","ok","MERCHANT_ACTIONS","returnsHandler","idToken","startsWith","decoded","auth","verifyIdToken","hostSnapshot","notify","already","returnSnapshot","suggested","returnRefundCents","amountCents","isFinite","wholeLines","returnWholeLineIds","index","orderLineRefunded","namedWorth","orderLineRefundCents","refund","lineItemIds","marked"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,oBAAoB,QAAQ,0BAAsB;AAC3D,SAASC,6BAA6B,QAAQ,iCAA6B;AAC3E,YAAYC,WAAW,sBAAqB;AAC5C,SAASC,iBAAiB,QAAQ,6CAA4C;AAE9E,SACEC,wBAAwB,EACxBC,aAAa,EACbC,aAAa,EACbC,mBAAmB,EACnBC,cAAc,EACdC,kBAAkB,EAClBC,yBAAyB,QACpB,2BAA0B;AACjC,SAASC,cAAc,QAAQ,oDAAmD;AAClF,SAASC,iBAAiB,EAAEC,SAAS,QAAQ,2BAA0B;AACvE,YAAYC,mBAAmB,oBAAU;AACzC,SACEC,qBAAqB,EACrBC,qBAAqB,EACrBC,qBAAqB,EACrBC,qBAAqB,EACrBC,sBAAsB,QACjB,2BAAuB;AAC9B,SAASC,eAAe,EAAEC,eAAe,QAAQ,oBAAgB;AACjE,SAASC,cAAc,EAAEC,sBAAsB,QAAQ,0BAAsB;AAC7E,SAASC,uBAAuB,QAAQ,kBAAc;AACtD,SAASC,0BAA0B,QAAQ,oBAAgB;AAC3D,SAASC,aAAa,QAAQ,cAAU;AA4BxC,MAAMC,aAAa,CAACC,WAAsBC,SAAmBD,UAAUE,UAAU,CAAC,SAASC,GAAG,CAACF;AAE/F,MAAMG,OAAO,CAACC,QAAkBC,QAAQD,UAAUA,MAAME,MAAM,IAAI,OAAO,CAAC,WAAWC,IAAI,CAACH,UAAU,CAACA,MAAMI,QAAQ,CAAC;AAEpH,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,EAAE+B,QAAQvB,KAAK;YACjG,sEAAsE;YACtE,2CAA2C;YAC3C,IAAIgE,WAAW,kBAAkBzC,QAAQ/B,KAAK,CAACC,MAAM,KAAK,YAAY;gBACpE,MAAM0K,UAAU5M,QAAQ,mBAAmBgE,QAAQ/B,KAAK,EAAE+B,QAAQvB,KAAK;YACzE;QACF;QACA,OAAO8H,IAAIrI,MAAM,CAAC,KAAKuI,IAAI,CAAC;YAC1B8D,IAAI;WACAvK,QAAQA,OAAO,KAAK,YAAY;YAAEiL,SAAS;QAAK,IAAI,CAAC;YACzD/M,QAAQ8B,QAAQ/B,KAAK,CAACC,MAAM;;IAEhC,EAAE,OAAOyI,OAAO;QACdqB,QAAQrB,KAAK,CAAC,yBAAyBlE,QAAQkE;QAC/C,qEAAqE;QACrE,oBAAoB;QACpB,OAAOJ,IAAIrI,MAAM,CAAC,KAAKuI,IAAI,CAAC;YAAEE,OAAO;QAAqD;IAC5F;AACF,EAAC"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/server/returns.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 { holdsCommerceSiteKey } from './commerce-site-keys'\nimport { commerceSitePermissionRefusal } from '../model/plugin-permissions'\nimport * as Aglyn from '@aglyn/aglyn/server'\nimport { createResourceUid } from '@aglyn/aglyn/app-utils/create-resource-uid'\nimport type { PluginApiHandler, PluginApiRequest, PluginApiResponse } from '@aglyn/aglyn/server'\nimport {\n findUserByUidAcrossPools,\n firebaseAdmin,\n getOrgForHost,\n hostSendingIdentity,\n meterHostEmail,\n notifyHostManagers,\n renderHostEmailWithTokens,\n} from '@aglyn/tenant-data-admin'\nimport { hostConsoleUrl } from '@aglyn/tenant-data-admin/server/host-console-link'\nimport { isEmailConfigured, sendEmail } from '@aglyn/shared-util-email'\nimport * as CommerceModel from '../model'\nimport {\n RETURN_APPROVED_EVENT,\n RETURN_DECLINED_EVENT,\n RETURN_RECEIVED_EVENT,\n RETURN_REFUNDED_EVENT,\n RETURN_REQUESTED_EVENT,\n} from '../model/order-events'\nimport { raiseOrderEvent, stageOrderEvent } from './order-events'\nimport { orderStatusUrl, verifyOrderStatusToken } from './order-status-token'\nimport { readActiveMemberSession } from './membership'\nimport { resolveTrackedRestockLines } from './restock-flag'\nimport { refundHandler } from './refund'\nimport { dropStorefrontPages, soldOutMoved } from './sold-out-drop'\n\n/**\n * Returns (RMA, AGL-3611): the buyer's door, the merchant's door, and the\n * writes behind both. The rules — what is returnable, the window, the state\n * machine, what a refund is worth — are the model's (`commerce-returns.ts`);\n * this is where they are re-asked under the write.\n *\n * EVERY WRITE IS A TRANSACTION that reads the return, the order and, when it\n * opens one, every return already on the order — so two requests for the last\n * unit cannot both be granted, and a stale console cannot approve a return a\n * colleague declined a moment ago.\n *\n * THE MONEY MOVES THROUGH THE ORDER'S REFUND ROUTE, never here. A return's\n * refund calls `refund.ts` with the merchant's own credentials and a key\n * derived from the return, so its gates (an admin of the whole workspace,\n * the over-refund cap, the open-dispute block, Stripe's idempotency) are the\n * ones every refund passes, and asking twice refunds once.\n *\n * STOCK COMES BACK WHEN THE PARCEL DOES. Receiving a return puts the units\n * the merchant chose back on the shelf, at the location they chose, with an\n * inventory adjustment row — the products hub's history shows it beside the\n * sale. A refund then answers the order's restock question with what the\n * return already did, so nobody is asked to restock the same units twice.\n */\n\ntype Firestore = FirebaseFirestore.Firestore\n\nconst hostRefFor = (firestore: Firestore, hostId: string) => firestore.collection('hosts').doc(hostId)\n\nconst isId = (value: string) => Boolean(value) && value.length <= 200 && !/^__.*__$/.test(value) && !value.includes('/')\n\n/** The store's settings document and what the return emails need from the host. */\nasync function readStore(firestore: Firestore, hostId: string) {\n const hostRef = hostRefFor(firestore, hostId)\n const [host, store] = await Promise.all([\n hostRef.get().catch(() => null),\n hostRef.collection('settings').doc('store').get().catch(() => null),\n ])\n const settings = (store?.data() ?? {}) as Record<string, unknown>\n return {\n host: (host?.data() ?? {}) as Record<string, unknown>,\n returnSettings: CommerceModel.readReturnSettings(settings['returns']),\n currency: String(settings['currency'] ?? 'USD').toUpperCase(),\n }\n}\n\nfunction money(cents: number, currency: string): string {\n try {\n return new Intl.NumberFormat('en-US', { style: 'currency', currency }).format(cents / 100)\n } catch {\n return `$${(cents / 100).toFixed(2)}`\n }\n}\n\n/** The return as an event carries it. */\nfunction returnEventView(id: string, entry: CommerceModel.HostReturn) {\n return {\n id,\n status: entry.status,\n lines: entry.lines.map((line) => ({ ...line })),\n refundCents: entry.refundCents ?? null,\n restock: entry.restock ? entry.restock.lines.map((line) => ({ ...line })) : null,\n }\n}\n\n/** \"1× House Blend — Arrived damaged\", one per line. */\nfunction returnItemsText(order: CommerceModel.HostOrder, entry: Pick<CommerceModel.HostReturn, 'lines'>): string {\n return entry.lines\n .map(\n (line) =>\n `${line.quantity}× ${order.lineItems?.[line.lineItemId]?.name ?? `line ${line.lineItemId}`} — ${\n CommerceModel.RETURN_REASON_LABELS[line.reason] ?? line.reason\n }`,\n )\n .join('\\n')\n}\n\nexport type OpenReturnOutcome =\n | { outcome: 'created'; returnId: string; entry: CommerceModel.HostReturn; order: CommerceModel.HostOrder }\n | { outcome: 'refused'; message: string }\n | { outcome: 'no_such_order' }\n\n/**\n * Opens a return. A buyer's starts `requested`; a merchant's starts\n * `approved`, since the merchant asking is the approval.\n */\nexport async function openReturn(request: {\n hostId: string\n orderId: string\n lines: ReadonlyArray<{ lineItemId: unknown; quantity: unknown; reason: unknown }>\n note?: string\n by: 'buyer' | 'merchant'\n now?: number\n}): Promise<OpenReturnOutcome> {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = hostRefFor(firestore, request.hostId)\n const orderRef = hostRef.collection('orders').doc(request.orderId)\n const { returnSettings } = await readStore(firestore, request.hostId)\n const now = request.now ?? Date.now()\n const note = String(request.note ?? '').trim().slice(0, CommerceModel.RETURN_NOTE_MAX)\n return firestore.runTransaction(async (transaction): Promise<OpenReturnOutcome> => {\n const orderSnapshot = await transaction.get(orderRef)\n if (!orderSnapshot.exists) return { outcome: 'no_such_order' }\n const existing = await transaction.get(\n hostRef.collection('returns').where('orderId', '==', request.orderId),\n )\n const order = CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never)\n const verdict = CommerceModel.validateReturnRequest({\n order,\n existing: existing.docs.map((entry) => entry.data() as CommerceModel.HostReturn),\n settings: returnSettings,\n lines: request.lines,\n by: request.by,\n now,\n })\n if (!('lines' in verdict)) {\n return { outcome: 'refused', message: CommerceModel.describeReturnRequestProblem(verdict) }\n }\n const status: CommerceModel.ReturnStatus = request.by === 'merchant' ? 'approved' : 'requested'\n const entry: CommerceModel.HostReturn = {\n orderId: request.orderId,\n orderNumber: CommerceModel.returnOrderNumber(order, request.orderId),\n customerEmail: order.customerEmail ?? null,\n customerName: order.customerName ?? null,\n lines: verdict.lines.map((line) => ({\n ...line,\n name: String(order.lineItems?.[line.lineItemId]?.name ?? '').slice(0, 200),\n })),\n status,\n requestedBy: request.by,\n ...(note ? (request.by === 'buyer' ? { customerNote: note } : { merchantNote: note }) : {}),\n timeline: [\n {\n atMs: now,\n event: status,\n detail: `${request.by === 'buyer' ? 'Requested by the buyer' : 'Opened by the store'}: ${CommerceModel.describeReturnLines(order, verdict.lines)}`,\n },\n ],\n createdAtMs: now,\n updatedAtMs: now,\n }\n const returnRef = hostRef.collection('returns').doc(createResourceUid())\n transaction.create(returnRef, entry)\n const orderPatch = {\n timeline: CommerceModel.appendOrderEvent(\n order,\n 'return-requested',\n CommerceModel.describeReturnLines(order, verdict.lines),\n now,\n ),\n }\n transaction.update(orderRef, orderPatch)\n stageOrderEvent(transaction, RETURN_REQUESTED_EVENT, {\n hostId: request.hostId,\n orderId: request.orderId,\n key: `return:${returnRef.id}`,\n order: { ...orderSnapshot.data(), ...orderPatch },\n extra: { return: returnEventView(returnRef.id, entry) },\n })\n // The store opening a return is its approval.\n if (status === 'approved') {\n stageOrderEvent(transaction, RETURN_APPROVED_EVENT, {\n hostId: request.hostId,\n orderId: request.orderId,\n key: `return-approved:${returnRef.id}`,\n order: { ...orderSnapshot.data(), ...orderPatch },\n extra: { return: returnEventView(returnRef.id, entry) },\n })\n }\n return { outcome: 'created', returnId: returnRef.id, entry, order }\n })\n}\n\nexport type ReturnAction = 'approve' | 'decline' | 'close' | 'note' | 'attach-label'\n\nexport type ChangeReturnOutcome =\n | { outcome: 'changed'; entry: CommerceModel.HostReturn; order: CommerceModel.HostOrder | null }\n | { outcome: 'already'; entry: CommerceModel.HostReturn }\n | { outcome: 'refused'; message: string }\n | { outcome: 'no_such_return' }\n\nconst ACTION_TARGET: Partial<Record<ReturnAction, CommerceModel.ReturnStatus>> = {\n approve: 'approved',\n decline: 'declined',\n close: 'closed',\n}\n\n/**\n * Approve, decline, close, a note, or a return label — the writes that move\n * no stock and no money. The transition is re-asked under the write.\n */\nexport async function changeReturn(request: {\n hostId: string\n returnId: string\n action: ReturnAction\n merchantNote?: string\n label?: { carrier: string; trackingNumber: string; labelUrl: string; trackingUrl?: string }\n now?: number\n}): Promise<ChangeReturnOutcome> {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = hostRefFor(firestore, request.hostId)\n const returnRef = hostRef.collection('returns').doc(request.returnId)\n const now = request.now ?? Date.now()\n const note = request.merchantNote === undefined ? undefined : String(request.merchantNote).trim().slice(0, CommerceModel.RETURN_NOTE_MAX)\n return firestore.runTransaction(async (transaction): Promise<ChangeReturnOutcome> => {\n const snapshot = await transaction.get(returnRef)\n if (!snapshot.exists) return { outcome: 'no_such_return' }\n const entry = snapshot.data() as CommerceModel.HostReturn\n const orderSnapshot = await transaction.get(hostRef.collection('orders').doc(entry.orderId))\n const order = orderSnapshot.exists ? CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never) : null\n const target = ACTION_TARGET[request.action]\n let patch: Partial<CommerceModel.HostReturn>\n if (target) {\n if (entry.status === target) return { outcome: 'already', entry }\n if (!CommerceModel.canTransitionReturn(entry.status, target)) {\n return {\n outcome: 'refused',\n message: `A return that is ${CommerceModel.RETURN_STATUS_LABELS[entry.status].toLowerCase()} cannot be ${CommerceModel.RETURN_STATUS_LABELS[target].toLowerCase()}`,\n }\n }\n patch = {\n status: target,\n ...(note !== undefined && note ? { merchantNote: note } : {}),\n timeline: CommerceModel.appendReturnEvent(entry, target, note || undefined, now),\n }\n } else if (request.action === 'attach-label') {\n const label = request.label\n if (!label?.labelUrl || !/^https:\\/\\//i.test(label.labelUrl)) {\n return { outcome: 'refused', message: 'A return label needs an https link' }\n }\n if (!CommerceModel.returnIsOpen(entry) || entry.status === 'refunded') {\n return { outcome: 'refused', message: 'This return is finished; a label can no longer be attached' }\n }\n const carrier = String(label.carrier ?? '').trim().slice(0, 40)\n const trackingNumber = String(label.trackingNumber ?? '').trim().slice(0, 60)\n const trackingUrl =\n (label.trackingUrl && /^https:\\/\\//i.test(label.trackingUrl) ? label.trackingUrl : null) ??\n CommerceModel.trackingUrlFor(carrier, trackingNumber)\n patch = {\n returnLabel: {\n carrier,\n trackingNumber,\n labelUrl: String(label.labelUrl).slice(0, 2048),\n ...(trackingUrl ? { trackingUrl } : {}),\n attachedAtMs: now,\n },\n timeline: CommerceModel.appendReturnEvent(entry, 'label', `${carrier} ${trackingNumber}`.trim() || 'Return label attached', now),\n }\n } else {\n patch = {\n merchantNote: note ?? '',\n timeline: CommerceModel.appendReturnEvent(entry, 'note', note || 'Note cleared', now),\n }\n }\n transaction.update(returnRef, { ...patch, updatedAtMs: now })\n const changed = { ...entry, ...patch, updatedAtMs: now }\n const event = target === 'approved' ? RETURN_APPROVED_EVENT : target === 'declined' ? RETURN_DECLINED_EVENT : null\n if (event && orderSnapshot.exists) {\n stageOrderEvent(transaction, event, {\n hostId: request.hostId,\n orderId: entry.orderId,\n key: `return-${target}:${request.returnId}`,\n order: orderSnapshot.data() ?? {},\n extra: { return: returnEventView(request.returnId, changed) },\n })\n }\n return { outcome: 'changed', entry: changed, order }\n })\n}\n\n/**\n * The parcel arrived: the return is `received`, and the units the merchant\n * chose go back on the shelf, at the location they chose. A unit the sale\n * never took off a tracked count (an untracked variant, a deleted product)\n * has nothing to go back to and is left out, as a cancellation leaves it.\n */\nexport async function receiveReturn(request: {\n hostId: string\n returnId: string\n restock: ReadonlyArray<{ lineItemId: unknown; quantity: unknown }>\n locationId?: string\n now?: number\n}): Promise<ChangeReturnOutcome & { units?: number }> {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = hostRefFor(firestore, request.hostId)\n const returnRef = hostRef.collection('returns').doc(request.returnId)\n const now = request.now ?? Date.now()\n const locationId = String(request.locationId ?? '').trim().slice(0, 100)\n // Whether the restock flipped a sold-out verdict the storefront shows\n // (AGL-4121); a retried attempt can only make the drop spurious.\n let soldOutFlipped = false\n const outcome = await firestore.runTransaction(async (transaction) => {\n const snapshot = await transaction.get(returnRef)\n if (!snapshot.exists) return { outcome: 'no_such_return' as const }\n const entry = snapshot.data() as CommerceModel.HostReturn\n if (entry.status === 'received' || entry.restock) return { outcome: 'already' as const, entry }\n if (!CommerceModel.canTransitionReturn(entry.status, 'received')) {\n return {\n outcome: 'refused' as const,\n message: `A return that is ${CommerceModel.RETURN_STATUS_LABELS[entry.status].toLowerCase()} cannot be received`,\n }\n }\n // What may go back: at most what the return covers, per line.\n const covered = new Map(entry.lines.map((line) => [line.lineItemId, line.quantity]))\n const wanted = new Map<number, number>()\n for (const line of request.restock) {\n const lineItemId = Number(line?.lineItemId)\n const quantity = Number(line?.quantity)\n if (!covered.has(lineItemId)) {\n return { outcome: 'refused' as const, message: `Line ${lineItemId} is not part of this return` }\n }\n if (!Number.isInteger(quantity) || quantity < 0) {\n return { outcome: 'refused' as const, message: 'Restock quantities must be whole numbers' }\n }\n const total = (wanted.get(lineItemId) ?? 0) + quantity\n if (total > (covered.get(lineItemId) ?? 0)) {\n return { outcome: 'refused' as const, message: `Only ${covered.get(lineItemId)} of line ${lineItemId} came back` }\n }\n if (quantity > 0) wanted.set(lineItemId, total)\n }\n if (locationId && wanted.size > 0) {\n const location = await transaction.get(hostRef.collection('locations').doc(locationId))\n if (!location.exists) return { outcome: 'refused' as const, message: 'That stock location no longer exists' }\n }\n const orderRef = hostRef.collection('orders').doc(entry.orderId)\n const orderSnapshot = await transaction.get(orderRef)\n const order = orderSnapshot.exists ? CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never) : null\n // Every read before any write, as a transaction requires.\n const tracked =\n order && wanted.size > 0\n ? await resolveTrackedRestockLines(hostRef, order, (ref) => transaction.get(ref))\n : { lines: [], products: new Map<string, CommerceModel.HostProduct>() }\n const byProduct = new Map<string, Array<{ variantId: string; quantity: number }>>()\n for (const line of tracked.lines) {\n const quantity = Math.min(line.quantity, wanted.get(line.lineIndex ?? -1) ?? 0)\n if (quantity <= 0) continue\n byProduct.set(line.productId, [...(byProduct.get(line.productId) ?? []), { variantId: line.variantId, quantity }])\n }\n let units = 0\n for (const [productId, lines] of byProduct) {\n const product = tracked.products.get(productId)\n if (!product) continue\n let variants = product.variants\n for (const line of lines) {\n variants = CommerceModel.adjustVariantInventory({ variants }, line.variantId, line.quantity, locationId || undefined)\n }\n transaction.update(hostRef.collection('products').doc(productId), {\n variants,\n ...CommerceModel.productStockFields({ ...product, variants }),\n updatedAtMs: now,\n })\n soldOutFlipped ||= soldOutMoved(product, { ...product, variants })\n for (const line of lines) {\n units += line.quantity\n transaction.create(hostRef.collection('inventoryAdjustments').doc(createResourceUid()), {\n productId,\n variantId: line.variantId,\n delta: line.quantity,\n reason: 'restock',\n orderId: entry.orderId,\n ...(locationId ? { locationId } : {}),\n atMs: now,\n } satisfies CommerceModel.InventoryAdjustment)\n }\n }\n const restock: CommerceModel.ReturnRestock = {\n lines: [...wanted.entries()].map(([lineItemId, quantity]) => ({ lineItemId, quantity })),\n ...(locationId ? { locationId } : {}),\n atMs: now,\n }\n const patch = {\n status: 'received' as const,\n restock,\n timeline: CommerceModel.appendReturnEvent(\n entry,\n 'received',\n units > 0 ? `${units} ${units === 1 ? 'unit' : 'units'} back in stock` : 'Nothing restocked',\n now,\n ),\n updatedAtMs: now,\n }\n transaction.update(returnRef, patch)\n const received = { ...entry, ...patch }\n if (order) {\n const orderPatch = {\n timeline: CommerceModel.appendOrderEvent(order, 'return-received', units > 0 ? `${units} restocked` : undefined, now),\n }\n transaction.update(orderRef, orderPatch)\n stageOrderEvent(transaction, RETURN_RECEIVED_EVENT, {\n hostId: request.hostId,\n orderId: entry.orderId,\n key: `return-received:${request.returnId}`,\n order: { ...orderSnapshot.data(), ...orderPatch },\n extra: { return: returnEventView(request.returnId, received) },\n })\n }\n return { outcome: 'changed' as const, entry: received, order, units }\n })\n if (soldOutFlipped && outcome.outcome === 'changed') {\n await dropStorefrontPages({\n hostId: request.hostId,\n reason: 'a received return restocked a product',\n })\n }\n return outcome\n}\n\n/**\n * Records a refund that went through the order's refund route against the\n * return, and answers the order's restock question with what the return\n * already did, so the merchant is not asked to restock the same units again.\n */\nexport async function markReturnRefunded(request: {\n hostId: string\n returnId: string\n refundCents: number\n uid: string\n now?: number\n}): Promise<ChangeReturnOutcome> {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = hostRefFor(firestore, request.hostId)\n const returnRef = hostRef.collection('returns').doc(request.returnId)\n const now = request.now ?? Date.now()\n return firestore.runTransaction(async (transaction): Promise<ChangeReturnOutcome> => {\n const snapshot = await transaction.get(returnRef)\n if (!snapshot.exists) return { outcome: 'no_such_return' }\n const entry = snapshot.data() as CommerceModel.HostReturn\n if (entry.status === 'refunded' || entry.refundedAtMs) return { outcome: 'already', entry }\n const orderRef = hostRef.collection('orders').doc(entry.orderId)\n const orderSnapshot = await transaction.get(orderRef)\n const order = orderSnapshot.exists ? CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never) : null\n const patch = {\n status: 'refunded' as const,\n refundedAtMs: now,\n refundCents: Math.max(0, Math.round(request.refundCents)),\n timeline: CommerceModel.appendReturnEvent(entry, 'refunded', undefined, now),\n updatedAtMs: now,\n }\n transaction.update(returnRef, patch)\n const check = order?.restockCheck\n if (order && check && !check.resolution && entry.restock) {\n const restocked = entry.restock.lines.some((line) => line.quantity > 0)\n transaction.update(orderRef, {\n restockCheck: {\n ...check,\n resolution: restocked ? 'restocked' : 'dismissed',\n resolvedAtMs: now,\n ...(request.uid ? { resolvedBy: request.uid } : {}),\n } satisfies CommerceModel.OrderRestockCheck,\n timeline: CommerceModel.appendOrderEvent(\n order,\n 'restock-check',\n restocked ? 'answered by the return — restocked' : 'answered by the return — no restock',\n now,\n ),\n })\n }\n return { outcome: 'changed', entry: { ...entry, ...patch }, order }\n })\n}\n\n/** Calls the order's own refund route in-process, with the caller's credentials. */\nasync function callRefundRoute(\n req: PluginApiRequest,\n body: Record<string, unknown>,\n idempotencyKey: string,\n): Promise<{ status: number; body: Record<string, any> }> {\n let status = 200\n let payload: Record<string, any> = {}\n const res = {\n status(code: number) {\n status = code\n return res\n },\n json(value: unknown) {\n payload = (value ?? {}) as Record<string, any>\n return res\n },\n send(value: unknown) {\n payload = { error: String(value ?? '') }\n return res\n },\n setHeader() {\n return res\n },\n end() {\n return res\n },\n } as unknown as PluginApiResponse\n await refundHandler(\n {\n ...req,\n method: 'POST',\n headers: { authorization: String(req.headers.authorization ?? ''), 'idempotency-key': idempotencyKey },\n body,\n } as PluginApiRequest,\n res,\n )\n return { status, body: payload }\n}\n\n// ---------------------------------------------------------------------------\n// Emails\n// ---------------------------------------------------------------------------\n\ntype ReturnEmailKey = 'return-requested' | 'return-approved' | 'return-declined' | 'return-refunded'\n\nasync function sendReturnEmail(\n hostId: string,\n key: ReturnEmailKey,\n to: string,\n tokens: Record<string, string>,\n fallback: { subject: string; text: string },\n): Promise<void> {\n if (!to || !isEmailConfigured()) return\n try {\n const firestore = firebaseAdmin.app().firestore()\n const designed = await renderHostEmailWithTokens(firestore, hostId, key, tokens)\n const org = await getOrgForHost(hostId).catch(() => null)\n await sendEmail({\n to,\n subject: designed?.subject ?? fallback.subject,\n text: designed?.text || fallback.text,\n ...(designed?.html ? { html: designed.html } : {}),\n fromName: Aglyn.resolveBrandingProfile(org?.org as never).fromName,\n sendingIdentity: await hostSendingIdentity(hostId),\n audience: 'tenant',\n context: key,\n // Owed to the recipient by their own order (AGL-3356).\n owedFor: 'order',\n })\n await meterHostEmail(hostId)\n } catch (error) {\n // A return that is recorded is not undone because its email failed.\n console.error(`[returns] ${key} not sent`, hostId, error)\n }\n}\n\n/** Tells the store a buyer asked, in the console and by email to the owner. */\nasync function tellStoreOfRequest(hostId: string, returnId: string, entry: CommerceModel.HostReturn, order: CommerceModel.HostOrder) {\n const items = returnItemsText(order, entry)\n await notifyHostManagers(\n hostId,\n {\n type: 'content.order',\n title: `Return requested for order ${entry.orderNumber}`,\n body: `${entry.customerEmail ?? 'A buyer'} wants to return ${CommerceModel.describeReturnLines(order, entry.lines)} from order ${entry.orderNumber} on {site}.`,\n link: `/${hostId}/products/returns?return=${encodeURIComponent(returnId)}`,\n },\n { skipOwnerEmail: true },\n ).catch(() => undefined)\n const ownerUid = (await getOrgForHost(hostId).catch(() => null))?.org?.ownerUid\n const owner = ownerUid ? (await findUserByUidAcrossPools(ownerUid).catch(() => null))?.record : null\n if (!owner?.email) return\n await sendReturnEmail(\n hostId,\n 'return-requested',\n owner.email,\n {\n 'order.number': entry.orderNumber,\n 'return.items': items,\n 'buyer.email': String(entry.customerEmail ?? ''),\n 'return.customerNote': String(entry.customerNote ?? ''),\n 'return.url': await hostConsoleUrl(\n hostId,\n `/${hostId}/products/returns?return=${encodeURIComponent(returnId)}`,\n ),\n },\n {\n subject: `Return requested for order ${entry.orderNumber}`,\n text:\n `${entry.customerEmail ?? 'A buyer'} wants to return items from order ${entry.orderNumber}.\\n\\n${items}` +\n (entry.customerNote ? `\\n\\n\"${entry.customerNote}\"` : '') +\n `\\n\\nApprove or decline it under Products → Returns.`,\n },\n )\n}\n\n/** Tells the buyer what happened to their return. */\nasync function tellBuyer(\n hostId: string,\n key: Exclude<ReturnEmailKey, 'return-requested'>,\n entry: CommerceModel.HostReturn,\n order: CommerceModel.HostOrder | null,\n): Promise<void> {\n const to = String(entry.customerEmail ?? order?.customerEmail ?? '').trim()\n if (!to || !order) return\n const firestore = firebaseAdmin.app().firestore()\n const store = await readStore(firestore, hostId)\n const statusUrl = orderStatusUrl(store.host as never, hostId, entry.orderId) ?? ''\n const items = returnItemsText(order, entry)\n const label = entry.returnLabel\n ? `Print your return label: ${entry.returnLabel.labelUrl}` +\n (entry.returnLabel.trackingNumber ? ` (${[entry.returnLabel.carrier, entry.returnLabel.trackingNumber].filter(Boolean).join(' ')})` : '')\n : ''\n const refundTotal = money(entry.refundCents ?? 0, store.currency)\n const tokens: Record<string, string> = {\n 'order.number': entry.orderNumber,\n 'return.items': items,\n 'return.note': String(entry.merchantNote ?? ''),\n 'return.label': label,\n 'return.refundTotal': refundTotal,\n 'order.statusUrl': statusUrl,\n }\n const fallback =\n key === 'return-approved'\n ? {\n subject: `Your return for order ${entry.orderNumber} is approved`,\n text: `Your return is approved:\\n\\n${items}` + (entry.merchantNote ? `\\n\\n${entry.merchantNote}` : '') + (label ? `\\n\\n${label}` : ''),\n }\n : key === 'return-declined'\n ? {\n subject: `About your return for order ${entry.orderNumber}`,\n text: `We could not accept the return of:\\n\\n${items}` + (entry.merchantNote ? `\\n\\n${entry.merchantNote}` : ''),\n }\n : {\n subject: `Your refund for order ${entry.orderNumber}`,\n text: `We received your return and refunded ${refundTotal}.\\n\\n${items}`,\n }\n if (statusUrl) fallback.text += `\\n\\nView your order: ${statusUrl}`\n await sendReturnEmail(hostId, key, to, tokens, fallback)\n}\n\n// ---------------------------------------------------------------------------\n// The buyer's door (tenant)\n// ---------------------------------------------------------------------------\n\n/**\n * Whether this request may act for the buyer of this order: the signed-in\n * site member whose email the order carries, or the holder of the order's\n * signed status link. Neither reveals whether the order exists to anyone\n * else — both refusals read the same.\n */\nasync function buyerMayActFor(\n req: PluginApiRequest,\n hostId: string,\n orderId: string,\n token: string,\n order: CommerceModel.HostOrder,\n): Promise<boolean> {\n if (token && verifyOrderStatusToken(hostId, orderId, token)) return true\n const session = await readActiveMemberSession(req, hostId)\n if (session.status !== 'active') return false\n const memberEmail = String(session.member.get('email') ?? '').trim().toLowerCase()\n return Boolean(memberEmail) && memberEmail === String(order.customerEmail ?? '').trim().toLowerCase()\n}\n\n/**\n * `GET /api/commerce/return-request?hostId&orderId&t` — what the buyer may\n * send back and until when, and the returns already open.\n * `POST` with `{ hostId, orderId, t?, lines: [{ lineItemId, quantity, reason }], note? }`\n * opens a return.\n */\nexport const returnRequestHandler: PluginApiHandler = async (req, res) => {\n if (req.method !== 'GET' && req.method !== 'POST') {\n return res.status(405).json({ error: 'Method not allowed' })\n }\n const input = (req.method === 'GET' ? req.query : typeof req.body === 'string' ? JSON.parse(req.body) : req.body) ?? {}\n const hostId = String(input.hostId ?? '')\n const orderId = String(input.orderId ?? input.o ?? '')\n const token = String(input.t ?? input.token ?? '').slice(0, 100)\n if (!isId(hostId) || !isId(orderId)) return res.status(400).json({ error: 'Missing hostId or orderId' })\n try {\n const firestore = firebaseAdmin.app().firestore()\n const hostRef = hostRefFor(firestore, hostId)\n const orderSnapshot = await hostRef.collection('orders').doc(orderId).get()\n const order = orderSnapshot.exists ? CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never) : null\n if (!order || !(await buyerMayActFor(req, hostId, orderId, token, order))) {\n return res.status(404).json({ error: 'We could not find that order' })\n }\n if (req.method === 'GET') {\n const [{ returnSettings }, existing] = await Promise.all([\n readStore(firestore, hostId),\n hostRef.collection('returns').where('orderId', '==', orderId).get(),\n ])\n const returns = existing.docs.map((entry) => ({ id: entry.id, ...(entry.data() as CommerceModel.HostReturn) }))\n const endsAt = CommerceModel.returnWindowEndsAtMs(order, returnSettings)\n return res.status(200).json({\n enabled: returnSettings.enabled,\n windowEndsAtMs: endsAt,\n windowOpen: endsAt === null || Date.now() <= endsAt,\n lines: CommerceModel.returnableLines(order, returns, returnSettings),\n reasons: CommerceModel.RETURN_REASONS.map((reason) => ({ value: reason, label: CommerceModel.RETURN_REASON_LABELS[reason] })),\n returns: returns.map((entry) => ({\n id: entry.id,\n status: entry.status,\n lines: entry.lines,\n createdAtMs: entry.createdAtMs,\n ...(entry.returnLabel ? { returnLabel: { labelUrl: entry.returnLabel.labelUrl, carrier: entry.returnLabel.carrier, trackingNumber: entry.returnLabel.trackingNumber } } : {}),\n ...(entry.status === 'declined' && entry.merchantNote ? { merchantNote: entry.merchantNote } : {}),\n })),\n })\n }\n const outcome = await openReturn({\n hostId,\n orderId,\n lines: Array.isArray(input.lines) ? (input.lines as unknown[]).slice(0, 100) as never : [],\n note: String(input.note ?? ''),\n by: 'buyer',\n })\n if (outcome.outcome === 'no_such_order') return res.status(404).json({ error: 'We could not find that order' })\n if (outcome.outcome === 'refused') return res.status(409).json({ error: outcome.message })\n await tellStoreOfRequest(hostId, outcome.returnId, outcome.entry, outcome.order)\n return res.status(200).json({ ok: true, returnId: outcome.returnId, status: outcome.entry.status })\n } catch (error) {\n console.error('return request failed', error)\n return res.status(500).json({ error: 'Your return could not be requested. Please try again.' })\n }\n}\n\n// ---------------------------------------------------------------------------\n// The merchant's door (console)\n// ---------------------------------------------------------------------------\n\nconst MERCHANT_ACTIONS = ['create', 'approve', 'decline', 'close', 'note', 'attach-label', 'receive', 'refund'] as const\ntype MerchantAction = (typeof MERCHANT_ACTIONS)[number]\n\n/**\n * `POST /api/commerce/returns` with `{ hostId, action, … }`. Admins and\n * editors run the return; the refund goes through the order's refund route,\n * which asks for an admin of the whole workspace on its own.\n */\nexport const returnsHandler: PluginApiHandler = async (req, res) => {\n if (req.method !== 'POST') return res.status(405).json({ error: 'Method not allowed' })\n const authorization = String(req.headers.authorization ?? '')\n const idToken = authorization.startsWith('Bearer ') ? authorization.slice('Bearer '.length) : undefined\n if (!idToken) return res.status(401).json({ error: 'Unauthenticated' })\n const body = (typeof req.body === 'string' ? JSON.parse(req.body) : req.body) ?? {}\n const hostId = String(body.hostId ?? '')\n const action = String(body.action ?? '') as MerchantAction\n const returnId = String(body.returnId ?? '')\n if (!isId(hostId)) return res.status(400).json({ error: 'Missing hostId' })\n if (!MERCHANT_ACTIONS.includes(action)) return res.status(400).json({ error: 'Unknown action' })\n if (action !== 'create' && !isId(returnId)) return res.status(400).json({ error: 'Missing returnId' })\n try {\n const decoded = await firebaseAdmin.app().auth().verifyIdToken(idToken)\n const firestore = firebaseAdmin.app().firestore()\n const hostSnapshot = await hostRefFor(firestore, hostId).get()\n if (!hostSnapshot.exists) return res.status(404).json({ error: 'Unknown site' })\n // \"Manage orders\" (`commerce.orders`, AGL-4047).\n if (!holdsCommerceSiteKey(hostSnapshot, decoded.uid, 'commerce.orders')) {\n return res.status(403).json({ error: commerceSitePermissionRefusal('commerce.orders') })\n }\n\n if (action === 'create') {\n const orderId = String(body.orderId ?? '')\n if (!isId(orderId)) return res.status(400).json({ error: 'Missing orderId' })\n const outcome = await openReturn({\n hostId,\n orderId,\n lines: Array.isArray(body.lines) ? (body.lines as unknown[]).slice(0, 100) as never : [],\n note: String(body.note ?? ''),\n by: 'merchant',\n })\n if (outcome.outcome === 'no_such_order') return res.status(404).json({ error: 'Unknown order' })\n if (outcome.outcome === 'refused') return res.status(409).json({ error: outcome.message })\n if (body.notify !== false) await tellBuyer(hostId, 'return-approved', outcome.entry, outcome.order)\n return res.status(200).json({ ok: true, returnId: outcome.returnId, status: outcome.entry.status })\n }\n\n if (action === 'receive') {\n const outcome = await receiveReturn({\n hostId,\n returnId,\n restock: Array.isArray(body.restock) ? (body.restock as unknown[]).slice(0, 100) as never : [],\n locationId: body.locationId ? String(body.locationId) : undefined,\n })\n if (outcome.outcome === 'no_such_return') return res.status(404).json({ error: 'Unknown return' })\n if (outcome.outcome === 'refused') return res.status(409).json({ error: outcome.message })\n return res.status(200).json({\n ok: true,\n ...(outcome.outcome === 'already' ? { already: true } : { units: outcome.units ?? 0 }),\n status: outcome.entry.status,\n })\n }\n\n if (action === 'refund') {\n const returnSnapshot = await hostRefFor(firestore, hostId).collection('returns').doc(returnId).get()\n if (!returnSnapshot.exists) return res.status(404).json({ error: 'Unknown return' })\n const entry = returnSnapshot.data() as CommerceModel.HostReturn\n if (entry.status === 'refunded' || entry.refundedAtMs) {\n return res.status(200).json({ ok: true, already: true, status: entry.status })\n }\n if (!CommerceModel.canTransitionReturn(entry.status, 'refunded')) {\n return res.status(409).json({ error: `A return that is ${CommerceModel.RETURN_STATUS_LABELS[entry.status].toLowerCase()} cannot be refunded` })\n }\n const orderSnapshot = await hostRefFor(firestore, hostId).collection('orders').doc(entry.orderId).get()\n if (!orderSnapshot.exists) return res.status(404).json({ error: 'The order for this return is gone' })\n const order = CommerceModel.liftLegacyOrder((orderSnapshot.data() ?? {}) as never)\n const suggested = CommerceModel.returnRefundCents(order, entry.lines)\n const amountCents = body.amountCents == null ? suggested : Math.round(Number(body.amountCents))\n if (!Number.isFinite(amountCents) || amountCents <= 0) {\n return res.status(400).json({ error: 'The refund amount must be above zero' })\n }\n const wholeLines = CommerceModel.returnWholeLineIds(order, entry.lines).filter(\n (index) => !CommerceModel.orderLineRefunded(order, index),\n )\n const namedWorth = CommerceModel.orderLineRefundCents(order, wholeLines)\n // Lines are named only when the amount covers them in full, which is\n // what the refund route requires before it withdraws a line's\n // entitlements; a smaller amount refunds by amount alone.\n const refund = await callRefundRoute(\n req,\n {\n hostId,\n orderId: entry.orderId,\n amountCents,\n ...(wholeLines.length > 0 && amountCents >= namedWorth ? { lineItemIds: wholeLines } : {}),\n },\n // One refund per return: asking again replays rather than pays twice.\n `return:${hostId}:${returnId}`,\n )\n if (refund.status !== 200) {\n return res.status(refund.status).json({ error: refund.body?.error ?? 'Refund failed' })\n }\n const marked = await markReturnRefunded({ hostId, returnId, refundCents: amountCents, uid: decoded.uid })\n if (marked.outcome === 'changed') {\n await raiseOrderEvent(RETURN_REFUNDED_EVENT, {\n hostId,\n orderId: entry.orderId,\n key: `return-refunded:${returnId}`,\n extra: { return: returnEventView(returnId, marked.entry) },\n })\n await tellBuyer(hostId, 'return-refunded', marked.entry, marked.order)\n }\n return res.status(200).json({ ok: true, refundCents: amountCents, status: 'refunded' })\n }\n\n const outcome = await changeReturn({\n hostId,\n returnId,\n action: action as ReturnAction,\n ...(body.merchantNote !== undefined ? { merchantNote: String(body.merchantNote) } : {}),\n ...(action === 'attach-label' ? { label: body.label } : {}),\n })\n if (outcome.outcome === 'no_such_return') return res.status(404).json({ error: 'Unknown return' })\n if (outcome.outcome === 'refused') return res.status(409).json({ error: outcome.message })\n if (outcome.outcome === 'changed' && body.notify !== false) {\n if (action === 'approve') await tellBuyer(hostId, 'return-approved', outcome.entry, outcome.order)\n if (action === 'decline') await tellBuyer(hostId, 'return-declined', outcome.entry, outcome.order)\n // A label attached after the approval reaches the buyer the same way,\n // in the approval email, which carries it.\n if (action === 'attach-label' && outcome.entry.status === 'approved') {\n await tellBuyer(hostId, 'return-approved', outcome.entry, outcome.order)\n }\n }\n return res.status(200).json({\n ok: true,\n ...(outcome.outcome === 'already' ? { already: true } : {}),\n status: outcome.entry.status,\n })\n } catch (error) {\n console.error('returns action failed', action, error)\n // Every write here is a transaction or keyed (the refund), so asking\n // again lands once.\n return res.status(500).json({ error: 'The return could not be updated. Retrying is safe.' })\n }\n}\n"],"names":["holdsCommerceSiteKey","commerceSitePermissionRefusal","Aglyn","createResourceUid","findUserByUidAcrossPools","firebaseAdmin","getOrgForHost","hostSendingIdentity","meterHostEmail","notifyHostManagers","renderHostEmailWithTokens","hostConsoleUrl","isEmailConfigured","sendEmail","CommerceModel","RETURN_APPROVED_EVENT","RETURN_DECLINED_EVENT","RETURN_RECEIVED_EVENT","RETURN_REFUNDED_EVENT","RETURN_REQUESTED_EVENT","raiseOrderEvent","stageOrderEvent","orderStatusUrl","verifyOrderStatusToken","readActiveMemberSession","resolveTrackedRestockLines","refundHandler","dropStorefrontPages","soldOutMoved","hostRefFor","firestore","hostId","collection","doc","isId","value","Boolean","length","test","includes","readStore","settings","hostRef","host","store","Promise","all","get","catch","data","returnSettings","readReturnSettings","currency","String","toUpperCase","money","cents","Intl","NumberFormat","style","format","toFixed","returnEventView","id","entry","status","lines","map","line","refundCents","restock","returnItemsText","order","quantity","lineItems","lineItemId","name","RETURN_REASON_LABELS","reason","join","openReturn","request","app","orderRef","orderId","now","Date","note","trim","slice","RETURN_NOTE_MAX","runTransaction","transaction","orderSnapshot","exists","outcome","existing","where","liftLegacyOrder","verdict","validateReturnRequest","docs","by","message","describeReturnRequestProblem","orderNumber","returnOrderNumber","customerEmail","customerName","requestedBy","customerNote","merchantNote","timeline","atMs","event","detail","describeReturnLines","createdAtMs","updatedAtMs","returnRef","create","orderPatch","appendOrderEvent","update","key","extra","return","returnId","ACTION_TARGET","approve","decline","close","changeReturn","undefined","snapshot","target","action","patch","canTransitionReturn","RETURN_STATUS_LABELS","toLowerCase","appendReturnEvent","label","labelUrl","returnIsOpen","carrier","trackingNumber","trackingUrl","trackingUrlFor","returnLabel","attachedAtMs","changed","receiveReturn","locationId","soldOutFlipped","covered","Map","wanted","Number","has","isInteger","total","set","size","location","tracked","ref","products","byProduct","Math","min","lineIndex","productId","variantId","units","product","variants","adjustVariantInventory","productStockFields","delta","entries","received","markReturnRefunded","refundedAtMs","max","round","check","restockCheck","resolution","restocked","some","resolvedAtMs","uid","resolvedBy","callRefundRoute","req","body","idempotencyKey","payload","res","code","json","send","error","setHeader","end","method","headers","authorization","sendReturnEmail","to","tokens","fallback","designed","org","subject","text","html","fromName","resolveBrandingProfile","sendingIdentity","audience","context","owedFor","console","tellStoreOfRequest","items","type","title","link","encodeURIComponent","skipOwnerEmail","ownerUid","owner","record","email","tellBuyer","statusUrl","filter","refundTotal","buyerMayActFor","token","session","memberEmail","member","returnRequestHandler","input","query","JSON","parse","o","t","returns","endsAt","returnWindowEndsAtMs","enabled","windowEndsAtMs","windowOpen","returnableLines","reasons","RETURN_REASONS","Array","isArray","ok","MERCHANT_ACTIONS","returnsHandler","idToken","startsWith","decoded","auth","verifyIdToken","hostSnapshot","notify","already","returnSnapshot","suggested","returnRefundCents","amountCents","isFinite","wholeLines","returnWholeLineIds","index","orderLineRefunded","namedWorth","orderLineRefundCents","refund","lineItemIds","marked"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,oBAAoB,QAAQ,0BAAsB;AAC3D,SAASC,6BAA6B,QAAQ,iCAA6B;AAC3E,YAAYC,WAAW,sBAAqB;AAC5C,SAASC,iBAAiB,QAAQ,6CAA4C;AAE9E,SACEC,wBAAwB,EACxBC,aAAa,EACbC,aAAa,EACbC,mBAAmB,EACnBC,cAAc,EACdC,kBAAkB,EAClBC,yBAAyB,QACpB,2BAA0B;AACjC,SAASC,cAAc,QAAQ,oDAAmD;AAClF,SAASC,iBAAiB,EAAEC,SAAS,QAAQ,2BAA0B;AACvE,YAAYC,mBAAmB,oBAAU;AACzC,SACEC,qBAAqB,EACrBC,qBAAqB,EACrBC,qBAAqB,EACrBC,qBAAqB,EACrBC,sBAAsB,QACjB,2BAAuB;AAC9B,SAASC,eAAe,EAAEC,eAAe,QAAQ,oBAAgB;AACjE,SAASC,cAAc,EAAEC,sBAAsB,QAAQ,0BAAsB;AAC7E,SAASC,uBAAuB,QAAQ,kBAAc;AACtD,SAASC,0BAA0B,QAAQ,oBAAgB;AAC3D,SAASC,aAAa,QAAQ,cAAU;AACxC,SAASC,mBAAmB,EAAEC,YAAY,QAAQ,qBAAiB;AA4BnE,MAAMC,aAAa,CAACC,WAAsBC,SAAmBD,UAAUE,UAAU,CAAC,SAASC,GAAG,CAACF;AAE/F,MAAMG,OAAO,CAACC,QAAkBC,QAAQD,UAAUA,MAAME,MAAM,IAAI,OA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G,IAAItI,MAAM,CAAC,KAAKwI,IAAI,CAAC;YAAEE,OAAO5G,QAAQQ,OAAO;QAAC;QACxF,IAAIR,QAAQA,OAAO,KAAK,aAAaqG,KAAK4E,MAAM,KAAK,OAAO;YAC1D,IAAIxI,WAAW,WAAW,MAAMoG,UAAU7M,QAAQ,mBAAmBgE,QAAQ/B,KAAK,EAAE+B,QAAQvB,KAAK;YACjG,IAAIgE,WAAW,WAAW,MAAMoG,UAAU7M,QAAQ,mBAAmBgE,QAAQ/B,KAAK,EAAE+B,QAAQvB,KAAK;YACjG,sEAAsE;YACtE,2CAA2C;YAC3C,IAAIgE,WAAW,kBAAkBzC,QAAQ/B,KAAK,CAACC,MAAM,KAAK,YAAY;gBACpE,MAAM2K,UAAU7M,QAAQ,mBAAmBgE,QAAQ/B,KAAK,EAAE+B,QAAQvB,KAAK;YACzE;QACF;QACA,OAAO+H,IAAItI,MAAM,CAAC,KAAKwI,IAAI,CAAC;YAC1B8D,IAAI;WACAxK,QAAQA,OAAO,KAAK,YAAY;YAAEkL,SAAS;QAAK,IAAI,CAAC;YACzDhN,QAAQ8B,QAAQ/B,KAAK,CAACC,MAAM;;IAEhC,EAAE,OAAO0I,OAAO;QACdqB,QAAQrB,KAAK,CAAC,yBAAyBnE,QAAQmE;QAC/C,qEAAqE;QACrE,oBAAoB;QACpB,OAAOJ,IAAItI,MAAM,CAAC,KAAKwI,IAAI,CAAC;YAAEE,OAAO;QAAqD;IAC5F;AACF,EAAC"}
|
|
@@ -23,6 +23,19 @@ import { createResourceUid } from "@aglyn/aglyn/app-utils/create-resource-uid";
|
|
|
23
23
|
import { AggregateField } from "firebase-admin/firestore";
|
|
24
24
|
import { MEMBER_SUSPENDED_ERROR, readActiveMemberSession } from "./membership.js";
|
|
25
25
|
import { inspectReviewToken, redeemReviewTokenIn } from "./review-tokens.js";
|
|
26
|
+
import { dropStorefrontPages } from "./sold-out-drop.js";
|
|
27
|
+
/**
|
|
28
|
+
* A verified review is published the moment it is submitted, and the product
|
|
29
|
+
* page bakes the rating and the list into its cached HTML (AGL-4121). The
|
|
30
|
+
* moderation card drops the site when a merchant approves one; a review that
|
|
31
|
+
* skips moderation has to do the same, or it waits out the week-long backstop.
|
|
32
|
+
* A pending review is not shown, so it drops nothing.
|
|
33
|
+
*/ async function announcePublishedReview(hostId) {
|
|
34
|
+
await dropStorefrontPages({
|
|
35
|
+
hostId,
|
|
36
|
+
reason: 'a verified review was published'
|
|
37
|
+
});
|
|
38
|
+
}
|
|
26
39
|
/**
|
|
27
40
|
* Approved reviews for a product, newest first, plus their aggregate.
|
|
28
41
|
*
|
|
@@ -496,6 +509,7 @@ async function submitReview(req, res, hostId, productId) {
|
|
|
496
509
|
merge: true
|
|
497
510
|
}).catch(()=>undefined);
|
|
498
511
|
}
|
|
512
|
+
await announcePublishedReview(hostId);
|
|
499
513
|
return res.status(200).json({
|
|
500
514
|
ok: true,
|
|
501
515
|
pending: false,
|
|
@@ -525,6 +539,7 @@ async function submitReview(req, res, hostId, productId) {
|
|
|
525
539
|
status: CommerceModel.initialReviewStatus(true),
|
|
526
540
|
createdAtMs: Date.now()
|
|
527
541
|
});
|
|
542
|
+
await announcePublishedReview(hostId);
|
|
528
543
|
return res.status(200).json({
|
|
529
544
|
ok: true,
|
|
530
545
|
pending: false,
|