This commit is contained in:
seb
2026-09-30 23:11:04 +02:00
parent 74ed74fc68
commit 9c770d765e
8 changed files with 202 additions and 56 deletions

View File

@@ -94,17 +94,42 @@ export default function setItemsByArticleSku(provider) {
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 (!item || typeof item !== 'object') {
throw new Error(`Item at index ${i} must be an object`);
}
if (!itemArticleId || Number.isNaN(itemArticleId) || itemArticleId <= 0) {
let itemArticleId = null;
if (item.articleId !== undefined && item.articleId !== null) {
if (
typeof item.articleId !== 'number' ||
!Number.isInteger(item.articleId) ||
item.articleId <= 0
) {
throw new Error(`Item at index ${i} must specify a valid articleId or sku`);
}
const res = await runner.query(
'SELECT id FROM article WHERE id = @itemArticleId AND company_id = @companyId',
{ itemArticleId: item.articleId, companyId },
);
if (!res.rows[0]) {
throw new Error(`Article not found for component: ${item.articleId}`);
}
itemArticleId = res.rows[0].id;
} else if (item.sku !== undefined && item.sku !== null) {
if (typeof item.sku !== 'string' || !item.sku.trim()) {
throw new Error(`Item at index ${i} must specify a valid articleId or sku`);
}
const itemSku = item.sku.trim();
const res = await runner.query(
'SELECT id FROM article WHERE sku = @itemSku AND company_id = @companyId',
{ itemSku, companyId },
);
if (!res.rows[0]) {
throw new Error(`Article not found for component: ${itemSku}`);
}
itemArticleId = res.rows[0].id;
} else {
throw new Error(`Item at index ${i} must specify a valid articleId or sku`);
}
@@ -117,16 +142,7 @@ export default function setItemsByArticleSku(provider) {
}
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 rawCount = item?.count;
const rawCount = item?.count === undefined ? 1 : item.count;
if (
typeof rawCount !== 'number' ||
!Number.isInteger(rawCount) ||
@@ -136,7 +152,13 @@ export default function setItemsByArticleSku(provider) {
throw new Error(`Invalid count for Stückliste item at index ${i}: must be a positive integer`);
}
const count = rawCount;
let sort = item?.sort != null && Number.isInteger(Number(item.sort)) ? Number(item.sort) : i;
if (item.sort !== undefined && item.sort !== null) {
if (typeof item.sort !== 'number' || !Number.isInteger(item.sort)) {
throw new Error(`Invalid sort for Stückliste item at index ${i}: must be an integer`);
}
}
let sort = (typeof item?.sort === 'number' && Number.isInteger(item.sort)) ? item.sort : i;
while (seenSorts.has(sort)) {
sort++;