@revenexx/cover 0.14.0 → 0.15.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/layout/header/LocaleSwitcher.vue +79 -12
- package/app/components/ui/forms/SearchInput.vue +205 -8
- package/app/composables/useProductCard.ts +14 -0
- package/app/composables/useProductListing.ts +27 -1
- package/app/composables/useSearchEvents.ts +67 -0
- package/app/interfaces/market.ts +7 -0
- package/app/plugins/search-events.client.ts +41 -0
- package/app/shared/locale.ts +133 -0
- package/app/shared/searchAttribution.ts +91 -0
- package/app/shared/themeEvents.ts +1 -1
- package/i18n/locales/de/search.json +7 -0
- package/i18n/locales/en/search.json +7 -0
- package/package.json +1 -1
- package/server/api/brands.get.ts +3 -2
- package/server/api/categories/[slug]/subcategories.get.ts +9 -6
- package/server/api/products/index.get.ts +6 -1
- package/server/api/search/events.post.ts +65 -0
- package/server/api/typesense/search.post.ts +12 -1
- package/server/api/typesense/suggest.post.ts +43 -22
- package/server/interfaces/brand.ts +3 -2
- package/server/interfaces/product.ts +5 -0
- package/server/services/ApiBrandService.ts +8 -8
- package/server/services/ApiMarketService.ts +9 -2
- package/server/services/ApiProductService.ts +1 -1
- package/server/utils/locale.ts +58 -4
- package/server/utils/searchClient.ts +27 -4
- package/server/utils/searchEvents.ts +46 -0
- package/server/utils/searchMultiSearchFallback.ts +2 -0
- package/server/utils/searchSuggest.ts +207 -0
- package/server/utils/suggestTiles.ts +141 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ShopMarket, ShopMarketCurrency, ShopMarketLocale } from "../../app/interfaces/market";
|
|
2
|
+
import { offeredLocales } from "../../app/shared/locale";
|
|
2
3
|
import type { IMarketService } from "../interfaces/market";
|
|
3
4
|
|
|
4
5
|
interface ApiMarketRow {
|
|
@@ -17,6 +18,8 @@ interface ApiLocaleRow {
|
|
|
17
18
|
country: string;
|
|
18
19
|
is_default: boolean;
|
|
19
20
|
position: number;
|
|
21
|
+
/** `preparing` | `active`; absent on markets builds before PE-866 (= active). */
|
|
22
|
+
status?: string | null;
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
interface ApiCurrencyRow {
|
|
@@ -72,12 +75,16 @@ export class ApiMarketService implements IMarketService {
|
|
|
72
75
|
name: market.name,
|
|
73
76
|
currency: market.currency,
|
|
74
77
|
isDefault: market.is_default,
|
|
75
|
-
|
|
78
|
+
// Default first, a `preparing` locale left out: the shop never
|
|
79
|
+
// offers a language whose catalogue and search are still being
|
|
80
|
+
// built (PE-866).
|
|
81
|
+
locales: offeredLocales(locales.map((locale): ShopMarketLocale => ({
|
|
76
82
|
code: locale.code,
|
|
77
83
|
language: locale.language,
|
|
78
84
|
country: locale.country,
|
|
79
85
|
isDefault: locale.is_default,
|
|
80
|
-
|
|
86
|
+
status: locale.status === "preparing" ? "preparing" : "active",
|
|
87
|
+
}))),
|
|
81
88
|
currencies: currencies.map((currency): ShopMarketCurrency => ({
|
|
82
89
|
code: currency.code,
|
|
83
90
|
isDefault: currency.is_default,
|
|
@@ -32,7 +32,7 @@ export class ApiProductService implements IProductService {
|
|
|
32
32
|
view: "listing",
|
|
33
33
|
...(categories.length ? { filters: { categories } } : {}),
|
|
34
34
|
perPage: LIST_PAGE_SIZE,
|
|
35
|
-
locale,
|
|
35
|
+
locale: filter?.searchLocale ?? locale,
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
return result.docs.map(doc => mapLiveDocToProduct(doc));
|
package/server/utils/locale.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import { getCookie, getHeader, type H3Event } from "h3";
|
|
2
2
|
|
|
3
|
+
import { pickMarketLocale } from "../../app/shared/locale";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Cookie the language choice persists the chosen MARKET locale in (`de-CH`,
|
|
7
|
+
* `fr-FR`). The URL prefix only says which UI strings render; this says
|
|
8
|
+
* which catalogue language the buyer asked for, which differs when the
|
|
9
|
+
* market offers a language the storefront has no UI strings for (PE-866).
|
|
10
|
+
*/
|
|
11
|
+
export const CONTENT_LOCALE_COOKIE_NAME = "cover-content-locale";
|
|
12
|
+
|
|
3
13
|
// Update when adding/removing locale subdirectories in i18n/locales/
|
|
4
14
|
const SUPPORTED = new Set(["de", "en"] as const);
|
|
5
15
|
export type Locale = "de" | "en";
|
|
@@ -34,10 +44,45 @@ export function resolveLocale(event: H3Event): Locale {
|
|
|
34
44
|
return fromAccept;
|
|
35
45
|
}
|
|
36
46
|
|
|
47
|
+
return defaultUiLanguage();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function defaultUiLanguage(): Locale {
|
|
37
51
|
const configDefault = useRuntimeConfig().public.defaultLocale;
|
|
38
52
|
return (SUPPORTED.has(configDefault as Locale) ? configDefault : "de") as Locale;
|
|
39
53
|
}
|
|
40
54
|
|
|
55
|
+
/**
|
|
56
|
+
* The market locale this request reads the catalogue in — what search sends
|
|
57
|
+
* as `X-Revenexx-Locale` and what a contact is stored under.
|
|
58
|
+
*
|
|
59
|
+
* The market is the source of truth for which locales a store offers (its
|
|
60
|
+
* `preparing` locales excluded): the buyer's explicit choice when it fits the
|
|
61
|
+
* page language, else the market's locale for the page language. Without
|
|
62
|
+
* markets, or when they cannot be read, the page language's full code — what
|
|
63
|
+
* the storefront asked before markets carried locales.
|
|
64
|
+
*/
|
|
65
|
+
export async function resolveMarketLocale(event: H3Event): Promise<string> {
|
|
66
|
+
const language = resolveLocale(event);
|
|
67
|
+
let locales: { code: string; language?: string; isDefault?: boolean; status?: string }[] = [];
|
|
68
|
+
try {
|
|
69
|
+
const markets = await getMarketService(event).listMarkets();
|
|
70
|
+
const selected = resolveSelectedMarket(event);
|
|
71
|
+
const market = markets.find(m => m.code === selected) ?? markets.find(m => m.isDefault) ?? markets[0];
|
|
72
|
+
locales = market?.locales ?? [];
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
locales = [];
|
|
76
|
+
}
|
|
77
|
+
return pickMarketLocale({
|
|
78
|
+
locales,
|
|
79
|
+
chosen: getCookie(event, CONTENT_LOCALE_COOKIE_NAME),
|
|
80
|
+
uiLanguage: language,
|
|
81
|
+
uiLanguages: [...SUPPORTED],
|
|
82
|
+
defaultUiLanguage: defaultUiLanguage(),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
41
86
|
/**
|
|
42
87
|
* The locale a CONTACT is stored under, which is not the locale the UI
|
|
43
88
|
* speaks. The storefront works in a language ("de"); the customers app keeps
|
|
@@ -46,6 +91,7 @@ export function resolveLocale(event: H3Event): Locale {
|
|
|
46
91
|
* language reads to the buyer as a registration that silently breaks.
|
|
47
92
|
*
|
|
48
93
|
* The market is the source of truth for which locales a store has. Falls back
|
|
94
|
+
* to the market's default locale when it has none for the page language, and
|
|
49
95
|
* to the bare language when markets cannot be read: a registration that
|
|
50
96
|
* reaches the app and may be refused beats one that never leaves.
|
|
51
97
|
*/
|
|
@@ -56,10 +102,18 @@ export async function resolveStoreLocale(event: H3Event): Promise<string> {
|
|
|
56
102
|
const selected = resolveSelectedMarket(event);
|
|
57
103
|
const market = markets.find(m => m.code === selected) ?? markets.find(m => m.isDefault) ?? markets[0];
|
|
58
104
|
const locales = market?.locales ?? [];
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
105
|
+
if (!locales.length) {
|
|
106
|
+
return language;
|
|
107
|
+
}
|
|
108
|
+
const picked = pickMarketLocale({
|
|
109
|
+
locales,
|
|
110
|
+
chosen: getCookie(event, CONTENT_LOCALE_COOKIE_NAME),
|
|
111
|
+
uiLanguage: language,
|
|
112
|
+
uiLanguages: [...SUPPORTED],
|
|
113
|
+
defaultUiLanguage: defaultUiLanguage(),
|
|
114
|
+
});
|
|
115
|
+
const known = locales.some(l => l.code === picked);
|
|
116
|
+
return known ? picked : (locales.find(l => l.isDefault) ?? locales[0])?.code ?? language;
|
|
63
117
|
}
|
|
64
118
|
catch {
|
|
65
119
|
return language;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { searchLocaleKeys } from "../../app/shared/locale";
|
|
1
|
+
import { localeLanguage, searchLocaleKeys } from "../../app/shared/locale";
|
|
2
2
|
import type { LiveProductDoc } from "./liveCatalog";
|
|
3
3
|
import { preferLocaleDocs } from "./liveCatalog";
|
|
4
4
|
import { searchViaMultiSearch } from "./searchMultiSearchFallback";
|
|
@@ -78,8 +78,18 @@ export interface SearchClientResult {
|
|
|
78
78
|
found: number;
|
|
79
79
|
page: number;
|
|
80
80
|
perPage: number;
|
|
81
|
-
/**
|
|
81
|
+
/**
|
|
82
|
+
* The locale whose index answered, "" when none. With one index per
|
|
83
|
+
* language (PE-865) the service answers a language it has no index for
|
|
84
|
+
* from the tenant's default language and says so here (it also sends
|
|
85
|
+
* `X-Revenexx-Locale-Served`, which the SDK call does not surface).
|
|
86
|
+
*/
|
|
82
87
|
locale: string;
|
|
88
|
+
/**
|
|
89
|
+
* True when another language answered than the one asked for — a page can
|
|
90
|
+
* say "shown in German" instead of passing the results off as French.
|
|
91
|
+
*/
|
|
92
|
+
localeFallback: boolean;
|
|
83
93
|
/** One document per product, the best-matching locale winning. */
|
|
84
94
|
docs: LiveProductDoc[];
|
|
85
95
|
facets: SearchFacetResult[];
|
|
@@ -194,7 +204,7 @@ function safeRedirectUrl(raw: unknown): string | null {
|
|
|
194
204
|
return /^https?:\/\//i.test(url) ? url : null;
|
|
195
205
|
}
|
|
196
206
|
|
|
197
|
-
function
|
|
207
|
+
export function normalizeSearchAction(raw: unknown): SearchAction | null {
|
|
198
208
|
if (!raw || typeof raw !== "object") {
|
|
199
209
|
return null;
|
|
200
210
|
}
|
|
@@ -212,6 +222,18 @@ function normalizeAction(raw: unknown): SearchAction | null {
|
|
|
212
222
|
return null;
|
|
213
223
|
}
|
|
214
224
|
|
|
225
|
+
/**
|
|
226
|
+
* Whether the served locale is another language than the requested one.
|
|
227
|
+
* A region answering for another region of the same language (`de` for
|
|
228
|
+
* `de-CH`) is not a fallback: the text is the buyer's language.
|
|
229
|
+
*/
|
|
230
|
+
export function isLocaleFallback(requested: string | undefined, served: string | undefined): boolean {
|
|
231
|
+
if (!requested || !served) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
return localeLanguage(requested) !== localeLanguage(served);
|
|
235
|
+
}
|
|
236
|
+
|
|
215
237
|
/** The query endpoint's answer in the shape every caller reads. */
|
|
216
238
|
export function mapSearchQueryResponse(
|
|
217
239
|
response: SearchQueryResponse,
|
|
@@ -230,6 +252,7 @@ export function mapSearchQueryResponse(
|
|
|
230
252
|
page: response.page ?? Math.max(1, params.page ?? 1),
|
|
231
253
|
perPage: response.per_page ?? params.perPage ?? hits.length,
|
|
232
254
|
locale: response.locale ?? "",
|
|
255
|
+
localeFallback: isLocaleFallback(params.locale, response.locale),
|
|
233
256
|
docs,
|
|
234
257
|
facets: (response.facets ?? []).map(facet => ({
|
|
235
258
|
field: facet.field,
|
|
@@ -237,7 +260,7 @@ export function mapSearchQueryResponse(
|
|
|
237
260
|
...(facet.stats ? { stats: facet.stats } : {}),
|
|
238
261
|
})),
|
|
239
262
|
actions: (response.actions ?? [])
|
|
240
|
-
.map(
|
|
263
|
+
.map(normalizeSearchAction)
|
|
241
264
|
.filter((action): action is SearchAction => action !== null),
|
|
242
265
|
ignoredFilters: response.ignored_filters ?? [],
|
|
243
266
|
ignoredSort: response.ignored_sort ?? null,
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The body POST /v1/search/events gets from the storefront's BFF (PE-867):
|
|
3
|
+
* the storefront's camelCase event, checked, in the search service's shape.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const TYPES = new Set(["click", "add_to_cart", "purchase"]);
|
|
7
|
+
const COLLECTION_RE = /^[a-z][a-z0-9_-]{0,62}$/;
|
|
8
|
+
const DOC_ID_RE = /^[\w.:-]{1,128}$/;
|
|
9
|
+
const QUERY_ID_RE = /^[\w-]{1,40}$/;
|
|
10
|
+
|
|
11
|
+
export interface SearchEventInput {
|
|
12
|
+
type?: string;
|
|
13
|
+
queryId?: string;
|
|
14
|
+
collection?: string;
|
|
15
|
+
docId?: string;
|
|
16
|
+
position?: number;
|
|
17
|
+
q?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The body the search service gets, or null when the storefront sent nonsense. */
|
|
21
|
+
export function searchEventBody(input: SearchEventInput, userToken: string): Record<string, unknown> | null {
|
|
22
|
+
if (!input.type || !TYPES.has(input.type) || !input.collection || !COLLECTION_RE.test(input.collection)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
if (input.queryId !== undefined && !QUERY_ID_RE.test(input.queryId)) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
if (input.docId !== undefined && !DOC_ID_RE.test(input.docId)) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
if (input.type === "click" && !input.queryId) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
const position = typeof input.position === "number" && Number.isInteger(input.position) && input.position >= 1 && input.position <= 1000
|
|
35
|
+
? input.position
|
|
36
|
+
: undefined;
|
|
37
|
+
return {
|
|
38
|
+
type: input.type,
|
|
39
|
+
collection: input.collection,
|
|
40
|
+
...(input.queryId ? { query_id: input.queryId } : {}),
|
|
41
|
+
...(input.docId ? { doc_id: input.docId } : {}),
|
|
42
|
+
...(position ? { position } : {}),
|
|
43
|
+
...(typeof input.q === "string" && input.q.trim() ? { q: input.q.trim().slice(0, 200) } : {}),
|
|
44
|
+
user_token: userToken,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -203,6 +203,8 @@ export async function searchViaMultiSearch(params: SearchClientParams): Promise<
|
|
|
203
203
|
page: response.page ?? page,
|
|
204
204
|
perPage,
|
|
205
205
|
locale,
|
|
206
|
+
// multi_search filters the mixed index by locale, it never substitutes one.
|
|
207
|
+
localeFallback: false,
|
|
206
208
|
docs,
|
|
207
209
|
facets: (response.facet_counts ?? []).map(fc => ({
|
|
208
210
|
field: fc.field_name,
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { searchLocaleKeys } from "../../app/shared/locale";
|
|
2
|
+
import type { LiveProductDoc } from "./liveCatalog";
|
|
3
|
+
import { preferLocaleDocs } from "./liveCatalog";
|
|
4
|
+
import type { SearchAction } from "./searchClient";
|
|
5
|
+
import { isSearchRouteMissing, normalizeSearchAction, searchClient } from "./searchClient";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The quick search (PE-867): products and the category, manufacturer and page
|
|
9
|
+
* tiles beside them, from ONE request to the search service's suggest
|
|
10
|
+
* endpoint. Each collection is searched with its own `suggest` preset; a
|
|
11
|
+
* collection the tenant does not have is simply not in the answer.
|
|
12
|
+
*
|
|
13
|
+
* A gateway without `POST /v1/search/suggest` answers 404/405. The quick
|
|
14
|
+
* search then asks the products' `suggest` view alone, as before, and has no
|
|
15
|
+
* tiles; that is remembered per tenant for a few minutes.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** A category document of the `categories` collection (products app, search.json v3). */
|
|
19
|
+
export interface LiveCategoryDoc {
|
|
20
|
+
id: string;
|
|
21
|
+
entity_id: string;
|
|
22
|
+
code: string;
|
|
23
|
+
name: string;
|
|
24
|
+
/** The ancestors' names, root first, in the document's language. */
|
|
25
|
+
path?: string[];
|
|
26
|
+
/** The ancestors' codes, root first. */
|
|
27
|
+
parents?: string[];
|
|
28
|
+
image?: string;
|
|
29
|
+
product_count?: number;
|
|
30
|
+
locale: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A manufacturer document of the `manufacturers` collection. */
|
|
34
|
+
export interface LiveManufacturerDoc {
|
|
35
|
+
id: string;
|
|
36
|
+
entity_id: string;
|
|
37
|
+
code: string;
|
|
38
|
+
name: string;
|
|
39
|
+
logo?: string;
|
|
40
|
+
product_count?: number;
|
|
41
|
+
locale: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A page document of the `pages` collection (pages app). */
|
|
45
|
+
export interface LivePageDoc {
|
|
46
|
+
id: string;
|
|
47
|
+
entity_id: string;
|
|
48
|
+
title: string;
|
|
49
|
+
slug?: string;
|
|
50
|
+
locale: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface SearchSuggestResult {
|
|
54
|
+
/** `suggest` (one request, tiles) or `query` (the products-only fallback). */
|
|
55
|
+
source: "suggest" | "query" | "multi_search";
|
|
56
|
+
/** For search events; "" when the answer had none. */
|
|
57
|
+
queryId: string;
|
|
58
|
+
locale: string;
|
|
59
|
+
products: LiveProductDoc[];
|
|
60
|
+
categories: LiveCategoryDoc[];
|
|
61
|
+
manufacturers: LiveManufacturerDoc[];
|
|
62
|
+
pages: LivePageDoc[];
|
|
63
|
+
actions: SearchAction[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface SearchSuggestParams {
|
|
67
|
+
readonly q: string;
|
|
68
|
+
readonly locale?: string;
|
|
69
|
+
/** Hits per collection; left to the presets when omitted. */
|
|
70
|
+
readonly perPage?: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The suggest endpoint's answer, as it comes off the wire. */
|
|
74
|
+
export interface SearchSuggestResponse {
|
|
75
|
+
query_id?: string;
|
|
76
|
+
locale?: string;
|
|
77
|
+
groups?: { collection: string; found?: number; hits?: { document: Record<string, unknown> }[] }[];
|
|
78
|
+
actions?: unknown[];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const SUGGEST_PATH = "/v1/search/suggest";
|
|
82
|
+
const ROUTE_MISSING_TTL_MS = 5 * 60 * 1000;
|
|
83
|
+
const routeMissingUntil = new Map<string, number>();
|
|
84
|
+
const routeMissingLogged = new Set<string>();
|
|
85
|
+
|
|
86
|
+
/** Clears the fallback memory — tests only. */
|
|
87
|
+
export function resetSearchSuggestState(): void {
|
|
88
|
+
routeMissingUntil.clear();
|
|
89
|
+
routeMissingLogged.clear();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Path, body and headers of the suggest request. */
|
|
93
|
+
export function buildSearchSuggestRequest(params: SearchSuggestParams): {
|
|
94
|
+
path: string;
|
|
95
|
+
body: Record<string, unknown>;
|
|
96
|
+
headers: Record<string, string>;
|
|
97
|
+
} {
|
|
98
|
+
const headers: Record<string, string> = {};
|
|
99
|
+
const locale = params.locale ? searchLocaleKeys(params.locale)[0] : undefined;
|
|
100
|
+
if (locale) {
|
|
101
|
+
headers["X-Revenexx-Locale"] = locale;
|
|
102
|
+
}
|
|
103
|
+
const body: Record<string, unknown> = { q: params.q.trim() };
|
|
104
|
+
if (params.perPage !== undefined) {
|
|
105
|
+
body.per_page = Math.min(20, Math.max(1, Math.floor(params.perPage)));
|
|
106
|
+
}
|
|
107
|
+
return { path: SUGGEST_PATH, body, headers };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function docsOf<T>(response: SearchSuggestResponse, collection: string): T[] {
|
|
111
|
+
const group = (response.groups ?? []).find(g => g.collection === collection);
|
|
112
|
+
return (group?.hits ?? [])
|
|
113
|
+
.map(hit => hit.document)
|
|
114
|
+
.filter((doc): doc is Record<string, unknown> => Boolean(doc)) as T[];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** One tile per entity: a language and its region can both come back. */
|
|
118
|
+
function onePerEntity<T extends { entity_id?: string; id: string }>(docs: T[]): T[] {
|
|
119
|
+
const seen = new Set<string>();
|
|
120
|
+
return docs.filter((doc) => {
|
|
121
|
+
const key = doc.entity_id || doc.id;
|
|
122
|
+
if (seen.has(key)) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
seen.add(key);
|
|
126
|
+
return true;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The suggest endpoint's answer in the shape the quick search reads. */
|
|
131
|
+
export function mapSearchSuggestResponse(response: SearchSuggestResponse, params: SearchSuggestParams): SearchSuggestResult {
|
|
132
|
+
const products = docsOf<LiveProductDoc>(response, "products");
|
|
133
|
+
return {
|
|
134
|
+
source: "suggest",
|
|
135
|
+
queryId: response.query_id ?? "",
|
|
136
|
+
locale: response.locale ?? "",
|
|
137
|
+
products: params.locale ? preferLocaleDocs(products, params.locale) : products,
|
|
138
|
+
categories: onePerEntity(docsOf<LiveCategoryDoc>(response, "categories")),
|
|
139
|
+
manufacturers: onePerEntity(docsOf<LiveManufacturerDoc>(response, "manufacturers")),
|
|
140
|
+
pages: onePerEntity(docsOf<LivePageDoc>(response, "pages")),
|
|
141
|
+
actions: (response.actions ?? [])
|
|
142
|
+
.map(normalizeSearchAction)
|
|
143
|
+
.filter((action): action is SearchAction => action !== null),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function tenantKey(): string {
|
|
148
|
+
try {
|
|
149
|
+
return revenexxTenantKey();
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return "";
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function productsOnly(params: SearchSuggestParams): Promise<SearchSuggestResult> {
|
|
157
|
+
const result = await searchClient.search({
|
|
158
|
+
view: "suggest",
|
|
159
|
+
q: params.q,
|
|
160
|
+
...(params.perPage !== undefined ? { perPage: params.perPage } : {}),
|
|
161
|
+
...(params.locale ? { locale: params.locale } : {}),
|
|
162
|
+
});
|
|
163
|
+
return {
|
|
164
|
+
source: result.source,
|
|
165
|
+
queryId: result.queryId,
|
|
166
|
+
locale: result.locale,
|
|
167
|
+
products: result.docs,
|
|
168
|
+
categories: [],
|
|
169
|
+
manufacturers: [],
|
|
170
|
+
pages: [],
|
|
171
|
+
actions: result.actions,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function suggest(params: SearchSuggestParams): Promise<SearchSuggestResult> {
|
|
176
|
+
const tenant = tenantKey();
|
|
177
|
+
const missingUntil = routeMissingUntil.get(tenant);
|
|
178
|
+
if (missingUntil && missingUntil > Date.now()) {
|
|
179
|
+
return await productsOnly(params);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const request = buildSearchSuggestRequest(params);
|
|
183
|
+
try {
|
|
184
|
+
const response = await useRevenexxSdk().call<SearchSuggestResponse>("POST", request.path, {
|
|
185
|
+
body: request.body,
|
|
186
|
+
headers: request.headers,
|
|
187
|
+
});
|
|
188
|
+
routeMissingUntil.delete(tenant);
|
|
189
|
+
return mapSearchSuggestResponse(response ?? {}, params);
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
if (!isSearchRouteMissing(err)) {
|
|
193
|
+
throw err;
|
|
194
|
+
}
|
|
195
|
+
routeMissingUntil.set(tenant, Date.now() + ROUTE_MISSING_TTL_MS);
|
|
196
|
+
if (!routeMissingLogged.has(tenant)) {
|
|
197
|
+
routeMissingLogged.add(tenant);
|
|
198
|
+
getLogService().warn(
|
|
199
|
+
`search: the gateway has no ${SUGGEST_PATH} route yet, the quick search shows products only`,
|
|
200
|
+
{ tenant, ...apiErrorContext(err) },
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
return await productsOnly(params);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export const searchSuggestClient = { suggest };
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { ProductCategory } from "../../app/config/navigation";
|
|
2
|
+
import type { LiveProductDoc } from "./liveCatalog";
|
|
3
|
+
import { liveImageSrc } from "./liveCatalog";
|
|
4
|
+
import type { LiveCategoryDoc, LiveManufacturerDoc, LivePageDoc } from "./searchSuggest";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What the quick search dropdown renders (PE-867). Paths are site paths
|
|
8
|
+
* without the locale prefix; the client localises them. `docId` (the
|
|
9
|
+
* record's id, the same in every language) goes back with the click event, so
|
|
10
|
+
* the analytics can tell which tile was taken.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export interface ProductSuggestion {
|
|
14
|
+
id: string;
|
|
15
|
+
docId: string;
|
|
16
|
+
name: string;
|
|
17
|
+
sku: string;
|
|
18
|
+
category: string;
|
|
19
|
+
categorySlug: string;
|
|
20
|
+
subcategorySlug?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CategoryTile {
|
|
24
|
+
docId: string;
|
|
25
|
+
code: string;
|
|
26
|
+
name: string;
|
|
27
|
+
/** The ancestors' names, root first — "Werkzeuge › Halter". */
|
|
28
|
+
path: string[];
|
|
29
|
+
image: string | null;
|
|
30
|
+
productCount: number;
|
|
31
|
+
to: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ManufacturerTile {
|
|
35
|
+
docId: string;
|
|
36
|
+
code: string;
|
|
37
|
+
name: string;
|
|
38
|
+
logo: string | null;
|
|
39
|
+
productCount: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface PageTile {
|
|
43
|
+
docId: string;
|
|
44
|
+
title: string;
|
|
45
|
+
to: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function productSuggestion(doc: LiveProductDoc): ProductSuggestion {
|
|
49
|
+
return {
|
|
50
|
+
id: doc.entity_id,
|
|
51
|
+
docId: doc.entity_id || doc.id,
|
|
52
|
+
name: doc.name,
|
|
53
|
+
sku: doc.sku,
|
|
54
|
+
category: doc.category_labels?.[0] ?? doc.categories?.[0] ?? "",
|
|
55
|
+
categorySlug: doc.categories?.[0] ?? "",
|
|
56
|
+
subcategorySlug: undefined,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Where a category tile leads. The storefront navigates two levels (see
|
|
62
|
+
* buildLiveCategoryTree): a top-level category is `/category/<code>`, one
|
|
63
|
+
* below it `/category/<top>/<code>`. A category deeper than that opens the
|
|
64
|
+
* second-level page it sits under, which lists it — the storefront has no page
|
|
65
|
+
* of its own for it. A category outside the navigation (an import's stray
|
|
66
|
+
* root) leads nowhere and gets no tile.
|
|
67
|
+
*/
|
|
68
|
+
export function categoryTilePath(doc: Pick<LiveCategoryDoc, "code" | "parents">, tree: readonly ProductCategory[]): string | null {
|
|
69
|
+
const chain = [...(doc.parents ?? []), doc.code];
|
|
70
|
+
const topIndex = chain.findIndex(code => tree.some(top => top.slug === code));
|
|
71
|
+
if (topIndex < 0) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const top = tree.find(t => t.slug === chain[topIndex])!;
|
|
75
|
+
const sub = chain[topIndex + 1];
|
|
76
|
+
if (sub !== undefined && top.subcategories.some(s => s.slug === sub)) {
|
|
77
|
+
return `/category/${encodeURIComponent(top.slug)}/${encodeURIComponent(sub)}`;
|
|
78
|
+
}
|
|
79
|
+
return `/category/${encodeURIComponent(top.slug)}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function categoryTiles(docs: LiveCategoryDoc[], tree: readonly ProductCategory[], limit: number): CategoryTile[] {
|
|
83
|
+
const shown = new Set(tree.map(t => t.slug));
|
|
84
|
+
const tiles: CategoryTile[] = [];
|
|
85
|
+
for (const doc of docs) {
|
|
86
|
+
const to = categoryTilePath(doc, tree);
|
|
87
|
+
if (!to) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
tiles.push({
|
|
91
|
+
docId: doc.entity_id || doc.id,
|
|
92
|
+
code: doc.code,
|
|
93
|
+
name: doc.name,
|
|
94
|
+
path: shopPath(doc, shown),
|
|
95
|
+
image: doc.image ? liveImageSrc(doc.image) : null,
|
|
96
|
+
productCount: doc.product_count ?? 0,
|
|
97
|
+
to,
|
|
98
|
+
});
|
|
99
|
+
if (tiles.length === limit) {
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return tiles;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The category's path as the shop shows it: an import's artificial root above
|
|
108
|
+
* the navigation is not part of it. Names and codes line up only when every
|
|
109
|
+
* ancestor has a name in this language; otherwise the names are kept as they
|
|
110
|
+
* are rather than cut at a guessed position.
|
|
111
|
+
*/
|
|
112
|
+
function shopPath(doc: LiveCategoryDoc, shown: Set<string>): string[] {
|
|
113
|
+
const names = doc.path ?? [];
|
|
114
|
+
const codes = doc.parents ?? [];
|
|
115
|
+
if (names.length !== codes.length) {
|
|
116
|
+
return names;
|
|
117
|
+
}
|
|
118
|
+
const first = codes.findIndex(code => shown.has(code));
|
|
119
|
+
return first < 0 ? [] : names.slice(first);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function manufacturerTiles(docs: LiveManufacturerDoc[], limit: number): ManufacturerTile[] {
|
|
123
|
+
return docs.slice(0, limit).map(doc => ({
|
|
124
|
+
docId: doc.entity_id || doc.id,
|
|
125
|
+
code: doc.code,
|
|
126
|
+
name: doc.name,
|
|
127
|
+
logo: doc.logo ? liveImageSrc(doc.logo) : null,
|
|
128
|
+
productCount: doc.product_count ?? 0,
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function pageTiles(docs: LivePageDoc[], limit: number): PageTile[] {
|
|
133
|
+
return docs
|
|
134
|
+
.filter(doc => typeof doc.slug === "string" && doc.slug.trim() !== "")
|
|
135
|
+
.slice(0, limit)
|
|
136
|
+
.map(doc => ({
|
|
137
|
+
docId: doc.entity_id || doc.id,
|
|
138
|
+
title: doc.title,
|
|
139
|
+
to: `/${doc.slug!.trim().replace(/^\/+/, "")}`,
|
|
140
|
+
}));
|
|
141
|
+
}
|