Files
dashlibstorage/src/store/articlesupplier/delete.js
seb 53a20c4353 refactor: introduce database provider abstraction, isolate MSSQL layer
- Add provider contract (src/providers/provider.js): query() -> {rows,
  rowsAffected, insertId}, callback transaction(fn), exec() for stored
  procs, initialize/purge lifecycle
- Add MssqlProvider (src/providers/mssql/index.js) wrapping an mssql
  ConnectionPool with normalized results, tx commit/rollback and
  constraint-error classifiers (unique 2627/2601, FK 547)
- LibDashStore constructor now takes a provider instead of a pool
  (breaking); adds LibDashStore.create() async factory; keeps pool as
  transitional bridge until store modules are converted (see TODO.md)
- Move MSSQL-specific code into the provider: src/sql/* ->
  src/providers/mssql/queries/*, src/schema.js ->
  src/providers/mssql/schema.js, purge SQL -> purgeSql.js
- Update scripts, generate-schema-graph.js and package.json exports
  (./providers/mssql, ./providers/provider)
- Add TODO.md with the remaining provider/SQLite migration plan

No SQL or behavioral changes.
2026-09-20 13:51:01 +02:00

70 lines
1.8 KiB
JavaScript

import {
DELETE_BY_ID_AND_ARTICLE_ID,
DELETE_PRICE,
GET_BY_ID_AND_ARTICLE_ID,
} from '../../providers/mssql/queries/articlesupplier.js';
import request from '../request.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
export default function del(pool) {
const resolve = resolveArticleBySku(pool);
/**
* Deletes a supplier assignment and cleans up its associated purchase price.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {number} id - The id of the articlesupplier record.
* @returns {Promise<void>}
* @throws {Error} If the supplier assignment is not found.
*
* @example
* await store.articlesupplier.delete(42, 'SKU-1', 12);
*/
return async (googleAuthId, sku, id) => {
if (id == null || !Number.isFinite(id)) {
throw new Error('Supplier assignment not found');
}
const { articleId, companyId } = await resolve(googleAuthId, sku);
const existingRes = await pool
.request()
.input('id', id)
.input('articleId', articleId)
.input('companyId', companyId)
.query(GET_BY_ID_AND_ARTICLE_ID);
const existing = existingRes.recordset[0];
if (!existing) {
throw new Error('Supplier assignment not found');
}
const tx = pool.transaction();
await tx.begin();
try {
await request(pool, tx)
.input('id', id)
.input('articleId', articleId)
.query(DELETE_BY_ID_AND_ARTICLE_ID);
if (existing.priceId != null) {
await request(pool, tx)
.input('id', existing.priceId)
.input('articleId', articleId)
.query(DELETE_PRICE);
}
await tx.commit();
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
}
};
}