@revenexx/cover 0.2.35 → 0.2.37
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/app/components/cart/view/CartSummaryPanel.vue +79 -17
- package/app/components/cart/view/CartView.vue +24 -2
- package/app/components/punchout/action/PunchoutTransferAction.vue +69 -0
- package/app/composables/useCartCalculation.ts +1 -0
- package/app/composables/useCartStore.ts +62 -1
- package/app/composables/usePunchoutVisit.ts +55 -0
- package/app/interfaces/cart-calculation.ts +6 -0
- package/app/interfaces/punchout.ts +79 -0
- package/app/layouts/default.vue +7 -0
- package/app/pages/punchout/transfer.vue +158 -0
- package/i18n/locales/de/cart.json +5 -2
- package/i18n/locales/de/punchout.json +45 -0
- package/i18n/locales/en/cart.json +5 -2
- package/i18n/locales/en/punchout.json +45 -0
- package/nuxt.config.ts +28 -0
- package/package.json +2 -1
- package/server/api/cart/calculate.post.ts +41 -2
- package/server/api/orders/index.post.ts +101 -0
- package/server/api/punchout/return.post.ts +66 -0
- package/server/api/punchout/session.get.ts +15 -0
- package/server/api/typesense/search.post.ts +87 -16
- package/server/interfaces/cartCalculation.ts +16 -0
- package/server/interfaces/punchout.ts +57 -0
- package/server/routes/punchout/start.get.ts +261 -0
- package/server/services/MockCartCalculationService.ts +19 -5
- package/server/utils/promotionsService.ts +275 -0
- package/server/utils/punchoutLanding.ts +159 -0
- package/server/utils/punchoutVisit.ts +134 -0
- package/server/utils/revenexxSdk.ts +34 -1
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import type { H3Event } from "h3";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The promotions domain has no service class — the cart endpoint branches on
|
|
5
|
+
* the resolved registry key ("mock" = the demo profile's own flat discount,
|
|
6
|
+
* "api" = the promotions app via the public revenexx API).
|
|
7
|
+
*
|
|
8
|
+
* Mock mode is untouched by any of this: the demo cart keeps its hard-coded
|
|
9
|
+
* volume discount, so the showcase keeps working without a tenant behind it.
|
|
10
|
+
*/
|
|
11
|
+
export function resolvePromotionsServiceKey(event?: H3Event): string {
|
|
12
|
+
return resolveServiceKey(event, {
|
|
13
|
+
domain: "promotionsService",
|
|
14
|
+
mockKey: "mock",
|
|
15
|
+
liveKey: "api",
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** One line as the promotions app wants to see it: priced, and numbered. */
|
|
20
|
+
interface PricedLine {
|
|
21
|
+
position: number;
|
|
22
|
+
sku?: string;
|
|
23
|
+
product_id?: string;
|
|
24
|
+
name?: string;
|
|
25
|
+
quantity: number;
|
|
26
|
+
unit_price: number;
|
|
27
|
+
tax_rate?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** What `POST /promotions/evaluate` answers, in the part the storefront uses. */
|
|
31
|
+
export interface PromotionEvaluation {
|
|
32
|
+
/** The discount per line, keyed by the 1-based position the line was sent under. */
|
|
33
|
+
lineDiscounts: Record<number, number>;
|
|
34
|
+
/** Every discount together — what the summary shows on its own row. */
|
|
35
|
+
discountTotal: number;
|
|
36
|
+
/** The promotions that applied, for the row label and the order that follows. */
|
|
37
|
+
applied: Array<{
|
|
38
|
+
promotion_id: string;
|
|
39
|
+
promotion_name: string;
|
|
40
|
+
/** The voucher behind it, when a code produced it. The redemption ledger
|
|
41
|
+
* counts a voucher's uses by this, so a hold without it never moves
|
|
42
|
+
* `usage_count` and the code stays usable for ever. */
|
|
43
|
+
voucher_id?: string | null;
|
|
44
|
+
voucher_code?: string | null;
|
|
45
|
+
value_type?: string | null;
|
|
46
|
+
percent?: number | null;
|
|
47
|
+
line_position?: number | null;
|
|
48
|
+
amount: number;
|
|
49
|
+
}>;
|
|
50
|
+
/** Codes the engine refused, so the cart can say WHICH code did not work. */
|
|
51
|
+
rejectedCodes: string[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Ask the promotions app what this cart is worth.
|
|
56
|
+
*
|
|
57
|
+
* The call writes nothing, which is what makes it safe on every keystroke, and
|
|
58
|
+
* it prices nothing — a line with no resolved unit price is refused rather than
|
|
59
|
+
* guessed at, so this runs AFTER the prices app has answered.
|
|
60
|
+
*
|
|
61
|
+
* Attribution is the point. The engine answers a discount PER LINE, and those
|
|
62
|
+
* are the figures carried through to the order — the BFF never re-derives a
|
|
63
|
+
* split of its own, because a split computed twice is a split that disagrees by
|
|
64
|
+
* a cent, and a cent that appears in the summary and not in the order is the
|
|
65
|
+
* defect this whole wiring exists to avoid.
|
|
66
|
+
*
|
|
67
|
+
* Failure is silent by design: an unreachable promotions app leaves the cart
|
|
68
|
+
* priced and orderable, without a discount. A storefront that refuses to show a
|
|
69
|
+
* cart because a promotion could not be evaluated is worse than one that shows
|
|
70
|
+
* the undiscounted price.
|
|
71
|
+
*/
|
|
72
|
+
export async function evaluatePromotions(
|
|
73
|
+
event: H3Event,
|
|
74
|
+
input: { lines: PricedLine[]; currency?: string; codes?: string[]; contactId?: string | null; organizationId?: string | null },
|
|
75
|
+
): Promise<PromotionEvaluation | null> {
|
|
76
|
+
if (resolvePromotionsServiceKey(event) !== "api" || !input.lines.length) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
// The escape hatch, not a typed method: the SDK vendored here predates
|
|
81
|
+
// the promotions app and carries no namespace for it. Same transport,
|
|
82
|
+
// same auth, same error type — it moves to a typed call when the SDK
|
|
83
|
+
// next carries one.
|
|
84
|
+
const answer = await useRevenexxSdk().call<{
|
|
85
|
+
lines?: Array<{ position?: number; discount?: number }>;
|
|
86
|
+
totals?: { discount_total?: number };
|
|
87
|
+
applied?: Array<Record<string, unknown>>;
|
|
88
|
+
rejected_codes?: string[];
|
|
89
|
+
}>("POST", "/promotions/evaluate", {
|
|
90
|
+
body: {
|
|
91
|
+
currency: input.currency,
|
|
92
|
+
lines: input.lines,
|
|
93
|
+
...(input.codes?.length ? { codes: input.codes } : {}),
|
|
94
|
+
...(input.contactId ? { contact_id: input.contactId } : {}),
|
|
95
|
+
...(input.organizationId ? { organization_id: input.organizationId } : {}),
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const lineDiscounts: Record<number, number> = {};
|
|
100
|
+
for (const line of answer.lines ?? []) {
|
|
101
|
+
const discount = Number(line.discount ?? 0);
|
|
102
|
+
if (line.position !== undefined && discount > 0) {
|
|
103
|
+
lineDiscounts[Number(line.position)] = discount;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
lineDiscounts,
|
|
108
|
+
discountTotal: Number(answer.totals?.discount_total ?? 0),
|
|
109
|
+
applied: (answer.applied ?? []).map(a => ({
|
|
110
|
+
promotion_id: String(a.promotion_id ?? a.id ?? ""),
|
|
111
|
+
promotion_name: String(a.name ?? a.promotion_name ?? "Promotion"),
|
|
112
|
+
voucher_id: (a.voucher_id ?? null) as string | null,
|
|
113
|
+
voucher_code: (a.voucher_code ?? a.code ?? null) as string | null,
|
|
114
|
+
value_type: (a.value_type ?? null) as string | null,
|
|
115
|
+
percent: a.percent != null ? Number(a.percent) : null,
|
|
116
|
+
line_position: a.line_position != null ? Number(a.line_position) : null,
|
|
117
|
+
amount: Number(a.amount ?? 0),
|
|
118
|
+
})),
|
|
119
|
+
rejectedCodes: answer.rejected_codes ?? [],
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
getLogService().error("Promotion evaluation failed", apiErrorContext(err));
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Hold what this cart is about to spend, then keep it or hand it back.
|
|
130
|
+
*
|
|
131
|
+
* Evaluating is free and writes nothing; RESERVING is the moment a voucher's
|
|
132
|
+
* remaining uses are taken out of circulation, so it happens on SUBMIT and not
|
|
133
|
+
* a moment earlier. A buyer who opens a checkout and wanders off must not hold
|
|
134
|
+
* the last use of a code against everyone else.
|
|
135
|
+
*
|
|
136
|
+
* Three outcomes and all three are written:
|
|
137
|
+
*
|
|
138
|
+
* reserve → commit the order exists, the redemption is spent
|
|
139
|
+
* reserve → release the order did not happen, the uses go back
|
|
140
|
+
* reserve → expire nothing was said; the hold lapses on its own, which is
|
|
141
|
+
* why the reservation carries an expiry and why a missed
|
|
142
|
+
* release is untidy rather than lost
|
|
143
|
+
*
|
|
144
|
+
* Every one of them is best-effort and none of them fails the order. A
|
|
145
|
+
* promotion that cannot be reserved leaves the order placed without it — which
|
|
146
|
+
* is a discount the buyer did not get, and a conversation. A promotion that
|
|
147
|
+
* cannot be COMMITTED leaves a hold that expires by itself. Neither is worth
|
|
148
|
+
* refusing a sale that the buyer has already paid for.
|
|
149
|
+
*/
|
|
150
|
+
/**
|
|
151
|
+
* Why a hold did not happen, because the three cases are not one.
|
|
152
|
+
*
|
|
153
|
+
* `refused` is the promotions app answering no — the code is spent, the budget
|
|
154
|
+
* is gone — and the discount goes with it. `unavailable` is nobody answering,
|
|
155
|
+
* which is not the buyer's fault and does not cost them their discount.
|
|
156
|
+
* `not_applicable` is there being nothing to hold in the first place.
|
|
157
|
+
*/
|
|
158
|
+
export type ReservationOutcome = "held" | "refused" | "unavailable" | "not_applicable";
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* What reserve and commit hold: the engine's own attribution, promotion by
|
|
162
|
+
* promotion. `/promotions/reserve` holds exactly what it is handed under
|
|
163
|
+
* `promotions` and RELEASES everything else on the cart, so calling it without
|
|
164
|
+
* that list holds nothing and quietly drops any hold that was already there.
|
|
165
|
+
*/
|
|
166
|
+
function heldPromotions(applied: PromotionEvaluation["applied"]) {
|
|
167
|
+
return applied
|
|
168
|
+
.filter(a => a.promotion_id)
|
|
169
|
+
.map(a => ({
|
|
170
|
+
promotion_id: a.promotion_id,
|
|
171
|
+
...(a.voucher_id ? { voucher_id: a.voucher_id } : {}),
|
|
172
|
+
amount: a.amount,
|
|
173
|
+
}));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function reserveRedemption(
|
|
177
|
+
event: H3Event,
|
|
178
|
+
input: {
|
|
179
|
+
cartId: string;
|
|
180
|
+
promotions: PromotionEvaluation["applied"];
|
|
181
|
+
currency?: string;
|
|
182
|
+
contactId?: string | null;
|
|
183
|
+
organizationId?: string | null;
|
|
184
|
+
},
|
|
185
|
+
): Promise<ReservationOutcome> {
|
|
186
|
+
if (resolvePromotionsServiceKey(event) !== "api") {
|
|
187
|
+
return "not_applicable";
|
|
188
|
+
}
|
|
189
|
+
const promotions = heldPromotions(input.promotions);
|
|
190
|
+
if (!promotions.length) {
|
|
191
|
+
return "not_applicable";
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
await useRevenexxSdk().call("POST", "/promotions/reserve", {
|
|
195
|
+
body: {
|
|
196
|
+
cart_id: input.cartId,
|
|
197
|
+
promotions,
|
|
198
|
+
...(input.currency ? { currency: input.currency } : {}),
|
|
199
|
+
...(input.contactId ? { contact_id: input.contactId } : {}),
|
|
200
|
+
...(input.organizationId ? { organization_id: input.organizationId } : {}),
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
return "held";
|
|
204
|
+
}
|
|
205
|
+
catch (err) {
|
|
206
|
+
// A refusal is an ANSWER: the promotions app looked and said no, so the
|
|
207
|
+
// discount it was refusing may not be charged for. Anything else means
|
|
208
|
+
// nobody answered — a sale is not failed over an unreachable service,
|
|
209
|
+
// and the hold is picked up by reconciliation.
|
|
210
|
+
// 409 and 422 only. `isApiUserError` is wider on purpose elsewhere, but
|
|
211
|
+
// here it would read a 401 or a 429 — our own auth or our own rate limit
|
|
212
|
+
// — as the buyer's code being spent, and take their discount away for an
|
|
213
|
+
// infrastructure fault they had no part in.
|
|
214
|
+
if (err instanceof RevenexxException && (err.code === 409 || err.code === 422)) {
|
|
215
|
+
getLogService().error("Promotion reservation refused", apiErrorContext(err));
|
|
216
|
+
return "refused";
|
|
217
|
+
}
|
|
218
|
+
getLogService().error("Promotion reservation failed", apiErrorContext(err));
|
|
219
|
+
return "unavailable";
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export async function commitRedemption(
|
|
224
|
+
event: H3Event,
|
|
225
|
+
input: {
|
|
226
|
+
orderId: string;
|
|
227
|
+
promotions: PromotionEvaluation["applied"];
|
|
228
|
+
cartId?: string | null;
|
|
229
|
+
currency?: string;
|
|
230
|
+
contactId?: string | null;
|
|
231
|
+
organizationId?: string | null;
|
|
232
|
+
},
|
|
233
|
+
): Promise<void> {
|
|
234
|
+
if (resolvePromotionsServiceKey(event) !== "api") {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
await useRevenexxSdk().call("POST", "/promotions/commit", {
|
|
239
|
+
body: {
|
|
240
|
+
order_id: input.orderId,
|
|
241
|
+
// Commit promotes the cart's held rows; the list is what it falls
|
|
242
|
+
// back to when a hold has expired or was never taken, and it is
|
|
243
|
+
// what moves a promotion's budget and a voucher's usage_count.
|
|
244
|
+
promotions: heldPromotions(input.promotions),
|
|
245
|
+
...(input.cartId ? { cart_id: input.cartId } : {}),
|
|
246
|
+
...(input.currency ? { currency: input.currency } : {}),
|
|
247
|
+
...(input.contactId ? { contact_id: input.contactId } : {}),
|
|
248
|
+
...(input.organizationId ? { organization_id: input.organizationId } : {}),
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
catch (err) {
|
|
253
|
+
// The order exists and the buyer has been charged. A hold that was not
|
|
254
|
+
// committed expires on its own, so this is a reconciliation job, not a
|
|
255
|
+
// reason to fail a completed sale.
|
|
256
|
+
getLogService().error("Promotion commit failed — the hold will expire", apiErrorContext(err));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export async function releaseRedemption(
|
|
261
|
+
event: H3Event,
|
|
262
|
+
input: { cartId: string; reason: string },
|
|
263
|
+
): Promise<void> {
|
|
264
|
+
if (resolvePromotionsServiceKey(event) !== "api") {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
await useRevenexxSdk().call("POST", "/promotions/release", {
|
|
269
|
+
body: { cart_id: input.cartId, reason: input.reason },
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
catch (err) {
|
|
273
|
+
getLogService().error("Promotion release failed — the hold will expire", apiErrorContext(err));
|
|
274
|
+
}
|
|
275
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { PunchoutIntent } from "../../app/interfaces/punchout";
|
|
2
|
+
import type { PunchoutLandingTarget } from "../interfaces/punchout";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Where a punchout buyer lands, and why it is decided here.
|
|
6
|
+
*
|
|
7
|
+
* An entry call is rarely "come in and shop". A cXML setup request can name
|
|
8
|
+
* one article or reopen a requisition; an IDS call can be `ADL` (one article,
|
|
9
|
+
* by `ghnummer`), `AS` (a search for `searchterm`) or `WKS` (a cart the trade
|
|
10
|
+
* software sent in). The punchout app records what was asked for as
|
|
11
|
+
* `entry_action` plus `entry_intent`, in its own vocabulary rather than any
|
|
12
|
+
* protocol's — so this is the one place the shop reads it, and nothing here
|
|
13
|
+
* knows what OCI, cXML or IDS are.
|
|
14
|
+
*
|
|
15
|
+
* Two of the promises this keeps are the ones the specs leave to the shop:
|
|
16
|
+
* cxml-entry AC-9 and ids-actions AC-11 — "the buyer arrives where the call
|
|
17
|
+
* pointed", which is the half a buyer actually experiences.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* What the visit asked for.
|
|
22
|
+
*
|
|
23
|
+
* `entry_action` is the primary reading; `entry_intent.kind` is the fallback,
|
|
24
|
+
* because the two are written together and an adapter that filled only the
|
|
25
|
+
* intent (as the IDS inbound cart does, once the cart has been imported) must
|
|
26
|
+
* still land the buyer correctly.
|
|
27
|
+
*
|
|
28
|
+
* An action this shop does not recognise lands on the shop rather than
|
|
29
|
+
* nowhere: the alternative is a buyer who got through the door and met a 404.
|
|
30
|
+
*/
|
|
31
|
+
export function punchoutLandingTarget(
|
|
32
|
+
entryAction: string | null | undefined,
|
|
33
|
+
intent: PunchoutIntent | null | undefined,
|
|
34
|
+
cartId: string | null | undefined,
|
|
35
|
+
): PunchoutLandingTarget {
|
|
36
|
+
const kind = String(intent?.kind ?? "").trim();
|
|
37
|
+
const action = String(entryAction ?? "").trim();
|
|
38
|
+
|
|
39
|
+
// A cart the external system sent in, or a requisition it reopened. It is
|
|
40
|
+
// always a NEW cart — a `WKS` import creates one every time — so the buyer
|
|
41
|
+
// is pointed at THAT cart and never at "the" cart.
|
|
42
|
+
if (cartId && (kind === "inbound_cart" || action === "inbound_cart" || action === "reopen" || action === "inspect")) {
|
|
43
|
+
return { kind: "cart" };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const supplierPartId = String(intent?.supplier_part_id ?? "").trim();
|
|
47
|
+
if (supplierPartId && (kind === "article" || action === "article")) {
|
|
48
|
+
return { kind: "article", supplierPartId };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const term = String(intent?.term ?? "").trim();
|
|
52
|
+
if (term && (kind === "search" || action === "search")) {
|
|
53
|
+
return { kind: "search", term };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return { kind: "shop" };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The path for a target that needs no lookup.
|
|
61
|
+
*
|
|
62
|
+
* An article does: the external system names an article NUMBER and the shop's
|
|
63
|
+
* product pages are addressed by id and slug, so resolving one is a catalogue
|
|
64
|
+
* question — `punchoutArticlePath()` below.
|
|
65
|
+
*/
|
|
66
|
+
export function punchoutLandingPath(target: PunchoutLandingTarget): string | null {
|
|
67
|
+
switch (target.kind) {
|
|
68
|
+
case "shop":
|
|
69
|
+
return "/";
|
|
70
|
+
case "cart":
|
|
71
|
+
return "/cart";
|
|
72
|
+
case "search":
|
|
73
|
+
return `/search?q=${encodeURIComponent(target.term)}`;
|
|
74
|
+
case "article":
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** One row of the search answer, as much of it as resolving an article needs. */
|
|
80
|
+
interface SearchHit {
|
|
81
|
+
readonly id?: string;
|
|
82
|
+
readonly name?: string;
|
|
83
|
+
readonly sku?: string;
|
|
84
|
+
readonly categorySlug?: string | null;
|
|
85
|
+
readonly subcategorySlug?: string | null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The product page of one article number, or the search for it.
|
|
90
|
+
*
|
|
91
|
+
* The catalogue is addressed by `{id}-{name}` slugs under the article's
|
|
92
|
+
* category, and an external system knows neither — it knows the number it
|
|
93
|
+
* sells under. So the number is resolved through the shop's own search, which
|
|
94
|
+
* queries `name,sku`, and only an EXACT sku match counts: "lands on the
|
|
95
|
+
* article named by ghnummer" is a promise about one article, and a fuzzy match
|
|
96
|
+
* would quietly land the buyer on a different one.
|
|
97
|
+
*
|
|
98
|
+
* Anything else — several matches, none, or a search backend that is not there
|
|
99
|
+
* — lands on the search results for that number. That is honest (the buyer
|
|
100
|
+
* sees what the shop found for it) where a 404 or the home page would silently
|
|
101
|
+
* lose what the call asked for.
|
|
102
|
+
*
|
|
103
|
+
* The slug is built here rather than taken from `categoryProductPath()`: that
|
|
104
|
+
* helper sits in a composables file that pulls the Vue runtime in with it, and
|
|
105
|
+
* a server route must not. The two have to stay in step — a product URL the
|
|
106
|
+
* shop cannot route is a landing that 404s — which is what
|
|
107
|
+
* `test/unit/punchout-landing.test.ts` asserts against the same fixtures the
|
|
108
|
+
* app-side helper is asserted against.
|
|
109
|
+
*/
|
|
110
|
+
export function punchoutArticlePath(hits: readonly SearchHit[], supplierPartId: string): string {
|
|
111
|
+
const wanted = supplierPartId.trim().toLowerCase();
|
|
112
|
+
const exact = hits.filter(hit => String(hit.sku ?? "").trim().toLowerCase() === wanted);
|
|
113
|
+
|
|
114
|
+
const only = exact.length === 1 ? exact[0] : undefined;
|
|
115
|
+
if (only?.id && only.name) {
|
|
116
|
+
const segments = [only.categorySlug, only.subcategorySlug]
|
|
117
|
+
.filter((segment): segment is string => Boolean(segment) && segment !== "undefined");
|
|
118
|
+
const prefix = segments.length > 0 ? segments.join("/") : "_";
|
|
119
|
+
const slug = `${only.id}-${only.name.toLowerCase().replace(/\s+/g, "-").replace(/[^a-z0-9-]/g, "")}`;
|
|
120
|
+
return `/category/${prefix}/product/${slug}`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return `/search?q=${encodeURIComponent(supplierPartId)}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The same path, addressed the way this storefront addresses pages.
|
|
128
|
+
*
|
|
129
|
+
* A punchout buyer is redirected by a SERVER route, which cannot ask the Vue
|
|
130
|
+
* router what a page's URL is. So the locale prefix is applied here, from the
|
|
131
|
+
* app's own i18n strategy (published into runtime config by the cover layer's
|
|
132
|
+
* `modules:done` hook, because @nuxtjs/i18n v10 does not publish it itself).
|
|
133
|
+
*
|
|
134
|
+
* This is not cosmetic. Under `prefix` — the demo shop — an unprefixed `/cart`
|
|
135
|
+
* is a 404, so a buyer would be signed in and dropped on an error page; under
|
|
136
|
+
* `prefix_except_default` the default locale's pages are only reachable
|
|
137
|
+
* WITHOUT one, so a prefix would 404 just as hard. Both storefronts in this
|
|
138
|
+
* repo exist, and they answer differently.
|
|
139
|
+
*/
|
|
140
|
+
export function punchoutLocalePath(
|
|
141
|
+
path: string,
|
|
142
|
+
locale: string,
|
|
143
|
+
strategy: string,
|
|
144
|
+
defaultLocale: string,
|
|
145
|
+
): string {
|
|
146
|
+
if (!locale || strategy === "no_prefix") {
|
|
147
|
+
return path;
|
|
148
|
+
}
|
|
149
|
+
if (strategy === "prefix_except_default" && locale === defaultLocale) {
|
|
150
|
+
return path;
|
|
151
|
+
}
|
|
152
|
+
if (strategy !== "prefix" && strategy !== "prefix_and_default" && strategy !== "prefix_except_default") {
|
|
153
|
+
// An unknown strategy is a future one: leaving the path alone is what a
|
|
154
|
+
// storefront with no prefixes wants, and the worst case is one redirect
|
|
155
|
+
// the framework fixes up rather than a path invented out of nothing.
|
|
156
|
+
return path;
|
|
157
|
+
}
|
|
158
|
+
return `/${locale}${path === "/" ? "" : path}`;
|
|
159
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { CookieSerializeOptions } from "cookie-es";
|
|
2
|
+
import type { H3Event } from "h3";
|
|
3
|
+
import { deleteCookie, getCookie, setCookie } from "h3";
|
|
4
|
+
|
|
5
|
+
import type { PunchoutVisitView } from "../../app/interfaces/punchout";
|
|
6
|
+
import type { PunchoutVisit } from "../interfaces/punchout";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The storefront's own record of one punchout visit.
|
|
10
|
+
*
|
|
11
|
+
* It sits beside the auth session rather than inside it, because the two end
|
|
12
|
+
* at different moments: the buyer is signed in the instant the hand-off secret
|
|
13
|
+
* is redeemed, and stays signed in for the platform session's own lifetime,
|
|
14
|
+
* while the VISIT ends when the cart is handed back, when it expires, or when
|
|
15
|
+
* somebody revokes it. Folding one into the other would mean a hand-back could
|
|
16
|
+
* not sign the buyer out without also forgetting which visit had just ended.
|
|
17
|
+
*
|
|
18
|
+
* HTTP-only, so the handle cannot be rewritten in the browser. The handle is
|
|
19
|
+
* not a credential — it identifies a visit and nothing more, which is why the
|
|
20
|
+
* sign-in secret goes over a back channel instead — but a value the browser
|
|
21
|
+
* could edit would let a buyer hand back somebody else's cart.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export const PUNCHOUT_COOKIE_NAME = "cover-punchout";
|
|
25
|
+
|
|
26
|
+
function visitCookieOptions(expiresAt?: string): CookieSerializeOptions {
|
|
27
|
+
return {
|
|
28
|
+
httpOnly: true,
|
|
29
|
+
secure: !import.meta.dev,
|
|
30
|
+
// `lax` and not `strict`: the buyer arrives here by a cross-site
|
|
31
|
+
// redirect from their procurement system, and a strict cookie set on
|
|
32
|
+
// that navigation would not be sent back on the page that follows it.
|
|
33
|
+
sameSite: "lax",
|
|
34
|
+
path: "/",
|
|
35
|
+
...(expiresAt ? { expires: new Date(expiresAt) } : {}),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Remember one claimed visit for the rest of the buyer's stay. */
|
|
40
|
+
export function setPunchoutVisit(event: H3Event, visit: PunchoutVisit): void {
|
|
41
|
+
setCookie(event, PUNCHOUT_COOKIE_NAME, JSON.stringify(visit), visitCookieOptions(visit.expiresAt));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The visit this request belongs to, or null — reading only.
|
|
46
|
+
*
|
|
47
|
+
* A visit past its expiry is read as no visit at all: the punchout app settles
|
|
48
|
+
* an expired session the next time anything reads it, and the shop must not go
|
|
49
|
+
* on offering a transfer action for a visit that can no longer be handed back.
|
|
50
|
+
*
|
|
51
|
+
* Kept free of side effects because it is called from the SDK factory, on every
|
|
52
|
+
* gateway call: a read that also wrote a `Set-Cookie` would try to touch the
|
|
53
|
+
* response from paths that have already answered.
|
|
54
|
+
*/
|
|
55
|
+
export function readPunchoutVisit(event: H3Event, now: Date = new Date()): PunchoutVisit | null {
|
|
56
|
+
const raw = getCookie(event, PUNCHOUT_COOKIE_NAME);
|
|
57
|
+
if (!raw) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let visit: PunchoutVisit;
|
|
62
|
+
try {
|
|
63
|
+
visit = JSON.parse(raw) as PunchoutVisit;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!visit?.psid || !visit.protocol) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
if (visit.expiresAt && new Date(visit.expiresAt).getTime() <= now.getTime()) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
return visit;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The same, and it tidies up after itself: a cookie that is there but no longer
|
|
80
|
+
* usable is dropped, so the browser stops sending a visit that ended. For the
|
|
81
|
+
* routes that answer a request of their own — everything but the SDK factory.
|
|
82
|
+
*/
|
|
83
|
+
export function getPunchoutVisit(event: H3Event, now: Date = new Date()): PunchoutVisit | null {
|
|
84
|
+
const visit = readPunchoutVisit(event, now);
|
|
85
|
+
if (!visit && getCookie(event, PUNCHOUT_COOKIE_NAME)) {
|
|
86
|
+
clearPunchoutVisit(event);
|
|
87
|
+
}
|
|
88
|
+
return visit;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Update the stored visit in place — used to mark it handed back. */
|
|
92
|
+
export function updatePunchoutVisit(event: H3Event, patch: Partial<PunchoutVisit>): PunchoutVisit | null {
|
|
93
|
+
const visit = getPunchoutVisit(event);
|
|
94
|
+
if (!visit) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const updated = { ...visit, ...patch };
|
|
98
|
+
setPunchoutVisit(event, updated);
|
|
99
|
+
return updated;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function clearPunchoutVisit(event: H3Event): void {
|
|
103
|
+
deleteCookie(event, PUNCHOUT_COOKIE_NAME, visitCookieOptions());
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* What the browser is told about the visit: enough to show the right action in
|
|
108
|
+
* the right words, and nothing that names the visit to the punchout app. The
|
|
109
|
+
* handle stays server-side — the transfer action goes through the BFF.
|
|
110
|
+
*/
|
|
111
|
+
export function punchoutVisitView(visit: PunchoutVisit | null): PunchoutVisitView {
|
|
112
|
+
if (!visit) {
|
|
113
|
+
return {
|
|
114
|
+
active: false,
|
|
115
|
+
protocol: null,
|
|
116
|
+
channelCode: null,
|
|
117
|
+
entryAction: null,
|
|
118
|
+
readOnly: false,
|
|
119
|
+
cartId: null,
|
|
120
|
+
expiresAt: null,
|
|
121
|
+
handedBack: false,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
active: true,
|
|
126
|
+
protocol: visit.protocol,
|
|
127
|
+
channelCode: visit.channelCode,
|
|
128
|
+
entryAction: visit.entryAction,
|
|
129
|
+
readOnly: visit.readOnly === true,
|
|
130
|
+
cartId: visit.cartId,
|
|
131
|
+
expiresAt: visit.expiresAt,
|
|
132
|
+
handedBack: visit.handedBack === true,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
@@ -128,6 +128,31 @@ function activeMarket(): string {
|
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
/**
|
|
132
|
+
* The channel the current request shops, when it belongs to a punchout visit.
|
|
133
|
+
* "" otherwise — an ordinary shop visit carries no channel and the gateway
|
|
134
|
+
* answers whatever the tenant's default is.
|
|
135
|
+
*
|
|
136
|
+
* A punchout buyer's channel is not a preference, it is the account's: it
|
|
137
|
+
* decides which assortment and which price list they see, and it has to ride
|
|
138
|
+
* on EVERY call for the rest of the visit (RAD-44). Without it a punchout
|
|
139
|
+
* buyer silently gets the public shop's catalogue — silently, because nothing
|
|
140
|
+
* about the answer says which channel produced it.
|
|
141
|
+
*
|
|
142
|
+
* Per-request and never part of a cache key, for the same reason the market is
|
|
143
|
+
* not: the value comes from a cookie, and a client-keyed cache is a cache a
|
|
144
|
+
* caller can fill.
|
|
145
|
+
*/
|
|
146
|
+
function activeChannel(): string {
|
|
147
|
+
try {
|
|
148
|
+
return readPunchoutVisit(useEvent())?.channelCode ?? "";
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
// No async context (e.g. startup code) — no channel scoping.
|
|
152
|
+
return "";
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
131
156
|
/**
|
|
132
157
|
* The tenant slug the current request's SDK calls are scoped to — the cache
|
|
133
158
|
* key for any server-side cache of gateway data (markets, categories, …).
|
|
@@ -144,8 +169,9 @@ export function revenexxTenantKey(): string {
|
|
|
144
169
|
export function useRevenexxSdk(): RevenexxSdk {
|
|
145
170
|
const { tenant, apiKey, jwt, source } = resolveRevenexxCredentials();
|
|
146
171
|
const market = activeMarket();
|
|
172
|
+
const channel = activeChannel();
|
|
147
173
|
|
|
148
|
-
const isCacheable = source !== "brokered" && !market;
|
|
174
|
+
const isCacheable = source !== "brokered" && !market && !channel;
|
|
149
175
|
if (isCacheable) {
|
|
150
176
|
const cached = clients.get(tenant);
|
|
151
177
|
if (cached) {
|
|
@@ -175,6 +201,13 @@ export function useRevenexxSdk(): RevenexxSdk {
|
|
|
175
201
|
if (market) {
|
|
176
202
|
client.setMarket(market);
|
|
177
203
|
}
|
|
204
|
+
// Active channel (punchout) → X-Revenexx-Channel. The SDK has no setter for
|
|
205
|
+
// it — channels are a scope dimension the generated services do not declare
|
|
206
|
+
// a parameter for — so it goes on the client's own header map, which is
|
|
207
|
+
// what setMarket() writes to as well.
|
|
208
|
+
if (channel) {
|
|
209
|
+
client.headers["X-Revenexx-Channel"] = channel;
|
|
210
|
+
}
|
|
178
211
|
// Auth: the brokered context wins — it is a Zitadel JWT the gateway
|
|
179
212
|
// verifies against the same JWKS as any interactive caller, so one theme
|
|
180
213
|
// build serves any tenant without per-site credentials. The explicit
|