153 lines
5.2 KiB
JavaScript
153 lines
5.2 KiB
JavaScript
import * as mssqlQueries from '../../providers/mssql/queries/optionslist.js';
|
|
import * as sqliteQueries from '../../providers/sqlite3/queries/optionslist.js';
|
|
import resolveArticleBySku from '../article/resolveArticleBySku.js';
|
|
import getOptionsByArticleSku from './getOptionsByArticleSku.js';
|
|
import makeRunner from '../runner.js';
|
|
|
|
export default function setOptionsByArticleSku(provider) {
|
|
const resolve = resolveArticleBySku(provider);
|
|
const getOptions = getOptionsByArticleSku(provider);
|
|
const isSqlite = provider?.dialect === 'sqlite3';
|
|
const queries = isSqlite ? sqliteQueries : mssqlQueries;
|
|
|
|
/**
|
|
* Sets or clears the options list 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, mandated?: boolean}>} [options=[]] - Array of options items.
|
|
* @param {object} [tx] - Optional transaction.
|
|
* @returns {Promise<Array<{id: number, articleId: number, sku: string, name: string|null, count: number, sort: number, mandated: boolean}>>}
|
|
*/
|
|
return async (googleAuthId, sku, options = [], tx) => {
|
|
if (!Array.isArray(options)) {
|
|
throw new Error('Invalid options 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 have options');
|
|
}
|
|
if (article.articlelist_id != null) {
|
|
throw new Error('A Stückliste article cannot have options');
|
|
}
|
|
|
|
// If options is empty: clear optionslist
|
|
if (options.length === 0) {
|
|
if (article.optionslist_id != null) {
|
|
await runner.query(queries.SET_ARTICLE_OPTIONSLIST_ID, {
|
|
articleId,
|
|
optionslistId: null,
|
|
});
|
|
await runner.query(queries.DELETE_OPTIONSLIST, {
|
|
id: article.optionslist_id,
|
|
});
|
|
}
|
|
return [];
|
|
}
|
|
|
|
// Validate options
|
|
const validated = [];
|
|
const seenArticleIds = new Set();
|
|
const seenSorts = new Set();
|
|
|
|
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 (!itemArticleId || Number.isNaN(itemArticleId) || itemArticleId <= 0) {
|
|
throw new Error(`Option at index ${i} must specify a valid articleId or sku`);
|
|
}
|
|
|
|
if (itemArticleId === articleId) {
|
|
throw new Error('An article cannot have itself as an option');
|
|
}
|
|
|
|
if (seenArticleIds.has(itemArticleId)) {
|
|
throw new Error(`Duplicate article in options: ${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 option: ${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);
|
|
|
|
const mandated = Boolean(item?.mandated);
|
|
|
|
validated.push({
|
|
articleId: itemArticleId,
|
|
count,
|
|
sort,
|
|
mandated,
|
|
});
|
|
}
|
|
|
|
let targetListId = article.optionslist_id;
|
|
if (targetListId == null) {
|
|
const listRes = await runner.query(queries.CREATE_OPTIONSLIST);
|
|
targetListId = listRes.rows[0]?.id ?? listRes.insertId;
|
|
await runner.query(queries.SET_ARTICLE_OPTIONSLIST_ID, {
|
|
articleId,
|
|
optionslistId: targetListId,
|
|
});
|
|
} else {
|
|
await runner.query(queries.DELETE_OPTIONSLIST_ARTICLES, {
|
|
optionslistId: targetListId,
|
|
});
|
|
}
|
|
|
|
for (const val of validated) {
|
|
await runner.query(queries.INSERT_OPTIONSLIST_ARTICLE, {
|
|
optionslistId: targetListId,
|
|
articleId: val.articleId,
|
|
count: val.count,
|
|
sort: val.sort,
|
|
mandated: val.mandated ? 1 : 0,
|
|
});
|
|
}
|
|
|
|
return getOptions(googleAuthId, sku, runner);
|
|
};
|
|
|
|
if (tx) {
|
|
return run(makeRunner(provider, tx));
|
|
}
|
|
if (typeof provider?.transaction === 'function') {
|
|
return provider.transaction(run);
|
|
}
|
|
return run(makeRunner(provider));
|
|
};
|
|
}
|