@revenexx/cover 0.5.0 → 0.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@revenexx/cover",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Cover \u2014 revenexx design system for Nuxt. Distributed as a Nuxt layer: generic UI components, theming tokens and stores shared by the demo shop, custom storefronts and the Blokkli theme.",
5
5
  "type": "module",
6
6
  "main": "./nuxt.config.ts",
@@ -171,6 +171,12 @@ export default defineEventHandler(async (event) => {
171
171
  l => l.status === "exceeded" && l.approval.required,
172
172
  );
173
173
 
174
+ // An organisation that approves its spending, in a storefront whose
175
+ // procurement app is live: then the app decides what this cart becomes,
176
+ // and the demo limits above decide nothing (the context carries none —
177
+ // see the B2B context service).
178
+ const throughProcurement = context.settings.approvals.enabled && isProcurementLive(event);
179
+
174
180
  // Live shipping: the chosen method must be a real rate for this
175
181
  // buyer context; its price replaces the demo calculation's shipping
176
182
  // line in the order totals and the amount the payment is created over.
@@ -337,7 +343,7 @@ export default defineEventHandler(async (event) => {
337
343
  discounts = [];
338
344
  }
339
345
 
340
- const placed = await useRevenexxSdk().orders.ordersPlace({
346
+ const submission = {
341
347
  ...(contactId ? { contactId } : {}),
342
348
  ...(organizationId ? { organizationId } : {}),
343
349
  ...(cartId ? { cartId } : {}),
@@ -375,7 +381,37 @@ export default defineEventHandler(async (event) => {
375
381
  ...(body.partialDelivery !== undefined ? { partial_delivery: body.partialDelivery } : {}),
376
382
  ...(body.requestedDate ? { requested_date: body.requestedDate } : {}),
377
383
  },
378
- }) as unknown as { id: string; number: string; grand_total?: number };
384
+ };
385
+
386
+ // Where spending is approved before it happens, the procurement
387
+ // app is what a checkout hands the cart to: it decides between
388
+ // an order and a request waiting for a decision. Everywhere else
389
+ // the orders app places the order directly, as it always has.
390
+ const outcome = throughProcurement
391
+ ? await submitThroughProcurement(submission as never)
392
+ : {
393
+ kind: "order" as const,
394
+ order: await useRevenexxSdk().orders.ordersPlace(submission as never) as unknown as SubmittedOrder,
395
+ };
396
+
397
+ if (outcome.kind === "request") {
398
+ // Nothing was bought, so nothing is charged and no promotion
399
+ // is spent: the hold goes back and waits for the order the
400
+ // approval may yet produce.
401
+ if (reserved === "held" && cartId) {
402
+ await releaseRedemption(event, { cartId, reason: PromotionsRedemptionsReleaseReason.CartReleased });
403
+ }
404
+ return {
405
+ orderId: outcome.request.number,
406
+ status: "approval-pending",
407
+ createdAt: new Date().toISOString(),
408
+ payment,
409
+ approvalRequired: true,
410
+ approvers: [],
411
+ };
412
+ }
413
+
414
+ const placed = outcome.order;
379
415
  orderId = placed.number;
380
416
  liveOrderUuid = placed.id;
381
417
  liveOrderTotal = typeof placed.grand_total === "number" ? placed.grand_total : null;
@@ -88,7 +88,15 @@ export class B2BContextService implements IB2BContextService {
88
88
  // Approval limits only apply when actually ordering — requesters never
89
89
  // order directly, so their context carries none (limits are not
90
90
  // checked for requisitions).
91
- const limitsApply = settings.approvals.enabled && role !== "requester";
91
+ //
92
+ // And where the procurement app is live it is the one that decides,
93
+ // at submission, against rules the merchant configured. The demo
94
+ // limits then carry none of that and must not pre-judge a cart in the
95
+ // shop: a buyer warned by one set of rules and stopped by another is
96
+ // worse off than one who is only ever told the truth once.
97
+ const limitsApply = settings.approvals.enabled
98
+ && role !== "requester"
99
+ && !isProcurementLive(this.event);
92
100
 
93
101
  const costCenterLimits = limitsApply
94
102
  ? costCenters.flatMap(cc => (cc.limit ? [cc.limit] : []))
@@ -71,7 +71,7 @@ const requisitionRegistry: Record<string, IRequisitionService> = {
71
71
 
72
72
  export function getRequisitionService(event?: H3Event): IRequisitionService {
73
73
  const key = resolveServiceKey(event, {
74
- domain: "requisitionService",
74
+ domain: "procurementService",
75
75
  mockKey: "mock",
76
76
  liveKey: "api",
77
77
  });
@@ -0,0 +1,98 @@
1
+ import type { H3Event } from "h3";
2
+
3
+ /**
4
+ * Submitting a cart where spending is approved before it happens.
5
+ *
6
+ * For such an organisation the procurement app — not the orders app — is what
7
+ * a checkout hands the cart to. It decides between the two things a submitted
8
+ * cart can become: an order, when nothing it checks objects, or a request
9
+ * waiting for somebody to say yes. The shop cannot make that call itself; it
10
+ * does not hold the rules, and guessing would mean charging for a purchase
11
+ * nobody approved.
12
+ *
13
+ * So the shop asks once and reads the answer. An order comes back the way the
14
+ * orders app would have answered it, which is what lets the rest of the
15
+ * checkout — the payment, the confirmation — carry on unchanged.
16
+ */
17
+ export function isProcurementLive(event?: H3Event): boolean {
18
+ return resolveServiceKey(event, {
19
+ domain: "procurementService",
20
+ mockKey: "mock",
21
+ liveKey: "api",
22
+ }) === "api";
23
+ }
24
+
25
+ /** An order, exactly as the orders app answers one. */
26
+ export interface SubmittedOrder {
27
+ id: string;
28
+ number: string;
29
+ grand_total?: number;
30
+ }
31
+
32
+ /** A request waiting for a decision. */
33
+ export interface SubmittedRequest {
34
+ id: string;
35
+ number: string;
36
+ status?: string;
37
+ }
38
+
39
+ export type SubmitOutcome =
40
+ | { readonly kind: "order"; readonly order: SubmittedOrder }
41
+ | { readonly kind: "request"; readonly request: SubmittedRequest };
42
+
43
+ /** What the orders app is asked for, in the words procurement asks for it. */
44
+ interface OrderLikePayload {
45
+ contactId?: string;
46
+ organizationId?: string;
47
+ cartId?: string;
48
+ currency: string;
49
+ customerOrderNumber?: string;
50
+ buyer?: unknown;
51
+ billingAddress?: unknown;
52
+ shippingAddress?: unknown;
53
+ payment?: { method: string };
54
+ shipping?: { method: string; price: number; tax_rate: number };
55
+ items: Array<Record<string, unknown>>;
56
+ userData?: Record<string, unknown>;
57
+ }
58
+
59
+ /**
60
+ * Hand the cart to procurement and say which of the two it became.
61
+ *
62
+ * The line shape is the app's own: what the orders app takes as an item, this
63
+ * one takes with the same field names, so nothing is translated twice.
64
+ */
65
+ export async function submitThroughProcurement(payload: OrderLikePayload): Promise<SubmitOutcome> {
66
+ const answer = await useRevenexxSdk().call<{
67
+ outcome?: string;
68
+ order?: SubmittedOrder;
69
+ purchase_request?: SubmittedRequest;
70
+ }>("POST", "/v1/procurement/submit", {
71
+ body: {
72
+ ...(payload.cartId ? { cart_id: payload.cartId } : {}),
73
+ ...(payload.contactId ? { contact_id: payload.contactId } : {}),
74
+ ...(payload.organizationId ? { organization_id: payload.organizationId } : {}),
75
+ currency: payload.currency,
76
+ ...(payload.customerOrderNumber ? { customer_order_number: payload.customerOrderNumber } : {}),
77
+ ...(payload.buyer ? { buyer: payload.buyer } : {}),
78
+ ...(payload.billingAddress ? { billing_address: payload.billingAddress } : {}),
79
+ ...(payload.shippingAddress ? { shipping_address: payload.shippingAddress } : {}),
80
+ ...(payload.payment ? { payment: payload.payment } : {}),
81
+ ...(payload.shipping ? { shipping: payload.shipping } : {}),
82
+ ...(payload.userData ? { user_data: payload.userData } : {}),
83
+ items: payload.items,
84
+ },
85
+ });
86
+
87
+ if (answer.outcome === "approval" && answer.purchase_request) {
88
+ return { kind: "request", request: answer.purchase_request };
89
+ }
90
+ if (answer.order) {
91
+ return { kind: "order", order: answer.order };
92
+ }
93
+
94
+ // Neither of the two: the app answered something this shop cannot act on,
95
+ // and acting on a guess would mean either charging for nothing or losing a
96
+ // purchase the buyer believes they made.
97
+ throw new Error(`procurement/submit answered no order and no request (outcome: ${answer.outcome ?? "none"})`);
98
+ }