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

@@ -12,6 +12,8 @@ import {
import {
GET_BY_COMPANY_AND_ID as SQLITE_GET_PROP,
} from '../../providers/sqlite3/queries/propertyname.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 makeRunner from '../runner.js';
@@ -21,6 +23,7 @@ export default function setByArticleSku(provider) {
const deleteSql = isSqlite ? SQLITE_DELETE : MSSQL_DELETE;
const insertSql = isSqlite ? SQLITE_INSERT : MSSQL_INSERT;
const getPropSql = isSqlite ? SQLITE_GET_PROP : MSSQL_GET_PROP;
const insertHistorySql = isSqlite ? SQLITE_INSERT_HISTORY : MSSQL_INSERT_HISTORY;
/**
* Replaces all properties for an article by its SKU.
@@ -38,7 +41,18 @@ export default function setByArticleSku(provider) {
}
const runSet = async (runner) => {
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
const { articleId, companyId, companyuserId } = await resolve(googleAuthId, sku, runner);
// Read old properties for history
const oldPropsRes = await runner.query(
`SELECT ap.property_id AS propertyId, pn.name, ap.value
FROM articleproperty ap
INNER JOIN propertyname pn ON pn.id = ap.property_id
WHERE ap.article_id = @articleId
ORDER BY ap.sort ASC, ap.id ASC`,
{ articleId },
);
const oldProps = oldPropsRes.rows;
const validated = [];
const seenExclusiveProps = new Set();
@@ -97,6 +111,18 @@ export default function setByArticleSku(provider) {
});
}
// Write history if properties changed
const fmtProp = (p) => `${p.name}=${p.value}`;
const oldStr = oldProps.map(fmtProp).join(', ');
const newStr = validated.map(fmtProp).join(', ');
if (oldStr !== newStr) {
await runner.query(insertHistorySql, {
articleId,
companyuserId,
text: `Eigenschaften geändert von [${oldStr}] auf [${newStr}]`,
});
}
return inserted;
};