This commit is contained in:
seb
2026-10-01 17:40:04 +02:00
parent 62c3e69ed4
commit 84e547660e
53 changed files with 4604 additions and 2059 deletions

View File

@@ -28,7 +28,7 @@ export default function removeChild(provider) {
*/
return async (googleAuthId, fatherSku, childSku, options = {}, tx) => {
const run = async (runner) => {
const { articleId: fatherId, companyuserId } = await resolve(
const { articleId: fatherId, languageId, companyuserId } = await resolve(
googleAuthId,
fatherSku,
runner,
@@ -45,9 +45,45 @@ export default function removeChild(provider) {
throw new Error('Child article does not belong to this father');
}
// Fetch child name before any deletion (needed for history regardless of path)
const childNameRes = await runner.query(
`SELECT at.name FROM articletext at
INNER JOIN article a ON a.id = at.article_id
WHERE a.id = @childId AND at.language_id = @languageId`,
{ childId, languageId },
);
const childName = childNameRes.rows[0]?.name || childSku;
const recompactFatherSort = async () => {
const remainingChildrenRes = await runner.query(
'SELECT id, fathersort FROM article WHERE fatherarticle_id = @fatherId ORDER BY fathersort ASC, id ASC',
{ fatherId },
);
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, {
articleId: childRow.id,
fatherArticleId: fatherId,
fathersort: i,
});
}
}
};
if (options.deleteArticle) {
const cascade = options.cascade !== undefined ? Boolean(options.cascade) : true;
await del(googleAuthId, childSku, { cascade }, runner);
await recompactFatherSort();
// History on father (child article is gone so we can only log on father)
await runner.query(insertHistorySql, {
articleId: fatherId,
companyuserId,
text: `Variante "${childName}" (${childSku}) gelöscht`,
});
return {
success: true,
fatherSku,
@@ -81,13 +117,22 @@ export default function removeChild(provider) {
);
}
// Add history
await recompactFatherSort();
// Add history on child
await runner.query(insertHistorySql, {
articleId: childId,
companyuserId,
text: `Artikel "${childSku}" von Vaterartikel "${fatherSku}" getrennt`,
});
// Add history on father
await runner.query(insertHistorySql, {
articleId: fatherId,
companyuserId,
text: `Variante "${childName}" (${childSku}) getrennt`,
});
return {
success: true,
fatherSku,