Add articlehistory on updateBySku and getHistoryBySku
This commit is contained in:
@@ -3,13 +3,23 @@ import {
|
||||
UPDATE_UNITAMOUNT,
|
||||
UPDATE_UNIT_AND_AMOUNT,
|
||||
} from '../../sql/article.js';
|
||||
import { INSERT as INSERT_HISTORY } from '../../sql/articlehistory.js';
|
||||
import { UPDATE_BY_ARTICLE_AND_LANGUAGE } from '../../sql/articletext.js';
|
||||
import { GET_BY_SI } from '../../sql/unit.js';
|
||||
import { GET_DETAIL_BY_ARTICLE_ID } from '../../sql/articleDetail.js';
|
||||
import { GET_FULL_BY_SI } from '../../sql/unit.js';
|
||||
import request from '../request.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
import slugify from '../slugify.js';
|
||||
|
||||
function formatUnitLabel(amount, symbol) {
|
||||
const sym = symbol ?? '';
|
||||
if (amount == null || !Number.isFinite(Number(amount))) {
|
||||
return sym;
|
||||
}
|
||||
return `${Number(amount)}${sym}`;
|
||||
}
|
||||
|
||||
export default function updateBySku(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const load = queryBySku(pool);
|
||||
@@ -33,7 +43,18 @@ export default function updateBySku(pool) {
|
||||
|
||||
const { articleId, languageId, companyId } = await resolve(googleAuthId, sku);
|
||||
|
||||
const currentResult = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.input('languageId', languageId)
|
||||
.query(GET_DETAIL_BY_ARTICLE_ID);
|
||||
const current = currentResult.recordset[0];
|
||||
if (!current) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
let unitId;
|
||||
let newUnitSymbol = current.unitSymbol ?? current.unitSi ?? '';
|
||||
if (hasUnitSi) {
|
||||
if (!patch.unitSi?.trim()) {
|
||||
throw new Error('Unit not found');
|
||||
@@ -42,11 +63,36 @@ export default function updateBySku(pool) {
|
||||
.request()
|
||||
.input('companyId', companyId)
|
||||
.input('si', patch.unitSi.trim())
|
||||
.query(GET_BY_SI);
|
||||
unitId = unit.recordset[0]?.id;
|
||||
.input('languageId', languageId)
|
||||
.query(GET_FULL_BY_SI);
|
||||
const unitRow = unit.recordset[0];
|
||||
unitId = unitRow?.id;
|
||||
if (!unitId) {
|
||||
throw new Error('Unit not found');
|
||||
}
|
||||
newUnitSymbol = unitRow.symbol ?? unitRow.si ?? '';
|
||||
}
|
||||
|
||||
const historyLines = [];
|
||||
if (hasName) {
|
||||
const oldName = current.name ?? '';
|
||||
const newName = patch.name.trim();
|
||||
if (oldName !== newName) {
|
||||
historyLines.push(`Name geändert von "${oldName}" auf "${newName}"`);
|
||||
}
|
||||
}
|
||||
if (hasUnitSi || hasUnitamount) {
|
||||
const oldLabel = formatUnitLabel(
|
||||
current.unitamount,
|
||||
current.unitSymbol ?? current.unitSi ?? '',
|
||||
);
|
||||
const newLabel = formatUnitLabel(
|
||||
hasUnitamount ? patch.unitamount : current.unitamount,
|
||||
newUnitSymbol,
|
||||
);
|
||||
if (oldLabel !== newLabel) {
|
||||
historyLines.push(`Einheit geändert von "${oldLabel}" auf "${newLabel}"`);
|
||||
}
|
||||
}
|
||||
|
||||
const tx = pool.transaction();
|
||||
@@ -85,6 +131,13 @@ export default function updateBySku(pool) {
|
||||
.query(UPDATE_UNITAMOUNT);
|
||||
}
|
||||
|
||||
if (historyLines.length) {
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('text', historyLines.join('\n'))
|
||||
.query(INSERT_HISTORY);
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
} catch (err) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user