@endora-commerce/contracts 0.107.0 → 0.109.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/dist/admin-actions.d.ts +12 -3
- package/dist/admin-actions.d.ts.map +1 -1
- package/dist/admin-actions.js +34 -3
- package/dist/admin-actions.js.map +1 -1
- package/dist/admin-contributions.d.ts +35 -6
- package/dist/admin-contributions.d.ts.map +1 -1
- package/dist/admin-contributions.js +46 -0
- package/dist/admin-contributions.js.map +1 -1
- package/dist/admin-feedback.d.ts +61 -0
- package/dist/admin-feedback.d.ts.map +1 -0
- package/dist/admin-feedback.js +71 -0
- package/dist/admin-feedback.js.map +1 -0
- package/dist/admin-name.d.ts +21 -0
- package/dist/admin-name.d.ts.map +1 -0
- package/dist/admin-name.js +30 -0
- package/dist/admin-name.js.map +1 -0
- package/dist/admin-notifications.d.ts +17 -1
- package/dist/admin-notifications.d.ts.map +1 -1
- package/dist/admin-preferences.d.ts +108 -0
- package/dist/admin-preferences.d.ts.map +1 -0
- package/dist/admin-preferences.js +133 -0
- package/dist/admin-preferences.js.map +1 -0
- package/dist/admin.d.ts +15 -1
- package/dist/admin.d.ts.map +1 -1
- package/dist/admin.js +7 -0
- package/dist/admin.js.map +1 -1
- package/dist/assets-library.d.ts +97 -3
- package/dist/assets-library.d.ts.map +1 -1
- package/dist/assets-library.js +5 -0
- package/dist/assets-library.js.map +1 -1
- package/dist/blog.d.ts +57 -0
- package/dist/blog.d.ts.map +1 -1
- package/dist/blog.js +63 -1
- package/dist/blog.js.map +1 -1
- package/dist/carts.d.ts +303 -50
- package/dist/carts.d.ts.map +1 -1
- package/dist/carts.js +95 -13
- package/dist/carts.js.map +1 -1
- package/dist/catalog.d.ts +512 -40
- package/dist/catalog.d.ts.map +1 -1
- package/dist/catalog.js +362 -23
- package/dist/catalog.js.map +1 -1
- package/dist/cms-brand.d.ts +2233 -0
- package/dist/cms-brand.d.ts.map +1 -0
- package/dist/cms-brand.js +311 -0
- package/dist/cms-brand.js.map +1 -0
- package/dist/cms-contact-form.d.ts +55 -0
- package/dist/cms-contact-form.d.ts.map +1 -0
- package/dist/cms-contact-form.js +100 -0
- package/dist/cms-contact-form.js.map +1 -0
- package/dist/cms.d.ts +537 -3
- package/dist/cms.d.ts.map +1 -1
- package/dist/cms.js +251 -0
- package/dist/cms.js.map +1 -1
- package/dist/common.d.ts +4 -0
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +14 -0
- package/dist/common.js.map +1 -1
- package/dist/comparisons.d.ts +7 -0
- package/dist/comparisons.d.ts.map +1 -1
- package/dist/comparisons.js +2 -0
- package/dist/comparisons.js.map +1 -1
- package/dist/credit-limits.d.ts.map +1 -1
- package/dist/credit-limits.js +3 -2
- package/dist/credit-limits.js.map +1 -1
- package/dist/crm-rich-text.d.ts +13 -0
- package/dist/crm-rich-text.d.ts.map +1 -0
- package/dist/crm-rich-text.js +13 -0
- package/dist/crm-rich-text.js.map +1 -0
- package/dist/crm.d.ts +180 -62
- package/dist/crm.d.ts.map +1 -1
- package/dist/crm.js +63 -107
- package/dist/crm.js.map +1 -1
- package/dist/custom-fields.d.ts +436 -5
- package/dist/custom-fields.d.ts.map +1 -1
- package/dist/custom-fields.js +325 -10
- package/dist/custom-fields.js.map +1 -1
- package/dist/customer-accounts.d.ts +30 -0
- package/dist/customer-accounts.d.ts.map +1 -1
- package/dist/customer-accounts.js +31 -0
- package/dist/customer-accounts.js.map +1 -1
- package/dist/document-templates.d.ts +211 -23
- package/dist/document-templates.d.ts.map +1 -1
- package/dist/document-templates.js +98 -1
- package/dist/document-templates.js.map +1 -1
- package/dist/document-values.d.ts +12 -1
- package/dist/document-values.d.ts.map +1 -1
- package/dist/document-values.js +41 -11
- package/dist/document-values.js.map +1 -1
- package/dist/email.d.ts +5 -0
- package/dist/email.d.ts.map +1 -1
- package/dist/errors.d.ts +142 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +189 -8
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +24 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +43 -1
- package/dist/index.js.map +1 -1
- package/dist/inventory.d.ts +3 -3
- package/dist/invoices.d.ts +6 -2
- package/dist/invoices.d.ts.map +1 -1
- package/dist/invoices.js +10 -4
- package/dist/invoices.js.map +1 -1
- package/dist/line-groups.d.ts +80 -0
- package/dist/line-groups.d.ts.map +1 -0
- package/dist/line-groups.js +130 -0
- package/dist/line-groups.js.map +1 -0
- package/dist/localized-name.d.ts +20 -0
- package/dist/localized-name.d.ts.map +1 -0
- package/dist/localized-name.js +45 -0
- package/dist/localized-name.js.map +1 -0
- package/dist/megamenu.d.ts +674 -0
- package/dist/megamenu.d.ts.map +1 -1
- package/dist/megamenu.js +96 -3
- package/dist/megamenu.js.map +1 -1
- package/dist/mfa.d.ts +1 -0
- package/dist/mfa.d.ts.map +1 -1
- package/dist/mfa.js +8 -2
- package/dist/mfa.js.map +1 -1
- package/dist/modules.d.ts +8 -1
- package/dist/modules.d.ts.map +1 -1
- package/dist/modules.js +5 -0
- package/dist/modules.js.map +1 -1
- package/dist/money-decimal.d.ts +162 -0
- package/dist/money-decimal.d.ts.map +1 -0
- package/dist/money-decimal.js +365 -0
- package/dist/money-decimal.js.map +1 -0
- package/dist/money-input.d.ts +64 -0
- package/dist/money-input.d.ts.map +1 -0
- package/dist/money-input.js +111 -0
- package/dist/money-input.js.map +1 -0
- package/dist/newsletter.d.ts +3 -3
- package/dist/order-minimums.d.ts +111 -0
- package/dist/order-minimums.d.ts.map +1 -0
- package/dist/order-minimums.js +196 -0
- package/dist/order-minimums.js.map +1 -0
- package/dist/orders.d.ts +206 -2
- package/dist/orders.d.ts.map +1 -1
- package/dist/orders.js +106 -5
- package/dist/orders.js.map +1 -1
- package/dist/page-builder-props.d.ts +1401 -0
- package/dist/page-builder-props.d.ts.map +1 -0
- package/dist/page-builder-props.js +266 -0
- package/dist/page-builder-props.js.map +1 -0
- package/dist/page-builder-values.d.ts +95 -0
- package/dist/page-builder-values.d.ts.map +1 -0
- package/dist/page-builder-values.js +164 -0
- package/dist/page-builder-values.js.map +1 -0
- package/dist/payment-methods.d.ts.map +1 -1
- package/dist/payment-methods.js +2 -1
- package/dist/payment-methods.js.map +1 -1
- package/dist/pim-connector.d.ts +1 -1
- package/dist/platform-language.d.ts +9 -0
- package/dist/platform-language.d.ts.map +1 -1
- package/dist/platform-language.js +14 -0
- package/dist/platform-language.js.map +1 -1
- package/dist/price-book-decimal.d.ts +84 -0
- package/dist/price-book-decimal.d.ts.map +1 -0
- package/dist/price-book-decimal.js +175 -0
- package/dist/price-book-decimal.js.map +1 -0
- package/dist/price-books.d.ts +2206 -0
- package/dist/price-books.d.ts.map +1 -0
- package/dist/price-books.js +777 -0
- package/dist/price-books.js.map +1 -0
- package/dist/price-lists.d.ts +309 -614
- package/dist/price-lists.d.ts.map +1 -1
- package/dist/price-lists.js +113 -453
- package/dist/price-lists.js.map +1 -1
- package/dist/product-feeds.d.ts +195 -54
- package/dist/product-feeds.d.ts.map +1 -1
- package/dist/product-feeds.js +36 -1
- package/dist/product-feeds.js.map +1 -1
- package/dist/product-scopes.d.ts +628 -0
- package/dist/product-scopes.d.ts.map +1 -0
- package/dist/product-scopes.js +365 -0
- package/dist/product-scopes.js.map +1 -0
- package/dist/product-summary-text.d.ts +23 -0
- package/dist/product-summary-text.d.ts.map +1 -0
- package/dist/product-summary-text.js +64 -0
- package/dist/product-summary-text.js.map +1 -0
- package/dist/product-url-keys.d.ts +204 -0
- package/dist/product-url-keys.d.ts.map +1 -0
- package/dist/product-url-keys.js +166 -0
- package/dist/product-url-keys.js.map +1 -0
- package/dist/product-value-resolver.d.ts +6 -0
- package/dist/product-value-resolver.d.ts.map +1 -1
- package/dist/product-value-resolver.js +6 -1
- package/dist/product-value-resolver.js.map +1 -1
- package/dist/projects.d.ts +2355 -0
- package/dist/projects.d.ts.map +1 -0
- package/dist/projects.js +837 -0
- package/dist/projects.js.map +1 -0
- package/dist/promotions.d.ts +2 -2
- package/dist/promotions.d.ts.map +1 -1
- package/dist/promotions.js +8 -2
- package/dist/promotions.js.map +1 -1
- package/dist/prompt-actions.d.ts +19 -1
- package/dist/prompt-actions.d.ts.map +1 -1
- package/dist/prompt-actions.js +31 -2
- package/dist/prompt-actions.js.map +1 -1
- package/dist/quick-order.d.ts +39 -1
- package/dist/quick-order.d.ts.map +1 -1
- package/dist/quick-order.js +44 -1
- package/dist/quick-order.js.map +1 -1
- package/dist/quote-requests.d.ts +305 -1
- package/dist/quote-requests.d.ts.map +1 -1
- package/dist/quote-requests.js +173 -5
- package/dist/quote-requests.js.map +1 -1
- package/dist/record-references.d.ts +252 -0
- package/dist/record-references.d.ts.map +1 -0
- package/dist/record-references.js +179 -0
- package/dist/record-references.js.map +1 -0
- package/dist/record-rich-text.d.ts +66 -0
- package/dist/record-rich-text.d.ts.map +1 -0
- package/dist/record-rich-text.js +303 -0
- package/dist/record-rich-text.js.map +1 -0
- package/dist/returns.d.ts +81 -2
- package/dist/returns.d.ts.map +1 -1
- package/dist/returns.js +39 -4
- package/dist/returns.js.map +1 -1
- package/dist/sales-channels.d.ts +53 -7
- package/dist/sales-channels.d.ts.map +1 -1
- package/dist/sales-channels.js +40 -4
- package/dist/sales-channels.js.map +1 -1
- package/dist/saved-list-views.d.ts +28 -0
- package/dist/saved-list-views.d.ts.map +1 -1
- package/dist/saved-list-views.js +14 -1
- package/dist/saved-list-views.js.map +1 -1
- package/dist/scoped-translations.d.ts +48 -0
- package/dist/scoped-translations.d.ts.map +1 -0
- package/dist/scoped-translations.js +78 -0
- package/dist/scoped-translations.js.map +1 -0
- package/dist/search.d.ts +73 -5
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +4 -0
- package/dist/search.js.map +1 -1
- package/dist/seo.d.ts +64 -5
- package/dist/seo.d.ts.map +1 -1
- package/dist/seo.js +31 -4
- package/dist/seo.js.map +1 -1
- package/dist/settings.d.ts +32 -3
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +37 -3
- package/dist/settings.js.map +1 -1
- package/dist/shipping-methods.d.ts.map +1 -1
- package/dist/shipping-methods.js +2 -1
- package/dist/shipping-methods.js.map +1 -1
- package/dist/shopping-lists.d.ts +24 -0
- package/dist/shopping-lists.d.ts.map +1 -1
- package/dist/shopping-lists.js +20 -1
- package/dist/shopping-lists.js.map +1 -1
- package/dist/storefront-cache-tags.d.ts +29 -0
- package/dist/storefront-cache-tags.d.ts.map +1 -1
- package/dist/storefront-cache-tags.js +24 -0
- package/dist/storefront-cache-tags.js.map +1 -1
- package/dist/storefront-language-path.d.ts +77 -0
- package/dist/storefront-language-path.d.ts.map +1 -0
- package/dist/storefront-language-path.js +123 -0
- package/dist/storefront-language-path.js.map +1 -0
- package/dist/storefront-value-overrides.d.ts +67 -0
- package/dist/storefront-value-overrides.d.ts.map +1 -0
- package/dist/storefront-value-overrides.js +93 -0
- package/dist/storefront-value-overrides.js.map +1 -0
- package/dist/tasks.d.ts +2842 -0
- package/dist/tasks.d.ts.map +1 -0
- package/dist/tasks.js +901 -0
- package/dist/tasks.js.map +1 -0
- package/dist/transactional-emails.d.ts +7 -4
- package/dist/transactional-emails.d.ts.map +1 -1
- package/dist/transactional-emails.js +13 -0
- package/dist/transactional-emails.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,777 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { isoDateTimeSchema, uuidSchema } from './common.js';
|
|
3
|
+
/**
|
|
4
|
+
* Price books — feature 160, D-287 P1 (`specs/160-pricing-model-v2/`).
|
|
5
|
+
*
|
|
6
|
+
* A price book is a header that fixes one currency, a set of sales channels and
|
|
7
|
+
* an audience set of one kind (everyone, a set of customer groups or a set of
|
|
8
|
+
* organizations), and rows that price a **selection** — a variant, a product,
|
|
9
|
+
* a category ∩ attribute value, a category, an attribute value or every product
|
|
10
|
+
* — for a quantity window, as a fixed amount, a signed percentage or a signed
|
|
11
|
+
* amount adjustment of a reference price (`specs/pricing/decision-record.md`
|
|
12
|
+
* §5.1). The semantics are D-287's and are not restated here; these are the
|
|
13
|
+
* shapes, defined before any route (Principle II).
|
|
14
|
+
*
|
|
15
|
+
* The rows are stored in `price_list_matrix_entries` and the header in
|
|
16
|
+
* `price_lists`, both owned by `price_lists`: a book is the evolved price list,
|
|
17
|
+
* which is why the database keeps the old names (research R3).
|
|
18
|
+
*/
|
|
19
|
+
const CURRENCY = z.string().regex(/^[A-Z]{3}$/, 'ISO 4217 currency code');
|
|
20
|
+
const POSITIVE_INT = z.number().int().positive();
|
|
21
|
+
/** A non-negative amount with at most four fractional digits. */
|
|
22
|
+
export const PRICE_BOOK_AMOUNT = z
|
|
23
|
+
.string()
|
|
24
|
+
.regex(/^\d{1,10}(\.\d{1,4})?$/, 'non-negative decimal with up to 4 fractional digits');
|
|
25
|
+
/** A signed amount adjustment with at most four fractional digits. */
|
|
26
|
+
export const PRICE_BOOK_SIGNED_AMOUNT = z
|
|
27
|
+
.string()
|
|
28
|
+
.regex(/^-?\d{1,10}(\.\d{1,4})?$/, 'signed decimal with up to 4 fractional digits');
|
|
29
|
+
/** The bounds of a signed percentage (owner ruling N9): a free line to an elevenfold price. */
|
|
30
|
+
export const PRICE_BOOK_PERCENT_MIN = -100;
|
|
31
|
+
export const PRICE_BOOK_PERCENT_MAX = 1000;
|
|
32
|
+
/** A signed percentage with at most two fractional digits, within the N9 bounds. */
|
|
33
|
+
export const PRICE_BOOK_PERCENT = z.string().superRefine((value, ctx) => {
|
|
34
|
+
if (!/^-?\d{1,4}(\.\d{1,2})?$/.test(value)) {
|
|
35
|
+
ctx.addIssue({ code: 'custom', message: 'signed percentage with up to 2 fractional digits' });
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const n = Number(value);
|
|
39
|
+
if (n < PRICE_BOOK_PERCENT_MIN || n > PRICE_BOOK_PERCENT_MAX) {
|
|
40
|
+
ctx.addIssue({
|
|
41
|
+
code: 'custom',
|
|
42
|
+
message: `percentage must be between ${PRICE_BOOK_PERCENT_MIN} and ${PRICE_BOOK_PERCENT_MAX}`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
// --- Header ------------------------------------------------------------------
|
|
47
|
+
/** `regular` prices the catalogue and buyer tiers; `sale` is charged only when lower. */
|
|
48
|
+
export const priceBookTypeSchema = z.enum(['regular', 'sale']);
|
|
49
|
+
export const priceBookStatusSchema = z.enum(['draft', 'scheduled', 'active', 'expired']);
|
|
50
|
+
export const priceBookAudienceKindSchema = z.enum(['everyone', 'customer_groups', 'organizations']);
|
|
51
|
+
/** Most members one audience set may name. */
|
|
52
|
+
export const PRICE_BOOK_MAX_MEMBERS = 1000;
|
|
53
|
+
/** Most channels one channel set may name. */
|
|
54
|
+
export const PRICE_BOOK_MAX_CHANNELS = 100;
|
|
55
|
+
/**
|
|
56
|
+
* The audience set (owner ruling Q1): one kind, never mixed. `everyone` names
|
|
57
|
+
* no member; the other two kinds name ids of that kind, which the service
|
|
58
|
+
* checks exist and are of the kind (FR-002, A4). A draft may name none; a book
|
|
59
|
+
* of those kinds needs at least one member to be activated.
|
|
60
|
+
*/
|
|
61
|
+
export const priceBookAudienceSchema = z
|
|
62
|
+
.object({
|
|
63
|
+
kind: priceBookAudienceKindSchema,
|
|
64
|
+
memberIds: z.array(uuidSchema).max(PRICE_BOOK_MAX_MEMBERS).default([]),
|
|
65
|
+
})
|
|
66
|
+
.superRefine((value, ctx) => {
|
|
67
|
+
if (value.kind === 'everyone' && value.memberIds.length > 0) {
|
|
68
|
+
ctx.addIssue({ code: 'custom', path: ['memberIds'], message: 'An everyone audience names no member.' });
|
|
69
|
+
}
|
|
70
|
+
if (new Set(value.memberIds).size !== value.memberIds.length) {
|
|
71
|
+
ctx.addIssue({ code: 'custom', path: ['memberIds'], message: 'A member is named twice.' });
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
export const priceBookChannelIdsSchema = z
|
|
75
|
+
.array(uuidSchema)
|
|
76
|
+
.max(PRICE_BOOK_MAX_CHANNELS)
|
|
77
|
+
.refine((ids) => new Set(ids).size === ids.length, 'A channel is named twice.');
|
|
78
|
+
const datesRefinement = (value, ctx) => {
|
|
79
|
+
if (value.startsAt && value.endsAt && Date.parse(value.endsAt) <= Date.parse(value.startsAt)) {
|
|
80
|
+
ctx.addIssue({ code: 'custom', path: ['endsAt'], message: 'endsAt must be after startsAt.' });
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
/** Body of `POST /api/v1/admin/price-books` (FR-001). The book is created as a draft. */
|
|
84
|
+
export const priceBookCreateSchema = z
|
|
85
|
+
.object({
|
|
86
|
+
name: z.string().trim().min(1).max(200),
|
|
87
|
+
type: priceBookTypeSchema,
|
|
88
|
+
currencyCode: CURRENCY,
|
|
89
|
+
channelIds: priceBookChannelIdsSchema.default([]),
|
|
90
|
+
audience: priceBookAudienceSchema,
|
|
91
|
+
priority: z.number().int().min(-1_000_000).max(1_000_000).default(0),
|
|
92
|
+
startsAt: isoDateTimeSchema.nullable().optional(),
|
|
93
|
+
endsAt: isoDateTimeSchema.nullable().optional(),
|
|
94
|
+
})
|
|
95
|
+
.superRefine(datesRefinement);
|
|
96
|
+
/**
|
|
97
|
+
* Body of `PATCH /api/v1/admin/price-books/:id`. The currency is not here: it is
|
|
98
|
+
* immutable (FR-004). The audience kind may change only while the book has
|
|
99
|
+
* never been active, which the service decides.
|
|
100
|
+
*/
|
|
101
|
+
export const priceBookPatchSchema = z
|
|
102
|
+
.object({
|
|
103
|
+
name: z.string().trim().min(1).max(200).optional(),
|
|
104
|
+
priority: z.number().int().min(-1_000_000).max(1_000_000).optional(),
|
|
105
|
+
startsAt: isoDateTimeSchema.nullable().optional(),
|
|
106
|
+
endsAt: isoDateTimeSchema.nullable().optional(),
|
|
107
|
+
channelIds: priceBookChannelIdsSchema.optional(),
|
|
108
|
+
audience: priceBookAudienceSchema.optional(),
|
|
109
|
+
})
|
|
110
|
+
.superRefine(datesRefinement);
|
|
111
|
+
// --- Rows --------------------------------------------------------------------
|
|
112
|
+
/** Derived from which selection coordinates are set; never stored. */
|
|
113
|
+
export const priceBookSelectionKindSchema = z.enum([
|
|
114
|
+
'variant',
|
|
115
|
+
'product',
|
|
116
|
+
'category_attribute',
|
|
117
|
+
'category',
|
|
118
|
+
'attribute',
|
|
119
|
+
'all',
|
|
120
|
+
]);
|
|
121
|
+
export const priceBookAttributeSchema = z.object({
|
|
122
|
+
key: z.string().min(1).max(64),
|
|
123
|
+
value: z.string().min(1).max(255),
|
|
124
|
+
});
|
|
125
|
+
export const priceBookSelectionSchema = z.object({
|
|
126
|
+
variantId: uuidSchema.nullable().optional(),
|
|
127
|
+
productId: uuidSchema.nullable().optional(),
|
|
128
|
+
categoryId: uuidSchema.nullable().optional(),
|
|
129
|
+
attribute: priceBookAttributeSchema.nullable().optional(),
|
|
130
|
+
});
|
|
131
|
+
/** `selectionKind` of a selection: variant > product > category ∩ attribute > category > attribute > all. */
|
|
132
|
+
export function priceBookSelectionKind(selection) {
|
|
133
|
+
if (selection.variantId)
|
|
134
|
+
return 'variant';
|
|
135
|
+
if (selection.productId)
|
|
136
|
+
return 'product';
|
|
137
|
+
if (selection.categoryId && selection.attribute)
|
|
138
|
+
return 'category_attribute';
|
|
139
|
+
if (selection.categoryId)
|
|
140
|
+
return 'category';
|
|
141
|
+
if (selection.attribute)
|
|
142
|
+
return 'attribute';
|
|
143
|
+
return 'all';
|
|
144
|
+
}
|
|
145
|
+
export const priceBookRowPriceSchema = z.discriminatedUnion('type', [
|
|
146
|
+
z.object({ type: z.literal('fixed'), amount: PRICE_BOOK_AMOUNT }),
|
|
147
|
+
z.object({ type: z.literal('percent'), percent: PRICE_BOOK_PERCENT }),
|
|
148
|
+
z.object({ type: z.literal('amount'), amount: PRICE_BOOK_SIGNED_AMOUNT }),
|
|
149
|
+
]);
|
|
150
|
+
/**
|
|
151
|
+
* The cross-field rules of a row (FR-005, FR-006): the CHECK constraints of
|
|
152
|
+
* `price_list_matrix_entries` that one field cannot express. One function so
|
|
153
|
+
* the request schema and the service's merge-then-validate apply the same rules.
|
|
154
|
+
*/
|
|
155
|
+
export function priceBookRowShapeIssues(row) {
|
|
156
|
+
const out = [];
|
|
157
|
+
const { selection } = row;
|
|
158
|
+
const named = (selection.variantId ?? null) !== null || (selection.productId ?? null) !== null;
|
|
159
|
+
if (named && ((selection.categoryId ?? null) !== null || (selection.attribute ?? null) !== null)) {
|
|
160
|
+
out.push({
|
|
161
|
+
path: ['selection'],
|
|
162
|
+
message: 'A product or variant selection cannot be combined with a category or an attribute.',
|
|
163
|
+
reason: 'selection_combined',
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
if (row.maxQuantity !== null && row.maxQuantity < row.minQuantity) {
|
|
167
|
+
out.push({ path: ['maxQuantity'], message: 'maxQuantity must be at least minQuantity.', reason: 'max_below_min' });
|
|
168
|
+
}
|
|
169
|
+
if (row.price.type === 'fixed' && priceBookSelectionKind(selection) === 'all') {
|
|
170
|
+
out.push({ path: ['price', 'type'], message: 'A fixed price needs a selection other than all products.', reason: 'fixed_on_all' });
|
|
171
|
+
}
|
|
172
|
+
return out;
|
|
173
|
+
}
|
|
174
|
+
export const priceBookRowInputSchema = z
|
|
175
|
+
.object({
|
|
176
|
+
id: uuidSchema.optional(),
|
|
177
|
+
selection: priceBookSelectionSchema,
|
|
178
|
+
minQuantity: POSITIVE_INT,
|
|
179
|
+
maxQuantity: POSITIVE_INT.nullable().default(null),
|
|
180
|
+
price: priceBookRowPriceSchema,
|
|
181
|
+
})
|
|
182
|
+
.superRefine((value, ctx) => {
|
|
183
|
+
for (const issue of priceBookRowShapeIssues(value)) {
|
|
184
|
+
ctx.addIssue({ code: 'custom', path: issue.path, message: issue.message });
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
/** At most this many rows per book, and per batch call (unchanged from feature 148). */
|
|
188
|
+
export const PRICE_BOOK_MAX_ROWS = 5000;
|
|
189
|
+
export const PRICE_BOOK_MAX_ROWS_PER_BATCH = 5000;
|
|
190
|
+
/** Body of `PUT /api/v1/admin/price-books/:id/rows` — one audited command (FR-035). */
|
|
191
|
+
export const priceBookRowBatchSchema = z
|
|
192
|
+
.object({
|
|
193
|
+
upsert: z.array(priceBookRowInputSchema).default([]),
|
|
194
|
+
delete: z.array(uuidSchema).default([]),
|
|
195
|
+
})
|
|
196
|
+
.refine((value) => value.upsert.length + value.delete.length <= PRICE_BOOK_MAX_ROWS_PER_BATCH, `A batch carries at most ${PRICE_BOOK_MAX_ROWS_PER_BATCH} rows.`);
|
|
197
|
+
// --- Base prices (FR-009) -----------------------------------------------------
|
|
198
|
+
export const basePriceTierSchema = z.object({
|
|
199
|
+
minQuantity: POSITIVE_INT,
|
|
200
|
+
maxQuantity: POSITIVE_INT.nullable().default(null),
|
|
201
|
+
amount: PRICE_BOOK_AMOUNT,
|
|
202
|
+
});
|
|
203
|
+
/**
|
|
204
|
+
* A variant's base row: its own tiers, or a percentage or amount adjustment of
|
|
205
|
+
* its product's base price at the same quantity (#377's three cases). `null`
|
|
206
|
+
* in a write means "inherits the product's base".
|
|
207
|
+
*/
|
|
208
|
+
export const variantBaseRowSchema = z.discriminatedUnion('type', [
|
|
209
|
+
z.object({ type: z.literal('fixed'), tiers: z.array(basePriceTierSchema).min(1).max(50) }),
|
|
210
|
+
z.object({ type: z.literal('percent'), percent: PRICE_BOOK_PERCENT }),
|
|
211
|
+
z.object({ type: z.literal('amount'), amount: PRICE_BOOK_SIGNED_AMOUNT }),
|
|
212
|
+
]);
|
|
213
|
+
export const basePricesForCurrencySchema = z.object({
|
|
214
|
+
product: z.array(basePriceTierSchema).max(50),
|
|
215
|
+
variants: z.record(uuidSchema, variantBaseRowSchema.nullable()).default({}),
|
|
216
|
+
});
|
|
217
|
+
/** Body of `PUT /api/v1/admin/price-books/base-prices/:productId` — one command across currencies. */
|
|
218
|
+
export const basePricesWriteSchema = z.object({
|
|
219
|
+
byCurrency: z.record(CURRENCY, basePricesForCurrencySchema),
|
|
220
|
+
});
|
|
221
|
+
// --- Resolution: source, cells, explanation (FR-020) ----------------------------
|
|
222
|
+
/** Which tier priced a line (FR-030). */
|
|
223
|
+
export const priceSourceSchema = z.enum(['base', 'catalogue', 'buyer', 'sale']);
|
|
224
|
+
/** What a cart or order line records about its unit price: a tier, or an agreed price. */
|
|
225
|
+
export const linePriceSourceSchema = z.enum(['agreed', 'base', 'catalogue', 'buyer', 'sale']);
|
|
226
|
+
/** The twelve buyer cells: audience kind × selection kind (decision-record §5.6). */
|
|
227
|
+
export const PRICE_BOOK_CELL_CODES = [
|
|
228
|
+
'org.variant',
|
|
229
|
+
'org.product',
|
|
230
|
+
'org.category_attribute',
|
|
231
|
+
'org.category',
|
|
232
|
+
'org.attribute',
|
|
233
|
+
'org.all',
|
|
234
|
+
'group.variant',
|
|
235
|
+
'group.product',
|
|
236
|
+
'group.category_attribute',
|
|
237
|
+
'group.category',
|
|
238
|
+
'group.attribute',
|
|
239
|
+
'group.all',
|
|
240
|
+
];
|
|
241
|
+
export const priceBookCellCodeSchema = z.enum(PRICE_BOOK_CELL_CODES);
|
|
242
|
+
/** Human labels of a row's selection, resolved in batch for the grid (G2). */
|
|
243
|
+
export const priceBookRowLabelsSchema = z.object({
|
|
244
|
+
productName: z.string().nullable(),
|
|
245
|
+
productSku: z.string().nullable(),
|
|
246
|
+
variantLabel: z.string().nullable(),
|
|
247
|
+
variantSku: z.string().nullable(),
|
|
248
|
+
/** Category names from the root, joined by ` › `. */
|
|
249
|
+
categoryPath: z.string().nullable(),
|
|
250
|
+
attributeLabel: z.string().nullable(),
|
|
251
|
+
attributeValueLabel: z.string().nullable(),
|
|
252
|
+
});
|
|
253
|
+
/** Most runner-up candidates an explanation lists. */
|
|
254
|
+
export const PRICE_BOOK_MAX_EXPLANATION_CANDIDATES = 10;
|
|
255
|
+
export const priceBookExplanationCandidateSchema = z.object({
|
|
256
|
+
cell: priceBookCellCodeSchema.nullable(),
|
|
257
|
+
bookId: uuidSchema,
|
|
258
|
+
/** Feature 161 (G16) — the runner-up's book, by name. */
|
|
259
|
+
bookName: z.string(),
|
|
260
|
+
rowId: uuidSchema,
|
|
261
|
+
/** Feature 161 (G16) — what the runner-up's row selects, in words (all `null` once the row is gone). */
|
|
262
|
+
labels: priceBookRowLabelsSchema,
|
|
263
|
+
amount: z.string(),
|
|
264
|
+
});
|
|
265
|
+
export const priceBookExplanationSchema = z.object({
|
|
266
|
+
tier: priceSourceSchema.nullable(),
|
|
267
|
+
cell: priceBookCellCodeSchema.nullable().optional(),
|
|
268
|
+
bookId: uuidSchema.nullable().optional(),
|
|
269
|
+
bookName: z.string().nullable().optional(),
|
|
270
|
+
rowId: uuidSchema.nullable().optional(),
|
|
271
|
+
/** The reference a percentage or adjustment was taken off, when one was. */
|
|
272
|
+
reference: z.string().nullable().optional(),
|
|
273
|
+
precedenceVersion: z.number().int().positive(),
|
|
274
|
+
candidates: z
|
|
275
|
+
.array(priceBookExplanationCandidateSchema)
|
|
276
|
+
.max(PRICE_BOOK_MAX_EXPLANATION_CANDIDATES)
|
|
277
|
+
.optional(),
|
|
278
|
+
});
|
|
279
|
+
// --- Precedence (FR-021–FR-023) -----------------------------------------------
|
|
280
|
+
export const precedenceDepthModeSchema = z.enum(['within_cell', 'nearest_organization_first']);
|
|
281
|
+
export const precedencePresetNameSchema = z.enum(['AF', 'SF', 'HY', 'AF-N']);
|
|
282
|
+
/**
|
|
283
|
+
* A precedence configuration: the order of the twelve buyer cells, the
|
|
284
|
+
* category-before-attribute toggle and the depth mode. The shape is checked
|
|
285
|
+
* here; the invariants I1–I5 are the pure `validatePrecedence` of
|
|
286
|
+
* `price_lists`, which the resolver and the (P3) write path share.
|
|
287
|
+
*/
|
|
288
|
+
export const precedenceConfigSchema = z.object({
|
|
289
|
+
cells: z
|
|
290
|
+
.array(priceBookCellCodeSchema)
|
|
291
|
+
.length(PRICE_BOOK_CELL_CODES.length)
|
|
292
|
+
.refine((cells) => new Set(cells).size === cells.length, 'Every cell is named exactly once.'),
|
|
293
|
+
categoryBeforeAttribute: z.boolean(),
|
|
294
|
+
depthMode: precedenceDepthModeSchema,
|
|
295
|
+
});
|
|
296
|
+
// --- Admin API (US7, `contracts/admin-price-books-api.md`) ---------------------
|
|
297
|
+
/** A channel binding of a book; `dormant` while the channel does not offer the book's currency (FR-011). */
|
|
298
|
+
export const priceBookChannelBindingSchema = z.object({
|
|
299
|
+
channelId: uuidSchema,
|
|
300
|
+
dormant: z.boolean(),
|
|
301
|
+
});
|
|
302
|
+
/**
|
|
303
|
+
* A book as the admin reads it. `version` moves on every write to the book —
|
|
304
|
+
* header, channel set, members, rows — and a write that names an older one is
|
|
305
|
+
* refused `409 PRICE_BOOK_STALE` (G5). `tierStarts` is every `minQuantity` a
|
|
306
|
+
* row of the book uses, ascending (G4): the grid's columns.
|
|
307
|
+
*/
|
|
308
|
+
export const priceBookSchema = z.object({
|
|
309
|
+
id: uuidSchema,
|
|
310
|
+
name: z.string(),
|
|
311
|
+
type: priceBookTypeSchema,
|
|
312
|
+
status: priceBookStatusSchema,
|
|
313
|
+
currencyCode: CURRENCY,
|
|
314
|
+
audience: z.object({ kind: priceBookAudienceKindSchema, memberIds: z.array(uuidSchema) }),
|
|
315
|
+
priority: z.number().int(),
|
|
316
|
+
startsAt: isoDateTimeSchema.nullable(),
|
|
317
|
+
endsAt: isoDateTimeSchema.nullable(),
|
|
318
|
+
isSystem: z.boolean(),
|
|
319
|
+
/** Whether the book was ever activated: the audience kind is fixed from then on (FR-004). */
|
|
320
|
+
everActivated: z.boolean(),
|
|
321
|
+
version: z.number().int().positive(),
|
|
322
|
+
tierStarts: z.array(POSITIVE_INT),
|
|
323
|
+
channels: z.array(priceBookChannelBindingSchema),
|
|
324
|
+
rowCount: z.number().int().nonnegative(),
|
|
325
|
+
/**
|
|
326
|
+
* Feature 161 (G11) — whether the caller may change this book: its header,
|
|
327
|
+
* rows, activation and deletion. The server's scope rule
|
|
328
|
+
* (`price-book-scope.ts` `bookWritable`); the browser does not know the
|
|
329
|
+
* caller's organization scope, so it reads this rather than deriving it.
|
|
330
|
+
*/
|
|
331
|
+
writable: z.boolean(),
|
|
332
|
+
});
|
|
333
|
+
/** A row of a book that prices the product a book list was filtered by (G1). */
|
|
334
|
+
export const priceBookRowMatchSchema = z.object({
|
|
335
|
+
rowId: uuidSchema,
|
|
336
|
+
selectionKind: priceBookSelectionKindSchema,
|
|
337
|
+
minQuantity: POSITIVE_INT,
|
|
338
|
+
maxQuantity: POSITIVE_INT.nullable(),
|
|
339
|
+
price: priceBookRowPriceSchema,
|
|
340
|
+
});
|
|
341
|
+
/** One line of the book list. */
|
|
342
|
+
export const priceBookSummarySchema = z.object({
|
|
343
|
+
id: uuidSchema,
|
|
344
|
+
name: z.string(),
|
|
345
|
+
type: priceBookTypeSchema,
|
|
346
|
+
status: priceBookStatusSchema,
|
|
347
|
+
currencyCode: CURRENCY,
|
|
348
|
+
audienceKind: priceBookAudienceKindSchema,
|
|
349
|
+
priority: z.number().int(),
|
|
350
|
+
startsAt: isoDateTimeSchema.nullable(),
|
|
351
|
+
endsAt: isoDateTimeSchema.nullable(),
|
|
352
|
+
isSystem: z.boolean(),
|
|
353
|
+
version: z.number().int().positive(),
|
|
354
|
+
tierStarts: z.array(POSITIVE_INT),
|
|
355
|
+
channelCount: z.number().int().nonnegative(),
|
|
356
|
+
memberCount: z.number().int().nonnegative(),
|
|
357
|
+
rowCount: z.number().int().nonnegative(),
|
|
358
|
+
/** A book of organizations or customer groups that names no member. */
|
|
359
|
+
appliesToNoOne: z.boolean(),
|
|
360
|
+
/** A non-base book with no channel binding that is not dormant. */
|
|
361
|
+
appliesNowhere: z.boolean(),
|
|
362
|
+
/** Only on a list filtered by `productId`: the rows of this book that price it directly (G1). */
|
|
363
|
+
matches: z.array(priceBookRowMatchSchema).optional(),
|
|
364
|
+
/** Feature 161 (G11) — whether the caller may change this book, as on {@link priceBookSchema}. */
|
|
365
|
+
writable: z.boolean(),
|
|
366
|
+
});
|
|
367
|
+
const PAGE = z.coerce.number().int().positive().default(1);
|
|
368
|
+
const PAGE_SIZE = z.coerce.number().int().positive().max(200).default(25);
|
|
369
|
+
const oneOrMany = (item) => z.preprocess((v) => (v === undefined ? undefined : Array.isArray(v) ? v : [v]), z.array(item).optional());
|
|
370
|
+
/**
|
|
371
|
+
* `GET /api/v1/admin/price-books`. With `productId` (and optionally
|
|
372
|
+
* `variantId`) the list holds only books with a row naming that product or
|
|
373
|
+
* variant directly, each carrying its `matches` (G1).
|
|
374
|
+
*/
|
|
375
|
+
export const priceBookListQuerySchema = z.object({
|
|
376
|
+
type: oneOrMany(priceBookTypeSchema),
|
|
377
|
+
status: oneOrMany(priceBookStatusSchema),
|
|
378
|
+
currency: CURRENCY.optional(),
|
|
379
|
+
channelId: uuidSchema.optional(),
|
|
380
|
+
audienceKind: priceBookAudienceKindSchema.optional(),
|
|
381
|
+
memberId: uuidSchema.optional(),
|
|
382
|
+
search: z.string().trim().min(1).max(200).optional(),
|
|
383
|
+
productId: uuidSchema.optional(),
|
|
384
|
+
variantId: uuidSchema.optional(),
|
|
385
|
+
page: PAGE,
|
|
386
|
+
pageSize: PAGE_SIZE,
|
|
387
|
+
});
|
|
388
|
+
export const pageMetaSchema = z.object({
|
|
389
|
+
page: z.number().int().positive(),
|
|
390
|
+
pageSize: z.number().int().positive(),
|
|
391
|
+
totalCount: z.number().int().nonnegative(),
|
|
392
|
+
});
|
|
393
|
+
export const priceBookListResponseSchema = z.object({
|
|
394
|
+
data: z.array(priceBookSummarySchema),
|
|
395
|
+
meta: pageMetaSchema.extend({
|
|
396
|
+
/**
|
|
397
|
+
* Feature 161 (G11) — the audience kinds the caller may create a book of
|
|
398
|
+
* (`price-book-scope.ts` `audienceKindWritable`): every kind for a platform
|
|
399
|
+
* administrator, `organizations` alone for a sales representative.
|
|
400
|
+
*/
|
|
401
|
+
creatableAudienceKinds: z.array(priceBookAudienceKindSchema),
|
|
402
|
+
}),
|
|
403
|
+
});
|
|
404
|
+
/** Body of `PATCH /api/v1/admin/price-books/:id`: the patch and the version it was made against (G5). */
|
|
405
|
+
export const priceBookPatchRequestSchema = z
|
|
406
|
+
.object({
|
|
407
|
+
name: z.string().trim().min(1).max(200).optional(),
|
|
408
|
+
priority: z.number().int().min(-1_000_000).max(1_000_000).optional(),
|
|
409
|
+
startsAt: isoDateTimeSchema.nullable().optional(),
|
|
410
|
+
endsAt: isoDateTimeSchema.nullable().optional(),
|
|
411
|
+
channelIds: priceBookChannelIdsSchema.optional(),
|
|
412
|
+
audience: priceBookAudienceSchema.optional(),
|
|
413
|
+
/** Required here or as an `If-Match` header. */
|
|
414
|
+
expectedVersion: POSITIVE_INT.optional(),
|
|
415
|
+
})
|
|
416
|
+
.superRefine(datesRefinement);
|
|
417
|
+
/** Body of `POST /api/v1/admin/price-books/:id/duplicate` (FR-036). */
|
|
418
|
+
export const priceBookDuplicateSchema = z.object({
|
|
419
|
+
name: z.string().trim().min(1).max(200),
|
|
420
|
+
channelIds: priceBookChannelIdsSchema.default([]),
|
|
421
|
+
currencyCode: CURRENCY,
|
|
422
|
+
audience: priceBookAudienceSchema,
|
|
423
|
+
/** Fixed and amount rows are multiplied, half-up to 4 dp; percentages are copied verbatim. */
|
|
424
|
+
amountMultiplier: z
|
|
425
|
+
.string()
|
|
426
|
+
.regex(/^\d{1,6}(\.\d{1,6})?$/, 'non-negative decimal')
|
|
427
|
+
.refine((v) => Number(v) > 0, 'The multiplier must be greater than zero.')
|
|
428
|
+
.default('1'),
|
|
429
|
+
});
|
|
430
|
+
// --- Rows (admin) ---------------------------------------------------------------
|
|
431
|
+
/** Why a stored row prices nothing now (FR-016 of 161). */
|
|
432
|
+
export const priceBookRowInactiveReasonSchema = z.enum([
|
|
433
|
+
'attribute_not_price_rule',
|
|
434
|
+
'attribute_deleted',
|
|
435
|
+
'target_deleted',
|
|
436
|
+
]);
|
|
437
|
+
/**
|
|
438
|
+
* The natural key of a row's selection (G10): what a CSV file names instead of
|
|
439
|
+
* ids. `categoryPath` is the category's slugs from the root, joined by `/`;
|
|
440
|
+
* `attribute.valueCode` is the option code stored on the row.
|
|
441
|
+
*/
|
|
442
|
+
export const priceBookNaturalKeySchema = z.object({
|
|
443
|
+
productSku: z.string().trim().min(1).max(255).optional(),
|
|
444
|
+
variantSku: z.string().trim().min(1).max(255).optional(),
|
|
445
|
+
categoryPath: z.string().trim().min(1).max(2000).optional(),
|
|
446
|
+
attribute: z.object({ key: z.string().min(1).max(64), valueCode: z.string().min(1).max(255) }).optional(),
|
|
447
|
+
});
|
|
448
|
+
export const priceBookRowSchema = z.object({
|
|
449
|
+
id: uuidSchema,
|
|
450
|
+
selectionKind: priceBookSelectionKindSchema,
|
|
451
|
+
selection: z.object({
|
|
452
|
+
variantId: uuidSchema.nullable(),
|
|
453
|
+
productId: uuidSchema.nullable(),
|
|
454
|
+
categoryId: uuidSchema.nullable(),
|
|
455
|
+
attribute: priceBookAttributeSchema.nullable(),
|
|
456
|
+
}),
|
|
457
|
+
minQuantity: POSITIVE_INT,
|
|
458
|
+
maxQuantity: POSITIVE_INT.nullable(),
|
|
459
|
+
price: priceBookRowPriceSchema,
|
|
460
|
+
inactiveReason: priceBookRowInactiveReasonSchema.nullable(),
|
|
461
|
+
/** The product, variant or category the row names no longer exists (G2). */
|
|
462
|
+
targetDeleted: z.boolean(),
|
|
463
|
+
labels: priceBookRowLabelsSchema,
|
|
464
|
+
naturalKey: priceBookNaturalKeySchema,
|
|
465
|
+
});
|
|
466
|
+
/** `GET /api/v1/admin/price-books/:id/rows`; `search` matches the labels and SKUs (G3). */
|
|
467
|
+
export const priceBookRowsQuerySchema = z.object({
|
|
468
|
+
selectionKind: oneOrMany(priceBookSelectionKindSchema),
|
|
469
|
+
productId: uuidSchema.optional(),
|
|
470
|
+
variantId: uuidSchema.optional(),
|
|
471
|
+
categoryId: uuidSchema.optional(),
|
|
472
|
+
attributeKey: z.string().min(1).max(64).optional(),
|
|
473
|
+
search: z.string().trim().min(1).max(200).optional(),
|
|
474
|
+
page: PAGE,
|
|
475
|
+
pageSize: z.coerce.number().int().positive().max(1000).default(200),
|
|
476
|
+
});
|
|
477
|
+
// --- Lines (161 G15) -------------------------------------------------------------
|
|
478
|
+
/** Most lines one page of `GET …/:id/lines` answers. */
|
|
479
|
+
export const PRICE_BOOK_LINES_MAX_PAGE_SIZE = 500;
|
|
480
|
+
/**
|
|
481
|
+
* `GET /api/v1/admin/price-books/:id/lines` (feature 161, G15): the grid's
|
|
482
|
+
* pages, cut by **line** — a selection with every row it holds, a product with
|
|
483
|
+
* its variants — never by row. `kind` narrows to selection kinds (a `variant`
|
|
484
|
+
* filter answers the product lines that carry a variant line); `search`
|
|
485
|
+
* matches the labels of attribute and category lines and the catalogue's name
|
|
486
|
+
* and SKU search for product lines.
|
|
487
|
+
*/
|
|
488
|
+
export const priceBookLinesQuerySchema = z.object({
|
|
489
|
+
kind: oneOrMany(priceBookSelectionKindSchema),
|
|
490
|
+
search: z.string().trim().min(1).max(200).optional(),
|
|
491
|
+
page: PAGE,
|
|
492
|
+
pageSize: z.coerce.number().int().positive().max(PRICE_BOOK_LINES_MAX_PAGE_SIZE).default(100),
|
|
493
|
+
});
|
|
494
|
+
const priceBookLineFields = {
|
|
495
|
+
/** The selection as one comparable string — the grid's key for the line. */
|
|
496
|
+
selectionKey: z.string(),
|
|
497
|
+
selectionKind: priceBookSelectionKindSchema,
|
|
498
|
+
selection: priceBookRowSchema.shape.selection,
|
|
499
|
+
labels: priceBookRowLabelsSchema,
|
|
500
|
+
naturalKey: priceBookNaturalKeySchema,
|
|
501
|
+
inactiveReason: priceBookRowInactiveReasonSchema.nullable(),
|
|
502
|
+
targetDeleted: z.boolean(),
|
|
503
|
+
/** Every row of the selection, by `minQuantity` — a line is never split across pages. */
|
|
504
|
+
rows: z.array(priceBookRowSchema),
|
|
505
|
+
};
|
|
506
|
+
/** A variant's line, nested under its product's. */
|
|
507
|
+
export const priceBookVariantLineSchema = z.object(priceBookLineFields);
|
|
508
|
+
/**
|
|
509
|
+
* One line of a book. A product line carries `variants` — its variants that
|
|
510
|
+
* the book prices — and exists even when only its variants hold rows (`rows`
|
|
511
|
+
* is then empty). Other kinds carry no `variants`.
|
|
512
|
+
*/
|
|
513
|
+
export const priceBookLineSchema = z.object({
|
|
514
|
+
...priceBookLineFields,
|
|
515
|
+
variants: z.array(priceBookVariantLineSchema).optional(),
|
|
516
|
+
});
|
|
517
|
+
/** Most products one `GET …/:id/product-lines` asks about (a product browser's pick is at most 200). */
|
|
518
|
+
export const PRICE_BOOK_PRODUCT_LINES_MAX_IDS = 200;
|
|
519
|
+
/**
|
|
520
|
+
* `GET /api/v1/admin/price-books/:id/product-lines?productIds=a,b` (feature
|
|
521
|
+
* 161, Checkpoint 5 review): which of the named products already have a line
|
|
522
|
+
* in the book — a row of the product or of one of its variants — in one read,
|
|
523
|
+
* so the grid refuses a picked product whose line sits on another page rather
|
|
524
|
+
* than overwriting its price.
|
|
525
|
+
*/
|
|
526
|
+
export const priceBookProductLinesQuerySchema = z.object({
|
|
527
|
+
productIds: z
|
|
528
|
+
.string()
|
|
529
|
+
.transform((raw) => [...new Set(raw.split(',').map((id) => id.trim()).filter((id) => id.length > 0))])
|
|
530
|
+
.pipe(z.array(uuidSchema).min(1).max(PRICE_BOOK_PRODUCT_LINES_MAX_IDS)),
|
|
531
|
+
});
|
|
532
|
+
export const priceBookProductLinesResponseSchema = z.object({
|
|
533
|
+
data: z.object({ productIds: z.array(uuidSchema) }),
|
|
534
|
+
});
|
|
535
|
+
/** The answer of `GET …/:id/lines`: general to specific, by label within a kind (FR-006). */
|
|
536
|
+
export const priceBookLinesResponseSchema = z.object({
|
|
537
|
+
data: z.array(priceBookLineSchema),
|
|
538
|
+
meta: z.object({
|
|
539
|
+
page: z.number().int().positive(),
|
|
540
|
+
pageSize: z.number().int().positive(),
|
|
541
|
+
totalLines: z.number().int().nonnegative(),
|
|
542
|
+
}),
|
|
543
|
+
});
|
|
544
|
+
/**
|
|
545
|
+
* One row of an admin batch: by ids (`selection`, the grid) or by natural key
|
|
546
|
+
* (`key`, a CSV import) — exactly one of the two (G10). A row without an `id`
|
|
547
|
+
* whose selection and `minQuantity` equal a stored row's replaces that row.
|
|
548
|
+
*/
|
|
549
|
+
export const priceBookBatchRowInputSchema = z
|
|
550
|
+
.object({
|
|
551
|
+
id: uuidSchema.optional(),
|
|
552
|
+
selection: priceBookSelectionSchema.optional(),
|
|
553
|
+
key: priceBookNaturalKeySchema.optional(),
|
|
554
|
+
minQuantity: POSITIVE_INT,
|
|
555
|
+
maxQuantity: POSITIVE_INT.nullable().default(null),
|
|
556
|
+
price: priceBookRowPriceSchema,
|
|
557
|
+
})
|
|
558
|
+
.superRefine((value, ctx) => {
|
|
559
|
+
if ((value.selection === undefined) === (value.key === undefined)) {
|
|
560
|
+
ctx.addIssue({ code: 'custom', path: ['selection'], message: 'A row names its selection by ids or by key, exactly one.' });
|
|
561
|
+
}
|
|
562
|
+
});
|
|
563
|
+
/**
|
|
564
|
+
* Body of `PUT /api/v1/admin/price-books/:id/rows` (and of its `?dryRun=true`
|
|
565
|
+
* preview, G10). `merge` (default) upserts the rows and deletes `delete`;
|
|
566
|
+
* `replace` makes the book's rows exactly `upsert` (`delete` must be empty).
|
|
567
|
+
*/
|
|
568
|
+
export const priceBookRowsWriteSchema = z
|
|
569
|
+
.object({
|
|
570
|
+
expectedVersion: POSITIVE_INT.optional(),
|
|
571
|
+
mode: z.enum(['merge', 'replace']).default('merge'),
|
|
572
|
+
upsert: z.array(z.unknown()).max(PRICE_BOOK_MAX_ROWS_PER_BATCH).default([]),
|
|
573
|
+
delete: z.array(uuidSchema).max(PRICE_BOOK_MAX_ROWS_PER_BATCH).default([]),
|
|
574
|
+
})
|
|
575
|
+
.superRefine((value, ctx) => {
|
|
576
|
+
if (value.upsert.length + value.delete.length > PRICE_BOOK_MAX_ROWS_PER_BATCH) {
|
|
577
|
+
ctx.addIssue({ code: 'custom', path: ['upsert'], message: `A batch carries at most ${PRICE_BOOK_MAX_ROWS_PER_BATCH} rows.` });
|
|
578
|
+
}
|
|
579
|
+
if (value.mode === 'replace' && value.delete.length > 0) {
|
|
580
|
+
ctx.addIssue({ code: 'custom', path: ['delete'], message: 'A replace names no row to delete: every row not in the batch goes.' });
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
/**
|
|
584
|
+
* Why one row of a batch was refused (G6). `index` is the row's position in
|
|
585
|
+
* `upsert` (for an import, the file line the client maps it to); `rowId` names
|
|
586
|
+
* a stored row (an overlap partner, a delete of a missing row).
|
|
587
|
+
*/
|
|
588
|
+
/**
|
|
589
|
+
* The rule behind a refused row (feature 161, Checkpoint 11 ruling 2): one
|
|
590
|
+
* code per rule, under its group `code`, so the consumer translates it
|
|
591
|
+
* (`specs/conventions/module-i18n.md`); `priceBookRowShapeIssues` above
|
|
592
|
+
* returns the shape rules' codes.
|
|
593
|
+
*/
|
|
594
|
+
export const PRICE_BOOK_ROW_ERROR_REASONS = [
|
|
595
|
+
// PRICE_BOOK_ROW_SHAPE
|
|
596
|
+
'row_invalid',
|
|
597
|
+
'selection_combined',
|
|
598
|
+
'max_below_min',
|
|
599
|
+
'fixed_on_all',
|
|
600
|
+
'variant_needs_product',
|
|
601
|
+
'base_book_kind',
|
|
602
|
+
'base_product_fixed',
|
|
603
|
+
// PRICE_BOOK_ROW_TARGET
|
|
604
|
+
'product_unknown',
|
|
605
|
+
'variant_unknown',
|
|
606
|
+
'variant_other_product',
|
|
607
|
+
'category_unknown',
|
|
608
|
+
'attribute_not_selectable',
|
|
609
|
+
'attribute_value_unknown',
|
|
610
|
+
'attribute_value_boolean',
|
|
611
|
+
'row_not_in_book',
|
|
612
|
+
// PRICE_BOOK_ROW_OVERLAP, PRICE_BOOK_ROW_LIMIT
|
|
613
|
+
'rows_overlap',
|
|
614
|
+
'book_row_limit',
|
|
615
|
+
];
|
|
616
|
+
export const priceBookRowErrorReasonSchema = z.enum(PRICE_BOOK_ROW_ERROR_REASONS);
|
|
617
|
+
export const PRICE_BOOK_ROW_ERROR_CODES = [
|
|
618
|
+
'PRICE_BOOK_ROW_SHAPE',
|
|
619
|
+
'PRICE_BOOK_ROW_OVERLAP',
|
|
620
|
+
'PRICE_BOOK_ROW_TARGET',
|
|
621
|
+
'PRICE_BOOK_ROW_LIMIT',
|
|
622
|
+
];
|
|
623
|
+
export const priceBookRowErrorSchema = z.object({
|
|
624
|
+
index: z.number().int().nonnegative().optional(),
|
|
625
|
+
rowId: uuidSchema.optional(),
|
|
626
|
+
field: z.string(),
|
|
627
|
+
code: z.enum(PRICE_BOOK_ROW_ERROR_CODES),
|
|
628
|
+
/** The rule the row broke (feature 161): the code a consumer translates; `message` is English, for logs. */
|
|
629
|
+
reason: priceBookRowErrorReasonSchema.optional(),
|
|
630
|
+
message: z.string(),
|
|
631
|
+
});
|
|
632
|
+
/** A stored or resolved row in a diff (no labels). */
|
|
633
|
+
export const priceBookDiffRowSchema = z.object({
|
|
634
|
+
id: uuidSchema.optional(),
|
|
635
|
+
selectionKind: priceBookSelectionKindSchema,
|
|
636
|
+
selection: priceBookRowSchema.shape.selection,
|
|
637
|
+
naturalKey: priceBookNaturalKeySchema,
|
|
638
|
+
minQuantity: POSITIVE_INT,
|
|
639
|
+
maxQuantity: POSITIVE_INT.nullable(),
|
|
640
|
+
price: priceBookRowPriceSchema,
|
|
641
|
+
});
|
|
642
|
+
/** The answer of `PUT …/rows?dryRun=true` (G10): what the same body would write, writing nothing. */
|
|
643
|
+
export const priceBookRowsDryRunSchema = z.object({
|
|
644
|
+
version: z.number().int().positive(),
|
|
645
|
+
/** Each `upsert` row with its selection resolved to ids (`null` where it could not be). */
|
|
646
|
+
resolved: z.array(priceBookDiffRowSchema.nullable()),
|
|
647
|
+
diff: z.object({
|
|
648
|
+
added: z.array(priceBookDiffRowSchema),
|
|
649
|
+
changed: z.array(z.object({ before: priceBookDiffRowSchema, after: priceBookDiffRowSchema })),
|
|
650
|
+
removed: z.array(priceBookDiffRowSchema),
|
|
651
|
+
unchangedCount: z.number().int().nonnegative(),
|
|
652
|
+
}),
|
|
653
|
+
errors: z.array(priceBookRowErrorSchema),
|
|
654
|
+
});
|
|
655
|
+
/** The answer of a committed `PUT …/rows`. */
|
|
656
|
+
export const priceBookRowsWriteResultSchema = z.object({
|
|
657
|
+
version: z.number().int().positive(),
|
|
658
|
+
rows: z.array(priceBookDiffRowSchema),
|
|
659
|
+
});
|
|
660
|
+
/** The answer of `GET /api/v1/admin/price-books/:id/rows` (G3): one page of rows. */
|
|
661
|
+
export const priceBookRowsResponseSchema = z.object({
|
|
662
|
+
data: z.array(priceBookRowSchema),
|
|
663
|
+
meta: pageMetaSchema,
|
|
664
|
+
});
|
|
665
|
+
// --- Targets: the pickers' answers (feature 161, T045) ---------------------------
|
|
666
|
+
/**
|
|
667
|
+
* The shapes `GET /api/v1/admin/price-books/targets/*` answer — the header
|
|
668
|
+
* editor's and the grid's pickers. Every answer is `{ data: { items } }`.
|
|
669
|
+
* Written down by feature 161 so the editor's client parses them (Principle
|
|
670
|
+
* II); the routes answered these shapes since feature 160.
|
|
671
|
+
*/
|
|
672
|
+
const targetsOf = (item) => z.object({ data: z.object({ items: z.array(item) }) });
|
|
673
|
+
/** `…/targets/organizations?search=` and `?ids=` (G12), scope-filtered. */
|
|
674
|
+
export const priceBookTargetOrganizationSchema = z.object({ id: uuidSchema, name: z.string(), taxId: z.string() });
|
|
675
|
+
export const priceBookTargetOrganizationsResponseSchema = targetsOf(priceBookTargetOrganizationSchema);
|
|
676
|
+
/** `…/targets/customer-groups`. */
|
|
677
|
+
export const priceBookTargetCustomerGroupSchema = z.object({ id: uuidSchema, code: z.string(), name: z.string() });
|
|
678
|
+
export const priceBookTargetCustomerGroupsResponseSchema = targetsOf(priceBookTargetCustomerGroupSchema);
|
|
679
|
+
/** `…/targets/categories`: live categories, as a flat list with their parent; `name` by language tag. */
|
|
680
|
+
export const priceBookTargetCategorySchema = z.object({
|
|
681
|
+
id: uuidSchema,
|
|
682
|
+
slug: z.string(),
|
|
683
|
+
name: z.record(z.string(), z.string()),
|
|
684
|
+
parentCategoryId: uuidSchema.nullable(),
|
|
685
|
+
sortOrder: z.number().int(),
|
|
686
|
+
});
|
|
687
|
+
export const priceBookTargetCategoriesResponseSchema = targetsOf(priceBookTargetCategorySchema);
|
|
688
|
+
/** `…/targets/attributes`: eligible *Price list rules* attributes with their option values. */
|
|
689
|
+
export const priceBookTargetAttributeSchema = z.object({
|
|
690
|
+
key: z.string(),
|
|
691
|
+
label: z.string(),
|
|
692
|
+
valueType: z.string(),
|
|
693
|
+
options: z.array(z.object({ value: z.string(), label: z.string() })),
|
|
694
|
+
});
|
|
695
|
+
export const priceBookTargetAttributesResponseSchema = targetsOf(priceBookTargetAttributeSchema);
|
|
696
|
+
/**
|
|
697
|
+
* `…/targets/sales-channels?currency=`: channels, narrowed to those offering a
|
|
698
|
+
* currency when one is asked; `name` by language tag.
|
|
699
|
+
*/
|
|
700
|
+
export const priceBookTargetSalesChannelSchema = z.object({
|
|
701
|
+
id: uuidSchema,
|
|
702
|
+
code: z.string(),
|
|
703
|
+
name: z.record(z.string(), z.string()),
|
|
704
|
+
currencies: z.array(CURRENCY),
|
|
705
|
+
defaultCurrency: z.string(),
|
|
706
|
+
});
|
|
707
|
+
export const priceBookTargetSalesChannelsResponseSchema = targetsOf(priceBookTargetSalesChannelSchema);
|
|
708
|
+
/** `…/targets/currencies`: every currency a channel offers, with the channels' codes. */
|
|
709
|
+
export const priceBookTargetCurrencySchema = z.object({ code: CURRENCY, exposedByChannels: z.array(z.string()) });
|
|
710
|
+
export const priceBookTargetCurrenciesResponseSchema = targetsOf(priceBookTargetCurrencySchema);
|
|
711
|
+
// --- Explanation (FR-020, FR-038) ----------------------------------------------
|
|
712
|
+
export const PRICE_BOOK_MAX_RESOLVE_LINES = 200;
|
|
713
|
+
export const priceBookResolveRequestSchema = z.object({
|
|
714
|
+
/** Absent: the system default channel, as the probe this replaces. */
|
|
715
|
+
salesChannelId: uuidSchema.optional(),
|
|
716
|
+
currencyCode: CURRENCY.optional(),
|
|
717
|
+
buyer: z
|
|
718
|
+
.object({
|
|
719
|
+
organizationId: uuidSchema.optional(),
|
|
720
|
+
customerAccountId: uuidSchema.optional(),
|
|
721
|
+
})
|
|
722
|
+
.default({}),
|
|
723
|
+
lines: z
|
|
724
|
+
.array(z.object({
|
|
725
|
+
key: z.string().min(1).max(100),
|
|
726
|
+
productId: uuidSchema,
|
|
727
|
+
variantId: uuidSchema.optional(),
|
|
728
|
+
quantity: POSITIVE_INT.default(1),
|
|
729
|
+
}))
|
|
730
|
+
.min(1)
|
|
731
|
+
.max(PRICE_BOOK_MAX_RESOLVE_LINES),
|
|
732
|
+
});
|
|
733
|
+
export const priceBookResolvedLineSchema = z.object({
|
|
734
|
+
key: z.string(),
|
|
735
|
+
price: z
|
|
736
|
+
.object({
|
|
737
|
+
amount: z.string(),
|
|
738
|
+
currency: CURRENCY,
|
|
739
|
+
source: priceSourceSchema,
|
|
740
|
+
compareAt: z.string().nullable().optional(),
|
|
741
|
+
})
|
|
742
|
+
.nullable(),
|
|
743
|
+
explanation: priceBookExplanationSchema,
|
|
744
|
+
});
|
|
745
|
+
// --- Precedence (read only until P3) --------------------------------------------
|
|
746
|
+
export const precedenceViewSchema = precedenceConfigSchema.extend({
|
|
747
|
+
version: z.number().int().positive(),
|
|
748
|
+
preset: precedencePresetNameSchema.nullable(),
|
|
749
|
+
});
|
|
750
|
+
// --- Settings (G8) ---------------------------------------------------------------
|
|
751
|
+
/** Setting code: show the catalogue price beside a lower buyer price (owner ruling Q8a, N4). */
|
|
752
|
+
export const SHOW_LIST_PRICE_SETTING_CODE = 'pricing.show_list_price';
|
|
753
|
+
/** Body of `PUT /api/v1/admin/price-books/settings/show-list-price`. */
|
|
754
|
+
export const showListPriceWriteSchema = z.object({ value: z.boolean() });
|
|
755
|
+
// --- Cross-module base-price writes (FR-049, owner ruling 2026-10-22) -----------
|
|
756
|
+
/**
|
|
757
|
+
* One product's base price per currency — the body of
|
|
758
|
+
* `GET /api/v1/admin/price-books/base-prices?productId=` and the answer of
|
|
759
|
+
* every {@link PriceBookWritePort} call. A variant's row is `null` when it
|
|
760
|
+
* inherits the product's base price.
|
|
761
|
+
*/
|
|
762
|
+
export const productBasePricesSchema = z.object({
|
|
763
|
+
productId: uuidSchema,
|
|
764
|
+
byCurrency: z.record(CURRENCY, z.object({
|
|
765
|
+
bookId: uuidSchema,
|
|
766
|
+
product: z.array(basePriceTierSchema),
|
|
767
|
+
variants: z.record(uuidSchema, variantBaseRowSchema.nullable()),
|
|
768
|
+
})),
|
|
769
|
+
/**
|
|
770
|
+
* Feature 161 (G11) — whether the caller may write these base prices: the
|
|
771
|
+
* base books are everyone's prices, so `false` for a sales representative;
|
|
772
|
+
* `true` for a platform administrator and for an in-process caller of
|
|
773
|
+
* {@link PriceBookWritePort}, which acts on every book.
|
|
774
|
+
*/
|
|
775
|
+
writable: z.boolean(),
|
|
776
|
+
});
|
|
777
|
+
//# sourceMappingURL=price-books.js.map
|