u
This commit is contained in:
@@ -15,34 +15,34 @@ export default function removeChild(provider) {
|
||||
const insertHistorySql = isSqlite ? SQLITE_INSERT_HISTORY : MSSQL_INSERT_HISTORY;
|
||||
|
||||
/**
|
||||
* Removes a child article from a father article, either by unlinking or deleting it.
|
||||
* Removes a child article from a parent article, either by unlinking or deleting it.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} fatherSku - The father article SKU.
|
||||
* @param {string} parentSku - The parent article SKU.
|
||||
* @param {string} childSku - The child article SKU.
|
||||
* @param {object} [options]
|
||||
* @param {boolean} [options.deleteArticle=false] - Whether to delete the child article entity entirely.
|
||||
* @param {boolean} [options.cascade=true] - Whether to cascade delete child subtrees when deleteArticle is true.
|
||||
* @param {object} [tx] - Optional transaction.
|
||||
* @returns {Promise<{success: boolean, fatherSku: string, childSku: string, deleted: boolean}>}
|
||||
* @returns {Promise<{success: boolean, parentSku: string, childSku: string, deleted: boolean}>}
|
||||
*/
|
||||
return async (googleAuthId, fatherSku, childSku, options = {}, tx) => {
|
||||
return async (googleAuthId, parentSku, childSku, options = {}, tx) => {
|
||||
const run = async (runner) => {
|
||||
const { articleId: fatherId, languageId, companyuserId } = await resolve(
|
||||
const { articleId: parentId, languageId, companyuserId } = await resolve(
|
||||
googleAuthId,
|
||||
fatherSku,
|
||||
parentSku,
|
||||
runner,
|
||||
);
|
||||
const { articleId: childId } = await resolve(googleAuthId, childSku, runner);
|
||||
|
||||
// Verify child relationship
|
||||
const childRes = await runner.query(
|
||||
'SELECT fatherarticle_id FROM article WHERE id = @childId',
|
||||
'SELECT parentarticle_id FROM article WHERE id = @childId',
|
||||
{ childId },
|
||||
);
|
||||
const child = childRes.rows[0];
|
||||
if (!child || child.fatherarticle_id !== fatherId) {
|
||||
throw new Error('Child article does not belong to this father');
|
||||
if (!child || child.parentarticle_id !== parentId) {
|
||||
throw new Error('Child article does not belong to this parent');
|
||||
}
|
||||
|
||||
// Fetch child name before any deletion (needed for history regardless of path)
|
||||
@@ -54,18 +54,18 @@ export default function removeChild(provider) {
|
||||
);
|
||||
const childName = childNameRes.rows[0]?.name || childSku;
|
||||
|
||||
const recompactFatherSort = async () => {
|
||||
const recompactParentSort = async () => {
|
||||
const remainingChildrenRes = await runner.query(
|
||||
'SELECT id, fathersort FROM article WHERE fatherarticle_id = @fatherId ORDER BY fathersort ASC, id ASC',
|
||||
{ fatherId },
|
||||
'SELECT id, parentsort FROM article WHERE parentarticle_id = @parentId ORDER BY parentsort ASC, id ASC',
|
||||
{ parentId },
|
||||
);
|
||||
for (let i = 0; i < remainingChildrenRes.rows.length; i++) {
|
||||
const childRow = remainingChildrenRes.rows[i];
|
||||
if (Number(childRow.fathersort) !== i) {
|
||||
await runner.query(queries.SET_FATHER_SORT, {
|
||||
if (Number(childRow.parentsort) !== i) {
|
||||
await runner.query(queries.SET_PARENT_SORT, {
|
||||
articleId: childRow.id,
|
||||
fatherArticleId: fatherId,
|
||||
fathersort: i,
|
||||
parentArticleId: parentId,
|
||||
parentsort: i,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -75,67 +75,67 @@ export default function removeChild(provider) {
|
||||
const cascade = options.cascade !== undefined ? Boolean(options.cascade) : true;
|
||||
await del(googleAuthId, childSku, { cascade }, runner);
|
||||
|
||||
await recompactFatherSort();
|
||||
await recompactParentSort();
|
||||
|
||||
// History on father (child article is gone so we can only log on father)
|
||||
// History on parent (child article is gone so we can only log on parent)
|
||||
await runner.query(insertHistorySql, {
|
||||
articleId: fatherId,
|
||||
articleId: parentId,
|
||||
companyuserId,
|
||||
text: `Variante "${childName}" (${childSku}) gelöscht`,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
fatherSku,
|
||||
parentSku,
|
||||
childSku,
|
||||
deleted: true,
|
||||
};
|
||||
}
|
||||
|
||||
// Fetch father's attribute name id
|
||||
const fatherRes = await runner.query(
|
||||
'SELECT fatherattributename_id FROM article WHERE id = @fatherId',
|
||||
{ fatherId },
|
||||
// Fetch parent's attribute name id
|
||||
const parentRes = await runner.query(
|
||||
'SELECT parentattributename_id FROM article WHERE id = @parentId',
|
||||
{ parentId },
|
||||
);
|
||||
const fatherAttributeNameId = fatherRes.rows[0]?.fatherattributename_id;
|
||||
const parentAttributeNameId = parentRes.rows[0]?.parentattributename_id;
|
||||
|
||||
// Unlink child from father
|
||||
// Unlink child from parent
|
||||
await runner.query(queries.UNLINK_CHILD, {
|
||||
articleId: childId,
|
||||
fatherArticleId: fatherId,
|
||||
parentArticleId: parentId,
|
||||
});
|
||||
|
||||
// Remove the child's attribute value link for the father's attribute
|
||||
if (fatherAttributeNameId) {
|
||||
// Remove the child's attribute value link for the parent's attribute
|
||||
if (parentAttributeNameId) {
|
||||
await runner.query(
|
||||
`DELETE FROM articleattributevalue
|
||||
WHERE article_id = @childId
|
||||
AND attributevalue_id IN (
|
||||
SELECT id FROM attributevalue WHERE attribute_id = @fatherAttributeNameId
|
||||
SELECT id FROM attributevalue WHERE attribute_id = @parentAttributeNameId
|
||||
)`,
|
||||
{ childId, fatherAttributeNameId },
|
||||
{ childId, parentAttributeNameId },
|
||||
);
|
||||
}
|
||||
|
||||
await recompactFatherSort();
|
||||
await recompactParentSort();
|
||||
|
||||
// Add history on child
|
||||
await runner.query(insertHistorySql, {
|
||||
articleId: childId,
|
||||
companyuserId,
|
||||
text: `Artikel "${childSku}" von Vaterartikel "${fatherSku}" getrennt`,
|
||||
text: `Artikel "${childSku}" von Vaterartikel "${parentSku}" getrennt`,
|
||||
});
|
||||
|
||||
// Add history on father
|
||||
// Add history on parent
|
||||
await runner.query(insertHistorySql, {
|
||||
articleId: fatherId,
|
||||
articleId: parentId,
|
||||
companyuserId,
|
||||
text: `Variante "${childName}" (${childSku}) getrennt`,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
fatherSku,
|
||||
parentSku,
|
||||
childSku,
|
||||
deleted: false,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user