This commit is contained in:
seb
2026-08-01 03:37:45 +02:00
parent 69a24192d3
commit 1756dc7ef0
10 changed files with 179 additions and 13 deletions

View File

@@ -0,0 +1,88 @@
import {
DELETE_ARTICLELIST,
DELETE_BY_ID,
DELETE_OPTIONSLIST,
DELETE_PICS_BY_ARTICLE,
DETACH_MANIFEST_ARTICLES,
GET_CHILD_COUNT,
GET_LIST_IDS,
GET_NAME_FOR_DELETE,
} from '../../sql/articleDelete.js';
import resolveArticleBySku from './resolveArticleBySku.js';
import request from '../request.js';
export default function del(pool) {
const resolve = resolveArticleBySku(pool);
return async (googleAuthId, sku) => {
const { articleId, languageId } = await resolve(googleAuthId, sku);
const childCount = await pool
.request()
.input('articleId', articleId)
.query(GET_CHILD_COUNT);
if (childCount.recordset[0].count > 0) {
throw new Error('Article has children');
}
const nameResult = await pool
.request()
.input('articleId', articleId)
.input('languageId', languageId)
.query(GET_NAME_FOR_DELETE);
const name = nameResult.recordset[0]?.name ?? sku;
const suffix = `article ${name} deleted`;
const listIds = await pool
.request()
.input('articleId', articleId)
.query(GET_LIST_IDS);
const { articlelistId, optionslistId } = listIds.recordset[0] ?? {};
const tx = pool.transaction();
await tx.begin();
try {
await request(pool, tx)
.input('articleId', articleId)
.input('suffix', suffix)
.query(DETACH_MANIFEST_ARTICLES);
await request(pool, tx)
.input('articleId', articleId)
.query(DELETE_PICS_BY_ARTICLE);
const deleted = await request(pool, tx)
.input('articleId', articleId)
.query(DELETE_BY_ID);
if (!deleted.rowsAffected[0]) {
throw new Error('Article not found');
}
if (articlelistId != null) {
await request(pool, tx)
.input('id', articlelistId)
.query(DELETE_ARTICLELIST);
}
if (optionslistId != null) {
await request(pool, tx)
.input('id', optionslistId)
.query(DELETE_OPTIONSLIST);
}
await tx.commit();
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
}
};
}