This commit is contained in:
seb
2026-09-25 07:27:22 +02:00
parent f8f61a1212
commit 387147ccf3
4 changed files with 76 additions and 14 deletions

View File

@@ -1,10 +1,24 @@
export const CHECK_MANIFESTARTICLE_ACCESS = `
SELECT ma.id
SELECT ma.id, ma.sku
FROM manifestarticle ma
INNER JOIN manifest m ON ma.manifest_id = m.id
WHERE ma.id = @manifestarticleId AND m.company_id = @companyId;
`;
export const FIND_ARTICLES_FOR_HISTORY = `
SELECT DISTINCT asup.article_id AS articleId
FROM articlesupplier asup
INNER JOIN entity e ON e.id = asup.entity_id
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
WHERE asup.sku = @sku
AND cg.company_id = @companyId;
`;
export const INSERT_ARTICLE_HISTORY = `
INSERT INTO articlehistory (article_id, companyuser_id, [text])
VALUES (@articleId, @companyuserId, @text);
`;
export const GET_LATEST_ROW = `
SELECT TOP 1
id,

View File

@@ -1,10 +1,24 @@
export const CHECK_MANIFESTARTICLE_ACCESS = `
SELECT ma.id
SELECT ma.id, ma.sku
FROM manifestarticle ma
INNER JOIN manifest m ON ma.manifest_id = m.id
WHERE ma.id = @manifestarticleId AND m.company_id = @companyId;
`;
export const FIND_ARTICLES_FOR_HISTORY = `
SELECT DISTINCT asup.article_id AS articleId
FROM articlesupplier asup
INNER JOIN entity e ON e.id = asup.entity_id
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
WHERE asup.sku = @sku
AND cg.company_id = @companyId;
`;
export const INSERT_ARTICLE_HISTORY = `
INSERT INTO articlehistory (article_id, companyuser_id, text)
VALUES (@articleId, @companyuserId, @text);
`;
export const GET_LATEST_ROW = `
SELECT
id,

View File

@@ -105,14 +105,15 @@ export default function add(provider) {
const transactionText = String(rawTransactionText);
const doAdd = async (runner) => {
const { companyId } = await access(googleAuthId, runner);
const { companyId, companyuserId, rootCategoryId } = await access(googleAuthId, runner);
const check = await runner.query(queries.CHECK_MANIFESTARTICLE_ACCESS, {
manifestarticleId,
companyId,
});
if (!check.rows[0]) {
const manifestArticle = check.rows[0];
if (!manifestArticle) {
throw new Error('Manifest article not found');
}
@@ -134,6 +135,22 @@ export default function add(provider) {
throw new Error('Failed to insert ledger entry');
}
const sku = manifestArticle.sku ? String(manifestArticle.sku).trim() : '';
if (sku) {
const articleMatches = await runner.query(queries.FIND_ARTICLES_FOR_HISTORY, {
sku,
companyId,
});
for (const match of articleMatches.rows) {
await runner.query(queries.INSERT_ARTICLE_HISTORY, {
articleId: match.articleId,
companyuserId,
text: transactionText,
});
}
}
return shape(row);
};