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

@@ -6,6 +6,8 @@ import {
ADD_CATEGORY as SQLITE_ADD_CATEGORY,
DELETE_CATEGORIES_BY_ARTICLE as SQLITE_DELETE_CATEGORIES,
} from '../../providers/sqlite3/queries/article.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 resolveCategoryAccess from '../category/resolveCategoryAccess.js';
import queryBySku from './queryBySku.js';
import resolveArticleBySku from './resolveArticleBySku.js';
@@ -19,6 +21,7 @@ export default function setCategoryIdsBySku(provider) {
const addCategorySql = isSqlite ? SQLITE_ADD_CATEGORY : MSSQL_ADD_CATEGORY;
const deleteCategoriesSql = isSqlite ? SQLITE_DELETE_CATEGORIES : MSSQL_DELETE_CATEGORIES;
const insertHistorySql = isSqlite ? SQLITE_INSERT_HISTORY : MSSQL_INSERT_HISTORY;
/**
* Replaces the category membership of an article by its SKU.
@@ -52,12 +55,19 @@ export default function setCategoryIdsBySku(provider) {
}
const runSet = async (runner) => {
const { articleId, languageId, companyId } = await resolve(
const { articleId, languageId, companyId, companyuserId } = await resolve(
googleAuthId,
sku,
runner,
);
// Read old category IDs for history
const oldCatRes = await runner.query(
'SELECT category_id FROM categoryarticle WHERE article_id = @articleId ORDER BY category_id',
{ articleId },
);
const oldCategoryIds = oldCatRes.rows.map((r) => r.category_id);
for (const categoryId of normalized) {
// Ensures the category belongs to the same company as the article.
const access = await categoryAccess(googleAuthId, categoryId, runner);
@@ -75,6 +85,17 @@ export default function setCategoryIdsBySku(provider) {
});
}
// Write history if categories changed
const oldStr = oldCategoryIds.join(', ');
const newStr = [...normalized].sort((a, b) => a - b).join(', ');
if (oldStr !== newStr) {
await runner.query(insertHistorySql, {
articleId,
companyuserId,
text: `Kategorien geändert von [${oldStr}] auf [${newStr}]`,
});
}
const article = await load(articleId, languageId, runner);
if (!article) {
throw new Error('Article not found');