u
This commit is contained in:
@@ -97,7 +97,26 @@ export default function getBySku(provider) {
|
||||
stock: Number(w.stock ?? 0),
|
||||
})),
|
||||
optionsCount: a.optionsCount,
|
||||
optionslistId: a.optionslistId ?? null,
|
||||
options: parseJsonArray(a.optionslistItemsJson).map((o) => ({
|
||||
id: o.id,
|
||||
articleId: o.articleId,
|
||||
sku: o.sku,
|
||||
name: o.name ?? null,
|
||||
count: Number(o.count ?? 1),
|
||||
sort: Number(o.sort ?? 0),
|
||||
mandated: Boolean(o.mandated),
|
||||
})),
|
||||
articleListCount: a.articleListCount,
|
||||
articlelistId: a.articlelistId ?? null,
|
||||
articlelist: parseJsonArray(a.articlelistItemsJson).map((item) => ({
|
||||
id: item.id,
|
||||
articleId: item.articleId,
|
||||
sku: item.sku,
|
||||
name: item.name ?? null,
|
||||
count: Number(item.count ?? 1),
|
||||
sort: Number(item.sort ?? 0),
|
||||
})),
|
||||
childCount: a.childCount,
|
||||
isFather: Boolean(a.isFather),
|
||||
fatherAttributeNameId: a.fatherAttributeNameId ?? null,
|
||||
|
||||
@@ -42,12 +42,19 @@ export default function toggleFather(provider) {
|
||||
}
|
||||
|
||||
const currentRes = await runner.query(
|
||||
'SELECT fatherarticle_id FROM article WHERE id = @articleId',
|
||||
'SELECT fatherarticle_id, articlelist_id, optionslist_id FROM article WHERE id = @articleId',
|
||||
{ articleId },
|
||||
);
|
||||
const currentArt = currentRes.rows[0];
|
||||
if (currentArt?.articlelist_id != null) {
|
||||
throw new Error('A Stückliste article cannot become a father');
|
||||
}
|
||||
if (currentArt?.optionslist_id != null) {
|
||||
throw new Error('An article with options cannot become a father');
|
||||
}
|
||||
|
||||
// If article is a child, verify attribute is not used by father or any father up the chain
|
||||
let currentFatherId = currentRes.rows[0]?.fatherarticle_id;
|
||||
let currentFatherId = currentArt?.fatherarticle_id;
|
||||
const visited = new Set();
|
||||
while (currentFatherId != null && !visited.has(currentFatherId)) {
|
||||
visited.add(currentFatherId);
|
||||
|
||||
55
src/store/articlelist/getItemsByArticleSku.js
Normal file
55
src/store/articlelist/getItemsByArticleSku.js
Normal 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));
|
||||
};
|
||||
}
|
||||
148
src/store/articlelist/setItemsByArticleSku.js
Normal file
148
src/store/articlelist/setItemsByArticleSku.js
Normal 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));
|
||||
};
|
||||
}
|
||||
56
src/store/optionslist/getOptionsByArticleSku.js
Normal file
56
src/store/optionslist/getOptionsByArticleSku.js
Normal file
@@ -0,0 +1,56 @@
|
||||
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 makeRunner from '../runner.js';
|
||||
|
||||
export default function getOptionsByArticleSku(provider) {
|
||||
const resolve = resolveArticleBySku(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const queries = isSqlite ? sqliteQueries : mssqlQueries;
|
||||
|
||||
/**
|
||||
* Returns the options list 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, mandated: boolean}>>}
|
||||
*/
|
||||
return async (googleAuthId, sku, tx) => {
|
||||
const run = async (runner) => {
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku, runner);
|
||||
|
||||
const artRes = await runner.query(
|
||||
'SELECT optionslist_id FROM article WHERE id = @articleId',
|
||||
{ articleId },
|
||||
);
|
||||
const optionslistId = artRes.rows[0]?.optionslist_id;
|
||||
if (!optionslistId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const itemsRes = await runner.query(queries.GET_OPTIONSLIST_ITEMS, {
|
||||
optionslistId,
|
||||
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),
|
||||
mandated: Boolean(row.mandated),
|
||||
}));
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return run(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(run);
|
||||
}
|
||||
return run(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
152
src/store/optionslist/setOptionsByArticleSku.js
Normal file
152
src/store/optionslist/setOptionsByArticleSku.js
Normal file
@@ -0,0 +1,152 @@
|
||||
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));
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user