This commit is contained in:
seb
2026-10-12 18:36:57 +02:00
parent 10b1d53f9d
commit 64d1772842
8 changed files with 72 additions and 24 deletions

View File

@@ -1,52 +1,60 @@
import { GET_TREE_FOR_COMPANY as MSSQL_GET_TREE } from '../../providers/mssql/queries/category.js';
import { GET_TREE_FOR_COMPANY as SQLITE_GET_TREE } from '../../providers/sqlite3/queries/category.js';
import {
GET_TREE_FOR_COMPANY as MSSQL_GET_TREE,
COUNT_ARTICLES_IN_TREE as MSSQL_COUNT_ARTICLES,
} from '../../providers/mssql/queries/category.js';
import {
GET_TREE_FOR_COMPANY as SQLITE_GET_TREE,
COUNT_ARTICLES_IN_TREE as SQLITE_COUNT_ARTICLES,
} from '../../providers/sqlite3/queries/category.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import queryCategoryArticles from './queryCategoryArticles.js';
import getCategoryPosterminalIds from '../posterminal/getCategoryPosterminalIds.js';
import makeRunner from '../runner.js';
export default function getTreeForCompany(provider) {
const access = resolveSelectedCompanyAccess(provider);
const articles = queryCategoryArticles(provider);
const posterminalIds = getCategoryPosterminalIds(provider);
const sql =
provider?.dialect === 'sqlite3' ? SQLITE_GET_TREE : MSSQL_GET_TREE;
const isSqlite = provider?.dialect === 'sqlite3';
const sql = isSqlite ? SQLITE_GET_TREE : MSSQL_GET_TREE;
const countSql = isSqlite ? SQLITE_COUNT_ARTICLES : MSSQL_COUNT_ARTICLES;
/**
* Returns the full category tree for the user's selected company together
* with its articles and the posterminal ids assigned to each category.
* with the article count and the posterminal ids assigned to each category.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{languageId: number, tree: Array<object>, articles: Array<object>}>} The category tree, articles and language.
* @returns {Promise<{languageId: number, tree: Array<{id: number, parentId: number|null, sort: number, name: string|null, slug: string|null, fullHash: string|null, thumbW: number|null, thumbH: number|null, thumbHash: string|null, articleCount: number, posterminalIds: number[]}>, articleCount: number}>} The category tree and article count.
*
* @example
* const tree = await store.category.getTreeForCompany(42);
* // => { languageId, tree: [{ id, posterminalIds, ... }], articles: [...] }
* // => { languageId, tree: [{ id, articleCount, posterminalIds, ... }], articleCount: 25 }
*/
return async (googleAuthId, tx) => {
const { companyId, rootCategoryId, languageId, posCustomerGroupId } =
const { companyId, rootCategoryId, languageId } =
await access(googleAuthId, tx);
const runner = makeRunner(provider, tx);
const [treeResult, articleRows, ptMap] = await Promise.all([
const [treeResult, countResult, ptMap] = await Promise.all([
runner.query(sql, {
rootId: rootCategoryId,
languageId,
}),
articles({ categoryId: rootCategoryId, languageId, posCustomerGroupId }, tx),
runner.query(countSql, {
rootId: rootCategoryId,
}),
posterminalIds(companyId, tx),
]);
const tree = treeResult.rows.map((row) => ({
...row,
articleCount: Number(row.articleCount ?? 0),
posterminalIds: ptMap[row.id] ?? [],
}));
return {
languageId,
tree,
articles: articleRows,
articleCount: Number(countResult.rows[0]?.total ?? 0),
};
};
}

View File

@@ -10,11 +10,11 @@ export default function list(provider) {
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<Array<object>>} The accessible company rows.
* @returns {Promise<Array<{id: number, name: string, rootCategoryId: number, languageId: number, iso639: string|null, taxrate: number, reducedtaxrate: number, taxfreerate: number}>>} The accessible company rows.
*
* @example
* const companies = await store.company.list(42);
* // => [{ id, name, ... }]
* // => [{ id, name, rootCategoryId, languageId, iso639, taxrate, reducedtaxrate, taxfreerate }]
*/
return async (googleAuthId, tx) => {
const result = await makeRunner(provider, tx).query(sql, { googleAuthId });