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_HASHES_BY_ARTICLE,
SET_TEMP_SORTS,
UPDATE_SORT_BY_HASH,
} from '../../providers/mssql/queries/articlepic.js';
import * as mssqlPic from '../../providers/mssql/queries/articlepic.js';
import * as sqlitePic from '../../providers/sqlite3/queries/articlepic.js';
import resolveArticleBySku from './resolveArticleBySku.js';
import request from '../request.js';
import makeRunner from '../runner.js';
export default function orderImages(pool) {
const resolve = resolveArticleBySku(pool);
export default function orderImages(provider) {
const resolve = resolveArticleBySku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const queries = isSqlite ? sqlitePic : mssqlPic;
/**
* Reorders the images of an article by its SKU.
@@ -16,6 +15,7 @@ export default function orderImages(pool) {
* @param {string} sku - The article SKU.
* @param {object} input - The reorder input.
* @param {Array<string>} input.fullHashes - The full image hashes in their new order.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{fullHashes: Array<string>}>} The applied order.
* @throws {Error} If the order is invalid.
*
@@ -23,62 +23,53 @@ export default function orderImages(pool) {
* const result = await store.article.orderImages(42, 'SKU-1', { fullHashes: ['a', 'b'] });
* // => { fullHashes: ['a', 'b'] }
*/
return async (googleAuthId, sku, { fullHashes }) => {
return async (googleAuthId, sku, { fullHashes }, tx) => {
if (!Array.isArray(fullHashes)) {
throw new Error('Invalid image 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_HASHES_BY_ARTICLE);
const current = await runner.query(queries.LIST_HASHES_BY_ARTICLE, { articleId });
const existing = new Set(current.rows.map((r) => r.fullHash));
const existing = new Set(current.recordset.map((r) => r.fullHash));
if (fullHashes.length !== existing.size) {
throw new Error('Invalid image order');
}
const seen = new Set();
for (const fullHash of fullHashes) {
if (!existing.has(fullHash) || seen.has(fullHash)) {
if (fullHashes.length !== existing.size) {
throw new Error('Invalid image order');
}
seen.add(fullHash);
}
const tx = pool.transaction();
await tx.begin();
const seen = new Set();
for (const fullHash of fullHashes) {
if (!existing.has(fullHash) || seen.has(fullHash)) {
throw new Error('Invalid image order');
}
seen.add(fullHash);
}
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 < fullHashes.length; sort++) {
const update = await request(pool, tx)
.input('articleId', articleId)
.input('fullHash', fullHashes[sort])
.input('sort', sort)
.query(UPDATE_SORT_BY_HASH);
const update = await runner.query(queries.UPDATE_SORT_BY_HASH, {
articleId,
fullHash: fullHashes[sort],
sort,
});
if (!update.rowsAffected[0]) {
if (!update.rowsAffected) {
throw new Error('Invalid image order');
}
}
await tx.commit();
return { fullHashes };
} 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));
};
}