@revenexx/cover 0.22.1 → 0.24.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.
Files changed (40) hide show
  1. package/app/components/cart/view/CartSummaryPanel.vue +13 -6
  2. package/app/components/checkout/onepage/CheckoutAddressSection.vue +2 -2
  3. package/app/components/checkout/onepage/CheckoutGuestDetailsSection.vue +6 -0
  4. package/app/components/checkout/onepage/CheckoutNotesSection.vue +2 -0
  5. package/app/components/checkout/onepage/CheckoutPaymentSection.vue +2 -0
  6. package/app/components/checkout/onepage/CheckoutSummarySection.vue +8 -0
  7. package/app/components/checkout/return/CheckoutPaymentReturn.vue +7 -0
  8. package/app/components/layout/footer/Footer.vue +21 -4
  9. package/app/components/layout/header/AppBar.vue +5 -2
  10. package/app/components/layout/header/MobileMenu.vue +4 -1
  11. package/app/components/punchout/action/PunchoutTransferAction.vue +4 -3
  12. package/app/components/ui/forms/FormKitDatePicker.vue +1 -1
  13. package/app/composables/usePunchoutVisit.ts +36 -1
  14. package/app/interfaces/punchout.ts +11 -0
  15. package/app/middleware/punchout-exits.global.ts +25 -0
  16. package/app/plugins/punchout-links.client.ts +46 -0
  17. package/app/shared/punchoutExits.ts +156 -0
  18. package/app/shared/signInLink.ts +86 -0
  19. package/i18n/locales/de/punchout.json +3 -0
  20. package/i18n/locales/en/punchout.json +3 -0
  21. package/nuxt.config.ts +13 -0
  22. package/package.json +23 -21
  23. package/server/api/campaigns/touches.post.ts +1 -1
  24. package/server/api/orders/index.post.ts +17 -0
  25. package/server/interfaces/punchout.ts +20 -1
  26. package/server/middleware/market-scope.ts +24 -0
  27. package/server/middleware/punchout-exits.ts +45 -0
  28. package/server/middleware/punchout-visit-csp.ts +35 -0
  29. package/server/plugins/punchout-cookies.ts +48 -0
  30. package/server/routes/punchout/start.get.ts +7 -0
  31. package/server/services/ApiAuthService.ts +14 -3
  32. package/server/utils/appSettings.ts +1 -1
  33. package/server/utils/buyerIdentity.ts +28 -1
  34. package/server/utils/locale.ts +2 -2
  35. package/server/utils/market.ts +131 -7
  36. package/server/utils/punchoutFrame.ts +131 -0
  37. package/server/utils/punchoutOrder.ts +40 -0
  38. package/server/utils/punchoutVisit.ts +41 -8
  39. package/server/utils/revenexxSdk.ts +33 -6
  40. package/server/middleware/punchout-hand-back-csp.ts +0 -17
@@ -18,11 +18,12 @@ const cart = useCartStore();
18
18
  const actions = useCartActions();
19
19
  const { calculation, pending } = useCartCalculation();
20
20
  const { settings, role, workflows } = useB2BContext();
21
- // Inside a punchout visit the cart does not end in an order here: it is handed
22
- // back to the buyer's own procurement system, which decides. So the checkout
23
- // CTA is replaced rather than joined — two primary actions would be an
24
- // invitation to order twice.
25
- const { isPunchout, isReadOnly } = usePunchoutVisit();
21
+ // Inside a punchout visit the cart ends where the account's purchase mode says
22
+ // (RAD-226): handed back to the buyer's own procurement system, which decides
23
+ // (`transfer`), or ordered here (`order`, `order_and_transfer`). Outside the
24
+ // checkout of an ordering visit, the checkout CTA is replaced rather than
25
+ // joined — two primary actions would be an invitation to order twice.
26
+ const { isPunchout, isReadOnly, offersCheckout, ordersThenHandsBack } = usePunchoutVisit();
26
27
  const { formatCurrency } = useCartSummaryFormatting();
27
28
 
28
29
  const codeInput = ref("");
@@ -331,7 +332,7 @@ const trustItems = computed(() => {
331
332
 
332
333
  <CartApprovalsPanel :limits="calculation?.approvalLimits ?? []" />
333
334
 
334
- <PunchoutTransferAction v-if="isPunchout" />
335
+ <PunchoutTransferAction v-if="isPunchout && !offersCheckout" />
335
336
  <template v-else-if="quoteMessage">
336
337
  <UButton
337
338
  color="primary"
@@ -370,6 +371,12 @@ const trustItems = computed(() => {
370
371
  >
371
372
  {{ t('cart.cta.notOrderable') }}
372
373
  </p>
374
+ <p
375
+ v-else-if="ordersThenHandsBack"
376
+ class="text-xs text-muted text-center"
377
+ >
378
+ {{ t('punchout.orderAndTransfer.hint') }}
379
+ </p>
373
380
  </template>
374
381
 
375
382
  <!-- Trust: the promises that close B2B orders -->
@@ -80,7 +80,7 @@ function changeBilling() {
80
80
  v-if="!profileLoading"
81
81
  variant="ghost"
82
82
  size="sm"
83
- class="shrink-0"
83
+ class="shrink-0 min-h-11 lg:min-h-0"
84
84
  tabindex="0"
85
85
  @click="showShippingPicker = true"
86
86
  >
@@ -155,7 +155,7 @@ function changeBilling() {
155
155
  v-if="!profileLoading"
156
156
  variant="ghost"
157
157
  size="sm"
158
- class="shrink-0"
158
+ class="shrink-0 min-h-11 lg:min-h-0"
159
159
  tabindex="0"
160
160
  @click="changeBilling"
161
161
  >
@@ -65,6 +65,7 @@ const addressFields: AddressFieldRow[] = [
65
65
  type="email"
66
66
  autocomplete="email"
67
67
  class="w-full"
68
+ :ui="{ base: 'min-h-11 lg:min-h-0' }"
68
69
  @blur="validateField('guestEmail')"
69
70
  />
70
71
  </UFormField>
@@ -74,6 +75,7 @@ const addressFields: AddressFieldRow[] = [
74
75
  type="tel"
75
76
  autocomplete="tel"
76
77
  class="w-full"
78
+ :ui="{ base: 'min-h-11 lg:min-h-0' }"
77
79
  />
78
80
  </UFormField>
79
81
  </div>
@@ -98,6 +100,7 @@ const addressFields: AddressFieldRow[] = [
98
100
  <UInput
99
101
  v-model="guestShipping[field.key]"
100
102
  class="w-full"
103
+ :ui="{ base: 'min-h-11 lg:min-h-0' }"
101
104
  @update:model-value="clearFieldError(`guestShipping.${field.key}`)"
102
105
  />
103
106
  </UFormField>
@@ -111,6 +114,7 @@ const addressFields: AddressFieldRow[] = [
111
114
  v-model="guestShipping.country"
112
115
  :items="countryItems"
113
116
  class="w-full"
117
+ :ui="{ base: 'min-h-11 lg:min-h-0' }"
114
118
  @update:model-value="clearFieldError('guestShipping.country')"
115
119
  />
116
120
  </UFormField>
@@ -141,6 +145,7 @@ const addressFields: AddressFieldRow[] = [
141
145
  <UInput
142
146
  v-model="guestBilling[field.key]"
143
147
  class="w-full"
148
+ :ui="{ base: 'min-h-11 lg:min-h-0' }"
144
149
  @update:model-value="clearFieldError(`guestBilling.${field.key}`)"
145
150
  />
146
151
  </UFormField>
@@ -154,6 +159,7 @@ const addressFields: AddressFieldRow[] = [
154
159
  v-model="guestBilling.country"
155
160
  :items="countryItems"
156
161
  class="w-full"
162
+ :ui="{ base: 'min-h-11 lg:min-h-0' }"
157
163
  @update:model-value="clearFieldError('guestBilling.country')"
158
164
  />
159
165
  </UFormField>
@@ -26,6 +26,7 @@ const { form, isGuest, orderNumberRequired, fieldErrors, validateField } = useCh
26
26
  v-model="form.orderNumber"
27
27
  maxlength="20"
28
28
  class="w-full"
29
+ :ui="{ base: 'min-h-11 lg:min-h-0' }"
29
30
  :placeholder="t('onepage.orderInfo.orderNumberPlaceholder')"
30
31
  @blur="validateField('orderNumber')"
31
32
  />
@@ -38,6 +39,7 @@ const { form, isGuest, orderNumberRequired, fieldErrors, validateField } = useCh
38
39
  v-model="form.quoteNumber"
39
40
  maxlength="20"
40
41
  class="w-full"
42
+ :ui="{ base: 'min-h-11 lg:min-h-0' }"
41
43
  :placeholder="t('onepage.orderInfo.quoteNumberPlaceholder')"
42
44
  />
43
45
  </UFormField>
@@ -249,6 +249,7 @@ if (import.meta.client) {
249
249
  <UInput
250
250
  v-model="form.poNumber"
251
251
  class="w-full sm:max-w-sm"
252
+ :ui="{ base: 'min-h-11 lg:min-h-0' }"
252
253
  :placeholder="t('onepage.payment.poNumberPlaceholder')"
253
254
  />
254
255
  </div>
@@ -263,6 +264,7 @@ if (import.meta.client) {
263
264
  <USelect
264
265
  v-model="form.costCenterId"
265
266
  class="w-full sm:max-w-sm"
267
+ :ui="{ base: 'min-h-11 lg:min-h-0' }"
266
268
  :items="costCenterOptions"
267
269
  />
268
270
  </div>
@@ -17,6 +17,7 @@ const isRequester = computed(() => role.value === "requester");
17
17
  const selectedWorkflowId = useState<string | null>("cart-selected-workflow", () => null);
18
18
  const { to } = useAppUrl();
19
19
  const cart = useCartStore();
20
+ const { ordersThenHandsBack, refresh: refreshPunchoutVisit } = usePunchoutVisit();
20
21
  const { formatCurrency } = useCartSummaryFormatting();
21
22
 
22
23
  // The checkout shows the same server-calculated document as the cart,
@@ -49,6 +50,13 @@ async function handlePlaceOrder() {
49
50
 
50
51
  validateField("acceptedTerms");
51
52
  const result = await submitOrder();
53
+ // `order_and_transfer` (RAD-226): one step — the order, then the hand-back
54
+ // of the ordered cart, which the hand-back page fetches and submits.
55
+ if (result && ordersThenHandsBack.value) {
56
+ await refreshPunchoutVisit();
57
+ await navigateTo(to("/punchout/transfer"));
58
+ return;
59
+ }
52
60
  if (result) {
53
61
  await navigateTo(to("/checkout/confirmation", {
54
62
  id: result.orderId,
@@ -22,6 +22,7 @@ const { icon } = useIcons();
22
22
  const { to } = useAppUrl();
23
23
  const localePath = useLocalePath();
24
24
  const route = useRoute();
25
+ const { ordersThenHandsBack, refresh: refreshPunchoutVisit } = usePunchoutVisit();
25
26
 
26
27
  const orderId = computed(() => {
27
28
  const raw = route.query[PSP_RETURN_ORDER_PARAM];
@@ -52,6 +53,12 @@ async function confirm(): Promise<void> {
52
53
  );
53
54
  canRetryFromAccount.value = answer.canRetryFromAccount;
54
55
  retryUrl.value = answer.retryUrl ?? null;
56
+ if (answer.outcome === "paid" && ordersThenHandsBack.value) {
57
+ // `order_and_transfer` (RAD-226): the paid order is handed back next.
58
+ await refreshPunchoutVisit();
59
+ await navigateTo(to("/punchout/transfer"), { replace: true });
60
+ return;
61
+ }
55
62
  if (answer.outcome === "paid") {
56
63
  await navigateTo(to("/checkout/confirmation", { id: answer.orderId }), { replace: true });
57
64
  return;
@@ -1,6 +1,7 @@
1
1
  <script setup lang="ts">
2
2
  import type { ShopContact } from "../../../config/navigation";
3
3
  import { footerLinks as defaultFooterLinks, legalLinks as defaultLegalLinks, shopContact as noContact, withdrawalPath } from "../../../config/navigation";
4
+ import { punchoutRefusal } from "../../../shared/punchoutExits";
4
5
 
5
6
  // Consumers replace the footer columns / legal links, in priority order:
6
7
  // per-request live menus (useState, e.g. the Blokkli theme from the pages app)
@@ -14,11 +15,24 @@ import { footerLinks as defaultFooterLinks, legalLinks as defaultLegalLinks, sho
14
15
  const appConfig = useAppConfig();
15
16
  const liveFooterLinks = useState<{ heading: string; links: { label: string; to: string }[] }[] | null>("cover:nav:footerLinks", () => null);
16
17
  const liveLegalLinks = useState<{ label: string; to: string }[] | null>("cover:nav:legalLinks", () => null);
17
- const footerLinks = computed(() => liveFooterLinks.value ?? (appConfig.footerLinks as typeof defaultFooterLinks | undefined) ?? defaultFooterLinks);
18
+ // Punchout mode (RAD-226): a live visit has one way out, its own action in the
19
+ // cart, so the footer keeps only the links that stay inside the shop and lead
20
+ // somewhere the visit may go — no external site, no account page.
21
+ const { isRestricted, visit } = usePunchoutVisit();
22
+ const staysInVisit = (to: string) => !isRestricted.value || (to.startsWith("/") && !to.startsWith("//") && !punchoutRefusal(to, "GET", visit.value));
23
+ const footerLinks = computed(() => {
24
+ const groups = liveFooterLinks.value ?? (appConfig.footerLinks as typeof defaultFooterLinks | undefined) ?? defaultFooterLinks;
25
+ if (!isRestricted.value || !Array.isArray(groups)) {
26
+ return groups;
27
+ }
28
+ return groups
29
+ .map(group => ({ ...group, links: group.links.filter(link => staysInVisit(link.to)) }))
30
+ .filter(group => group.links.length > 0);
31
+ });
18
32
  // The withdrawal entry is rendered on its own below, so a tenant menu that
19
33
  // already lists it does not print it twice.
20
34
  const legalLinks = computed(() => (liveLegalLinks.value ?? (appConfig.legalLinks as typeof defaultLegalLinks | undefined) ?? defaultLegalLinks)
21
- .filter(link => link.to !== withdrawalPath));
35
+ .filter(link => link.to !== withdrawalPath && staysInVisit(link.to)));
22
36
  // The shop's own contact details. The design system holds none, so a storefront
23
37
  // that states nothing prints nothing rather than somebody else's address.
24
38
  const shopContact = computed<ShopContact>(() => (appConfig.shopContact as ShopContact | undefined) ?? noContact);
@@ -166,8 +180,11 @@ const year = new Date().getFullYear();
166
180
  </ConsentPreferencesLink>
167
181
  </nav>
168
182
 
169
- <!-- Social icons -->
170
- <div class="flex items-center gap-1">
183
+ <!-- Social icons — not in a punchout visit, which never leaves the shop -->
184
+ <div
185
+ v-if="!isRestricted"
186
+ class="flex items-center gap-1"
187
+ >
171
188
  <UButton
172
189
  to="https://linkedin.com"
173
190
  target="_blank"
@@ -41,6 +41,8 @@ onMounted(() => {
41
41
  onUnmounted(() => mq?.removeEventListener("change", closeMenu));
42
42
 
43
43
  const { navigateToSearch } = useSearch();
44
+ // Punchout mode (RAD-226): the account area and signing in or out are hidden.
45
+ const { isRestricted } = usePunchoutVisit();
44
46
  </script>
45
47
 
46
48
  <template>
@@ -70,13 +72,14 @@ const { navigateToSearch } = useSearch();
70
72
 
71
73
  <template #right>
72
74
  <LayoutHeaderActionItem
73
- v-if="features.directOrder !== false"
75
+ v-if="features.directOrder !== false && !isRestricted"
74
76
  :icon="icon('direct-order')"
75
77
  :label="t('topbar.directOrder')"
76
78
  :to="localePath('/account/direct-order')"
77
79
  class="hidden md:flex"
78
80
  />
79
- <AccountMenu />
81
+ <!-- Not in a punchout visit: it has no account area, and signing in or out is not its exit (RAD-226) -->
82
+ <AccountMenu v-if="!isRestricted" />
80
83
  <CartButton />
81
84
  </template>
82
85
 
@@ -9,6 +9,7 @@ const { categoryNavItems } = useCategories();
9
9
  // buyer on a phone had no way to search at all.
10
10
  const { navigateToSearch } = useSearch();
11
11
  const auth = useAuthStore();
12
+ const { isRestricted } = usePunchoutVisit();
12
13
  const toast = useToast();
13
14
  const { t } = useI18n();
14
15
  const localePath = useLocalePath();
@@ -66,7 +67,9 @@ const onLogout = async () => {
66
67
 
67
68
  <USeparator class="md:hidden" />
68
69
 
69
- <template v-if="auth.isLoggedIn">
70
+ <!-- Punchout mode (RAD-226): no account area, no signing in or out -->
71
+ <template v-if="isRestricted" />
72
+ <template v-else-if="auth.isLoggedIn">
70
73
  <UNavigationMenu
71
74
  :items="accountLinks"
72
75
  orientation="vertical"
@@ -2,7 +2,8 @@
2
2
  /**
3
3
  * "Transfer cart" — the end of a punchout visit, as the buyer meets it.
4
4
  *
5
- * Offered ONLY inside a punchout session, and worded by that session's
5
+ * Offered ONLY inside a punchout session whose purchase mode hands the cart
6
+ * back (RAD-226) — after the order, for `order_and_transfer` — and worded by that session's
6
7
  * protocol: the same button is "transfer the basket back" to an SRM buyer,
7
8
  * "return the requisition" to a cXML one and "send the cart to your trade
8
9
  * software" to an IDS craftsman.
@@ -15,7 +16,7 @@
15
16
  const { icon } = useIcons();
16
17
  const { t } = useI18n();
17
18
  const { to } = useAppUrl();
18
- const { isPunchout, isReadOnly, protocol, transferLabelKey } = usePunchoutVisit();
19
+ const { isReadOnly, offersHandBack, protocol, transferLabelKey } = usePunchoutVisit();
19
20
  const cart = useCartStore();
20
21
 
21
22
  const props = withDefaults(defineProps<{
@@ -33,7 +34,7 @@ const isEmpty = computed(() => cart.items.length === 0);
33
34
 
34
35
  <template>
35
36
  <div
36
- v-if="isPunchout"
37
+ v-if="offersHandBack"
37
38
  class="space-y-2"
38
39
  >
39
40
  <UButton
@@ -82,7 +82,7 @@ function clear(): void {
82
82
  type="button"
83
83
  tabindex="0"
84
84
  :disabled="Boolean(context.disabled)"
85
- class="w-full flex items-center justify-between gap-2 px-3 py-2 text-sm border rounded-md
85
+ class="w-full min-h-11 lg:min-h-0 flex items-center justify-between gap-2 px-3 py-2 text-sm border rounded-md
86
86
  bg-transparent border-(--ui-border) text-left cursor-pointer
87
87
  focus:outline-none focus:ring-2 focus:ring-(--ui-primary)"
88
88
  :class="calendarValue ? 'text-highlighted' : 'text-muted'"
@@ -1,4 +1,5 @@
1
1
  import type { PunchoutVisitView } from "../interfaces/punchout";
2
+ import { punchoutExits } from "../shared/punchoutExits";
2
3
 
3
4
  const EMPTY: PunchoutVisitView = {
4
5
  active: false,
@@ -9,6 +10,8 @@ const EMPTY: PunchoutVisitView = {
9
10
  cartId: null,
10
11
  expiresAt: null,
11
12
  handedBack: false,
13
+ purchaseMode: "transfer",
14
+ ordered: false,
12
15
  };
13
16
 
14
17
  /**
@@ -51,5 +54,37 @@ export function usePunchoutVisit() {
51
54
  `punchout.transfer.action.${visit.value.protocol ?? "oci"}`,
52
55
  );
53
56
 
54
- return { visit, isPunchout, isReadOnly, protocol, transferLabelKey, refresh };
57
+ /**
58
+ * Punchout mode (RAD-226): a live visit offers only its own exits, and
59
+ * the shop hides every other way out — the account area, signing in or
60
+ * out, registering, links that leave the shop. Over once the cart was
61
+ * handed back.
62
+ */
63
+ const isRestricted = computed(() => visit.value.active && !visit.value.handedBack);
64
+ const exits = computed(() => punchoutExits(visit.value));
65
+ /** The checkout places this visit's order — `order`, or `order_and_transfer`. */
66
+ const offersCheckout = computed(() => isRestricted.value && exits.value.checkout);
67
+ /**
68
+ * The cart is handed back — `transfer`, or after the order in
69
+ * `order_and_transfer`, whose hand-back the punchout app refuses without
70
+ * the order. Still true once handed back, so the hand-back page can
71
+ * replay it.
72
+ */
73
+ const offersHandBack = computed(() => isPunchout.value && exits.value.handBack
74
+ && (visit.value.purchaseMode !== "order_and_transfer" || visit.value.ordered || visit.value.handedBack));
75
+ /** The checkout is followed by the hand-back, as one step. */
76
+ const ordersThenHandsBack = computed(() => isPunchout.value && visit.value.purchaseMode === "order_and_transfer");
77
+
78
+ return {
79
+ visit,
80
+ isPunchout,
81
+ isReadOnly,
82
+ isRestricted,
83
+ offersCheckout,
84
+ offersHandBack,
85
+ ordersThenHandsBack,
86
+ protocol,
87
+ transferLabelKey,
88
+ refresh,
89
+ };
55
90
  }
@@ -11,6 +11,13 @@
11
11
  /** The three protocols a visit can be entered by. */
12
12
  export type PunchoutProtocol = "oci" | "cxml" | "ids";
13
13
 
14
+ /**
15
+ * Which exit a visit has, as the punchout account says: hand the cart back,
16
+ * order in the shop, or order and then hand the ordered cart back in one step.
17
+ * Anything but `transfer` is IDS only.
18
+ */
19
+ export type PunchoutPurchaseMode = "transfer" | "order" | "order_and_transfer";
20
+
14
21
  /**
15
22
  * What the visit is FOR, in the punchout app's own words rather than any
16
23
  * protocol's. The shop branches on this and never on OCI's `FUNCTION`, cXML's
@@ -47,6 +54,10 @@ export interface PunchoutVisitView {
47
54
  readonly expiresAt: string | null;
48
55
  /** True once the cart has been handed back — the visit is over. */
49
56
  readonly handedBack: boolean;
57
+ /** Which exit the visit has; `transfer` outside a visit too. */
58
+ readonly purchaseMode: PunchoutPurchaseMode;
59
+ /** True once the checkout placed this visit's order. */
60
+ readonly ordered: boolean;
50
61
  }
51
62
 
52
63
  /**
@@ -0,0 +1,25 @@
1
+ import type { PunchoutVisitView } from "../interfaces/punchout";
2
+ import { punchoutRefusal } from "../shared/punchoutExits";
3
+
4
+ /**
5
+ * Punchout mode inside the app (RAD-226): a link followed during a live visit
6
+ * to an exit the visit does not have — the account area, signing in, the
7
+ * checkout of a visit that only hands its cart back — goes to the visit's
8
+ * cart instead. The same rules as the BFF's, which holds them for a typed
9
+ * address; this half covers a navigation the server never sees.
10
+ *
11
+ * Reads the visit the shop shell already fetched rather than asking again:
12
+ * on the first page load the server has decided, and every later navigation
13
+ * finds it in the payload.
14
+ */
15
+ export default defineNuxtRouteMiddleware((to) => {
16
+ const { data } = useNuxtData<PunchoutVisitView>("punchout-visit");
17
+ const visit = data.value;
18
+ if (!visit?.active || visit.handedBack) {
19
+ return;
20
+ }
21
+ const refusal = punchoutRefusal(to.path, "GET", visit);
22
+ if (refusal?.kind === "redirect") {
23
+ return navigateTo(refusal.to, { replace: true });
24
+ }
25
+ });
@@ -0,0 +1,46 @@
1
+ import type { PunchoutVisitView } from "../interfaces/punchout";
2
+
3
+ /**
4
+ * Punchout mode for links the shop does not render itself (RAD-226): CMS
5
+ * content, a product description, a tenant's footer menu. During a live
6
+ * visit a link that would leave the shop does nothing, and a link that would
7
+ * open a new window opens in place — the visit runs inside the buyer's
8
+ * procurement system, and a window or a site outside it is a way out that
9
+ * strands the cart.
10
+ *
11
+ * One listener on the document rather than a rule in every component: the
12
+ * links that matter here are exactly the ones no component knows about.
13
+ */
14
+ export default defineNuxtPlugin(() => {
15
+ const visit = () => useNuxtData<PunchoutVisitView>("punchout-visit").data.value;
16
+
17
+ document.addEventListener("click", (event) => {
18
+ const current = visit();
19
+ if (!current?.active || current.handedBack || event.defaultPrevented) {
20
+ return;
21
+ }
22
+ const anchor = (event.target as Element | null)?.closest?.("a[href]") as HTMLAnchorElement | null;
23
+ if (!anchor) {
24
+ return;
25
+ }
26
+ let url: URL;
27
+ try {
28
+ url = new URL(anchor.href, window.location.href);
29
+ }
30
+ catch {
31
+ return;
32
+ }
33
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
34
+ // mailto:, tel: — they open another program, not another site.
35
+ return;
36
+ }
37
+ if (url.origin !== window.location.origin) {
38
+ event.preventDefault();
39
+ return;
40
+ }
41
+ if (anchor.target && anchor.target !== "_self") {
42
+ event.preventDefault();
43
+ void navigateTo(`${url.pathname}${url.search}${url.hash}`);
44
+ }
45
+ }, true);
46
+ });
@@ -0,0 +1,156 @@
1
+ import type { PunchoutPurchaseMode } from "../interfaces/punchout";
2
+
3
+ /** What the rules read off a visit — the server's record and the browser's view alike. */
4
+ export interface PunchoutExitVisit {
5
+ readonly purchaseMode?: PunchoutPurchaseMode | null;
6
+ /** The checkout placed this visit's order. */
7
+ readonly ordered?: boolean;
8
+ }
9
+
10
+ /**
11
+ * The exits a punchout visit has, and the routes it may not reach (RAD-226).
12
+ *
13
+ * A punchout visit runs inside the buyer's procurement system and has one way
14
+ * out: the one its account's purchase mode names. Everything a storefront
15
+ * session offers besides — the account area, signing in or out, registering,
16
+ * the checkout of a visit that only hands its cart back — is hidden in the
17
+ * shop AND refused here, because a buyer can type an address.
18
+ *
19
+ * Shared by the BFF (a typed address, a full page load) and the shop's route
20
+ * middleware (a link followed inside the app), so the two cannot disagree.
21
+ *
22
+ * The rule holds only while the visit is live. A visit that was handed back,
23
+ * or that ran out, is over, and the shop is an ordinary shop again.
24
+ */
25
+
26
+ /** A purchase mode the claim did not answer is `transfer` (punchout's own default). */
27
+ export function purchaseModeOf(value: unknown): PunchoutPurchaseMode {
28
+ return value === "order" || value === "order_and_transfer" ? value : "transfer";
29
+ }
30
+
31
+ export interface PunchoutExits {
32
+ /** The shop's checkout places an order. */
33
+ readonly checkout: boolean;
34
+ /** The cart can be handed back to the procurement system. */
35
+ readonly handBack: boolean;
36
+ }
37
+
38
+ /**
39
+ * Which exits a visit offers.
40
+ *
41
+ * `order_and_transfer` offers both, as ONE step: the checkout, and then the
42
+ * hand-back of the ordered cart, which the punchout app refuses without the
43
+ * order. A visit already ordered in `order` mode has no exit left.
44
+ */
45
+ export function punchoutExits(visit: PunchoutExitVisit): PunchoutExits {
46
+ const mode = purchaseModeOf(visit.purchaseMode);
47
+ if (mode === "order") {
48
+ return { checkout: visit.ordered !== true, handBack: false };
49
+ }
50
+ if (mode === "order_and_transfer") {
51
+ return { checkout: visit.ordered !== true, handBack: true };
52
+ }
53
+ return { checkout: false, handBack: true };
54
+ }
55
+
56
+ /** A path without its locale prefix (`/de/account` → `/account`) and query. */
57
+ export function unprefixedPath(path: string): { readonly prefix: string; readonly rest: string } {
58
+ const bare = (path.split("?")[0] ?? "/").replace(/\/+$/, "") || "/";
59
+ const match = /^\/([a-z]{2}(?:-[a-z]{2})?)(\/.*|$)/i.exec(bare);
60
+ // `/de` alone is the home page in that locale, not a page called `de`.
61
+ if (match && !/^\/(api|_nuxt|punchout)\b/.test(bare)) {
62
+ return { prefix: `/${match[1]}`, rest: match[2] || "/" };
63
+ }
64
+ return { prefix: "", rest: bare };
65
+ }
66
+
67
+ /** Pages a live visit never shows: the account area and everything about signing in. */
68
+ const PAGES_NEVER = [
69
+ /^\/account(?:\/|$)/,
70
+ /^\/login(?:\/|$)/,
71
+ /^\/register(?:\/|$)/,
72
+ /^\/forgot-password$/,
73
+ /^\/reset-password$/,
74
+ /^\/callback$/,
75
+ ];
76
+
77
+ /** The checkout pages — only for a visit that may order. */
78
+ const CHECKOUT_PAGES = /^\/checkout(?:\/|$)/;
79
+ /** The hand-back page — only for a visit that may hand back. */
80
+ const HAND_BACK_PAGE = /^\/punchout\/transfer$/;
81
+
82
+ /** BFF routes a live visit never calls: signing in or out, registering, recovering a password. */
83
+ const API_NEVER = [
84
+ /^\/api\/auth\/(?:login|logout|register|recovery|magic-link|otp)$/,
85
+ ];
86
+
87
+ /** BFF routes that place an order. */
88
+ const CHECKOUT_API = [
89
+ /^\/api\/orders$/,
90
+ /^\/api\/checkout\/session$/,
91
+ ];
92
+
93
+ /** The BFF route that hands the cart back. */
94
+ const HAND_BACK_API = /^\/api\/punchout\/return$/;
95
+
96
+ export type PunchoutRefusal =
97
+ | { readonly kind: "redirect"; readonly to: string }
98
+ | { readonly kind: "refuse"; readonly type: "punchout_exit_not_offered" | "punchout_session_closed" };
99
+
100
+ /**
101
+ * What a live visit does with this request: nothing (null), send the page back
102
+ * to the visit's cart, or refuse the call.
103
+ *
104
+ * Only state-changing calls are refused on the BFF side: what a buyer READS
105
+ * about their own account is theirs to read, and a product page that asks
106
+ * for an order list should not break because the visit has no account area.
107
+ */
108
+ export function punchoutRefusal(
109
+ path: string,
110
+ method: string,
111
+ visit: PunchoutExitVisit,
112
+ ): PunchoutRefusal | null {
113
+ const { prefix, rest } = unprefixedPath(path);
114
+ const exits = punchoutExits(visit);
115
+ const back: PunchoutRefusal = { kind: "redirect", to: `${prefix}/cart` };
116
+
117
+ if (rest.startsWith("/api/")) {
118
+ const writes = method.toUpperCase() !== "GET" && method.toUpperCase() !== "HEAD";
119
+ if (!writes) {
120
+ return null;
121
+ }
122
+ if (API_NEVER.some(rule => rule.test(rest))) {
123
+ return { kind: "refuse", type: "punchout_exit_not_offered" };
124
+ }
125
+ if (!exits.checkout && CHECKOUT_API.some(rule => rule.test(rest))) {
126
+ return { kind: "refuse", type: "punchout_exit_not_offered" };
127
+ }
128
+ if (!exits.handBack && HAND_BACK_API.test(rest)) {
129
+ return { kind: "refuse", type: "punchout_exit_not_offered" };
130
+ }
131
+ return null;
132
+ }
133
+
134
+ if (PAGES_NEVER.some(rule => rule.test(rest))) {
135
+ return back;
136
+ }
137
+ // The confirmation of the order this visit placed stays reachable: the
138
+ // buyer has to be able to read what they ordered.
139
+ if (!exits.checkout && CHECKOUT_PAGES.test(rest) && !(visit.ordered === true && rest === "/checkout/confirmation")) {
140
+ return back;
141
+ }
142
+ if (!exits.handBack && HAND_BACK_PAGE.test(rest)) {
143
+ return back;
144
+ }
145
+ return null;
146
+ }
147
+
148
+ /**
149
+ * The checkout calls a visit that RAN OUT may not make: an order created after
150
+ * the visit's expiry does not count for it, and the buyer would have placed
151
+ * an order their procurement system never hears of.
152
+ */
153
+ export function isCheckoutCall(path: string, method: string): boolean {
154
+ const { rest } = unprefixedPath(path);
155
+ return method.toUpperCase() === "POST" && CHECKOUT_API.some(rule => rule.test(rest));
156
+ }