This commit is contained in:
seb
2026-10-04 21:22:05 +02:00
parent 0268583ae3
commit 4718cb5726
48 changed files with 758 additions and 758 deletions

View File

@@ -10,7 +10,7 @@ import resolveArticleBySku from './resolveArticleBySku.js';
import getBySku from './getBySku.js';
import makeRunner from '../runner.js';
export default function toggleFather(provider) {
export default function toggleParent(provider) {
const resolve = resolveArticleBySku(provider);
const load = getBySku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
@@ -21,39 +21,39 @@ export default function toggleFather(provider) {
const insertHistorySql = isSqlite ? SQLITE_INSERT_HISTORY : MSSQL_INSERT_HISTORY;
/**
* Toggles an article into or out of father mode.
* Toggles an article into or out of parent mode.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {object} options
* @param {boolean} options.isFather - Whether the article should be a father article.
* @param {number} [options.attributeNameId] - The attribute name id required when enabling father mode.
* @param {number} [options.fatherAttributeNameId] - Alias for attributeNameId.
* @param {boolean} options.isParent - Whether the article should be a parent article.
* @param {number} [options.attributeNameId] - The attribute name id required when enabling parent mode.
* @param {number} [options.parentAttributeNameId] - Alias for attributeNameId.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<object>} The updated article detail.
*/
return async (googleAuthId, sku, { isFather, attributeNameId, fatherAttributeNameId } = {}, tx) => {
return async (googleAuthId, sku, { isParent, attributeNameId, parentAttributeNameId } = {}, tx) => {
const run = async (runner) => {
const { articleId, companyId, companyuserId } = await resolve(googleAuthId, sku, runner);
const targetIsFather = Boolean(isFather);
const targetAttrId = attributeNameId ?? fatherAttributeNameId;
const targetIsParent = Boolean(isParent);
const targetAttrId = attributeNameId ?? parentAttributeNameId;
if (targetIsFather) {
if (targetIsParent) {
if (!targetAttrId || Number(targetAttrId) <= 0) {
throw new Error('attributeNameId is required to become a father article');
throw new Error('attributeNameId is required to become a parent article');
}
const currentRes = await runner.query(
'SELECT fatherarticle_id, articlelist_id, optionslist_id FROM article WHERE id = @articleId',
'SELECT parentarticle_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');
throw new Error('A Stückliste article cannot become a parent');
}
if (currentArt?.optionslist_id != null) {
throw new Error('An article with options cannot become a father');
throw new Error('An article with options cannot become a parent');
}
const stockRes = await runner.query(
@@ -70,7 +70,7 @@ export default function toggleFather(provider) {
);
const totalStock = Number(stockRes.rows[0]?.totalStock ?? 0);
if (totalStock !== 0) {
throw new Error('An article with existing stock cannot become a father article');
throw new Error('An article with existing stock cannot become a parent article');
}
const supRes = await runner.query(
@@ -78,7 +78,7 @@ export default function toggleFather(provider) {
{ articleId },
);
if ((supRes.rows[0]?.count ?? 0) > 0) {
throw new Error('An article with assigned suppliers cannot become a father article');
throw new Error('An article with assigned suppliers cannot become a parent article');
}
// Fetch existing prices before deletion to record in articlehistory
@@ -96,22 +96,22 @@ export default function toggleFather(provider) {
await runner.query('DELETE FROM price WHERE article_id = @articleId', { articleId });
await runner.query('UPDATE article SET track_stock = 0 WHERE id = @articleId', { articleId });
// If article is a child, verify attribute is not used by father or any father up the chain
let currentFatherId = currentArt?.fatherarticle_id;
// If article is a child, verify attribute is not used by parent or any parent up the chain
let currentParentId = currentArt?.parentarticle_id;
const visited = new Set();
while (currentFatherId != null && !visited.has(currentFatherId)) {
visited.add(currentFatherId);
while (currentParentId != null && !visited.has(currentParentId)) {
visited.add(currentParentId);
const fRes = await runner.query(
'SELECT a.fatherarticle_id, a.fatherattributename_id, an.name AS fatherAttributeName FROM article a LEFT JOIN attributename an ON an.id = a.fatherattributename_id WHERE a.id = @currentFatherId',
{ currentFatherId },
'SELECT a.parentarticle_id, a.parentattributename_id, an.name AS parentAttributeName FROM article a LEFT JOIN attributename an ON an.id = a.parentattributename_id WHERE a.id = @currentParentId',
{ currentParentId },
);
const fRow = fRes.rows[0];
if (!fRow) break;
if (fRow.fatherattributename_id != null && Number(fRow.fatherattributename_id) === Number(targetAttrId)) {
const attrName = fRow.fatherAttributeName || 'ancestor';
throw new Error(`Attribute "${attrName}" is already used by an ancestor father article`);
if (fRow.parentattributename_id != null && Number(fRow.parentattributename_id) === Number(targetAttrId)) {
const attrName = fRow.parentAttributeName || 'ancestor';
throw new Error(`Attribute "${attrName}" is already used by an ancestor parent article`);
}
currentFatherId = fRow.fatherarticle_id;
currentParentId = fRow.parentarticle_id;
}
// Verify attribute exists and belongs to company
@@ -132,16 +132,16 @@ export default function toggleFather(provider) {
{ articleId, attributeId: Number(targetAttrId) },
);
if (existingValRes.rows.length > 0) {
throw new Error('A father article cannot have attribute values for its variant attribute');
throw new Error('A parent article cannot have attribute values for its variant attribute');
}
await runner.query(queries.TOGGLE_FATHER, {
await runner.query(queries.TOGGLE_PARENT, {
articleId,
isFather: 1,
fatherAttributeNameId: Number(targetAttrId),
isParent: 1,
parentAttributeNameId: Number(targetAttrId),
});
// Add history entry recording father conversion and deleted prices
// Add history entry recording parent conversion and deleted prices
const historyLines = [
`Artikel zu Vaterartikel konvertiert (Varianten-Attribut: "${attributeName}")`,
];
@@ -170,17 +170,17 @@ export default function toggleFather(provider) {
text: historyLines.join('\n'),
});
} else {
// Disabling father: verify zero children
// Disabling parent: verify zero children
const childCountRes = await runner.query(getChildCountSql, { articleId });
const childCount = childCountRes.rows[0]?.count ?? 0;
if (childCount > 0) {
throw new Error('Cannot disable father mode on an article that still has children');
throw new Error('Cannot disable parent mode on an article that still has children');
}
await runner.query(queries.TOGGLE_FATHER, {
await runner.query(queries.TOGGLE_PARENT, {
articleId,
isFather: 0,
fatherAttributeNameId: null,
isParent: 0,
parentAttributeNameId: null,
});
await runner.query(insertHistorySql, {