This commit is contained in:
seb
2026-10-01 07:12:39 +02:00
parent 9c770d765e
commit 62c3e69ed4
11 changed files with 273 additions and 13 deletions

View File

@@ -1,5 +1,7 @@
import * as mssqlQueries from '../../providers/mssql/queries/optionslist.js';
import * as sqliteQueries from '../../providers/sqlite3/queries/optionslist.js';
import { INSERT as MSSQL_INSERT_HISTORY } from '../../providers/mssql/queries/articlehistory.js';
import { INSERT as SQLITE_INSERT_HISTORY } from '../../providers/sqlite3/queries/articlehistory.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import getOptionsByArticleSku from './getOptionsByArticleSku.js';
import makeRunner from '../runner.js';
@@ -42,6 +44,7 @@ export default function setOptionsByArticleSku(provider) {
const getOptions = getOptionsByArticleSku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const queries = isSqlite ? sqliteQueries : mssqlQueries;
const insertHistorySql = isSqlite ? SQLITE_INSERT_HISTORY : MSSQL_INSERT_HISTORY;
/**
* Sets or clears the options list for an article.
@@ -58,7 +61,7 @@ export default function setOptionsByArticleSku(provider) {
}
const run = async (runner) => {
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
const { articleId, companyId, companyuserId } = await resolve(googleAuthId, sku, runner);
const artRes = await runner.query(
'SELECT id, is_father, articlelist_id, optionslist_id FROM article WHERE id = @articleId',
@@ -76,6 +79,18 @@ export default function setOptionsByArticleSku(provider) {
// If options is empty: clear optionslist
if (options.length === 0) {
if (article.optionslist_id != null) {
// Read old options for history before clearing
const oldOptRes = await runner.query(
`SELECT a.sku, at.name
FROM optionslistarticle ola
INNER JOIN article a ON a.id = ola.article_id
LEFT JOIN articletext at ON at.article_id = a.id
WHERE ola.optionslist_id = @optionslistId
ORDER BY ola.sort ASC, ola.id ASC`,
{ optionslistId: article.optionslist_id },
);
const oldOptNames = oldOptRes.rows.map((r) => r.name || r.sku);
await runner.query(queries.SET_ARTICLE_OPTIONSLIST_ID, {
articleId,
optionslistId: null,
@@ -83,6 +98,14 @@ export default function setOptionsByArticleSku(provider) {
await runner.query(queries.DELETE_OPTIONSLIST, {
id: article.optionslist_id,
});
if (oldOptNames.length) {
await runner.query(insertHistorySql, {
articleId,
companyuserId,
text: `Optionsliste geleert (war: [${oldOptNames.join(', ')}])`,
});
}
}
return [];
}
@@ -202,7 +225,17 @@ export default function setOptionsByArticleSku(provider) {
});
}
return getOptions(googleAuthId, sku, runner);
const result = await getOptions(googleAuthId, sku, runner);
// Write history for options list change
const newNames = result.map((r) => r.name || r.sku);
await runner.query(insertHistorySql, {
articleId,
companyuserId,
text: `Optionsliste gesetzt: [${newNames.join(', ')}]`,
});
return result;
};
if (tx) {