@revenexx/cover 0.2.42 → 0.2.44

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.2.42",
3
+ "version": "0.2.44",
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",
@@ -12,7 +12,7 @@ export default defineEventHandler(async (event) => {
12
12
  }
13
13
 
14
14
  try {
15
- return await getCheckoutProfileService().getProfile(event, user?.$id ?? null);
15
+ return await getCheckoutProfileService().getProfile(event, user ?? null);
16
16
  }
17
17
  catch (err) {
18
18
  getLogService().error("Service error: checkout/profile", toErrorContext(err));
@@ -28,7 +28,7 @@ export default defineEventHandler(async (event) => {
28
28
 
29
29
  if (resolvePaymentServiceKey(event) !== "api") {
30
30
  const user = await getAuthService(event).me(event);
31
- const profile = await getCheckoutProfileService().getProfile(event, user?.$id ?? null);
31
+ const profile = await getCheckoutProfileService().getProfile(event, user ?? null);
32
32
  return {
33
33
  managed: false,
34
34
  methods: profile.availablePaymentMethods,
@@ -32,7 +32,7 @@ export default defineEventHandler(async (event): Promise<QuoteRequestResult> =>
32
32
  throw createError({ statusCode: 401, message: "Sign in to ask for a quote" });
33
33
  }
34
34
 
35
- const profile = await getCheckoutProfileService().getProfile(event, actor.user.$id);
35
+ const profile = await getCheckoutProfileService().getProfile(event, actor.user);
36
36
  const shippingAddress = profile.addresses.find(a => a.id === body.shippingAddressId);
37
37
  const billingAddress = profile.addresses.find(a => a.id === body.billingAddressId);
38
38
  if (!shippingAddress || !billingAddress) {
@@ -37,7 +37,7 @@ export default defineEventHandler(async (event) => {
37
37
 
38
38
  if (resolveShippingServiceKey(event) !== "api") {
39
39
  const user = await getAuthService(event).me(event);
40
- const profile = await getCheckoutProfileService().getProfile(event, user?.$id ?? null);
40
+ const profile = await getCheckoutProfileService().getProfile(event, user ?? null);
41
41
  return {
42
42
  managed: false,
43
43
  methods: profile.availableDeliveryMethods,
@@ -1,5 +1,6 @@
1
1
  import type { H3Event } from "h3";
2
2
 
3
+ import type { AuthUser } from "../../app/interfaces/auth";
3
4
  import type { CheckoutProfile } from "../../app/interfaces/checkout";
4
5
 
5
6
  export interface ICheckoutProfileService {
@@ -7,6 +8,10 @@ export interface ICheckoutProfileService {
7
8
  * The signed-in user's checkout profile. Addresses come from the
8
9
  * address book service (mock or live per `addressService`); the event
9
10
  * carries the session and the per-request service-mode resolution.
11
+ *
12
+ * The buyer themselves is passed, not just their id: an address book fed
13
+ * by an ERP carries no recipient name, and an order without one is
14
+ * refused — the person placing it is the answer.
10
15
  */
11
- getProfile(event: H3Event, userId: string | null): Promise<CheckoutProfile>;
16
+ getProfile(event: H3Event, user: AuthUser | null): Promise<CheckoutProfile>;
12
17
  }
@@ -31,14 +31,18 @@ function splitStreet(street: string): { street: string; streetNumber: string } {
31
31
  : { street: street.trim(), streetNumber: "" };
32
32
  }
33
33
 
34
- function toAccountAddress(row: ApiAddressRow): AccountAddress {
34
+ function toAccountAddress(row: ApiAddressRow, companyFallback = ""): AccountAddress {
35
35
  const name = (row.name ?? "").trim();
36
36
  const [firstName, ...rest] = name.split(/\s+/).filter(Boolean);
37
37
  const streetParts = splitStreet(row.street);
38
+ // An address the ERP hung on the company carries no company line of its
39
+ // own — the file has no such column. The company it belongs to is the
40
+ // answer, and an order will not go through without one.
41
+ const company = row.company || (row.organization_id ? companyFallback : "") || "";
38
42
  return {
39
43
  id: row.id,
40
- company: row.company ?? "",
41
- label: row.company || name || streetParts.street,
44
+ company,
45
+ label: company || name || streetParts.street,
42
46
  firstName: firstName ?? "",
43
47
  lastName: rest.join(" "),
44
48
  street: streetParts.street,
@@ -52,15 +56,37 @@ function toAccountAddress(row: ApiAddressRow): AccountAddress {
52
56
  }
53
57
 
54
58
  /**
55
- * Live address book: the signed-in contact's addresses in the customers
56
- * app. The contact comes from the session cookie; guests have no address
57
- * book. The customers schema requires ISO 3166-1 alpha-2 countries.
59
+ * Live address book: the signed-in buyer's addresses in the customers app —
60
+ * their own AND their company's, because in B2B the delivery addresses belong
61
+ * to the company. Both references come from the session cookie; guests have no
62
+ * address book. The customers schema requires ISO 3166-1 alpha-2 countries.
58
63
  */
59
64
  export class ApiAddressBookService implements IAddressBookService {
60
65
  private contactRef(event: H3Event): { contact_id?: string; organization_id?: string } {
61
66
  return sessionOrderRefs(event);
62
67
  }
63
68
 
69
+ /**
70
+ * The company's name, for addresses that carry none. Best effort: a
71
+ * missing name is a worse label, while a failure here would be a checkout
72
+ * that cannot list an address at all.
73
+ */
74
+ private async companyName(organizationId?: string): Promise<string> {
75
+ if (!organizationId) {
76
+ return "";
77
+ }
78
+ try {
79
+ const org = await useRevenexxSdk().customers.customersOrganizationsGet({
80
+ id: organizationId,
81
+ }) as unknown as { name?: string };
82
+ return org?.name ?? "";
83
+ }
84
+ catch (err) {
85
+ getLogService().error("API request failed: addresses/company-name", apiErrorContext(err));
86
+ return "";
87
+ }
88
+ }
89
+
64
90
  private requireContact(event: H3Event): string {
65
91
  const { contact_id } = this.contactRef(event);
66
92
  if (!contact_id) {
@@ -69,19 +95,52 @@ export class ApiAddressBookService implements IAddressBookService {
69
95
  return contact_id;
70
96
  }
71
97
 
98
+ /**
99
+ * The addresses this buyer may ship and bill to: their own, plus their
100
+ * company's.
101
+ *
102
+ * The company half is not a nicety. In B2B the delivery addresses are the
103
+ * COMPANY's — that is how they arrive from an ERP, hung on the
104
+ * organisation with no person attached — and a buyer read by contact alone
105
+ * then signs in to "no address available" while the merchant is looking at
106
+ * four of them in the Cockpit. Nobody can order, and nothing in the shop
107
+ * explains why.
108
+ *
109
+ * Two calls rather than one because the list filter is an AND: there is no
110
+ * way to ask for "mine or my company's" in a single query. They are
111
+ * merged by id, personal first, so a buyer's own entry stays the one a
112
+ * checkout adopts when neither is marked default.
113
+ */
72
114
  async list(event: H3Event): Promise<AccountAddress[]> {
73
- const { contact_id } = this.contactRef(event);
74
- if (!contact_id) {
115
+ const { contact_id, organization_id } = this.contactRef(event);
116
+ if (!contact_id && !organization_id) {
75
117
  return [];
76
118
  }
77
119
  try {
78
120
  // Typed list filter (customers.addresses.list declares its query
79
121
  // params — @revenexx/sdk >= 0.0.11).
80
- const { items } = await useRevenexxSdk().customers.customersAddressesList({
81
- contactId: contact_id,
82
- limit: 200,
83
- }) as unknown as { items: ApiAddressRow[] };
84
- return items.map(toAccountAddress);
122
+ const sdk = useRevenexxSdk();
123
+ const [own, company] = await Promise.all([
124
+ contact_id
125
+ ? sdk.customers.customersAddressesList({ contactId: contact_id, limit: 200 }) as unknown as Promise<{ items: ApiAddressRow[] }>
126
+ : Promise.resolve({ items: [] as ApiAddressRow[] }),
127
+ organization_id
128
+ ? sdk.customers.customersAddressesList({ organizationId: organization_id, limit: 200 }) as unknown as Promise<{ items: ApiAddressRow[] }>
129
+ : Promise.resolve({ items: [] as ApiAddressRow[] }),
130
+ ]);
131
+ const seen = new Set<string>();
132
+ const rows = [...(own.items ?? []), ...(company.items ?? [])]
133
+ .filter((row) => {
134
+ if (seen.has(row.id)) {
135
+ return false;
136
+ }
137
+ seen.add(row.id);
138
+ return true;
139
+ });
140
+ const companyName = rows.some(row => !row.company && row.organization_id)
141
+ ? await this.companyName(organization_id)
142
+ : "";
143
+ return rows.map(row => toAccountAddress(row, companyName));
85
144
  }
86
145
  catch (err) {
87
146
  getLogService().error("API request failed: addresses/list", apiErrorContext(err));
@@ -2,6 +2,7 @@ import type { H3Event } from "h3";
2
2
 
3
3
  import type { AccountAddress } from "../../app/interfaces/account/address-list";
4
4
  import type { Address } from "../../app/interfaces/address";
5
+ import type { AuthUser } from "../../app/interfaces/auth";
5
6
  import type { CheckoutProfile } from "../../app/interfaces/checkout";
6
7
 
7
8
  import type { ICheckoutProfileService } from "../interfaces/checkoutProfile";
@@ -88,16 +89,19 @@ function methodsFor(event: H3Event, mock: MethodBlocks): MethodBlocks {
88
89
  }
89
90
 
90
91
  export class LocalFileCheckoutProfileService implements ICheckoutProfileService {
91
- async getProfile(event: H3Event, userId: string | null): Promise<CheckoutProfile> {
92
- if (!userId) {
92
+ async getProfile(event: H3Event, user: AuthUser | null): Promise<CheckoutProfile> {
93
+ if (!user) {
93
94
  return { ...GUEST_PROFILE, ...methodsFor(event, GUEST_PROFILE) };
94
95
  }
95
96
  try {
96
97
  const entries = await getAddressBookService(event).list(event);
98
+ // A company address out of an ERP names no recipient — the file has
99
+ // no such column — and a business order is refused without one. The
100
+ // person placing the order is who it goes to.
97
101
  const addresses: Address[] = entries.map(entry => ({
98
102
  id: entry.id,
99
103
  companyName: entry.company || `${entry.firstName} ${entry.lastName}`.trim(),
100
- contactName: `${entry.firstName} ${entry.lastName}`.trim(),
104
+ contactName: `${entry.firstName} ${entry.lastName}`.trim() || user.name,
101
105
  street: `${entry.street} ${entry.streetNumber}`.trim(),
102
106
  city: entry.city,
103
107
  postalCode: entry.zip,