@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
package/app/shared/locale.ts
CHANGED
|
@@ -112,3 +112,136 @@ export function pickLocalizedLabel(
|
|
|
112
112
|
}
|
|
113
113
|
return fallback;
|
|
114
114
|
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Where a market locale stands in its rollout (markets app, PE-866):
|
|
118
|
+
* `preparing` while the catalogue and search are being built for it, then
|
|
119
|
+
* `active`. A locale without a status predates the field and is active.
|
|
120
|
+
*/
|
|
121
|
+
export type MarketLocaleStatus = "preparing" | "active";
|
|
122
|
+
|
|
123
|
+
/** The part of a market locale the language choice needs. */
|
|
124
|
+
export interface MarketLocaleRef {
|
|
125
|
+
/** BCP-47 code as the market stores it, e.g. "de-CH". */
|
|
126
|
+
code: string;
|
|
127
|
+
/** ISO 639-1 language, e.g. "de". Derived from the code when empty. */
|
|
128
|
+
language?: string;
|
|
129
|
+
isDefault?: boolean;
|
|
130
|
+
status?: MarketLocaleStatus | string | null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Whether the storefront may offer this locale: anything but `preparing`. */
|
|
134
|
+
export function isOfferedLocale(locale: Pick<MarketLocaleRef, "status">): boolean {
|
|
135
|
+
return locale.status !== "preparing";
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The locales a market offers its buyers, in the order to show them: the
|
|
140
|
+
* market's default first, the rest as the market lists them. A `preparing`
|
|
141
|
+
* locale is left out, and so is a second entry for the same code.
|
|
142
|
+
*/
|
|
143
|
+
export function offeredLocales<T extends MarketLocaleRef>(locales: readonly T[] | null | undefined): T[] {
|
|
144
|
+
const seen = new Set<string>();
|
|
145
|
+
const offered: T[] = [];
|
|
146
|
+
for (const locale of locales ?? []) {
|
|
147
|
+
const code = normalizeLocaleCode(locale.code);
|
|
148
|
+
if (!code || !isOfferedLocale(locale) || seen.has(code)) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
seen.add(code);
|
|
152
|
+
offered.push(locale);
|
|
153
|
+
}
|
|
154
|
+
return [...offered.filter(l => l.isDefault), ...offered.filter(l => !l.isDefault)];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The language of a market locale, from its `language` or its code. */
|
|
158
|
+
export function marketLocaleLanguage(locale: MarketLocaleRef): string {
|
|
159
|
+
return (locale.language?.trim() || localeLanguage(locale.code)).toLowerCase();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** One entry of the language choice. */
|
|
163
|
+
export interface LanguageOption {
|
|
164
|
+
/** The market locale the catalogue and search are asked in, e.g. "de-CH". */
|
|
165
|
+
locale: string;
|
|
166
|
+
language: string;
|
|
167
|
+
/** The UI language the pages render in: the language itself, or the default UI language. */
|
|
168
|
+
uiLocale: string;
|
|
169
|
+
/** True when there are no UI strings for this language and the default UI language stands in. */
|
|
170
|
+
uiFallback: boolean;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The languages a market offers, one per language (the first locale the
|
|
175
|
+
* market offers for it wins, so the market's default leads). A language the
|
|
176
|
+
* storefront has no UI strings for is still offered: its catalogue and
|
|
177
|
+
* search answer in that language, the pages render in the default UI
|
|
178
|
+
* language. Null when the market offers no locale: the caller keeps its
|
|
179
|
+
* fixed UI languages, as before markets carried locales.
|
|
180
|
+
*/
|
|
181
|
+
export function marketLanguageOptions(
|
|
182
|
+
locales: readonly MarketLocaleRef[] | null | undefined,
|
|
183
|
+
uiLanguages: readonly string[],
|
|
184
|
+
defaultUiLanguage: string,
|
|
185
|
+
): LanguageOption[] | null {
|
|
186
|
+
const offered = offeredLocales(locales ?? []);
|
|
187
|
+
if (!offered.length) {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
const ui = new Set(uiLanguages.map(code => code.toLowerCase()));
|
|
191
|
+
const options: LanguageOption[] = [];
|
|
192
|
+
const languages = new Set<string>();
|
|
193
|
+
for (const entry of offered) {
|
|
194
|
+
const language = marketLocaleLanguage(entry);
|
|
195
|
+
if (!language || languages.has(language)) {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
languages.add(language);
|
|
199
|
+
const hasUi = ui.has(language);
|
|
200
|
+
options.push({
|
|
201
|
+
locale: normalizeLocaleCode(entry.code),
|
|
202
|
+
language,
|
|
203
|
+
uiLocale: hasUi ? language : defaultUiLanguage,
|
|
204
|
+
uiFallback: !hasUi,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return options;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The market locale the catalogue and search are asked in, for a page that
|
|
212
|
+
* renders in `uiLanguage`.
|
|
213
|
+
*
|
|
214
|
+
* `chosen` is the buyer's explicit choice (a market locale code). It wins
|
|
215
|
+
* when it is offered and agrees with the page: same language, or a language
|
|
216
|
+
* without UI strings shown in the default UI language. Otherwise the
|
|
217
|
+
* market's locale for the page language, else `fallback` (the bare
|
|
218
|
+
* language's full code, what the storefront asked before markets carried
|
|
219
|
+
* locales). A `preparing` locale is never answered.
|
|
220
|
+
*/
|
|
221
|
+
export function pickMarketLocale(input: {
|
|
222
|
+
locales: readonly MarketLocaleRef[] | null | undefined;
|
|
223
|
+
chosen?: string | null;
|
|
224
|
+
uiLanguage: string;
|
|
225
|
+
uiLanguages: readonly string[];
|
|
226
|
+
defaultUiLanguage: string;
|
|
227
|
+
}): string {
|
|
228
|
+
const uiLanguage = input.uiLanguage.toLowerCase();
|
|
229
|
+
const offered = offeredLocales(input.locales ?? []);
|
|
230
|
+
const ui = new Set(input.uiLanguages.map(code => code.toLowerCase()));
|
|
231
|
+
|
|
232
|
+
if (input.chosen) {
|
|
233
|
+
const chosen = normalizeLocaleCode(input.chosen);
|
|
234
|
+
const hit = offered.find(entry => normalizeLocaleCode(entry.code) === chosen);
|
|
235
|
+
if (hit) {
|
|
236
|
+
const language = marketLocaleLanguage(hit);
|
|
237
|
+
const agrees = language === uiLanguage
|
|
238
|
+
|| (!ui.has(language) && uiLanguage === input.defaultUiLanguage.toLowerCase());
|
|
239
|
+
if (agrees) {
|
|
240
|
+
return normalizeLocaleCode(hit.code);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const sameLanguage = offered.find(entry => marketLocaleLanguage(entry) === uiLanguage);
|
|
246
|
+
return sameLanguage ? normalizeLocaleCode(sameLanguage.code) : fullLocaleCode(uiLanguage);
|
|
247
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { StorageLike } from "./themeEvents";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Which search led to a product (PE-867). A click from a search result or the
|
|
5
|
+
* quick search is remembered per article number, so the add-to-cart and the
|
|
6
|
+
* purchase that follow can be counted for that search. Kept in the tab's
|
|
7
|
+
* session storage only: it never leaves the browser except as the events
|
|
8
|
+
* themselves, and it ends with the tab.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export interface SearchAttribution {
|
|
12
|
+
queryId: string;
|
|
13
|
+
/** The product's search document id, sent back as the event's doc_id. */
|
|
14
|
+
docId: string;
|
|
15
|
+
q: string;
|
|
16
|
+
at: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const KEY = "rvx:search-attribution";
|
|
20
|
+
/** A click older than this no longer explains a cart. */
|
|
21
|
+
export const ATTRIBUTION_TTL_MS = 24 * 60 * 60 * 1000;
|
|
22
|
+
/** Products remembered at most; the oldest go first. */
|
|
23
|
+
export const ATTRIBUTION_LIMIT = 50;
|
|
24
|
+
|
|
25
|
+
type Store = Record<string, SearchAttribution>;
|
|
26
|
+
|
|
27
|
+
function read(storage: StorageLike | null | undefined, now: number): Store {
|
|
28
|
+
try {
|
|
29
|
+
const parsed = JSON.parse(storage?.getItem(KEY) ?? "{}") as Store;
|
|
30
|
+
const fresh: Store = {};
|
|
31
|
+
for (const [sku, entry] of Object.entries(parsed ?? {})) {
|
|
32
|
+
if (entry && typeof entry.queryId === "string" && typeof entry.at === "number" && now - entry.at < ATTRIBUTION_TTL_MS) {
|
|
33
|
+
fresh[sku] = entry;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return fresh;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return {};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function write(storage: StorageLike | null | undefined, store: Store): void {
|
|
44
|
+
try {
|
|
45
|
+
storage?.setItem(KEY, JSON.stringify(store));
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// storage full or blocked: attribution is a nicety, never an error
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Remember that `sku` was opened from the search `entry.queryId`. */
|
|
53
|
+
export function rememberSearchClick(
|
|
54
|
+
storage: StorageLike | null | undefined,
|
|
55
|
+
sku: string,
|
|
56
|
+
entry: Omit<SearchAttribution, "at">,
|
|
57
|
+
now = Date.now(),
|
|
58
|
+
): void {
|
|
59
|
+
if (!sku || !entry.queryId) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const store = read(storage, now);
|
|
63
|
+
delete store[sku];
|
|
64
|
+
store[sku] = { ...entry, at: now };
|
|
65
|
+
const skus = Object.keys(store);
|
|
66
|
+
for (const old of skus.slice(0, Math.max(0, skus.length - ATTRIBUTION_LIMIT))) {
|
|
67
|
+
delete store[old];
|
|
68
|
+
}
|
|
69
|
+
write(storage, store);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The searches behind these article numbers, for those that have one. */
|
|
73
|
+
export function searchAttributions(
|
|
74
|
+
storage: StorageLike | null | undefined,
|
|
75
|
+
skus: readonly string[],
|
|
76
|
+
now = Date.now(),
|
|
77
|
+
): { sku: string; attribution: SearchAttribution }[] {
|
|
78
|
+
const store = read(storage, now);
|
|
79
|
+
return skus
|
|
80
|
+
.filter((sku, i) => skus.indexOf(sku) === i && store[sku] !== undefined)
|
|
81
|
+
.map(sku => ({ sku, attribution: store[sku]! }));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Forget these article numbers — after a purchase, a later order is not this search's. */
|
|
85
|
+
export function forgetSearchAttributions(storage: StorageLike | null | undefined, skus: readonly string[], now = Date.now()): void {
|
|
86
|
+
const store = read(storage, now);
|
|
87
|
+
for (const sku of skus) {
|
|
88
|
+
delete store[sku];
|
|
89
|
+
}
|
|
90
|
+
write(storage, store);
|
|
91
|
+
}
|
|
@@ -53,6 +53,13 @@
|
|
|
53
53
|
"searchPlaceholder": "In dieser Kategorie suchen…",
|
|
54
54
|
"allProducts": "Alle Produkte",
|
|
55
55
|
"productCount": "{count} Produkt | {count} Produkte"
|
|
56
|
+
},
|
|
57
|
+
"suggest": {
|
|
58
|
+
"categories": "Kategorien",
|
|
59
|
+
"manufacturers": "Hersteller",
|
|
60
|
+
"products": "Produkte",
|
|
61
|
+
"pages": "Seiten",
|
|
62
|
+
"productCount": "{count} Artikel"
|
|
56
63
|
}
|
|
57
64
|
}
|
|
58
65
|
}
|
|
@@ -53,6 +53,13 @@
|
|
|
53
53
|
"searchPlaceholder": "Search within this category…",
|
|
54
54
|
"allProducts": "All products",
|
|
55
55
|
"productCount": "{count} product | {count} products"
|
|
56
|
+
},
|
|
57
|
+
"suggest": {
|
|
58
|
+
"categories": "Categories",
|
|
59
|
+
"manufacturers": "Manufacturers",
|
|
60
|
+
"products": "Products",
|
|
61
|
+
"pages": "Pages",
|
|
62
|
+
"productCount": "{count} item | {count} items"
|
|
56
63
|
}
|
|
57
64
|
}
|
|
58
65
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@revenexx/cover",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Cover — 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",
|
package/server/api/brands.get.ts
CHANGED
|
@@ -16,9 +16,10 @@ export default defineEventHandler(async (event) => {
|
|
|
16
16
|
try {
|
|
17
17
|
const service = getBrandService(event);
|
|
18
18
|
const locale = resolveLocale(event);
|
|
19
|
+
const searchLocale = await resolveMarketLocale(event);
|
|
19
20
|
return wantsAll
|
|
20
|
-
? await service.listBrandNames(locale)
|
|
21
|
-
: await service.listBrands(limit, locale);
|
|
21
|
+
? await service.listBrandNames(locale, searchLocale)
|
|
22
|
+
: await service.listBrands(limit, locale, searchLocale);
|
|
22
23
|
}
|
|
23
24
|
catch (err) {
|
|
24
25
|
getLogService().error("Service error: brands/list", toErrorContext(err));
|
|
@@ -85,7 +85,7 @@ function toTiles(
|
|
|
85
85
|
* the view the category page's listing asks, so the cards and the filter rail
|
|
86
86
|
* can never disagree. The service pins locale and enabled products.
|
|
87
87
|
*/
|
|
88
|
-
async function liveCounts(categorySlug: string, locale: Locale): Promise<Map<string, number>> {
|
|
88
|
+
async function liveCounts(categorySlug: string, locale: Locale, searchLocale: string): Promise<Map<string, number>> {
|
|
89
89
|
const { branchOf } = await liveSubcategoryIndex(categorySlug, locale);
|
|
90
90
|
const leafCodes = await liveCategorySubtreeCodes([categorySlug]);
|
|
91
91
|
if (!leafCodes.length) {
|
|
@@ -96,7 +96,7 @@ async function liveCounts(categorySlug: string, locale: Locale): Promise<Map<str
|
|
|
96
96
|
view: "category",
|
|
97
97
|
filters: { categories: leafCodes },
|
|
98
98
|
perPage: 1,
|
|
99
|
-
locale,
|
|
99
|
+
locale: searchLocale,
|
|
100
100
|
});
|
|
101
101
|
|
|
102
102
|
return rollUpSubcategoryCounts(
|
|
@@ -114,7 +114,7 @@ async function liveCounts(categorySlug: string, locale: Locale): Promise<Map<str
|
|
|
114
114
|
* which is exactly how this looked when it was broken. A child whose lookup
|
|
115
115
|
* fails keeps its icon; the others keep their images.
|
|
116
116
|
*/
|
|
117
|
-
async function liveImages(categorySlug: string, locale: Locale): Promise<Map<string, string>> {
|
|
117
|
+
async function liveImages(categorySlug: string, locale: Locale, searchLocale: string): Promise<Map<string, string>> {
|
|
118
118
|
const { branchOf } = await liveSubcategoryIndex(categorySlug, locale);
|
|
119
119
|
|
|
120
120
|
// Invert leaf → child into child → its leaves. No extra catalogue call: the
|
|
@@ -134,7 +134,7 @@ async function liveImages(categorySlug: string, locale: Locale): Promise<Map<str
|
|
|
134
134
|
view: "category",
|
|
135
135
|
filters: { categories: leavesByChild.get(child) ?? [child] },
|
|
136
136
|
perPage: 1,
|
|
137
|
-
locale,
|
|
137
|
+
locale: searchLocale,
|
|
138
138
|
})));
|
|
139
139
|
|
|
140
140
|
const failed = results.find((r): r is PromiseRejectedResult => r.status === "rejected");
|
|
@@ -267,14 +267,17 @@ export default defineEventHandler(async (event): Promise<SubcategoryTile[]> => {
|
|
|
267
267
|
}
|
|
268
268
|
|
|
269
269
|
const isLive = resolveProductServiceKey(event) === "api";
|
|
270
|
+
// The market's locale for this page: the counts must come from the same
|
|
271
|
+
// language index the category listing searches (PE-866).
|
|
272
|
+
const searchLocale = isLive ? await resolveMarketLocale(event) : locale;
|
|
270
273
|
|
|
271
274
|
const [counts, images] = await Promise.all([
|
|
272
275
|
(isLive
|
|
273
|
-
? liveCounts(category.slug, locale)
|
|
276
|
+
? liveCounts(category.slug, locale, searchLocale)
|
|
274
277
|
: mockCounts(category.slug)
|
|
275
278
|
).catch(degrade<number>("product counts")),
|
|
276
279
|
(isLive
|
|
277
|
-
? liveImages(category.slug, locale)
|
|
280
|
+
? liveImages(category.slug, locale, searchLocale)
|
|
278
281
|
: mockImages(category.slug, category.subcategories)
|
|
279
282
|
).catch(degrade<string>("preview images")),
|
|
280
283
|
]);
|
|
@@ -7,7 +7,12 @@ export default defineEventHandler(async (event) => {
|
|
|
7
7
|
try {
|
|
8
8
|
// Catalog shell only — price + availability are the Offer domain,
|
|
9
9
|
// resolved separately via POST /api/offers (never blocks the catalog).
|
|
10
|
-
return await getProductService(event).listProducts({
|
|
10
|
+
return await getProductService(event).listProducts({
|
|
11
|
+
category,
|
|
12
|
+
subcategory,
|
|
13
|
+
locale,
|
|
14
|
+
searchLocale: await resolveMarketLocale(event),
|
|
15
|
+
});
|
|
11
16
|
}
|
|
12
17
|
catch (err) {
|
|
13
18
|
getLogService().error("Service error: products/list", toErrorContext(err));
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import { searchLocaleKeys } from "../../../app/shared/locale";
|
|
4
|
+
import type { SearchEventInput } from "../../utils/searchEvents";
|
|
5
|
+
import { searchEventBody } from "../../utils/searchEvents";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A shopper's click on a search result or tile, and the add-to-cart and
|
|
9
|
+
* purchase that follow (PE-867), handed to the search service's events
|
|
10
|
+
* endpoint. Answers 202 and never fails the storefront: an event the gateway
|
|
11
|
+
* refuses is logged and dropped.
|
|
12
|
+
*
|
|
13
|
+
* An anonymous shopper is a pseudonymous token in a session cookie — a
|
|
14
|
+
* random id, nothing about the person, gone with the browser session. The
|
|
15
|
+
* search service stores it only salted and hashed.
|
|
16
|
+
*
|
|
17
|
+
* A signed-in buyer is the contact (and company) the identity service
|
|
18
|
+
* verified for this request: the SDK client sends it as X-Revenexx-Principal,
|
|
19
|
+
* as on every call made for a buyer, and the gateway resolves it into the
|
|
20
|
+
* contact and organization the search analytics count per customer. The same
|
|
21
|
+
* holds for the searches and quick searches, which go through the same client.
|
|
22
|
+
* Nothing here reads either id from the browser.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const TOKEN_COOKIE = "rvx_search";
|
|
26
|
+
|
|
27
|
+
export default defineEventHandler(async (event) => {
|
|
28
|
+
const input = await readBody<SearchEventInput>(event).catch(() => ({}) as SearchEventInput);
|
|
29
|
+
setResponseStatus(event, 202);
|
|
30
|
+
|
|
31
|
+
// The local docker index has no search service to tell.
|
|
32
|
+
if (resolveProductServiceKey(event) !== "api") {
|
|
33
|
+
return { accepted: false };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let token = getCookie(event, TOKEN_COOKIE);
|
|
37
|
+
if (!token || !/^[\w-]{8,64}$/.test(token)) {
|
|
38
|
+
token = randomUUID();
|
|
39
|
+
setCookie(event, TOKEN_COOKIE, token, {
|
|
40
|
+
httpOnly: true,
|
|
41
|
+
sameSite: "lax",
|
|
42
|
+
secure: !import.meta.dev,
|
|
43
|
+
path: "/",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const body = searchEventBody(input, token);
|
|
48
|
+
if (!body) {
|
|
49
|
+
return { accepted: false };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// The market locale the search was sent with (PE-866), so the event is
|
|
53
|
+
// logged for the same language index that answered it.
|
|
54
|
+
const locale = searchLocaleKeys(await resolveMarketLocale(event))[0];
|
|
55
|
+
try {
|
|
56
|
+
await useRevenexxSdk().call("POST", "/v1/search/events", {
|
|
57
|
+
body,
|
|
58
|
+
headers: locale ? { "X-Revenexx-Locale": locale } : {},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
getLogService().warn("search event not delivered", { type: body.type, ...apiErrorContext(err) });
|
|
63
|
+
}
|
|
64
|
+
return { accepted: true };
|
|
65
|
+
});
|
|
@@ -55,6 +55,12 @@ interface SearchAnswer {
|
|
|
55
55
|
* or an http(s) URL). Only the search page follows it.
|
|
56
56
|
*/
|
|
57
57
|
redirect?: string;
|
|
58
|
+
/** The search's id, for the click events its results send (PE-867); live mode only. */
|
|
59
|
+
queryId?: string;
|
|
60
|
+
/** The locale whose index answered (live only; see SearchClientResult.locale). */
|
|
61
|
+
locale?: string;
|
|
62
|
+
/** True when the index of another language answered (see SearchClientResult.localeFallback). */
|
|
63
|
+
localeFallback?: boolean;
|
|
58
64
|
}
|
|
59
65
|
|
|
60
66
|
export default defineEventHandler(async (event): Promise<SearchAnswer | null> => {
|
|
@@ -78,6 +84,9 @@ export default defineEventHandler(async (event): Promise<SearchAnswer | null> =>
|
|
|
78
84
|
// storefront's filter names and maps the facets back.
|
|
79
85
|
if (resolveProductServiceKey(event) === "api") {
|
|
80
86
|
const locale = resolveLocale(event);
|
|
87
|
+
// The market's locale for this page (`de-CH`, `fr-FR`): the search
|
|
88
|
+
// service routes it to that language's index (PE-866).
|
|
89
|
+
const searchLocale = await resolveMarketLocale(event);
|
|
81
90
|
try {
|
|
82
91
|
const request = await buildLiveListingFilters(filters, priceRange, locale);
|
|
83
92
|
const view = request.pinnedCategoryCode ? "category" : "search";
|
|
@@ -89,7 +98,7 @@ export default defineEventHandler(async (event): Promise<SearchAnswer | null> =>
|
|
|
89
98
|
sort,
|
|
90
99
|
page,
|
|
91
100
|
perPage,
|
|
92
|
-
locale,
|
|
101
|
+
locale: searchLocale,
|
|
93
102
|
});
|
|
94
103
|
|
|
95
104
|
// Catalog shell only — offers (price + availability) hydrate
|
|
@@ -108,6 +117,8 @@ export default defineEventHandler(async (event): Promise<SearchAnswer | null> =>
|
|
|
108
117
|
perPage,
|
|
109
118
|
priceFilter: liveListingPriceFilter(result, request.priceRequested),
|
|
110
119
|
...(redirect ? { redirect } : {}),
|
|
120
|
+
...(result.queryId ? { queryId: result.queryId } : {}),
|
|
121
|
+
...(result.locale ? { locale: result.locale, localeFallback: result.localeFallback } : {}),
|
|
111
122
|
};
|
|
112
123
|
}
|
|
113
124
|
catch (err) {
|
|
@@ -1,39 +1,58 @@
|
|
|
1
|
+
import type { CategoryTile, ManufacturerTile, PageTile, ProductSuggestion } from "../../utils/suggestTiles";
|
|
2
|
+
import {
|
|
3
|
+
categoryTiles,
|
|
4
|
+
manufacturerTiles,
|
|
5
|
+
pageTiles,
|
|
6
|
+
productSuggestion,
|
|
7
|
+
} from "../../utils/suggestTiles";
|
|
8
|
+
|
|
1
9
|
const SUGGESTION_COUNT = 6;
|
|
10
|
+
const TILE_COUNT = 4;
|
|
11
|
+
const PAGE_COUNT = 3;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The quick search answer: product suggestions plus category, manufacturer and
|
|
15
|
+
* page tiles (PE-867), the `queryId` the dropdown sends back with its click
|
|
16
|
+
* events, and where curation sends the query on Enter, if anywhere.
|
|
17
|
+
*/
|
|
18
|
+
export interface SuggestAnswer {
|
|
19
|
+
suggestions: ProductSuggestion[];
|
|
20
|
+
categories: CategoryTile[];
|
|
21
|
+
manufacturers: ManufacturerTile[];
|
|
22
|
+
pages: PageTile[];
|
|
23
|
+
queryId: string;
|
|
24
|
+
redirect?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const EMPTY: SuggestAnswer = { suggestions: [], categories: [], manufacturers: [], pages: [], queryId: "" };
|
|
2
28
|
|
|
3
|
-
export default defineEventHandler(async (event) => {
|
|
29
|
+
export default defineEventHandler(async (event): Promise<SuggestAnswer> => {
|
|
4
30
|
const { q } = await readBody<{ q: string }>(event);
|
|
5
31
|
|
|
6
32
|
if (!q?.trim()) {
|
|
7
|
-
return {
|
|
33
|
+
return { ...EMPTY };
|
|
8
34
|
}
|
|
9
35
|
|
|
10
|
-
// Live mode
|
|
11
|
-
//
|
|
36
|
+
// Live mode: one request to the search service's suggest endpoint, every
|
|
37
|
+
// collection with its own `suggest` preset (searchSuggest.ts); the local
|
|
38
|
+
// docker index stays the mock implementation, products only.
|
|
12
39
|
if (resolveProductServiceKey(event) === "api") {
|
|
13
|
-
const locale =
|
|
40
|
+
const locale = await resolveMarketLocale(event);
|
|
14
41
|
try {
|
|
15
42
|
// Twice the suggestions: on the deprecated multi_search fallback a
|
|
16
43
|
// product still indexed under both `de` and `de-DE` comes back twice
|
|
17
44
|
// and is folded into one, which would leave the list short.
|
|
18
|
-
const result = await
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
perPage: SUGGESTION_COUNT * 2,
|
|
22
|
-
locale,
|
|
23
|
-
});
|
|
24
|
-
const docs = result.docs.slice(0, SUGGESTION_COUNT);
|
|
45
|
+
const result = await searchSuggestClient.suggest({ q, locale, perPage: SUGGESTION_COUNT * 2 });
|
|
46
|
+
const tree = result.categories.length ? await getLiveCategories(locale) : [];
|
|
47
|
+
const redirect = searchRedirect(result);
|
|
25
48
|
|
|
26
49
|
return {
|
|
27
|
-
suggestions:
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
categorySlug: doc.categories?.[0] ?? "",
|
|
34
|
-
subcategorySlug: undefined,
|
|
35
|
-
};
|
|
36
|
-
}),
|
|
50
|
+
suggestions: result.products.slice(0, SUGGESTION_COUNT).map(productSuggestion),
|
|
51
|
+
categories: categoryTiles(result.categories, tree, TILE_COUNT),
|
|
52
|
+
manufacturers: manufacturerTiles(result.manufacturers, TILE_COUNT),
|
|
53
|
+
pages: pageTiles(result.pages, PAGE_COUNT),
|
|
54
|
+
queryId: result.queryId,
|
|
55
|
+
...(redirect ? { redirect } : {}),
|
|
37
56
|
};
|
|
38
57
|
}
|
|
39
58
|
catch (err) {
|
|
@@ -54,10 +73,12 @@ export default defineEventHandler(async (event) => {
|
|
|
54
73
|
});
|
|
55
74
|
|
|
56
75
|
return {
|
|
76
|
+
...EMPTY,
|
|
57
77
|
suggestions: (results.hits ?? []).map((hit) => {
|
|
58
78
|
const doc = hit.document as Record<string, unknown>;
|
|
59
79
|
return {
|
|
60
80
|
id: String(doc.id),
|
|
81
|
+
docId: String(doc.id),
|
|
61
82
|
name: doc.name as string,
|
|
62
83
|
sku: doc.sku as string,
|
|
63
84
|
category: doc.category as string,
|
|
@@ -7,11 +7,12 @@ import type { Locale } from "../utils/locale";
|
|
|
7
7
|
* Register the active implementation via app.config → brandService.
|
|
8
8
|
*/
|
|
9
9
|
export interface IBrandService {
|
|
10
|
-
|
|
10
|
+
/** `searchLocale`: the market locale to search in, defaults to `locale` (PE-866). */
|
|
11
|
+
listBrands(limit: number, locale: Locale, searchLocale?: string): Promise<ShopBrand[]>;
|
|
11
12
|
/**
|
|
12
13
|
* The complete manufacturer list without logos (`logo: null`) — for pickers
|
|
13
14
|
* that must offer every brand, not the top N a grid shows. Cheaper than
|
|
14
15
|
* `listBrands`: no per-brand logo lookups.
|
|
15
16
|
*/
|
|
16
|
-
listBrandNames(locale: Locale): Promise<ShopBrand[]>;
|
|
17
|
+
listBrandNames(locale: Locale, searchLocale?: string): Promise<ShopBrand[]>;
|
|
17
18
|
}
|
|
@@ -9,6 +9,11 @@ export interface ProductListFilter {
|
|
|
9
9
|
subcategory?: string;
|
|
10
10
|
/** Request locale — live implementations serve locale-scoped documents. */
|
|
11
11
|
locale?: Locale;
|
|
12
|
+
/**
|
|
13
|
+
* The market locale to search in (`de-CH`, `fr-FR`); defaults to `locale`.
|
|
14
|
+
* The search service routes it to that language's index (PE-866).
|
|
15
|
+
*/
|
|
16
|
+
searchLocale?: string;
|
|
12
17
|
}
|
|
13
18
|
|
|
14
19
|
/**
|
|
@@ -26,14 +26,14 @@ const cache = new Map<string, { brands: ShopBrand[]; loadedAt: number }>();
|
|
|
26
26
|
* for the same brand.
|
|
27
27
|
*/
|
|
28
28
|
export class ApiBrandService implements IBrandService {
|
|
29
|
-
async listBrands(limit: number, locale: Locale): Promise<ShopBrand[]> {
|
|
30
|
-
const cacheKey = `${revenexxTenantKey()}:${
|
|
29
|
+
async listBrands(limit: number, locale: Locale, searchLocale: string = locale): Promise<ShopBrand[]> {
|
|
30
|
+
const cacheKey = `${revenexxTenantKey()}:${searchLocale}:${limit}`;
|
|
31
31
|
const cached = cache.get(cacheKey);
|
|
32
32
|
if (cached && Date.now() - cached.loadedAt < BRAND_CACHE_TTL_MS) {
|
|
33
33
|
return cached.brands;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
const counts = await this.facetManufacturers(
|
|
36
|
+
const counts = await this.facetManufacturers(searchLocale);
|
|
37
37
|
const top = counts.filter(entry => entry.value.trim().length > 0).slice(0, limit);
|
|
38
38
|
|
|
39
39
|
if (top.length === 0) {
|
|
@@ -53,7 +53,7 @@ export class ApiBrandService implements IBrandService {
|
|
|
53
53
|
view: "brand",
|
|
54
54
|
filters: { manufacturer: [entry.value] },
|
|
55
55
|
perPage: 1,
|
|
56
|
-
locale,
|
|
56
|
+
locale: searchLocale,
|
|
57
57
|
}))));
|
|
58
58
|
}
|
|
59
59
|
|
|
@@ -70,15 +70,15 @@ export class ApiBrandService implements IBrandService {
|
|
|
70
70
|
return brands;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
async listBrandNames(locale: Locale): Promise<ShopBrand[]> {
|
|
73
|
+
async listBrandNames(locale: Locale, searchLocale: string = locale): Promise<ShopBrand[]> {
|
|
74
74
|
// "names" cannot collide with a numeric limit key.
|
|
75
|
-
const cacheKey = `${revenexxTenantKey()}:${
|
|
75
|
+
const cacheKey = `${revenexxTenantKey()}:${searchLocale}:names`;
|
|
76
76
|
const cached = cache.get(cacheKey);
|
|
77
77
|
if (cached && Date.now() - cached.loadedAt < BRAND_CACHE_TTL_MS) {
|
|
78
78
|
return cached.brands;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
const counts = await this.facetManufacturers(
|
|
81
|
+
const counts = await this.facetManufacturers(searchLocale);
|
|
82
82
|
const brands = counts
|
|
83
83
|
.filter(entry => entry.value.trim().length > 0)
|
|
84
84
|
.map((entry): ShopBrand => ({ name: entry.value, logo: null, productCount: entry.count }));
|
|
@@ -88,7 +88,7 @@ export class ApiBrandService implements IBrandService {
|
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
/** Round trip 1 of listBrands: names + counts, count-descending. */
|
|
91
|
-
private async facetManufacturers(locale:
|
|
91
|
+
private async facetManufacturers(locale: string): Promise<{ value: string; count: number }[]> {
|
|
92
92
|
const result = await searchClient.search({ view: "brand", locale });
|
|
93
93
|
|
|
94
94
|
return result.facets.find(facet => facet.field === "manufacturer")?.counts ?? [];
|