@revenexx/cover 0.12.0 → 0.13.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/app/components/cart/recommend/CartCrossSell.vue +2 -1
- package/app/components/listing/ListingFilterBar.vue +1 -0
- package/app/components/listing/ListingFilterDrawer.vue +1 -0
- package/app/components/listing/ListingFilterRail.vue +1 -0
- package/app/components/search/filter/SearchFacets.vue +10 -3
- package/app/components/search/filter/SearchFacetsBar.vue +7 -3
- package/app/components/search/filter/SearchFacetsDrawer.vue +7 -2
- package/app/components/ui/forms/SearchInput.vue +4 -1
- package/app/composables/useFacetMeta.ts +6 -6
- package/app/composables/useProductListing.ts +18 -2
- package/app/composables/useSearchListing.ts +16 -0
- package/app/shared/locale.ts +114 -0
- package/nuxt.config.ts +22 -0
- package/package.json +1 -1
- package/server/api/account/quotes/[id]/order.post.ts +14 -3
- package/server/api/cart/calculate.post.ts +4 -0
- package/server/api/categories/[slug]/subcategories.get.ts +34 -31
- package/server/api/orders/index.post.ts +78 -31
- package/server/api/payment/psp-config.get.ts +11 -21
- package/server/api/quotes/request.post.ts +2 -2
- package/server/api/typesense/search.post.ts +61 -317
- package/server/api/typesense/suggest.post.ts +14 -9
- package/server/{api/typesense/drop.get.ts → dev/typesense/drop.ts} +2 -1
- package/server/{api/typesense/health.get.ts → dev/typesense/health.ts} +1 -0
- package/server/dev/typesense/seed.ts +59 -0
- package/server/dev/typesense/seedDocuments.ts +105 -0
- package/server/routes/punchout/start.get.ts +40 -3
- package/server/services/ApiBrandService.ts +28 -42
- package/server/services/ApiProductService.ts +10 -11
- package/server/utils/buyerIdentity.ts +11 -0
- package/server/utils/liveCatalog.ts +132 -49
- package/server/utils/liveListingSearch.ts +201 -0
- package/server/utils/livePrices.ts +24 -9
- package/server/utils/orderLinePrices.ts +72 -0
- package/server/utils/paymentFee.ts +59 -0
- package/server/utils/pspConfig.ts +42 -0
- package/server/utils/revenexxSdk.ts +25 -2
- package/server/utils/searchClient.ts +305 -0
- package/server/utils/searchMultiSearchFallback.ts +216 -0
- package/server/api/typesense/seed.get.ts +0 -111
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { H3Event } from "h3";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
defineEventHandler,
|
|
4
|
+
getQuery,
|
|
5
|
+
getRequestHeader,
|
|
6
|
+
sendRedirect,
|
|
7
|
+
setResponseHeader,
|
|
8
|
+
setResponseStatus,
|
|
9
|
+
} from "h3";
|
|
3
10
|
|
|
4
11
|
import type { StoredSession } from "../../../app/interfaces/auth";
|
|
5
12
|
import type { PunchoutClaim, PunchoutVisit } from "../../interfaces/punchout";
|
|
@@ -136,7 +143,7 @@ export default defineEventHandler(async (event) => {
|
|
|
136
143
|
// WHERE the buyer goes and this is about how this storefront spells it.
|
|
137
144
|
const punchoutConfig = (useRuntimeConfig(event).punchout ?? {}) as { localeStrategy?: string; defaultLocale?: string };
|
|
138
145
|
const path = punchoutLocalePath(
|
|
139
|
-
await landingPath(claim),
|
|
146
|
+
await landingPath(event, claim),
|
|
140
147
|
resolveLocale(event),
|
|
141
148
|
String(punchoutConfig.localeStrategy ?? "no_prefix"),
|
|
142
149
|
String(punchoutConfig.defaultLocale ?? ""),
|
|
@@ -152,7 +159,7 @@ export default defineEventHandler(async (event) => {
|
|
|
152
159
|
* article needs the catalogue, because an external system names an article
|
|
153
160
|
* NUMBER and the shop's product pages are addressed by id and slug.
|
|
154
161
|
*/
|
|
155
|
-
async function landingPath(claim: PunchoutClaim): Promise<string> {
|
|
162
|
+
async function landingPath(event: H3Event, claim: PunchoutClaim): Promise<string> {
|
|
156
163
|
const target = punchoutLandingTarget(claim.entry_action, claim.entry_intent, claim.cart_id);
|
|
157
164
|
const direct = punchoutLandingPath(target);
|
|
158
165
|
if (direct) {
|
|
@@ -165,6 +172,7 @@ async function landingPath(claim: PunchoutClaim): Promise<string> {
|
|
|
165
172
|
const found = await $fetch<SearchAnswer>("/api/typesense/search", {
|
|
166
173
|
method: "POST",
|
|
167
174
|
body: { q: supplierPartId, page: 1, perPage: 5 },
|
|
175
|
+
headers: searchRequestHeaders(event),
|
|
168
176
|
});
|
|
169
177
|
return punchoutArticlePath(found?.products ?? [], supplierPartId);
|
|
170
178
|
}
|
|
@@ -176,6 +184,35 @@ async function landingPath(claim: PunchoutClaim): Promise<string> {
|
|
|
176
184
|
}
|
|
177
185
|
}
|
|
178
186
|
|
|
187
|
+
/**
|
|
188
|
+
* What the internal search call needs to see the same shop as this request.
|
|
189
|
+
*
|
|
190
|
+
* An internal `$fetch` starts a fresh request with none of the caller's
|
|
191
|
+
* headers, so the search ran without the brokered tenant context, without
|
|
192
|
+
* the market and service-mode cookies, and in whatever locale it could guess:
|
|
193
|
+
* an article number could resolve in the wrong catalogue or not at all.
|
|
194
|
+
*/
|
|
195
|
+
const FORWARDED_SEARCH_HEADERS = [
|
|
196
|
+
"cookie",
|
|
197
|
+
"accept-language",
|
|
198
|
+
"x-revenexx-tenant",
|
|
199
|
+
"x-revenexx-context",
|
|
200
|
+
"x-revenexx-market",
|
|
201
|
+
"x-revenexx-channel",
|
|
202
|
+
] as const;
|
|
203
|
+
|
|
204
|
+
function searchRequestHeaders(event: H3Event): Record<string, string> {
|
|
205
|
+
const headers: Record<string, string> = {};
|
|
206
|
+
for (const name of FORWARDED_SEARCH_HEADERS) {
|
|
207
|
+
const value = getRequestHeader(event, name);
|
|
208
|
+
if (value) {
|
|
209
|
+
headers[name] = value;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
headers["x-locale"] = resolveLocale(event);
|
|
213
|
+
return headers;
|
|
214
|
+
}
|
|
215
|
+
|
|
179
216
|
/** Why a visit could not be opened — one page per cause, in plain words. */
|
|
180
217
|
type RefusalCause = "missing" | "unusable" | "unavailable";
|
|
181
218
|
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import type { ShopBrand } from "../../app/interfaces/brand";
|
|
2
2
|
import type { IBrandService } from "../interfaces/brand";
|
|
3
|
+
import type { SearchClientResult } from "../utils/searchClient";
|
|
3
4
|
import type { Locale } from "../utils/locale";
|
|
4
5
|
|
|
5
6
|
const BRAND_CACHE_TTL_MS = 5 * 60 * 1000;
|
|
6
7
|
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
-
* would silently cap the grid at 10 brands. Mirrors search.post.ts.
|
|
10
|
-
*/
|
|
11
|
-
const LIVE_MAX_FACET_VALUES = 250;
|
|
8
|
+
/** Logo lookups in flight at once — one search per brand. */
|
|
9
|
+
const LOGO_LOOKUP_CONCURRENCY = 8;
|
|
12
10
|
|
|
13
11
|
// Partitioned by tenant — a multi-tenant theme must never serve one tenant's
|
|
14
12
|
// brands to another (see revenexxTenantKey()). Locale and limit are part of the
|
|
@@ -16,18 +14,16 @@ const LIVE_MAX_FACET_VALUES = 250;
|
|
|
16
14
|
const cache = new Map<string, { brands: ShopBrand[]; loadedAt: number }>();
|
|
17
15
|
|
|
18
16
|
/**
|
|
19
|
-
* Live brands from the platform's Typesense product index
|
|
17
|
+
* Live brands from the platform's Typesense product index, through the
|
|
18
|
+
* search service's `brand` view (searchClient.ts):
|
|
19
|
+
* 1. the view's `manufacturer` facet over the whole catalogue → names +
|
|
20
|
+
* article counts, already ranked by count descending.
|
|
21
|
+
* 2. one 1-hit search per top brand → the logo, which lives on the product
|
|
22
|
+
* document (`attrs.<logoAttribute>`), not on any brand entity.
|
|
20
23
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* 2. ONE multi_search with a sub-search per top brand → the logo, which lives
|
|
25
|
-
* on the product document (`attrs.<logoAttribute>`), not on any brand
|
|
26
|
-
* entity. `include_fields` cannot narrow this: asking for `attrs` returns
|
|
27
|
-
* an empty object, so the full document has to come back.
|
|
28
|
-
*
|
|
29
|
-
* Both carry the same pinned filters the storefront's search uses — without
|
|
30
|
-
* them the counts drift from what the search page reports for the same brand.
|
|
24
|
+
* The service pins locale and enabled products on both, the same pins the
|
|
25
|
+
* storefront's search gets — so the counts match what the search page reports
|
|
26
|
+
* for the same brand.
|
|
31
27
|
*/
|
|
32
28
|
export class ApiBrandService implements IBrandService {
|
|
33
29
|
async listBrands(limit: number, locale: Locale): Promise<ShopBrand[]> {
|
|
@@ -48,19 +44,22 @@ export class ApiBrandService implements IBrandService {
|
|
|
48
44
|
// Same attribute the product detail reads its logo from (liveCatalog).
|
|
49
45
|
const attribute = manufacturerLogoAttribute();
|
|
50
46
|
|
|
51
|
-
// One representative product per brand
|
|
52
|
-
//
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
47
|
+
// One representative product per brand. Settled one by one: a brand
|
|
48
|
+
// whose lookup fails costs that brand its logo and nothing more.
|
|
49
|
+
const logoResults: (PromiseSettledResult<SearchClientResult> | undefined)[] = [];
|
|
50
|
+
for (let index = 0; index < top.length; index += LOGO_LOOKUP_CONCURRENCY) {
|
|
51
|
+
const batch = top.slice(index, index + LOGO_LOOKUP_CONCURRENCY);
|
|
52
|
+
logoResults.push(...await Promise.allSettled(batch.map(entry => searchClient.search({
|
|
53
|
+
view: "brand",
|
|
54
|
+
filters: { manufacturer: [entry.value] },
|
|
55
|
+
perPage: 1,
|
|
56
|
+
locale,
|
|
57
|
+
}))));
|
|
58
|
+
}
|
|
61
59
|
|
|
62
60
|
const brands = top.map((entry, index): ShopBrand => {
|
|
63
|
-
const
|
|
61
|
+
const settled = logoResults[index];
|
|
62
|
+
const raw = settled?.status === "fulfilled" ? settled.value.docs[0]?.attrs?.[attribute] : undefined;
|
|
64
63
|
const logo = typeof raw === "string" && raw.trim().length > 0
|
|
65
64
|
? liveImageSrc(raw.trim())
|
|
66
65
|
: null;
|
|
@@ -90,21 +89,8 @@ export class ApiBrandService implements IBrandService {
|
|
|
90
89
|
|
|
91
90
|
/** Round trip 1 of listBrands: names + counts, count-descending. */
|
|
92
91
|
private async facetManufacturers(locale: Locale): Promise<{ value: string; count: number }[]> {
|
|
93
|
-
const
|
|
94
|
-
q: "*",
|
|
95
|
-
queryBy: "name",
|
|
96
|
-
filterBy: this.pinnedFilter(locale),
|
|
97
|
-
facetBy: "manufacturer",
|
|
98
|
-
maxFacetValues: LIVE_MAX_FACET_VALUES,
|
|
99
|
-
perPage: 0,
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
return (facetResult.facet_counts ?? [])
|
|
103
|
-
.find(facet => facet.field_name === "manufacturer")
|
|
104
|
-
?.counts ?? [];
|
|
105
|
-
}
|
|
92
|
+
const result = await searchClient.search({ view: "brand", locale });
|
|
106
93
|
|
|
107
|
-
|
|
108
|
-
return `locale:=${tsFilterValue(locale)} && enabled:=true`;
|
|
94
|
+
return result.facets.find(facet => facet.field === "manufacturer")?.counts ?? [];
|
|
109
95
|
}
|
|
110
96
|
}
|
|
@@ -11,32 +11,31 @@ const LIST_PAGE_SIZE = 48;
|
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Live product reads via the public revenexx API: the products app's real
|
|
14
|
-
* Typesense collection, scoped per locale (document id = `{entity_id}:{locale}
|
|
14
|
+
* Typesense collection, scoped per locale (document id = `{entity_id}:{locale}`,
|
|
15
|
+
* the locale a full code like `de-DE` or, before the migration, a bare `de`).
|
|
16
|
+
* Listings go through the search service's `listing` view (searchClient.ts);
|
|
17
|
+
* single products are looked up by document id (getLiveProductDoc).
|
|
15
18
|
* Prices, stocks and images are not in the index yet — the mapping serves
|
|
16
19
|
* neutral defaults (see liveCatalog.ts).
|
|
17
20
|
*/
|
|
18
21
|
export class ApiProductService implements IProductService {
|
|
19
22
|
async listProducts(filter?: ProductListFilter): Promise<Product[]> {
|
|
20
23
|
const locale: Locale = filter?.locale ?? "de";
|
|
21
|
-
const filterParts = [`locale:=${tsFilterValue(locale)}`, "enabled:=true"];
|
|
22
24
|
|
|
23
25
|
// Category facets carry the category CODE — our category slugs.
|
|
24
26
|
// Products are indexed with their LEAF category only, so a branch
|
|
25
27
|
// filters across its whole subtree, however deep the tree runs.
|
|
26
28
|
const branch = filter?.subcategory ?? filter?.category;
|
|
27
|
-
|
|
28
|
-
const codes = await liveCategorySubtreeCodes([branch]);
|
|
29
|
-
filterParts.push(`categories:=[${codes.map(tsFilterValue).join(",")}]`);
|
|
30
|
-
}
|
|
29
|
+
const categories = branch ? await liveCategorySubtreeCodes([branch]) : [];
|
|
31
30
|
|
|
32
|
-
const result = await
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
filterBy: filterParts.join(" && "),
|
|
31
|
+
const result = await searchClient.search({
|
|
32
|
+
view: "listing",
|
|
33
|
+
...(categories.length ? { filters: { categories } } : {}),
|
|
36
34
|
perPage: LIST_PAGE_SIZE,
|
|
35
|
+
locale,
|
|
37
36
|
});
|
|
38
37
|
|
|
39
|
-
return
|
|
38
|
+
return result.docs.map(doc => mapLiveDocToProduct(doc));
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
async getProductById(id: string, locale: Locale = "de"): Promise<Product | null> {
|
|
@@ -88,6 +88,17 @@ export function buyerPrincipal(event: H3Event | undefined): string {
|
|
|
88
88
|
return typeof value === "string" ? value : "";
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* The contact the request acts for, or undefined. A body that names a contact
|
|
93
|
+
* to an app enforcing the principal (payments 1.0) has to name exactly this
|
|
94
|
+
* one, or is refused as `buyer_mismatch` — so it is read from the principal,
|
|
95
|
+
* not from the session cookie.
|
|
96
|
+
*/
|
|
97
|
+
export function principalContactId(event: H3Event | undefined): string | undefined {
|
|
98
|
+
const principal = buyerPrincipal(event);
|
|
99
|
+
return principal.startsWith("contact:") ? principal.slice("contact:".length) || undefined : undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
91
102
|
/**
|
|
92
103
|
* Least → most privileged, as the customers app orders its built-ins. A role
|
|
93
104
|
* the shop does not know ranks below all of them, so it can grant nothing.
|
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
ProductVariantAxis,
|
|
14
14
|
ProductVariants,
|
|
15
15
|
} from "../../app/interfaces/product-variants";
|
|
16
|
+
import { pickLocalizedLabel, searchLocaleKeys } from "../../app/shared/locale";
|
|
16
17
|
import type { Locale } from "./locale";
|
|
17
18
|
|
|
18
19
|
/**
|
|
@@ -164,7 +165,13 @@ const LIVE_DEFAULT_TAX_RATE = 19;
|
|
|
164
165
|
|
|
165
166
|
|
|
166
167
|
/**
|
|
167
|
-
* Several product searches in ONE request.
|
|
168
|
+
* Several product searches in ONE request, over the raw multi_search route.
|
|
169
|
+
*
|
|
170
|
+
* Searches go through searchClient.ts (the view-based query endpoint) now.
|
|
171
|
+
* What remains here: the deprecated fallback for gateways without that route
|
|
172
|
+
* (searchMultiSearchFallback.ts), and the document lookups by id below
|
|
173
|
+
* (getLiveProductDoc, association targets) — fetching known documents is a
|
|
174
|
+
* different contract from a search and is not part of the query endpoint.
|
|
168
175
|
*
|
|
169
176
|
* Raw call: the gateway 404s the POST single-search route its spec declares and
|
|
170
177
|
* the GET contract does not declare facet_by, so neither the generated search
|
|
@@ -230,6 +237,69 @@ export function tsFilterValue(value: string): string {
|
|
|
230
237
|
return "`" + value.replace(/`/g, "") + "`";
|
|
231
238
|
}
|
|
232
239
|
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
// Locale scoping — one document per product AND locale (`{entity_id}:{locale}`).
|
|
242
|
+
// The index is moving from bare languages (`de`) to full codes (`de-DE`); every
|
|
243
|
+
// live query accepts both and prefers the full code (app/shared/locale.ts), so
|
|
244
|
+
// the storefront keeps working before, during and after that migration.
|
|
245
|
+
// ---------------------------------------------------------------------------
|
|
246
|
+
|
|
247
|
+
/** `locale:=[`de-DE`,`de`]` — the documents of one storefront locale. */
|
|
248
|
+
export function liveLocaleClause(locale: string): string {
|
|
249
|
+
return `locale:=[${searchLocaleKeys(locale).map(tsFilterValue).join(",")}]`;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The clauses every live catalogue query pins: the request's locale and
|
|
254
|
+
* enabled products only. Kept as separate parts so a caller can drop one the
|
|
255
|
+
* collection turns out not to declare without losing the other.
|
|
256
|
+
*
|
|
257
|
+
* Searches no longer build these: the query endpoint pins locale and enabled
|
|
258
|
+
* on the server (searchClient.ts). They remain for the deprecated multi_search
|
|
259
|
+
* fallback and the document lookups by id below.
|
|
260
|
+
*/
|
|
261
|
+
export function liveCatalogFilterParts(locale: string): string[] {
|
|
262
|
+
return [liveLocaleClause(locale), "enabled:=true"];
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The pinned clauses plus any extra ones, joined into one `filter_by`. */
|
|
266
|
+
export function liveCatalogFilter(locale: string, ...extra: string[]): string {
|
|
267
|
+
return [...liveCatalogFilterParts(locale), ...extra].join(" && ");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** `id:=[…]` over every document id these products may carry in this locale. */
|
|
271
|
+
export function liveDocIdClause(entityIds: readonly string[], locale: string): string {
|
|
272
|
+
const keys = searchLocaleKeys(locale);
|
|
273
|
+
const ids = entityIds.flatMap(id => keys.map(key => tsFilterValue(`${id}:${key}`)));
|
|
274
|
+
return `id:=[${ids.join(",")}]`;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* One document per product, the best-matching locale winning, in the order the
|
|
279
|
+
* products first appear. While both `{id}:de` and `{id}:de-DE` exist, a query
|
|
280
|
+
* over both keys returns the product twice; this keeps the full-code one.
|
|
281
|
+
*/
|
|
282
|
+
export function preferLocaleDocs<T extends Pick<LiveProductDoc, "id" | "entity_id" | "locale">>(
|
|
283
|
+
docs: readonly T[],
|
|
284
|
+
locale: string,
|
|
285
|
+
): T[] {
|
|
286
|
+
const keys = searchLocaleKeys(locale);
|
|
287
|
+
const rank = (doc: T): number => {
|
|
288
|
+
const docLocale = doc.locale || doc.id.slice(doc.id.lastIndexOf(":") + 1);
|
|
289
|
+
const index = keys.indexOf(docLocale);
|
|
290
|
+
return index === -1 ? keys.length : index;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const best = new Map<string, T>();
|
|
294
|
+
for (const doc of docs) {
|
|
295
|
+
const current = best.get(doc.entity_id);
|
|
296
|
+
if (!current || rank(doc) < rank(current)) {
|
|
297
|
+
best.set(doc.entity_id, doc);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return [...best.values()];
|
|
301
|
+
}
|
|
302
|
+
|
|
233
303
|
export function mapLiveDocToProduct(doc: LiveProductDoc): Product {
|
|
234
304
|
const categorySlug = doc.categories?.[0] ?? "";
|
|
235
305
|
const category = doc.category_labels?.[0] ?? categorySlug;
|
|
@@ -307,15 +377,20 @@ export async function mapLiveDocToDetail(doc: LiveProductDoc, locale: Locale = "
|
|
|
307
377
|
* Uses a filtered search instead of the document-GET route — the search
|
|
308
378
|
* proxy's document endpoint currently rejects gateway-trust calls
|
|
309
379
|
* (responds 401 "x-typesense-api-key"), and `id` is filterable anyway.
|
|
380
|
+
*
|
|
381
|
+
* Stays on multi_search rather than the view-based query endpoint: a lookup
|
|
382
|
+
* by document id is not a search, and the query endpoint has no contract for
|
|
383
|
+
* it (it takes no `id` filter and folds locale variants itself).
|
|
310
384
|
*/
|
|
311
385
|
export async function getLiveProductDoc(entityId: string, locale: Locale): Promise<LiveProductDoc | null> {
|
|
312
386
|
const result = await searchLiveProducts({
|
|
313
387
|
q: "*",
|
|
314
388
|
queryBy: "name",
|
|
315
|
-
filterBy:
|
|
316
|
-
perPage:
|
|
389
|
+
filterBy: liveDocIdClause([entityId], locale),
|
|
390
|
+
perPage: searchLocaleKeys(locale).length,
|
|
317
391
|
});
|
|
318
|
-
|
|
392
|
+
const docs = (result.hits ?? []).map(hit => hit.document);
|
|
393
|
+
return preferLocaleDocs(docs, locale)[0] ?? null;
|
|
319
394
|
}
|
|
320
395
|
|
|
321
396
|
// ---------------------------------------------------------------------------
|
|
@@ -325,51 +400,22 @@ export async function getLiveProductDoc(entityId: string, locale: Locale): Promi
|
|
|
325
400
|
// ---------------------------------------------------------------------------
|
|
326
401
|
|
|
327
402
|
/**
|
|
328
|
-
*
|
|
403
|
+
* Localised label with the products app's fallback chain: requested locale,
|
|
404
|
+
* then German, then English, then the technical code.
|
|
329
405
|
*
|
|
330
406
|
* Tenants do not agree on the key form: one PIM writes `de` / `en`, another
|
|
331
407
|
* writes BCP-47 tags (`de-DE`, `en-GB`) for the same two languages. An
|
|
332
408
|
* exact-key lookup silently missed the second kind and EVERY label degraded to
|
|
333
409
|
* its technical code — a whole navigation reading `1`, `10837`, `10876`, and
|
|
334
|
-
* specification rows reading `schneidbreite_bs`.
|
|
335
|
-
*
|
|
336
|
-
* Exact key first, then the first key whose language subtag matches, taken in
|
|
337
|
-
* sorted key order so a map carrying several regions (`de-AT`, `de-DE`) always
|
|
338
|
-
* answers the same way whatever the insertion order was. An empty string counts
|
|
339
|
-
* as no label — it renders as a blank name, which is worse than the code.
|
|
340
|
-
*/
|
|
341
|
-
function labelForLanguage(labels: Record<string, string>, language: string): string | undefined {
|
|
342
|
-
const exact = labels[language];
|
|
343
|
-
if (exact) {
|
|
344
|
-
return exact;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
const prefix = `${language}-`;
|
|
348
|
-
for (const key of Object.keys(labels).sort()) {
|
|
349
|
-
const normalized = key.toLowerCase().replace(/_/g, "-");
|
|
350
|
-
if ((normalized === language || normalized.startsWith(prefix)) && labels[key]) {
|
|
351
|
-
return labels[key];
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
return undefined;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* Localised label with the products app's fallback chain: requested locale,
|
|
359
|
-
* then German, then English, then the technical code.
|
|
410
|
+
* specification rows reading `schneidbreite_bs`. Full and short codes match in
|
|
411
|
+
* both directions (app/shared/locale.ts, shared with the client's facet labels).
|
|
360
412
|
*/
|
|
361
413
|
export function pickLiveLabel(
|
|
362
414
|
labels: Record<string, string> | null | undefined,
|
|
363
|
-
locale: Locale,
|
|
415
|
+
locale: Locale | string,
|
|
364
416
|
fallback: string,
|
|
365
417
|
): string {
|
|
366
|
-
|
|
367
|
-
return fallback;
|
|
368
|
-
}
|
|
369
|
-
return labelForLanguage(labels, locale)
|
|
370
|
-
?? labelForLanguage(labels, "de")
|
|
371
|
-
?? labelForLanguage(labels, "en")
|
|
372
|
-
?? fallback;
|
|
418
|
+
return pickLocalizedLabel(labels, locale, fallback);
|
|
373
419
|
}
|
|
374
420
|
|
|
375
421
|
// ---------------------------------------------------------------------------
|
|
@@ -596,6 +642,34 @@ export function subcategoryIndex(
|
|
|
596
642
|
return { labels, branchOf };
|
|
597
643
|
}
|
|
598
644
|
|
|
645
|
+
/**
|
|
646
|
+
* The top level of the storefront's category tree as a facet axis: each
|
|
647
|
+
* top-level category's label, and every code beneath it mapped back to it.
|
|
648
|
+
*
|
|
649
|
+
* The search page offers a `category` facet; the live index only facets
|
|
650
|
+
* `categories`, at the leaf. Rolled up through this, leaf counts become
|
|
651
|
+
* counts per top-level category — the same categories the navigation shows,
|
|
652
|
+
* because the same tree builder decides what "top level" means.
|
|
653
|
+
*/
|
|
654
|
+
export async function liveTopCategoryIndex(locale: Locale): Promise<LiveSubcategoryIndex> {
|
|
655
|
+
return topCategoryIndex(await loadLiveCategoryRows(), locale);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/** Pure core of liveTopCategoryIndex(), exported for tests. */
|
|
659
|
+
export function topCategoryIndex(rows: LiveCategoryRow[], locale: Locale): LiveSubcategoryIndex {
|
|
660
|
+
const labels = new Map<string, string>();
|
|
661
|
+
const branchOf = new Map<string, string>();
|
|
662
|
+
for (const top of buildLiveCategoryTree(rows, locale)) {
|
|
663
|
+
labels.set(top.slug, top.label);
|
|
664
|
+
for (const code of subtreeCodes(rows, [top.slug])) {
|
|
665
|
+
if (!branchOf.has(code)) {
|
|
666
|
+
branchOf.set(code, top.slug);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
return { labels, branchOf };
|
|
671
|
+
}
|
|
672
|
+
|
|
599
673
|
/**
|
|
600
674
|
* Roll `categories` facet counts up onto the direct children they belong to.
|
|
601
675
|
*
|
|
@@ -914,8 +988,13 @@ function liveOrderUnitLabel(raw: unknown, locale: Locale): string | null {
|
|
|
914
988
|
}
|
|
915
989
|
|
|
916
990
|
function livePimName(product: LivePimProduct, locale: Locale): string {
|
|
917
|
-
const
|
|
918
|
-
|
|
991
|
+
const names: Record<string, string> = {};
|
|
992
|
+
for (const [key, values] of Object.entries(product.attribute_values?.locale_specific ?? {})) {
|
|
993
|
+
if (values?.name) {
|
|
994
|
+
names[key] = values.name;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
return pickLiveLabel(names, locale, product.sku);
|
|
919
998
|
}
|
|
920
999
|
|
|
921
1000
|
/**
|
|
@@ -1114,7 +1193,10 @@ function isMandatoryAssociation(type: LiveAssociationTypeRow): boolean {
|
|
|
1114
1193
|
return type.is_quantified === true || type.code === "mandatory";
|
|
1115
1194
|
}
|
|
1116
1195
|
|
|
1117
|
-
/**
|
|
1196
|
+
/**
|
|
1197
|
+
* Typesense caps a batch; resolve targets in chunks and keep one round trip per
|
|
1198
|
+
* chunk. 100 targets × two locale keys stays under its 250 hits per page.
|
|
1199
|
+
*/
|
|
1118
1200
|
const TARGET_CHUNK_SIZE = 100;
|
|
1119
1201
|
|
|
1120
1202
|
async function resolveAssociationTargets(
|
|
@@ -1128,19 +1210,20 @@ async function resolveAssociationTargets(
|
|
|
1128
1210
|
|
|
1129
1211
|
// Filtering on `id` (the `{entity_id}:{locale}` document id) rather than
|
|
1130
1212
|
// `entity_id`, which is not a filterable field — same reason
|
|
1131
|
-
// getLiveProductDoc() goes through `id:=`.
|
|
1213
|
+
// getLiveProductDoc() goes through `id:=`. Each target may answer under
|
|
1214
|
+
// the full and the short locale code, hence the page size.
|
|
1215
|
+
const perTarget = searchLocaleKeys(locale).length;
|
|
1132
1216
|
const results = await searchLiveProductsMulti(chunks.map(chunk => ({
|
|
1133
1217
|
q: "*",
|
|
1134
1218
|
queryBy: "name",
|
|
1135
|
-
filterBy:
|
|
1136
|
-
perPage: chunk.length,
|
|
1219
|
+
filterBy: liveDocIdClause(chunk, locale),
|
|
1220
|
+
perPage: chunk.length * perTarget,
|
|
1137
1221
|
})));
|
|
1138
1222
|
|
|
1223
|
+
const docs = results.flatMap(result => (result.hits ?? []).map(hit => hit.document));
|
|
1139
1224
|
const byId = new Map<string, Product>();
|
|
1140
|
-
for (const
|
|
1141
|
-
|
|
1142
|
-
byId.set(hit.document.entity_id, mapLiveDocToProduct(hit.document));
|
|
1143
|
-
}
|
|
1225
|
+
for (const doc of preferLocaleDocs(docs, locale)) {
|
|
1226
|
+
byId.set(doc.entity_id, mapLiveDocToProduct(doc));
|
|
1144
1227
|
}
|
|
1145
1228
|
return byId;
|
|
1146
1229
|
}
|