Add article.listSearchIndex for client-side search preload

Returns all company articles with id, sku, name, and eans as a flat
array. Walks the category tree to find reachable articles. Single
query using recursive CTE + FOR JSON for eans.
This commit is contained in:
seb
2026-08-03 22:31:36 +02:00
parent eaf4df4f5a
commit 84bb561031
4 changed files with 70 additions and 1 deletions

View File

@@ -0,0 +1,23 @@
import { SEARCH_ARTICLES } from '../../sql/article.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function listSearchIndex(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId) => {
const { rootCategoryId, languageId } = await access(googleAuthId, companyId);
const result = await pool
.request()
.input('rootCategoryId', rootCategoryId)
.input('languageId', languageId)
.query(SEARCH_ARTICLES);
return result.recordset.map((row) => ({
id: row.id,
sku: row.sku,
name: row.name,
eans: row.eansJson ? JSON.parse(row.eansJson).map((r) => r.ean) : [],
}));
};
}