Add articlehistory on updateBySku and getHistoryBySku
This commit is contained in:
@@ -20,6 +20,7 @@ import articleOrderImages from './store/article/orderImages.js';
|
||||
import articleAddImage from './store/article/addImage.js';
|
||||
import articleCreateUnderParent from './store/article/createUnderParent.js';
|
||||
import articleGetBySku from './store/article/getBySku.js';
|
||||
import articleGetHistoryBySku from './store/article/getHistoryBySku.js';
|
||||
import articleListSearchIndex from './store/article/listSearchIndex.js';
|
||||
import articleListExport from './store/article/listExport.js';
|
||||
import articleUpdateBySku from './store/article/updateBySku.js';
|
||||
@@ -97,6 +98,7 @@ export default class LibDashStore {
|
||||
this.article = {
|
||||
createUnderParent: articleCreateUnderParent(pool),
|
||||
getBySku: articleGetBySku(pool),
|
||||
getHistoryBySku: articleGetHistoryBySku(pool),
|
||||
listSearchIndex: articleListSearchIndex(pool),
|
||||
listExport: articleListExport(pool),
|
||||
updateBySku: articleUpdateBySku(pool),
|
||||
|
||||
11
src/sql/articlehistory.js
Normal file
11
src/sql/articlehistory.js
Normal file
@@ -0,0 +1,11 @@
|
||||
export const INSERT = `
|
||||
INSERT INTO articlehistory (article_id, [text])
|
||||
VALUES (@articleId, @text)
|
||||
`;
|
||||
|
||||
export const LIST_BY_ARTICLE_ID = `
|
||||
SELECT id, [date], [text]
|
||||
FROM articlehistory
|
||||
WHERE article_id = @articleId
|
||||
ORDER BY [date] DESC, id DESC
|
||||
`;
|
||||
21
src/store/article/getHistoryBySku.js
Normal file
21
src/store/article/getHistoryBySku.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { LIST_BY_ARTICLE_ID } from '../../sql/articlehistory.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
|
||||
export default function getHistoryBySku(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
|
||||
return async (googleAuthId, sku) => {
|
||||
const { articleId } = await resolve(googleAuthId, sku);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.query(LIST_BY_ARTICLE_ID);
|
||||
|
||||
return result.recordset.map((row) => ({
|
||||
id: row.id,
|
||||
date: row.date,
|
||||
text: row.text,
|
||||
}));
|
||||
};
|
||||
}
|
||||
@@ -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