This commit is contained in:
seb
2026-09-29 20:10:49 +02:00
parent 23d6742a9f
commit 9885c489f0
17 changed files with 715 additions and 3 deletions

View File

@@ -0,0 +1,55 @@
import * as mssqlQueries from '../../providers/mssql/queries/articlelist.js';
import * as sqliteQueries from '../../providers/sqlite3/queries/articlelist.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import makeRunner from '../runner.js';
export default function getItemsByArticleSku(provider) {
const resolve = resolveArticleBySku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const queries = isSqlite ? sqliteQueries : mssqlQueries;
/**
* Returns the Stückliste (bundle items) for an article by its SKU.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, articleId: number, sku: string, name: string|null, count: number, sort: number}>>}
*/
return async (googleAuthId, sku, tx) => {
const run = async (runner) => {
const { articleId, languageId } = await resolve(googleAuthId, sku, runner);
const artRes = await runner.query(
'SELECT articlelist_id FROM article WHERE id = @articleId',
{ articleId },
);
const articlelistId = artRes.rows[0]?.articlelist_id;
if (!articlelistId) {
return [];
}
const itemsRes = await runner.query(queries.GET_ARTICLELIST_ITEMS, {
articlelistId,
languageId,
});
return itemsRes.rows.map((row) => ({
id: row.id,
articleId: row.articleId,
sku: row.sku,
name: row.name ?? null,
count: Number(row.count ?? 1),
sort: Number(row.sort ?? 0),
}));
};
if (tx) {
return run(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(run);
}
return run(makeRunner(provider));
};
}

View File

@@ -0,0 +1,148 @@
import * as mssqlQueries from '../../providers/mssql/queries/articlelist.js';
import * as sqliteQueries from '../../providers/sqlite3/queries/articlelist.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import getItemsByArticleSku from './getItemsByArticleSku.js';
import makeRunner from '../runner.js';
export default function setItemsByArticleSku(provider) {
const resolve = resolveArticleBySku(provider);
const getItems = getItemsByArticleSku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const queries = isSqlite ? sqliteQueries : mssqlQueries;
/**
* Sets or clears the Stückliste (bundle items) for an article.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The master article SKU.
* @param {Array<{articleId?: number, sku?: string, count?: number, sort?: number}>} [items=[]] - Array of component items.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, articleId: number, sku: string, name: string|null, count: number, sort: number}>>}
*/
return async (googleAuthId, sku, items = [], tx) => {
if (!Array.isArray(items)) {
throw new Error('Invalid items array');
}
const run = async (runner) => {
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
const artRes = await runner.query(
'SELECT id, is_father, articlelist_id, optionslist_id FROM article WHERE id = @articleId',
{ articleId },
);
const article = artRes.rows[0];
if (!article) {
throw new Error('Article not found');
}
if (article.is_father) {
throw new Error('A father article cannot be a Stückliste');
}
if (article.optionslist_id != null) {
throw new Error('An article with options cannot be a Stückliste');
}
// If items is empty: clear Stückliste
if (items.length === 0) {
if (article.articlelist_id != null) {
await runner.query(queries.SET_ARTICLE_ARTICLELIST_ID, {
articleId,
articlelistId: null,
});
await runner.query(queries.DELETE_ARTICLELIST, {
id: article.articlelist_id,
});
}
return [];
}
// Validate items
const validated = [];
const seenArticleIds = new Set();
const seenSorts = new Set();
for (let i = 0; i < items.length; i++) {
const item = items[i];
let itemArticleId = item?.articleId != null ? Number(item.articleId) : null;
if (itemArticleId == null && item?.sku) {
const res = await runner.query(
'SELECT id FROM article WHERE sku = @itemSku AND company_id = @companyId',
{ itemSku: item.sku, companyId },
);
itemArticleId = res.rows[0]?.id;
}
if (!itemArticleId || Number.isNaN(itemArticleId) || itemArticleId <= 0) {
throw new Error(`Item at index ${i} must specify a valid articleId or sku`);
}
if (itemArticleId === articleId) {
throw new Error('A Stückliste article cannot contain itself');
}
if (seenArticleIds.has(itemArticleId)) {
throw new Error(`Duplicate article in Stückliste: ${itemArticleId}`);
}
seenArticleIds.add(itemArticleId);
// Verify component article exists in same company
const itemArtRes = await runner.query(
'SELECT id FROM article WHERE id = @itemArticleId AND company_id = @companyId',
{ itemArticleId, companyId },
);
if (!itemArtRes.rows[0]) {
throw new Error(`Article not found for component: ${itemArticleId}`);
}
const count = item?.count != null && Number(item.count) > 0 ? Number(item.count) : 1;
let sort = item?.sort != null && Number.isInteger(Number(item.sort)) ? Number(item.sort) : i;
while (seenSorts.has(sort)) {
sort++;
}
seenSorts.add(sort);
validated.push({
articleId: itemArticleId,
count,
sort,
});
}
let targetListId = article.articlelist_id;
if (targetListId == null) {
const listRes = await runner.query(queries.CREATE_ARTICLELIST);
targetListId = listRes.rows[0]?.id ?? listRes.insertId;
await runner.query(queries.SET_ARTICLE_ARTICLELIST_ID, {
articleId,
articlelistId: targetListId,
});
} else {
await runner.query(queries.DELETE_ARTICLELIST_ARTICLES, {
articlelistId: targetListId,
});
}
for (const val of validated) {
await runner.query(queries.INSERT_ARTICLELIST_ARTICLE, {
articlelistId: targetListId,
articleId: val.articleId,
count: val.count,
sort: val.sort,
});
}
return getItems(googleAuthId, sku, runner);
};
if (tx) {
return run(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(run);
}
return run(makeRunner(provider));
};
}