@revenexx/cover 0.3.2 → 0.4.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.
@@ -6,7 +6,13 @@
6
6
  *
7
7
  * Composed from per-section widget components so the same sections can be
8
8
  * rearranged as individual blocks in the page editor (see cover-theme).
9
+ *
10
+ * The two governance widgets follow the organisation's settings. They used to
11
+ * render for every shop, each with a "view all" link into a route only a shop
12
+ * with governance has — so a storefront without it put two dead links on the
13
+ * first page a buyer sees after signing in (RX-791's sibling).
9
14
  */
15
+ const { settings } = useB2BContext();
10
16
  </script>
11
17
 
12
18
  <template>
@@ -19,10 +25,10 @@
19
25
  <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 items-start">
20
26
  <div class="lg:col-span-2 space-y-6">
21
27
  <AccountDashboardRecentOrders />
22
- <AccountDashboardRequisitions />
28
+ <AccountDashboardRequisitions v-if="settings?.approvals?.enabled" />
23
29
  </div>
24
30
  <div class="space-y-6">
25
- <AccountDashboardBudgets />
31
+ <AccountDashboardBudgets v-if="settings?.costCenters?.enabled" />
26
32
  <AccountDashboardQuickActions />
27
33
  </div>
28
34
  </div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@revenexx/cover",
3
- "version": "0.3.2",
3
+ "version": "0.4.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",
@@ -0,0 +1,33 @@
1
+ import type { H3Event } from "h3";
2
+
3
+ import type { CostCenter } from "../../app/interfaces/b2b";
4
+
5
+ /**
6
+ * Where a buyer's cost centres come from.
7
+ *
8
+ * A cost centre is the account a line is booked against, and which ones a
9
+ * buyer may book to is a fact about the organisation, not about the shop. The
10
+ * demo implementation composes them from the bundled organisation and the
11
+ * records the account area created; the live one reads the cost-centers app.
12
+ *
13
+ * Kept apart from the rest of the B2B context on purpose: the context also
14
+ * carries approval limits and workflows, which no app answers yet. A single
15
+ * "live context" would have to invent those, and inventing is what the
16
+ * registry key exists to avoid — a domain goes live when its app does, and
17
+ * not before.
18
+ */
19
+ export interface ICostCenterService {
20
+ /**
21
+ * The cost centres this buyer may book to, in the order they should be
22
+ * offered. An empty list means the buyer books to none, which is a valid
23
+ * answer and not an error.
24
+ */
25
+ list(event: H3Event | undefined, buyer: CostCenterBuyer): Promise<CostCenter[]>;
26
+ }
27
+
28
+ /** Who is asking — the shop knows the buyer, the app knows their organisation. */
29
+ export interface CostCenterBuyer {
30
+ readonly userId: string | null;
31
+ readonly contactId?: string | null;
32
+ readonly organizationId?: string | null;
33
+ }
@@ -0,0 +1,54 @@
1
+ import type { CostCenter } from "../../app/interfaces/b2b";
2
+ import type { CostCenterBuyer, ICostCenterService } from "../interfaces/costCenters";
3
+
4
+ /** One cost centre as the cost-centers app answers it. */
5
+ interface ApiCostCenterRow {
6
+ id: string;
7
+ code: string;
8
+ name: string;
9
+ organization_id?: string | null;
10
+ active?: boolean;
11
+ }
12
+
13
+ /** How many a buyer's organisation can plausibly keep. */
14
+ const PAGE_LIMIT = 200;
15
+
16
+ /**
17
+ * The cost centres a buyer's organisation keeps, from the cost-centers app.
18
+ *
19
+ * Scoped by organisation, which is how the app itself answers: asked which
20
+ * centres are usable while naming an organisation, it offers that
21
+ * organisation's and no others. A buyer with no organisation gets none rather
22
+ * than the tenant's whole list — a cost centre is somebody's, and showing one
23
+ * company's booking accounts to another is the failure this scoping prevents.
24
+ */
25
+ export class ApiCostCenterService implements ICostCenterService {
26
+ async list(_event: unknown, buyer: CostCenterBuyer): Promise<CostCenter[]> {
27
+ if (!buyer.organizationId) {
28
+ return [];
29
+ }
30
+
31
+ try {
32
+ // The escape hatch, not a typed method: `cost_centers.cost_centers.list`
33
+ // declares limit/offset/order and a punchout account code, but not the
34
+ // organisation the record carries — the app filters on it, the contract
35
+ // does not declare it. It moves to the typed call when it does.
36
+ const { items } = await useRevenexxSdk().call<{ items: ApiCostCenterRow[] }>(
37
+ "GET",
38
+ "/v1/cost-centers/cost-centers",
39
+ { query: { organization_id: buyer.organizationId, limit: PAGE_LIMIT, order: "code.asc" } },
40
+ );
41
+
42
+ return (items ?? [])
43
+ .filter(row => row.active !== false)
44
+ .map(row => ({ id: row.id, code: row.code, label: row.name }));
45
+ }
46
+ catch (err) {
47
+ // A buyer who cannot be told their cost centres must not be told a
48
+ // wrong set either: an unreachable app answers none, and the surfaces
49
+ // that offer a choice fall back to offering none.
50
+ getLogService().error("API request failed: cost-centers/list", apiErrorContext(err));
51
+ return [];
52
+ }
53
+ }
54
+ }
@@ -1,3 +1,4 @@
1
+ import type { H3Event } from "h3";
1
2
  import type { B2BContext, B2BRole, ApprovalLimit, ApprovalWorkflow, CostCenter, OrganizationSettings, Requisition } from "../../app/interfaces/b2b";
2
3
  import type { AuthRole } from "../../app/interfaces/auth";
3
4
 
@@ -27,8 +28,22 @@ const GUEST_ROLE = null;
27
28
  * workflows. An override for the organization settings can be injected per
28
29
  * request (demo shop dev panel) via the constructor.
29
30
  */
30
- export class MockB2BContextService implements IB2BContextService {
31
- constructor(private readonly settingsOverride?: Partial<OrganizationSettings>) {}
31
+ /**
32
+ * The buyer's B2B context: their role, the organisation's settings, the cost
33
+ * centres they may book to, the approval limits that apply and the workflows
34
+ * they can be sent through.
35
+ *
36
+ * Only ONE of those has a live source today. Cost centres come from the
37
+ * `costCenterService` registry key, which is the cost-centers app when a
38
+ * storefront sets it; the rest is the demo organisation, because no app
39
+ * answers approval limits or workflows yet. That split is deliberate and
40
+ * visible here rather than hidden behind a class named for one of the two.
41
+ */
42
+ export class B2BContextService implements IB2BContextService {
43
+ constructor(
44
+ private readonly settingsOverride?: Partial<OrganizationSettings>,
45
+ private readonly event?: H3Event,
46
+ ) {}
32
47
 
33
48
  async getContext(userId: string | null, fallbackRole?: B2BRole | null): Promise<B2BContext> {
34
49
  const org = await readCoverConfigJson<OrganizationConfig>("account/organization.json");
@@ -57,13 +72,17 @@ export class MockB2BContextService implements IB2BContextService {
57
72
  ...overrides.workflows,
58
73
  ];
59
74
 
60
- // Account-created cost centers belong to the whole organization;
61
- // bundled ones follow the persona assignment. Patches overlay both.
75
+ // Whose cost centres these are is the cost-centre source's question, not
76
+ // this one's: the demo composes them from the bundled organisation, the
77
+ // live one reads the buyer's organisation out of the cost-centers app.
78
+ // A shop that does not book to cost centres asks neither.
79
+ const refs = this.event ? sessionOrderRefs(this.event) : undefined;
62
80
  const costCenters = settings.costCenters.enabled
63
- ? [
64
- ...org.costCenters.filter(cc => (persona?.costCenterIds ?? []).includes(cc.id)),
65
- ...overrides.costCenters,
66
- ].map(cc => ({ ...cc, ...overrides.costCenterPatches[cc.id] }))
81
+ ? await getCostCenterService(this.event).list(this.event, {
82
+ userId,
83
+ contactId: refs?.contact_id ?? null,
84
+ organizationId: refs?.organization_id ?? null,
85
+ })
67
86
  : [];
68
87
 
69
88
  // Approval limits only apply when actually ordering — requesters never
@@ -0,0 +1,40 @@
1
+ import type { CostCenter } from "../../app/interfaces/b2b";
2
+ import type { CostCenterBuyer, ICostCenterService } from "../interfaces/costCenters";
3
+
4
+ interface OrganizationConfig {
5
+ costCenters: CostCenter[];
6
+ }
7
+
8
+ interface PersonaConfig {
9
+ id: string;
10
+ costCenterIds?: string[];
11
+ }
12
+
13
+ /**
14
+ * The demo tenant's cost centres: the bundled organisation's, narrowed to the
15
+ * ones the signed-in persona is assigned, plus whatever the account area
16
+ * created, with patches overlaid.
17
+ *
18
+ * This is the composition the B2B context used to carry itself; it moved here
19
+ * when the domain got a registry key, so the demo and the live source answer
20
+ * the same question through the same seam.
21
+ */
22
+ export class MockCostCenterService implements ICostCenterService {
23
+ async list(_event: unknown, buyer: CostCenterBuyer): Promise<CostCenter[]> {
24
+ if (!buyer.userId) {
25
+ return [];
26
+ }
27
+
28
+ const org = await readCoverConfigJson<OrganizationConfig>("account/organization.json");
29
+ const personas = await readCoverConfigJson<PersonaConfig[]>("account/personas.json");
30
+ const persona = personas.find(p => p.id === buyer.userId);
31
+ const overrides = await readGovernanceOverrides();
32
+
33
+ // Account-created cost centres belong to the whole organisation;
34
+ // bundled ones follow the persona assignment. Patches overlay both.
35
+ return [
36
+ ...org.costCenters.filter(cc => (persona?.costCenterIds ?? []).includes(cc.id)),
37
+ ...overrides.costCenters,
38
+ ].map(cc => ({ ...cc, ...overrides.costCenterPatches[cc.id] }));
39
+ }
40
+ }
@@ -4,7 +4,7 @@ import type { OrganizationSettings } from "../../app/interfaces/b2b";
4
4
 
5
5
  import type { IB2BContextService, IRequisitionService } from "../interfaces/b2bContext";
6
6
  import type { ICartCalculationService } from "../interfaces/cartCalculation";
7
- import { MockB2BContextService, MockRequisitionService } from "../services/MockB2BContextService";
7
+ import { B2BContextService, MockRequisitionService } from "../services/MockB2BContextService";
8
8
  import { MockCartCalculationService } from "../services/MockCartCalculationService";
9
9
 
10
10
  /**
@@ -51,7 +51,7 @@ export function resolveOrgSettingsOverride(event?: H3Event): Partial<Organizatio
51
51
  export function getB2BContextService(event?: H3Event): IB2BContextService {
52
52
  // Per-request instantiation: the service carries the request's settings
53
53
  // override; the underlying data reads are cached by Nitro's storage.
54
- return new MockB2BContextService(resolveOrgSettingsOverride(event));
54
+ return new B2BContextService(resolveOrgSettingsOverride(event), event);
55
55
  }
56
56
 
57
57
  const requisitionService: IRequisitionService = new MockRequisitionService();
@@ -0,0 +1,28 @@
1
+ import type { H3Event } from "h3";
2
+
3
+ import type { ICostCenterService } from "../interfaces/costCenters";
4
+ import { ApiCostCenterService } from "../services/ApiCostCenterService";
5
+ import { MockCostCenterService } from "../services/MockCostCenterService";
6
+
7
+ /**
8
+ * Registry of cost-centre sources:
9
+ * - "mock" — the bundled demo organisation and what the account area created
10
+ * - "api" — the cost-centers app through the public revenexx API
11
+ *
12
+ * The rest of the B2B context — approval limits and workflows — stays on the
13
+ * demo data whichever key is set, because no app answers those yet. A domain
14
+ * goes live when its app does.
15
+ */
16
+ const serviceRegistry: Record<string, ICostCenterService> = {
17
+ mock: new MockCostCenterService(),
18
+ api: new ApiCostCenterService(),
19
+ };
20
+
21
+ export function getCostCenterService(event?: H3Event): ICostCenterService {
22
+ const key = resolveServiceKey(event, {
23
+ domain: "costCenterService",
24
+ mockKey: "mock",
25
+ liveKey: "api",
26
+ });
27
+ return serviceRegistry[key] ?? serviceRegistry["mock"]!;
28
+ }