This commit is contained in:
seb
2026-09-22 07:25:16 +02:00
parent 4ed6260e91
commit cca131d83a
148 changed files with 5343 additions and 3466 deletions

View File

@@ -1,83 +1,60 @@
import {
DELETE_ARTICLELIST,
DELETE_BY_ID,
DELETE_OPTIONSLIST,
DELETE_PICS_BY_ARTICLE,
GET_CHILD_COUNT,
GET_LIST_IDS,
} from '../../providers/mssql/queries/articleDelete.js';
import * as mssqlQueries from '../../providers/mssql/queries/articleDelete.js';
import * as sqliteQueries from '../../providers/sqlite3/queries/articleDelete.js';
import resolveArticleBySku from './resolveArticleBySku.js';
import request from '../request.js';
import makeRunner from '../runner.js';
export default function del(pool) {
const resolve = resolveArticleBySku(pool);
export default function del(provider) {
const resolve = resolveArticleBySku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const queries = isSqlite ? sqliteQueries : mssqlQueries;
/**
* Deletes an article by its SKU, refusing when it still has children.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<void>}
* @throws {Error} If the article has children or is not found.
*
* @example
* await store.article.delete(42, 'SKU-1');
*/
return async (googleAuthId, sku) => {
const { articleId } = await resolve(googleAuthId, sku);
return async (googleAuthId, sku, tx) => {
const runDelete = async (runner) => {
const { articleId } = await resolve(googleAuthId, sku, runner);
const childCount = await pool
.request()
.input('articleId', articleId)
.query(GET_CHILD_COUNT);
const childCount = await runner.query(queries.GET_CHILD_COUNT, { articleId });
if (childCount.rows[0].count > 0) {
throw new Error('Article has children');
}
if (childCount.recordset[0].count > 0) {
throw new Error('Article has children');
}
const listIds = await runner.query(queries.GET_LIST_IDS, { articleId });
const { articlelistId, optionslistId } = listIds.rows[0] ?? {};
const listIds = await pool
.request()
.input('articleId', articleId)
.query(GET_LIST_IDS);
await runner.query(queries.DELETE_PICS_BY_ARTICLE, { articleId });
const { articlelistId, optionslistId } = listIds.recordset[0] ?? {};
const tx = pool.transaction();
await tx.begin();
try {
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]) {
const deleted = await runner.query(queries.DELETE_BY_ID, { articleId });
if (!deleted.rowsAffected) {
throw new Error('Article not found');
}
if (articlelistId != null) {
await request(pool, tx)
.input('id', articlelistId)
.query(DELETE_ARTICLELIST);
await runner.query(queries.DELETE_ARTICLELIST, { id: articlelistId });
}
if (optionslistId != null) {
await request(pool, tx)
.input('id', optionslistId)
.query(DELETE_OPTIONSLIST);
await runner.query(queries.DELETE_OPTIONSLIST, { id: optionslistId });
}
};
await tx.commit();
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
if (tx) {
return runDelete(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(runDelete);
}
return runDelete(makeRunner(provider));
};
}