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,13 +1,12 @@
import {
LIST_IDS_BY_ARTICLE,
SET_TEMP_SORTS,
UPDATE_SORT,
} from '../../providers/mssql/queries/articlesupplier.js';
import request from '../request.js';
import * as mssqlQueries from '../../providers/mssql/queries/articlesupplier.js';
import * as sqliteQueries from '../../providers/sqlite3/queries/articlesupplier.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import makeRunner from '../runner.js';
export default function order(pool) {
const resolve = resolveArticleBySku(pool);
export default function order(provider) {
const resolve = resolveArticleBySku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const queries = isSqlite ? sqliteQueries : mssqlQueries;
/**
* Reorders the supplier assignments of an article by its SKU.
@@ -16,6 +15,7 @@ export default function order(pool) {
* @param {string} sku - The article SKU.
* @param {object} input - The reorder input.
* @param {Array<number>} input.ids - The articlesupplier ids in their new order.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{ids: Array<number>}>} The applied order.
* @throws {Error} If the order is invalid.
*
@@ -23,62 +23,58 @@ export default function order(pool) {
* const result = await store.articlesupplier.order(42, 'SKU-1', { ids: [12, 10, 15] });
* // => { ids: [12, 10, 15] }
*/
return async (googleAuthId, sku, { ids } = {}) => {
return async (googleAuthId, sku, { ids } = {}, tx) => {
if (!Array.isArray(ids)) {
throw new Error('Invalid supplier order');
}
const { articleId } = await resolve(googleAuthId, sku);
const runOrder = async (runner) => {
const { articleId } = await resolve(googleAuthId, sku, runner);
const current = await pool
.request()
.input('articleId', articleId)
.query(LIST_IDS_BY_ARTICLE);
const current = await runner.query(queries.LIST_IDS_BY_ARTICLE, {
articleId,
});
const existing = new Set(current.recordset.map((r) => r.id));
const existing = new Set(current.rows.map((r) => r.id));
if (ids.length !== existing.size) {
throw new Error('Invalid supplier order');
}
const seen = new Set();
for (const id of ids) {
if (!Number.isFinite(id) || !existing.has(id) || seen.has(id)) {
if (ids.length !== existing.size) {
throw new Error('Invalid supplier order');
}
seen.add(id);
}
const tx = pool.transaction();
await tx.begin();
const seen = new Set();
for (const id of ids) {
if (!Number.isFinite(id) || !existing.has(id) || seen.has(id)) {
throw new Error('Invalid supplier order');
}
seen.add(id);
}
try {
await request(pool, tx)
.input('articleId', articleId)
.query(SET_TEMP_SORTS);
await runner.query(queries.SET_TEMP_SORTS, {
articleId,
});
for (let sort = 0; sort < ids.length; sort++) {
const updateRes = await request(pool, tx)
.input('id', ids[sort])
.input('articleId', articleId)
.input('sort', sort)
.query(UPDATE_SORT);
const updateRes = await runner.query(queries.UPDATE_SORT, {
id: ids[sort],
articleId,
sort,
});
if (!updateRes.rowsAffected[0]) {
if (!updateRes.rowsAffected) {
throw new Error('Invalid supplier order');
}
}
await tx.commit();
return { ids };
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
};
if (tx) {
return runOrder(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(runOrder);
}
return runOrder(makeRunner(provider));
};
}