u
This commit is contained in:
@@ -94,17 +94,42 @@ export default function setOptionsByArticleSku(provider) {
|
||||
|
||||
for (let i = 0; i < options.length; i++) {
|
||||
const item = options[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(`Option 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(`Option 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 option: ${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(`Option 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 option: ${itemSku}`);
|
||||
}
|
||||
itemArticleId = res.rows[0].id;
|
||||
} else {
|
||||
throw new Error(`Option at index ${i} must specify a valid articleId or sku`);
|
||||
}
|
||||
|
||||
@@ -117,16 +142,7 @@ export default function setOptionsByArticleSku(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 option: ${itemArticleId}`);
|
||||
}
|
||||
|
||||
const rawCount = item?.count;
|
||||
const rawCount = item?.count === undefined ? 1 : item.count;
|
||||
if (
|
||||
typeof rawCount !== 'number' ||
|
||||
!Number.isInteger(rawCount) ||
|
||||
@@ -136,13 +152,22 @@ export default function setOptionsByArticleSku(provider) {
|
||||
throw new Error(`Invalid count for options 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 options 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++;
|
||||
}
|
||||
seenSorts.add(sort);
|
||||
|
||||
if (item.mandated !== undefined && item.mandated !== null && typeof item.mandated !== 'boolean') {
|
||||
throw new Error(`Invalid mandated for options item at index ${i}: must be a boolean`);
|
||||
}
|
||||
const mandated = Boolean(item?.mandated);
|
||||
|
||||
validated.push({
|
||||
|
||||
Reference in New Issue
Block a user