This commit is contained in:
seb
2026-09-22 07:25:16 +02:00
parent 4ed6260e91
commit cca131d83a
148 changed files with 5343 additions and 3466 deletions

View File

@@ -1,4 +1,6 @@
import { GET_ARTICLE_BY_SKU_FULL } from '../../providers/mssql/queries/article.js';
import { GET_ARTICLE_BY_SKU_FULL as MSSQL_QUERY } from '../../providers/mssql/queries/article.js';
import { GET_ARTICLE_BY_SKU_FULL as SQLITE_QUERY } from '../../providers/sqlite3/queries/article.js';
import makeRunner from '../runner.js';
function parseJsonArray(raw) {
if (!raw) return [];
@@ -6,13 +8,16 @@ function parseJsonArray(raw) {
return Array.isArray(arr) ? arr : [];
}
export default function getBySku(pool) {
export default function getBySku(provider) {
const sql = provider?.dialect === 'sqlite3' ? SQLITE_QUERY : MSSQL_QUERY;
/**
* Returns the full detail of an article by its SKU, including unit, brand,
* categories, photos, EANs and prices.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{article: {id: number, sku: string, name: string, slug: string, unitamount: number|null, weight: number|null, unit: {si: string|null, basesi: string|null, basesifactor: number|null, symbol: string|null, name: string|null}, brand: {name: string, thumbW: number, thumbH: number, thumbHash: string}|null, categoryIds: number[], photos: Array<{sort: number, fullHash: string, fullW: number, fullH: number, fullSize: number, thumbW: number, thumbH: number, thumbHash: string}>, eans: string[], prices: Array<{minmultiplier: number, net: number, taxRate: number, customergroupId: number}>, suppliers: Array<{id: number, articleId: number, entityId: number, supplierName: string|null, sku: string|null, pu: number, stock: number|null, sort: number, priceId: number|null, price: {net: number, taxId: number, taxRate?: number}|null}>, optionsCount: number, articleListCount: number, childCount: number, fatherArticleId: number|null, fatherSku: string|null}}>} The full article detail.
* @throws {Error} If the article is not found or the user is not authorized.
*
@@ -20,16 +25,13 @@ export default function getBySku(pool) {
* const article = await store.article.getBySku(42, 'SKU-1');
* // => { article: { id, sku, name, unit, brand, categoryIds, photos, eans, prices, ... } }
*/
return async (googleAuthId, sku) => {
return async (googleAuthId, sku, tx) => {
const t0 = performance.now();
const result = await pool
.request()
.input('sku', sku)
.input('googleAuthId', googleAuthId)
.query(GET_ARTICLE_BY_SKU_FULL);
const runner = makeRunner(provider, tx);
const result = await runner.query(sql, { sku, googleAuthId });
console.log(`[getBySku] single query ${(performance.now() - t0).toFixed(1)}ms`);
const row = result.recordset[0];
const row = result.rows[0];
if (!row) {
throw new Error('Article not found');
}
@@ -43,6 +45,7 @@ export default function getBySku(pool) {
throw new Error('Not authorized');
}
const a = typeof row.articleJson === 'string' ? JSON.parse(row.articleJson) : row.articleJson;
return {