@aglyn/plugins-commerce 1.0.0-beta.231 → 1.0.0-beta.233
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/package.json +13 -13
- package/src/lib/components/cart-extras.d.ts +6 -0
- package/src/lib/components/cart-extras.js +4 -1
- package/src/lib/components/cart-extras.js.map +1 -1
- package/src/lib/components/cart-fulfillment.d.ts +117 -0
- package/src/lib/components/cart-fulfillment.js +367 -0
- package/src/lib/components/cart-fulfillment.js.map +1 -0
- package/src/lib/components/cart.js +52 -10
- package/src/lib/components/cart.js.map +1 -1
- package/src/lib/components/commerce-console-page.js +42 -1
- package/src/lib/components/commerce-console-page.js.map +1 -1
- package/src/lib/components/console/catalog-organization-card.component.js +1 -0
- package/src/lib/components/console/catalog-organization-card.component.js.map +1 -1
- package/src/lib/components/console/host-orders-card.component.js +42 -11
- package/src/lib/components/console/host-orders-card.component.js.map +1 -1
- package/src/lib/components/console/local-delivery-settings-card.component.d.ts +37 -0
- package/src/lib/components/console/local-delivery-settings-card.component.js +542 -0
- package/src/lib/components/console/local-delivery-settings-card.component.js.map +1 -0
- package/src/lib/components/console/locations-card.component.js +177 -3
- package/src/lib/components/console/locations-card.component.js.map +1 -1
- package/src/lib/components/console/order-detail-dialog.component.js +10 -0
- package/src/lib/components/console/order-detail-dialog.component.js.map +1 -1
- package/src/lib/components/console/order-local-fulfillment-panel.component.d.ts +44 -0
- package/src/lib/components/console/order-local-fulfillment-panel.component.js +265 -0
- package/src/lib/components/console/order-local-fulfillment-panel.component.js.map +1 -0
- package/src/lib/components/console/order-offline-notice.component.d.ts +12 -0
- package/src/lib/components/console/order-offline-notice.component.js +53 -0
- package/src/lib/components/console/order-offline-notice.component.js.map +1 -0
- package/src/lib/components/console/pickup-delivery-queue-card.component.d.ts +35 -0
- package/src/lib/components/console/pickup-delivery-queue-card.component.js +222 -0
- package/src/lib/components/console/pickup-delivery-queue-card.component.js.map +1 -0
- package/src/lib/components/console/pos/pos-api.d.ts +24 -1
- package/src/lib/components/console/pos/pos-api.js +13 -0
- package/src/lib/components/console/pos/pos-api.js.map +1 -1
- package/src/lib/components/console/pos/pos-item-dialog.component.d.ts +12 -0
- package/src/lib/components/console/pos/pos-item-dialog.component.js +10 -8
- package/src/lib/components/console/pos/pos-item-dialog.component.js.map +1 -1
- package/src/lib/components/console/pos/pos-kiosk-queue.component.d.ts +22 -0
- package/src/lib/components/console/pos/pos-kiosk-queue.component.js +168 -0
- package/src/lib/components/console/pos/pos-kiosk-queue.component.js.map +1 -0
- package/src/lib/components/console/pos/pos-tender-dialogs.component.d.ts +18 -0
- package/src/lib/components/console/pos/pos-tender-dialogs.component.js +116 -1
- package/src/lib/components/console/pos/pos-tender-dialogs.component.js.map +1 -1
- package/src/lib/components/console/pos/pos-tender-panel.component.d.ts +8 -0
- package/src/lib/components/console/pos/pos-tender-panel.component.js +77 -18
- package/src/lib/components/console/pos/pos-tender-panel.component.js.map +1 -1
- package/src/lib/components/console/pos/use-pos-display.d.ts +5 -2
- package/src/lib/components/console/pos/use-pos-display.js +3 -2
- package/src/lib/components/console/pos/use-pos-display.js.map +1 -1
- package/src/lib/components/console/pos-devices-card.component.js +2 -2
- package/src/lib/components/console/pos-devices-card.component.js.map +1 -1
- package/src/lib/components/console/pos-display/pairing-screen.d.ts +5 -2
- package/src/lib/components/console/pos-display/pairing-screen.js +7 -5
- package/src/lib/components/console/pos-display/pairing-screen.js.map +1 -1
- package/src/lib/components/console/pos-display/pos-display-api.d.ts +7 -1
- package/src/lib/components/console/pos-display/pos-display-api.js +7 -2
- package/src/lib/components/console/pos-display/pos-display-api.js.map +1 -1
- package/src/lib/components/console/pos-kiosk/kiosk-checkout.d.ts +41 -0
- package/src/lib/components/console/pos-kiosk/kiosk-checkout.js +275 -0
- package/src/lib/components/console/pos-kiosk/kiosk-checkout.js.map +1 -0
- package/src/lib/components/console/pos-kiosk/kiosk-menu.d.ts +32 -0
- package/src/lib/components/console/pos-kiosk/kiosk-menu.js +344 -0
- package/src/lib/components/console/pos-kiosk/kiosk-menu.js.map +1 -0
- package/src/lib/components/console/pos-kiosk/kiosk-staff.d.ts +12 -0
- package/src/lib/components/console/pos-kiosk/kiosk-staff.js +257 -0
- package/src/lib/components/console/pos-kiosk/kiosk-staff.js.map +1 -0
- package/src/lib/components/console/pos-kiosk/pos-kiosk-api.d.ts +114 -0
- package/src/lib/components/console/pos-kiosk/pos-kiosk-api.js +189 -0
- package/src/lib/components/console/pos-kiosk/pos-kiosk-api.js.map +1 -0
- package/src/lib/components/console/pos-kiosk/use-kiosk-idle.d.ts +16 -0
- package/src/lib/components/console/pos-kiosk/use-kiosk-idle.js +80 -0
- package/src/lib/components/console/pos-kiosk/use-kiosk-idle.js.map +1 -0
- package/src/lib/components/console/pos-kiosk-page.component.d.ts +28 -0
- package/src/lib/components/console/pos-kiosk-page.component.js +791 -0
- package/src/lib/components/console/pos-kiosk-page.component.js.map +1 -0
- package/src/lib/components/console/pos-offline/pos-offline-api.d.ts +44 -0
- package/src/lib/components/console/pos-offline/pos-offline-api.js +69 -0
- package/src/lib/components/console/pos-offline/pos-offline-api.js.map +1 -0
- package/src/lib/components/console/pos-offline/pos-offline-banner.component.d.ts +7 -0
- package/src/lib/components/console/pos-offline/pos-offline-banner.component.js +135 -0
- package/src/lib/components/console/pos-offline/pos-offline-banner.component.js.map +1 -0
- package/src/lib/components/console/pos-offline/pos-offline-checkout.component.d.ts +32 -0
- package/src/lib/components/console/pos-offline/pos-offline-checkout.component.js +287 -0
- package/src/lib/components/console/pos-offline/pos-offline-checkout.component.js.map +1 -0
- package/src/lib/components/console/pos-offline/pos-offline-store.d.ts +63 -0
- package/src/lib/components/console/pos-offline/pos-offline-store.js +145 -0
- package/src/lib/components/console/pos-offline/pos-offline-store.js.map +1 -0
- package/src/lib/components/console/pos-offline/register-offline.d.ts +24 -0
- package/src/lib/components/console/pos-offline/register-offline.js +24 -0
- package/src/lib/components/console/pos-offline/register-offline.js.map +1 -0
- package/src/lib/components/console/pos-offline/use-pos-offline.d.ts +85 -0
- package/src/lib/components/console/pos-offline/use-pos-offline.js +542 -0
- package/src/lib/components/console/pos-offline/use-pos-offline.js.map +1 -0
- package/src/lib/components/console/pos-ops/pos-shift-history-card.component.js +72 -17
- package/src/lib/components/console/pos-ops/pos-shift-history-card.component.js.map +1 -1
- package/src/lib/components/console/pos-page.component.js +228 -51
- package/src/lib/components/console/pos-page.component.js.map +1 -1
- package/src/lib/components/console/pos-zones.d.ts +23 -0
- package/src/lib/components/console/pos-zones.js +31 -0
- package/src/lib/components/console/pos-zones.js.map +1 -0
- package/src/lib/components/console/product-editor-dialog.component.js +10 -64
- package/src/lib/components/console/product-editor-dialog.component.js.map +1 -1
- package/src/lib/components/console/products-hub-card.component.js +46 -30
- package/src/lib/components/console/products-hub-card.component.js.map +1 -1
- package/src/lib/components/console/shipping-settings-card.component.js +1 -1
- package/src/lib/components/console/shipping-settings-card.component.js.map +1 -1
- package/src/lib/components/console/stock-movements-card.component.js +48 -16
- package/src/lib/components/console/stock-movements-card.component.js.map +1 -1
- package/src/lib/components/console/store-zones.d.ts +8 -1
- package/src/lib/components/console/store-zones.js +11 -1
- package/src/lib/components/console/store-zones.js.map +1 -1
- package/src/lib/components/console/storefront-tax-summary-card.component.js +1 -0
- package/src/lib/components/console/storefront-tax-summary-card.component.js.map +1 -1
- package/src/lib/components/order-status.js +66 -0
- package/src/lib/components/order-status.js.map +1 -1
- package/src/lib/components/product-detail.js +42 -15
- package/src/lib/components/product-detail.js.map +1 -1
- package/src/lib/constants/local-fulfillment-list-query.d.ts +48 -0
- package/src/lib/constants/local-fulfillment-list-query.js +84 -0
- package/src/lib/constants/local-fulfillment-list-query.js.map +1 -0
- package/src/lib/constants/orders-list-query.d.ts +7 -2
- package/src/lib/constants/orders-list-query.js +89 -8
- package/src/lib/constants/orders-list-query.js.map +1 -1
- package/src/lib/constants/pos-shift-list-query.d.ts +22 -0
- package/src/lib/constants/pos-shift-list-query.js +115 -0
- package/src/lib/constants/pos-shift-list-query.js.map +1 -0
- package/src/lib/constants/product-list-query.d.ts +6 -1
- package/src/lib/constants/product-list-query.js +77 -8
- package/src/lib/constants/product-list-query.js.map +1 -1
- package/src/lib/constants/stock-movement-query.d.ts +3 -2
- package/src/lib/constants/stock-movement-query.js +52 -8
- package/src/lib/constants/stock-movement-query.js.map +1 -1
- package/src/lib/declarations.server.js +21 -0
- package/src/lib/declarations.server.js.map +1 -1
- package/src/lib/model/buyer-notifications.d.ts +5 -3
- package/src/lib/model/buyer-notifications.js +25 -4
- package/src/lib/model/buyer-notifications.js.map +1 -1
- package/src/lib/model/commerce-orders.d.ts +103 -1
- package/src/lib/model/commerce-orders.js +2 -1
- package/src/lib/model/commerce-orders.js.map +1 -1
- package/src/lib/model/commerce-pos-kiosk.d.ts +175 -0
- package/src/lib/model/commerce-pos-kiosk.js +147 -0
- package/src/lib/model/commerce-pos-kiosk.js.map +1 -0
- package/src/lib/model/commerce-pos-offline.d.ts +234 -0
- package/src/lib/model/commerce-pos-offline.js +301 -0
- package/src/lib/model/commerce-pos-offline.js.map +1 -0
- package/src/lib/model/commerce-pos-ops.d.ts +16 -6
- package/src/lib/model/commerce-pos-ops.js +12 -4
- package/src/lib/model/commerce-pos-ops.js.map +1 -1
- package/src/lib/model/commerce-pos.d.ts +13 -1
- package/src/lib/model/commerce-pos.js +5 -4
- package/src/lib/model/commerce-pos.js.map +1 -1
- package/src/lib/model/commerce-stock-holds.d.ts +24 -3
- package/src/lib/model/commerce-stock-holds.js +26 -8
- package/src/lib/model/commerce-stock-holds.js.map +1 -1
- package/src/lib/model/commerce.d.ts +7 -1
- package/src/lib/model/commerce.js.map +1 -1
- package/src/lib/model/index.d.ts +5 -0
- package/src/lib/model/index.js +5 -0
- package/src/lib/model/index.js.map +1 -1
- package/src/lib/model/local-fulfillment-settings.d.ts +178 -0
- package/src/lib/model/local-fulfillment-settings.js +361 -0
- package/src/lib/model/local-fulfillment-settings.js.map +1 -0
- package/src/lib/model/order-list-fields.d.ts +10 -1
- package/src/lib/model/order-list-fields.js +1 -0
- package/src/lib/model/order-list-fields.js.map +1 -1
- package/src/lib/model/order-local-fulfillment.d.ts +131 -0
- package/src/lib/model/order-local-fulfillment.js +125 -0
- package/src/lib/model/order-local-fulfillment.js.map +1 -0
- package/src/lib/model/order-shipping-export.js +4 -0
- package/src/lib/model/order-shipping-export.js.map +1 -1
- package/src/lib/model/order-status-view.d.ts +20 -1
- package/src/lib/model/order-status-view.js +60 -6
- package/src/lib/model/order-status-view.js.map +1 -1
- package/src/lib/model/product-write.d.ts +186 -0
- package/src/lib/model/product-write.js +116 -0
- package/src/lib/model/product-write.js.map +1 -0
- package/src/lib/plugin-config.js +6 -2
- package/src/lib/plugin-config.js.map +1 -1
- package/src/lib/plugin.js +11 -0
- package/src/lib/plugin.js.map +1 -1
- package/src/lib/pos-kiosk-config.d.ts +26 -0
- package/src/lib/pos-kiosk-config.js +59 -0
- package/src/lib/pos-kiosk-config.js.map +1 -0
- package/src/lib/server/api-v1/openapi.js +8 -0
- package/src/lib/server/api-v1/openapi.js.map +1 -1
- package/src/lib/server/api-v1/order-view.d.ts +19 -0
- package/src/lib/server/api-v1/order-view.js +35 -0
- package/src/lib/server/api-v1/order-view.js.map +1 -1
- package/src/lib/server/billing-webhook.js +238 -87
- package/src/lib/server/billing-webhook.js.map +1 -1
- package/src/lib/server/cart-checkout.d.ts +2 -0
- package/src/lib/server/cart-checkout.js +173 -13
- package/src/lib/server/cart-checkout.js.map +1 -1
- package/src/lib/server/channel-order-updates.d.ts +50 -0
- package/src/lib/server/channel-order-updates.js +290 -0
- package/src/lib/server/channel-order-updates.js.map +1 -0
- package/src/lib/server/channel-orders.d.ts +85 -0
- package/src/lib/server/channel-orders.js +543 -0
- package/src/lib/server/channel-orders.js.map +1 -0
- package/src/lib/server/checkout-extras.js +18 -4
- package/src/lib/server/checkout-extras.js.map +1 -1
- package/src/lib/server/checkout.js +71 -14
- package/src/lib/server/checkout.js.map +1 -1
- package/src/lib/server/local-fulfillment-status.d.ts +86 -0
- package/src/lib/server/local-fulfillment-status.js +379 -0
- package/src/lib/server/local-fulfillment-status.js.map +1 -0
- package/src/lib/server/local-fulfillment.d.ts +190 -0
- package/src/lib/server/local-fulfillment.js +580 -0
- package/src/lib/server/local-fulfillment.js.map +1 -0
- package/src/lib/server/membership-register.js +3 -1
- package/src/lib/server/membership-register.js.map +1 -1
- package/src/lib/server/order-notifications.js +61 -0
- package/src/lib/server/order-notifications.js.map +1 -1
- package/src/lib/server/pos-display.d.ts +27 -1
- package/src/lib/server/pos-display.js +50 -9
- package/src/lib/server/pos-display.js.map +1 -1
- package/src/lib/server/pos-kiosk-principal.d.ts +29 -0
- package/src/lib/server/pos-kiosk-principal.js +42 -0
- package/src/lib/server/pos-kiosk-principal.js.map +1 -0
- package/src/lib/server/pos-kiosk.d.ts +54 -0
- package/src/lib/server/pos-kiosk.js +972 -0
- package/src/lib/server/pos-kiosk.js.map +1 -0
- package/src/lib/server/pos-offline-sync.d.ts +85 -0
- package/src/lib/server/pos-offline-sync.js +765 -0
- package/src/lib/server/pos-offline-sync.js.map +1 -0
- package/src/lib/server/pos-order.js +22 -4
- package/src/lib/server/pos-order.js.map +1 -1
- package/src/lib/server/pos-payment.d.ts +6 -0
- package/src/lib/server/pos-payment.js +225 -6
- package/src/lib/server/pos-payment.js.map +1 -1
- package/src/lib/server/pos-print.js +5 -0
- package/src/lib/server/pos-print.js.map +1 -1
- package/src/lib/server/pos-return.js +27 -0
- package/src/lib/server/pos-return.js.map +1 -1
- package/src/lib/server/pos-sale.d.ts +6 -0
- package/src/lib/server/pos-sale.js +2 -0
- package/src/lib/server/pos-sale.js.map +1 -1
- package/src/lib/server/pos-shift.js +8 -1
- package/src/lib/server/pos-shift.js.map +1 -1
- package/src/lib/server/product-catalog.js +5 -1
- package/src/lib/server/product-catalog.js.map +1 -1
- package/src/lib/server/product-drafts.d.ts +5 -1
- package/src/lib/server/product-drafts.js +3 -3
- package/src/lib/server/product-drafts.js.map +1 -1
- package/src/lib/server/product-writer.d.ts +19 -0
- package/src/lib/server/product-writer.js +261 -0
- package/src/lib/server/product-writer.js.map +1 -0
- package/src/lib/server/products-write.d.ts +61 -0
- package/src/lib/server/products-write.js +375 -0
- package/src/lib/server/products-write.js.map +1 -0
- package/src/lib/server/refund.js +3 -0
- package/src/lib/server/refund.js.map +1 -1
- package/src/lib/server/stock-hold.d.ts +9 -1
- package/src/lib/server/stock-hold.js +15 -7
- package/src/lib/server/stock-hold.js.map +1 -1
- package/src/lib/server.js +21 -0
- package/src/lib/server.js.map +1 -1
- package/src/lib/tenant-emails.js +161 -0
- package/src/lib/tenant-emails.js.map +1 -1
- package/src/lib/testing/memory-firestore.d.ts +16 -3
- package/src/lib/testing/memory-firestore.js +11 -3
- package/src/lib/testing/memory-firestore.js.map +1 -1
- package/src/lib/transfer/products.server.d.ts +5 -0
- package/src/lib/transfer/products.server.js +2 -2
- package/src/lib/transfer/products.server.js.map +1 -1
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2026 Aglyn LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/ import { createHash } from "node:crypto";
|
|
18
|
+
import { isRefusedIdToken } from "@aglyn/tenant-data-admin/server/id-token-refusal";
|
|
19
|
+
import { createResourceUid } from "@aglyn/aglyn/app-utils/create-resource-uid";
|
|
20
|
+
import { hostRoleCanWrite } from "@aglyn/aglyn/app-utils/organizations";
|
|
21
|
+
import { dropPluginSiteCache } from "@aglyn/aglyn/plugin-manager/plugin-site-cache";
|
|
22
|
+
import { firebaseAdmin, getLockdownVerdict, getOrgForHost, isImpersonationSession } from "@aglyn/tenant-data-admin";
|
|
23
|
+
import { FieldValue } from "firebase-admin/firestore";
|
|
24
|
+
import { productCollectionIds, productEditWrite, productMembershipInput, productSaveFields, smartCollectionRulesOf, stockAdjustmentWrite, validateProduct } from "../model/index.js";
|
|
25
|
+
import { planRefusal, roomRefusal } from "./product-drafts.js";
|
|
26
|
+
const ROLE_REFUSAL = 'Editing requires the editor role';
|
|
27
|
+
const MAX_BODY_BYTES = 256000;
|
|
28
|
+
/** The reasons the Adjust stock dialog offers. */ export const STOCK_ADJUST_REASONS = [
|
|
29
|
+
'restock',
|
|
30
|
+
'correction',
|
|
31
|
+
'damage',
|
|
32
|
+
'refund'
|
|
33
|
+
];
|
|
34
|
+
function bodyOf(req) {
|
|
35
|
+
const raw = req.body;
|
|
36
|
+
if (typeof raw === 'string') {
|
|
37
|
+
if (raw.length > MAX_BODY_BYTES) return {};
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(raw || '{}');
|
|
40
|
+
} catch (unused) {
|
|
41
|
+
return {};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return raw != null ? raw : {};
|
|
45
|
+
}
|
|
46
|
+
const validId = (value)=>typeof value === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(value);
|
|
47
|
+
/**
|
|
48
|
+
* Who may write this site's products, as the rules decide it. Answers the
|
|
49
|
+
* caller's reply on a refusal and returns null.
|
|
50
|
+
*/ async function gate(req, res, hostId, deps) {
|
|
51
|
+
var _req_headers_authorization, _host_get, _ref;
|
|
52
|
+
var _this;
|
|
53
|
+
const authorization = String((_req_headers_authorization = req.headers.authorization) != null ? _req_headers_authorization : '');
|
|
54
|
+
const token = authorization.startsWith('Bearer ') ? authorization.slice('Bearer '.length) : '';
|
|
55
|
+
if (!token) return res.status(401).json({
|
|
56
|
+
error: 'Unauthenticated'
|
|
57
|
+
}), null;
|
|
58
|
+
let decoded;
|
|
59
|
+
try {
|
|
60
|
+
decoded = await deps.verifyIdToken(token);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
// Only a refused token is the caller's; an Auth outage is a 500 (AGL-2852).
|
|
63
|
+
if (!isRefusedIdToken(error)) throw error;
|
|
64
|
+
return res.status(401).json({
|
|
65
|
+
error: 'Sign in again to continue'
|
|
66
|
+
}), null;
|
|
67
|
+
}
|
|
68
|
+
const staff = decoded['staff'] === true;
|
|
69
|
+
if (!staff && decoded['email_verified'] !== true && !isImpersonationSession(decoded)) {
|
|
70
|
+
return res.status(403).json({
|
|
71
|
+
error: 'Verify your email address to make changes'
|
|
72
|
+
}), null;
|
|
73
|
+
}
|
|
74
|
+
const host = await deps.firestore().collection('hosts').doc(hostId).get();
|
|
75
|
+
if (!host.exists) return res.status(404).json({
|
|
76
|
+
error: 'Unknown site'
|
|
77
|
+
}), null;
|
|
78
|
+
if (!staff && !hostRoleCanWrite(((_host_get = host.get('memberRoles')) != null ? _host_get : {})[decoded.uid])) {
|
|
79
|
+
return res.status(403).json({
|
|
80
|
+
error: ROLE_REFUSAL
|
|
81
|
+
}), null;
|
|
82
|
+
}
|
|
83
|
+
const org = (_ref = (_this = await deps.orgForHost(hostId)) == null ? void 0 : _this.org) != null ? _ref : {};
|
|
84
|
+
const locked = await deps.lockdown({
|
|
85
|
+
staff,
|
|
86
|
+
uid: decoded.uid,
|
|
87
|
+
org: org,
|
|
88
|
+
host: host.data(),
|
|
89
|
+
request: {
|
|
90
|
+
method: 'POST'
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
if (locked) return res.status(423).json({
|
|
94
|
+
error: 'This site is not accepting changes right now'
|
|
95
|
+
}), null;
|
|
96
|
+
return {
|
|
97
|
+
uid: decoded.uid,
|
|
98
|
+
staff,
|
|
99
|
+
host,
|
|
100
|
+
org
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** The site-wide cache drop a product change is owed (AGL-3386): an outbox entry in the write, then the drop. */ function outboxEntry(firestore, hostId) {
|
|
104
|
+
return {
|
|
105
|
+
ref: firestore.collection('publishOutbox').doc(createResourceUid()),
|
|
106
|
+
data: {
|
|
107
|
+
hostId,
|
|
108
|
+
paths: [
|
|
109
|
+
'/'
|
|
110
|
+
],
|
|
111
|
+
createdAt: FieldValue.serverTimestamp(),
|
|
112
|
+
attempts: 0,
|
|
113
|
+
entireHost: true
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
async function settleCache(deps, hostId, entry, reason) {
|
|
118
|
+
const dropped = await deps.dropCache({
|
|
119
|
+
hostIds: [
|
|
120
|
+
hostId
|
|
121
|
+
],
|
|
122
|
+
reason
|
|
123
|
+
}).catch(()=>({
|
|
124
|
+
complete: false
|
|
125
|
+
}));
|
|
126
|
+
if (dropped.complete) await entry.delete().catch(()=>undefined);
|
|
127
|
+
}
|
|
128
|
+
function resolved(deps) {
|
|
129
|
+
var _deps_firestore, _deps_verifyIdToken, _deps_orgForHost, _deps_lockdown, _deps_dropCache, _deps_now;
|
|
130
|
+
return {
|
|
131
|
+
firestore: (_deps_firestore = deps.firestore) != null ? _deps_firestore : ()=>firebaseAdmin.app().firestore(),
|
|
132
|
+
verifyIdToken: (_deps_verifyIdToken = deps.verifyIdToken) != null ? _deps_verifyIdToken : (token)=>firebaseAdmin.app().auth().verifyIdToken(token),
|
|
133
|
+
orgForHost: (_deps_orgForHost = deps.orgForHost) != null ? _deps_orgForHost : (hostId)=>getOrgForHost(hostId),
|
|
134
|
+
lockdown: (_deps_lockdown = deps.lockdown) != null ? _deps_lockdown : (options)=>getLockdownVerdict(options),
|
|
135
|
+
dropCache: (_deps_dropCache = deps.dropCache) != null ? _deps_dropCache : dropPluginSiteCache,
|
|
136
|
+
now: (_deps_now = deps.now) != null ? _deps_now : ()=>Date.now()
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Saves a product as the console editor does. `create: true` makes a new
|
|
141
|
+
* product under the client-minted `productId` (a retry finds its own product
|
|
142
|
+
* and answers `replayed`); otherwise it replaces the live product, keeping
|
|
143
|
+
* its live stock holds.
|
|
144
|
+
*/ export function createProductSaveHandler(options = {}) {
|
|
145
|
+
return async (req, res)=>{
|
|
146
|
+
if (req.method !== 'POST') return res.status(405).json({
|
|
147
|
+
error: 'Method not allowed'
|
|
148
|
+
});
|
|
149
|
+
const deps = resolved(options);
|
|
150
|
+
const body = bodyOf(req);
|
|
151
|
+
const hostId = body['hostId'];
|
|
152
|
+
const productId = body['productId'];
|
|
153
|
+
const product = body['product'];
|
|
154
|
+
if (!validId(hostId) || !validId(productId) || !product || typeof product !== 'object' || !Array.isArray(product.variants)) {
|
|
155
|
+
return res.status(400).json({
|
|
156
|
+
error: 'Missing site, product or its variants'
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
if (typeof product.name !== 'string' || !product.name.trim()) return res.status(400).json({
|
|
160
|
+
error: 'Product name is required'
|
|
161
|
+
});
|
|
162
|
+
try {
|
|
163
|
+
const door = await gate(req, res, hostId, deps);
|
|
164
|
+
if (!door) return;
|
|
165
|
+
const nowMs = deps.now();
|
|
166
|
+
const fields = productSaveFields(product, nowMs);
|
|
167
|
+
const problem = validateProduct(fields);
|
|
168
|
+
if (problem) return res.status(400).json({
|
|
169
|
+
error: problem
|
|
170
|
+
});
|
|
171
|
+
const db = deps.firestore();
|
|
172
|
+
const hostRef = db.collection('hosts').doc(hostId);
|
|
173
|
+
const products = hostRef.collection('products');
|
|
174
|
+
const productRef = products.doc(productId);
|
|
175
|
+
const smartQuery = hostRef.collection('collections').where('kind', '==', 'catalog').where('mode', '==', 'smart');
|
|
176
|
+
const outbox = outboxEntry(db, hostId);
|
|
177
|
+
const create = body['create'] === true;
|
|
178
|
+
const answer = await db.runTransaction(async (tx)=>{
|
|
179
|
+
const [existing, smart] = await Promise.all([
|
|
180
|
+
tx.get(productRef),
|
|
181
|
+
tx.get(smartQuery)
|
|
182
|
+
]);
|
|
183
|
+
const membership = {
|
|
184
|
+
collectionIds: productCollectionIds(productMembershipInput(fields, product), smart.docs.map((doc)=>smartCollectionRulesOf(doc.id, doc.data())))
|
|
185
|
+
};
|
|
186
|
+
if (create) {
|
|
187
|
+
if (existing.exists) return {
|
|
188
|
+
status: 200,
|
|
189
|
+
body: {
|
|
190
|
+
ok: true,
|
|
191
|
+
id: productId,
|
|
192
|
+
replayed: true
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
const early = planRefusal(door.org);
|
|
196
|
+
if (early) return {
|
|
197
|
+
status: early.status,
|
|
198
|
+
body: {
|
|
199
|
+
error: early.error
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
const used = (await tx.get(products.count())).data().count;
|
|
203
|
+
const room = roomRefusal(door.org, Number(used) || 0);
|
|
204
|
+
if (room) return {
|
|
205
|
+
status: room.status,
|
|
206
|
+
body: {
|
|
207
|
+
error: room.error
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
tx.create(productRef, _extends({}, fields, membership, {
|
|
211
|
+
createdAtMs: nowMs,
|
|
212
|
+
deletedAt: null,
|
|
213
|
+
createdAt: FieldValue.serverTimestamp(),
|
|
214
|
+
updatedAt: FieldValue.serverTimestamp(),
|
|
215
|
+
createdBy: door.uid
|
|
216
|
+
}));
|
|
217
|
+
} else {
|
|
218
|
+
var _existing_data;
|
|
219
|
+
if (!existing.exists || existing.get('deletedAt')) return {
|
|
220
|
+
status: 404,
|
|
221
|
+
body: {
|
|
222
|
+
error: 'This product was deleted'
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
tx.set(productRef, _extends({}, storedTimestamps((_existing_data = existing.data()) != null ? _existing_data : {}, product), productEditWrite(fields, existing.get('stockHolds')), membership, {
|
|
226
|
+
updatedAt: FieldValue.serverTimestamp()
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
tx.set(outbox.ref, outbox.data);
|
|
230
|
+
return {
|
|
231
|
+
status: 200,
|
|
232
|
+
body: {
|
|
233
|
+
ok: true,
|
|
234
|
+
id: productId,
|
|
235
|
+
replayed: false
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
});
|
|
239
|
+
if (answer.status === 200 && !answer.body.replayed) {
|
|
240
|
+
await settleCache(deps, hostId, outbox.ref, 'product saved from the app');
|
|
241
|
+
}
|
|
242
|
+
return res.status(answer.status).json(answer.body);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
console.error('[commerce/products/save]', error);
|
|
245
|
+
return res.status(500).json({
|
|
246
|
+
error: 'The product could not be saved'
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* The stored timestamps an edit keeps. The console's replace writes back the
|
|
253
|
+
* Timestamps its seed carried (`createdAt`, a publish time); a client's JSON
|
|
254
|
+
* cannot carry one, so each stored Timestamp field the request does not name
|
|
255
|
+
* is kept as stored.
|
|
256
|
+
*/ export function storedTimestamps(stored, asked) {
|
|
257
|
+
return Object.fromEntries(Object.entries(stored).filter(([key, value])=>!(key in asked) && typeof (value == null ? void 0 : value.toDate) === 'function'));
|
|
258
|
+
}
|
|
259
|
+
/** The ledger row id one attempt owns: the same key from the same member is the same row. */ export function stockAttemptId(uid, key) {
|
|
260
|
+
return 'app-' + createHash('sha256').update(`${uid}:${key}`).digest('hex').slice(0, 40);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Adjusts one variant's stock as the Adjust stock dialog does: the variants
|
|
264
|
+
* and stock fields on the product, and one `inventoryAdjustments` row, in one
|
|
265
|
+
* transaction. The Idempotency-Key names that row, so a retried attempt finds
|
|
266
|
+
* it and moves nothing twice.
|
|
267
|
+
*/ export function createProductStockHandler(options = {}) {
|
|
268
|
+
return async (req, res)=>{
|
|
269
|
+
var _req_headers_idempotencykey;
|
|
270
|
+
if (req.method !== 'POST') return res.status(405).json({
|
|
271
|
+
error: 'Method not allowed'
|
|
272
|
+
});
|
|
273
|
+
const deps = resolved(options);
|
|
274
|
+
const body = bodyOf(req);
|
|
275
|
+
const hostId = body['hostId'];
|
|
276
|
+
const productId = body['productId'];
|
|
277
|
+
const variantId = body['variantId'];
|
|
278
|
+
const key = String((_req_headers_idempotencykey = req.headers['idempotency-key']) != null ? _req_headers_idempotencykey : '').trim().slice(0, 200);
|
|
279
|
+
const reason = body['reason'];
|
|
280
|
+
const delta = Math.round(Number(body['delta']));
|
|
281
|
+
const locationId = typeof body['locationId'] === 'string' && body['locationId'] ? body['locationId'] : undefined;
|
|
282
|
+
if (!validId(hostId) || !validId(productId) || typeof variantId !== 'string' || !variantId) {
|
|
283
|
+
return res.status(400).json({
|
|
284
|
+
error: 'Missing site, product or variant'
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
if (!key) return res.status(400).json({
|
|
288
|
+
error: 'Missing Idempotency-Key'
|
|
289
|
+
});
|
|
290
|
+
if (!STOCK_ADJUST_REASONS.includes(reason)) return res.status(400).json({
|
|
291
|
+
error: 'Unknown reason'
|
|
292
|
+
});
|
|
293
|
+
if (!Number.isFinite(delta) || delta === 0) return res.status(400).json({
|
|
294
|
+
error: 'Enter a number of units'
|
|
295
|
+
});
|
|
296
|
+
try {
|
|
297
|
+
const door = await gate(req, res, hostId, deps);
|
|
298
|
+
if (!door) return;
|
|
299
|
+
const db = deps.firestore();
|
|
300
|
+
const hostRef = db.collection('hosts').doc(hostId);
|
|
301
|
+
const productRef = hostRef.collection('products').doc(productId);
|
|
302
|
+
const ledgerRef = hostRef.collection('inventoryAdjustments').doc(stockAttemptId(door.uid, key));
|
|
303
|
+
const outbox = outboxEntry(db, hostId);
|
|
304
|
+
const answer = await db.runTransaction(async (tx)=>{
|
|
305
|
+
var _product_variants, _write_update_inventory;
|
|
306
|
+
const [ledger, stored] = await Promise.all([
|
|
307
|
+
tx.get(ledgerRef),
|
|
308
|
+
tx.get(productRef)
|
|
309
|
+
]);
|
|
310
|
+
if (ledger.exists) {
|
|
311
|
+
var _stored_get;
|
|
312
|
+
return {
|
|
313
|
+
status: 200,
|
|
314
|
+
body: {
|
|
315
|
+
ok: true,
|
|
316
|
+
replayed: true,
|
|
317
|
+
inventory: (_stored_get = stored.get('inventory')) != null ? _stored_get : null
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
if (!stored.exists || stored.get('deletedAt')) return {
|
|
322
|
+
status: 404,
|
|
323
|
+
body: {
|
|
324
|
+
error: 'This product was deleted'
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
const product = stored.data();
|
|
328
|
+
if (!((_product_variants = product.variants) != null ? _product_variants : []).some((variant)=>variant.id === variantId)) {
|
|
329
|
+
return {
|
|
330
|
+
status: 404,
|
|
331
|
+
body: {
|
|
332
|
+
error: 'That variant is no longer on this product'
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
const write = stockAdjustmentWrite(product, productId, {
|
|
337
|
+
variantId,
|
|
338
|
+
delta,
|
|
339
|
+
reason,
|
|
340
|
+
locationId
|
|
341
|
+
}, deps.now());
|
|
342
|
+
if (!write) return {
|
|
343
|
+
status: 400,
|
|
344
|
+
body: {
|
|
345
|
+
error: 'Enter a number of units'
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
tx.update(productRef, write.update);
|
|
349
|
+
tx.create(ledgerRef, write.ledger);
|
|
350
|
+
tx.set(outbox.ref, outbox.data);
|
|
351
|
+
return {
|
|
352
|
+
status: 200,
|
|
353
|
+
body: {
|
|
354
|
+
ok: true,
|
|
355
|
+
replayed: false,
|
|
356
|
+
inventory: (_write_update_inventory = write.update.inventory) != null ? _write_update_inventory : null
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
});
|
|
360
|
+
if (answer.status === 200 && !answer.body.replayed) {
|
|
361
|
+
await settleCache(deps, hostId, outbox.ref, 'stock adjusted from the app');
|
|
362
|
+
}
|
|
363
|
+
return res.status(answer.status).json(answer.body);
|
|
364
|
+
} catch (error) {
|
|
365
|
+
console.error('[commerce/products/stock]', error);
|
|
366
|
+
return res.status(500).json({
|
|
367
|
+
error: 'Stock could not be adjusted'
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
export const productSaveHandler = createProductSaveHandler();
|
|
373
|
+
export const productStockHandler = createProductStockHandler();
|
|
374
|
+
|
|
375
|
+
//# sourceMappingURL=products-write.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/server/products-write.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 { createHash } from 'node:crypto'\nimport { isRefusedIdToken } from '@aglyn/tenant-data-admin/server/id-token-refusal'\nimport { createResourceUid } from '@aglyn/aglyn/app-utils/create-resource-uid'\nimport { hostRoleCanWrite } from '@aglyn/aglyn/app-utils/organizations'\nimport { dropPluginSiteCache } from '@aglyn/aglyn/plugin-manager/plugin-site-cache'\nimport { type PluginApiHandler } from '@aglyn/aglyn/server'\nimport { firebaseAdmin, getLockdownVerdict, getOrgForHost, isImpersonationSession } from '@aglyn/tenant-data-admin'\nimport { FieldValue } from 'firebase-admin/firestore'\nimport {\n type EditedProduct,\n productCollectionIds,\n productEditWrite,\n productMembershipInput,\n productSaveFields,\n smartCollectionRulesOf,\n stockAdjustmentWrite,\n validateProduct,\n type InventoryAdjustmentReason,\n} from '../model'\nimport { planRefusal, roomRefusal } from './product-drafts'\n\n/*\n * `POST /api/commerce/products/save` and `POST /api/commerce/products/stock`\n * (AGL-3652): the console product editor's save and its Adjust stock dialog,\n * for the native apps.\n *\n * The console makes both writes from the browser, computing derived fields\n * first (search keys, stock verdict, price, image, smart-collection\n * membership). These routes run THAT computation (`model/product-write.ts`,\n * which the console imports too) and make the same writes, under the gate\n * the Firestore rules put on them: a verified member with a write role on the\n * site (`canWriteHostContent`), or staff, while the site, its org and the\n * platform are not frozen (`hostWritesFrozen`). A new product also meets the\n * create path's own door: the plan's `commerce` entitlement and its\n * `productsPerHost` quota, counted in the same transaction as the create.\n */\n\ntype Firestore = FirebaseFirestore.Firestore\n\nconst ROLE_REFUSAL = 'Editing requires the editor role'\nconst MAX_BODY_BYTES = 256_000\n\n/** The reasons the Adjust stock dialog offers. */\nexport const STOCK_ADJUST_REASONS: readonly InventoryAdjustmentReason[] = ['restock', 'correction', 'damage', 'refund']\n\nexport interface ProductsWriteDeps {\n firestore?: () => Firestore\n verifyIdToken?: (token: string) => Promise<Record<string, unknown> & { uid: string }>\n orgForHost?: (hostId: string) => Promise<{ org: Record<string, unknown> } | null>\n lockdown?: (options: Parameters<typeof getLockdownVerdict>[0]) => Promise<unknown>\n dropCache?: typeof dropPluginSiteCache\n now?: () => number\n}\n\ntype Reply = Parameters<PluginApiHandler>[1]\n\nfunction bodyOf(req: Parameters<PluginApiHandler>[0]): Record<string, unknown> {\n const raw = req.body\n if (typeof raw === 'string') {\n if (raw.length > MAX_BODY_BYTES) return {}\n try {\n return JSON.parse(raw || '{}')\n } catch {\n return {}\n }\n }\n return (raw ?? {}) as Record<string, unknown>\n}\n\nconst validId = (value: unknown): value is string =>\n typeof value === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(value)\n\ninterface Gate {\n uid: string\n staff: boolean\n host: FirebaseFirestore.DocumentSnapshot\n org: Record<string, unknown>\n}\n\n/**\n * Who may write this site's products, as the rules decide it. Answers the\n * caller's reply on a refusal and returns null.\n */\nasync function gate(req: Parameters<PluginApiHandler>[0], res: Reply, hostId: string, deps: Required<ProductsWriteDeps>): Promise<Gate | null> {\n const authorization = String(req.headers.authorization ?? '')\n const token = authorization.startsWith('Bearer ') ? authorization.slice('Bearer '.length) : ''\n if (!token) return void res.status(401).json({ error: 'Unauthenticated' }), null\n let decoded: Record<string, unknown> & { uid: string }\n try {\n decoded = await deps.verifyIdToken(token)\n } catch (error) {\n // Only a refused token is the caller's; an Auth outage is a 500 (AGL-2852).\n if (!isRefusedIdToken(error)) throw error\n return void res.status(401).json({ error: 'Sign in again to continue' }), null\n }\n const staff = decoded['staff'] === true\n if (!staff && decoded['email_verified'] !== true && !isImpersonationSession(decoded as never)) {\n return void res.status(403).json({ error: 'Verify your email address to make changes' }), null\n }\n const host = await deps.firestore().collection('hosts').doc(hostId).get()\n if (!host.exists) return void res.status(404).json({ error: 'Unknown site' }), null\n if (!staff && !hostRoleCanWrite((host.get('memberRoles') ?? {})[decoded.uid])) {\n return void res.status(403).json({ error: ROLE_REFUSAL }), null\n }\n const org = ((await deps.orgForHost(hostId))?.org ?? {}) as Record<string, unknown>\n const locked = await deps.lockdown({ staff, uid: decoded.uid, org: org as never, host: host.data() as never, request: { method: 'POST' } as never })\n if (locked) return void res.status(423).json({ error: 'This site is not accepting changes right now' }), null\n return { uid: decoded.uid, staff, host, org }\n}\n\n/** The site-wide cache drop a product change is owed (AGL-3386): an outbox entry in the write, then the drop. */\nfunction outboxEntry(firestore: Firestore, hostId: string) {\n return {\n ref: firestore.collection('publishOutbox').doc(createResourceUid()),\n data: { hostId, paths: ['/'], createdAt: FieldValue.serverTimestamp(), attempts: 0, entireHost: true },\n }\n}\n\nasync function settleCache(deps: Required<ProductsWriteDeps>, hostId: string, entry: FirebaseFirestore.DocumentReference, reason: string) {\n const dropped = await deps.dropCache({ hostIds: [hostId], reason }).catch(() => ({ complete: false }))\n if (dropped.complete) await entry.delete().catch(() => undefined)\n}\n\nfunction resolved(deps: ProductsWriteDeps): Required<ProductsWriteDeps> {\n return {\n firestore: deps.firestore ?? (() => firebaseAdmin.app().firestore() as unknown as Firestore),\n verifyIdToken: deps.verifyIdToken ?? ((token) => firebaseAdmin.app().auth().verifyIdToken(token) as never),\n orgForHost: deps.orgForHost ?? ((hostId) => getOrgForHost(hostId) as never),\n lockdown: deps.lockdown ?? ((options) => getLockdownVerdict(options)),\n dropCache: deps.dropCache ?? dropPluginSiteCache,\n now: deps.now ?? (() => Date.now()),\n }\n}\n\n/**\n * Saves a product as the console editor does. `create: true` makes a new\n * product under the client-minted `productId` (a retry finds its own product\n * and answers `replayed`); otherwise it replaces the live product, keeping\n * its live stock holds.\n */\nexport function createProductSaveHandler(options: ProductsWriteDeps = {}): PluginApiHandler {\n return async (req, res) => {\n if (req.method !== 'POST') return res.status(405).json({ error: 'Method not allowed' })\n const deps = resolved(options)\n const body = bodyOf(req)\n const hostId = body['hostId']\n const productId = body['productId']\n const product = body['product'] as EditedProduct | undefined\n if (!validId(hostId) || !validId(productId) || !product || typeof product !== 'object' || !Array.isArray(product.variants)) {\n return res.status(400).json({ error: 'Missing site, product or its variants' })\n }\n if (typeof product.name !== 'string' || !product.name.trim()) return res.status(400).json({ error: 'Product name is required' })\n try {\n const door = await gate(req, res, hostId, deps)\n if (!door) return\n const nowMs = deps.now()\n const fields = productSaveFields(product, nowMs)\n const problem = validateProduct(fields as never)\n if (problem) return res.status(400).json({ error: problem })\n const db = deps.firestore()\n const hostRef = db.collection('hosts').doc(hostId)\n const products = hostRef.collection('products')\n const productRef = products.doc(productId)\n const smartQuery = hostRef.collection('collections').where('kind', '==', 'catalog').where('mode', '==', 'smart')\n const outbox = outboxEntry(db, hostId)\n const create = body['create'] === true\n const answer = await db.runTransaction(async (tx) => {\n const [existing, smart] = await Promise.all([tx.get(productRef), tx.get(smartQuery)])\n const membership = {\n collectionIds: productCollectionIds(\n productMembershipInput(fields, product) as never,\n smart.docs.map((doc) => smartCollectionRulesOf(doc.id, doc.data())),\n ),\n }\n if (create) {\n if (existing.exists) return { status: 200, body: { ok: true, id: productId, replayed: true } }\n const early = planRefusal(door.org as never)\n if (early) return { status: early.status, body: { error: early.error } }\n const used = (await tx.get(products.count())).data().count\n const room = roomRefusal(door.org as never, Number(used) || 0)\n if (room) return { status: room.status, body: { error: room.error } }\n tx.create(productRef, {\n ...fields,\n ...membership,\n createdAtMs: nowMs,\n deletedAt: null,\n createdAt: FieldValue.serverTimestamp(),\n updatedAt: FieldValue.serverTimestamp(),\n createdBy: door.uid,\n })\n } else {\n if (!existing.exists || existing.get('deletedAt')) return { status: 404, body: { error: 'This product was deleted' } }\n tx.set(productRef, {\n ...storedTimestamps(existing.data() ?? {}, product as unknown as Record<string, unknown>),\n ...productEditWrite(fields, existing.get('stockHolds')),\n ...membership,\n updatedAt: FieldValue.serverTimestamp(),\n })\n }\n tx.set(outbox.ref, outbox.data)\n return { status: 200, body: { ok: true, id: productId, replayed: false } }\n })\n if (answer.status === 200 && !(answer.body as { replayed?: boolean }).replayed) {\n await settleCache(deps, hostId, outbox.ref, 'product saved from the app')\n }\n return res.status(answer.status).json(answer.body)\n } catch (error) {\n console.error('[commerce/products/save]', error)\n return res.status(500).json({ error: 'The product could not be saved' })\n }\n }\n}\n\n/**\n * The stored timestamps an edit keeps. The console's replace writes back the\n * Timestamps its seed carried (`createdAt`, a publish time); a client's JSON\n * cannot carry one, so each stored Timestamp field the request does not name\n * is kept as stored.\n */\nexport function storedTimestamps(stored: Record<string, unknown>, asked: Record<string, unknown>): Record<string, unknown> {\n return Object.fromEntries(\n Object.entries(stored).filter(\n ([key, value]) => !(key in asked) && typeof (value as { toDate?: unknown } | null)?.toDate === 'function',\n ),\n )\n}\n\n/** The ledger row id one attempt owns: the same key from the same member is the same row. */\nexport function stockAttemptId(uid: string, key: string): string {\n return 'app-' + createHash('sha256').update(`${uid}:${key}`).digest('hex').slice(0, 40)\n}\n\n/**\n * Adjusts one variant's stock as the Adjust stock dialog does: the variants\n * and stock fields on the product, and one `inventoryAdjustments` row, in one\n * transaction. The Idempotency-Key names that row, so a retried attempt finds\n * it and moves nothing twice.\n */\nexport function createProductStockHandler(options: ProductsWriteDeps = {}): PluginApiHandler {\n return async (req, res) => {\n if (req.method !== 'POST') return res.status(405).json({ error: 'Method not allowed' })\n const deps = resolved(options)\n const body = bodyOf(req)\n const hostId = body['hostId']\n const productId = body['productId']\n const variantId = body['variantId']\n const key = String(req.headers['idempotency-key'] ?? '').trim().slice(0, 200)\n const reason = body['reason'] as InventoryAdjustmentReason\n const delta = Math.round(Number(body['delta']))\n const locationId = typeof body['locationId'] === 'string' && body['locationId'] ? body['locationId'] : undefined\n if (!validId(hostId) || !validId(productId) || typeof variantId !== 'string' || !variantId) {\n return res.status(400).json({ error: 'Missing site, product or variant' })\n }\n if (!key) return res.status(400).json({ error: 'Missing Idempotency-Key' })\n if (!STOCK_ADJUST_REASONS.includes(reason)) return res.status(400).json({ error: 'Unknown reason' })\n if (!Number.isFinite(delta) || delta === 0) return res.status(400).json({ error: 'Enter a number of units' })\n try {\n const door = await gate(req, res, hostId, deps)\n if (!door) return\n const db = deps.firestore()\n const hostRef = db.collection('hosts').doc(hostId)\n const productRef = hostRef.collection('products').doc(productId)\n const ledgerRef = hostRef.collection('inventoryAdjustments').doc(stockAttemptId(door.uid, key))\n const outbox = outboxEntry(db, hostId)\n const answer = await db.runTransaction(async (tx) => {\n const [ledger, stored] = await Promise.all([tx.get(ledgerRef), tx.get(productRef)])\n if (ledger.exists) {\n return { status: 200, body: { ok: true, replayed: true, inventory: stored.get('inventory') ?? null } }\n }\n if (!stored.exists || stored.get('deletedAt')) return { status: 404, body: { error: 'This product was deleted' } }\n const product = stored.data() as never as Parameters<typeof stockAdjustmentWrite>[0]\n if (!(product.variants ?? []).some((variant) => variant.id === variantId)) {\n return { status: 404, body: { error: 'That variant is no longer on this product' } }\n }\n const write = stockAdjustmentWrite(product, productId, { variantId, delta, reason, locationId }, deps.now())\n if (!write) return { status: 400, body: { error: 'Enter a number of units' } }\n tx.update(productRef, write.update)\n tx.create(ledgerRef, write.ledger)\n tx.set(outbox.ref, outbox.data)\n return { status: 200, body: { ok: true, replayed: false, inventory: (write.update as { inventory?: unknown }).inventory ?? null } }\n })\n if (answer.status === 200 && !(answer.body as { replayed?: boolean }).replayed) {\n await settleCache(deps, hostId, outbox.ref, 'stock adjusted from the app')\n }\n return res.status(answer.status).json(answer.body)\n } catch (error) {\n console.error('[commerce/products/stock]', error)\n return res.status(500).json({ error: 'Stock could not be adjusted' })\n }\n }\n}\n\nexport const productSaveHandler = createProductSaveHandler()\nexport const productStockHandler = createProductStockHandler()\n"],"names":["createHash","isRefusedIdToken","createResourceUid","hostRoleCanWrite","dropPluginSiteCache","firebaseAdmin","getLockdownVerdict","getOrgForHost","isImpersonationSession","FieldValue","productCollectionIds","productEditWrite","productMembershipInput","productSaveFields","smartCollectionRulesOf","stockAdjustmentWrite","validateProduct","planRefusal","roomRefusal","ROLE_REFUSAL","MAX_BODY_BYTES","STOCK_ADJUST_REASONS","bodyOf","req","raw","body","length","JSON","parse","validId","value","test","gate","res","hostId","deps","host","authorization","String","headers","token","startsWith","slice","status","json","error","decoded","verifyIdToken","staff","firestore","collection","doc","get","exists","uid","org","orgForHost","locked","lockdown","data","request","method","outboxEntry","ref","paths","createdAt","serverTimestamp","attempts","entireHost","settleCache","entry","reason","dropped","dropCache","hostIds","catch","complete","delete","undefined","resolved","app","auth","options","now","Date","createProductSaveHandler","productId","product","Array","isArray","variants","name","trim","door","nowMs","fields","problem","db","hostRef","products","productRef","smartQuery","where","outbox","create","answer","runTransaction","tx","existing","smart","Promise","all","membership","collectionIds","docs","map","id","ok","replayed","early","used","count","room","Number","createdAtMs","deletedAt","updatedAt","createdBy","set","storedTimestamps","console","stored","asked","Object","fromEntries","entries","filter","key","toDate","stockAttemptId","update","digest","createProductStockHandler","variantId","delta","Math","round","locationId","includes","isFinite","ledgerRef","ledger","in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A,OAAO9B,IAAIU,MAAM,CAAC8D,OAAO9D,MAAM,EAAEC,IAAI,CAAC6D,OAAOhF,IAAI;QACnD,EAAE,OAAOoB,OAAO;YACdqF,QAAQrF,KAAK,CAAC,6BAA6BA;YAC3C,OAAOZ,IAAIU,MAAM,CAAC,KAAKC,IAAI,CAAC;gBAAEC,OAAO;YAA8B;QACrE;IACF;AACF;AAEA,OAAO,MAAM+G,qBAAqBvE,2BAA0B;AAC5D,OAAO,MAAMwE,sBAAsBf,4BAA2B"}
|
package/src/lib/server/refund.js
CHANGED
|
@@ -342,6 +342,9 @@ import { posRegisterRefundAuthority } from "./pos-refund-authority.js";
|
|
|
342
342
|
hostId,
|
|
343
343
|
orderId,
|
|
344
344
|
kind: 'commerce-refund',
|
|
345
|
+
// The site, as every claim names its scope: the staff claims list
|
|
346
|
+
// sorts by `scopeId`, and an `orderBy` drops a claim without it.
|
|
347
|
+
scopeId: hostId,
|
|
345
348
|
status: 'pending',
|
|
346
349
|
createdAt: firebaseAdmin.firestore.FieldValue.serverTimestamp(),
|
|
347
350
|
createdAtMs: Date.now(),
|