u
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import * as mssqlQueries from '../../providers/mssql/queries/articleDelete.js';
|
||||
import * as sqliteQueries from '../../providers/sqlite3/queries/articleDelete.js';
|
||||
import * as mssqlArticle from '../../providers/mssql/queries/article.js';
|
||||
import * as sqliteArticle from '../../providers/sqlite3/queries/article.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
@@ -7,6 +9,7 @@ export default function del(provider) {
|
||||
const resolve = resolveArticleBySku(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const queries = isSqlite ? sqliteQueries : mssqlQueries;
|
||||
const articleQueries = isSqlite ? sqliteArticle : mssqlArticle;
|
||||
|
||||
/**
|
||||
* Deletes an article by its SKU, refusing when it still has children unless cascade is true.
|
||||
@@ -40,6 +43,8 @@ export default function del(provider) {
|
||||
const listIds = await runner.query(queries.GET_LIST_IDS, { articleId: id });
|
||||
const { articlelistId, optionslistId } = listIds.rows[0] ?? {};
|
||||
|
||||
await runner.query(queries.CREATE_TOMBSTONE, { articleId: id });
|
||||
|
||||
await runner.query(queries.DELETE_PICS_BY_ARTICLE, { articleId: id });
|
||||
|
||||
const deleted = await runner.query(queries.DELETE_BY_ID, { articleId: id });
|
||||
@@ -64,16 +69,38 @@ export default function del(provider) {
|
||||
await deleteArticleAndLists(id);
|
||||
};
|
||||
|
||||
const fatherRes = await runner.query(
|
||||
'SELECT fatherarticle_id FROM article WHERE id = @articleId',
|
||||
{ articleId },
|
||||
);
|
||||
const fatherArticleId = fatherRes.rows[0]?.fatherarticle_id;
|
||||
|
||||
const childCount = await runner.query(queries.GET_CHILD_COUNT, { articleId });
|
||||
if (childCount.rows[0].count > 0) {
|
||||
if (!cascade) {
|
||||
throw new Error('Article has children');
|
||||
}
|
||||
await deleteSubtree(articleId);
|
||||
return;
|
||||
} else {
|
||||
await deleteArticleAndLists(articleId);
|
||||
}
|
||||
|
||||
await deleteArticleAndLists(articleId);
|
||||
if (fatherArticleId != null) {
|
||||
const remainingChildrenRes = await runner.query(
|
||||
'SELECT id, fathersort FROM article WHERE fatherarticle_id = @fatherArticleId ORDER BY fathersort ASC, id ASC',
|
||||
{ fatherArticleId },
|
||||
);
|
||||
for (let i = 0; i < remainingChildrenRes.rows.length; i++) {
|
||||
const childRow = remainingChildrenRes.rows[i];
|
||||
if (Number(childRow.fathersort) !== i) {
|
||||
await runner.query(articleQueries.SET_FATHER_SORT, {
|
||||
articleId: childRow.id,
|
||||
fatherArticleId,
|
||||
fathersort: i,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
|
||||
Reference in New Issue
Block a user