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

@@ -34,6 +34,25 @@ async function run() {
}); });
await store.google_auth.setSelectedCompany(googleAuthId, company.id); await store.google_auth.setSelectedCompany(googleAuthId, company.id);
await store.unit.upsert(googleAuthId, {
si: 'stk',
basesi: 'stk',
basesifactor: 1,
symbol: 'Stk',
name: 'Stück',
});
const supplier = await store.supplier.create(googleAuthId, { name: 'Supplier X' });
const article = await store.article.createUnderParent({
googleAuthId,
parentId: company.rootCategoryId,
name: 'Ledger Linked Article',
});
await store.articlesupplier.create(googleAuthId, article.sku, {
entityId: supplier.id,
sku: 'SKU-ABC',
pu: 1,
});
// Create a delivery manifest with lines // Create a delivery manifest with lines
const delivery = await store.delivery.create(googleAuthId, { const delivery = await store.delivery.create(googleAuthId, {
externalNumber: 'DEL-1', externalNumber: 'DEL-1',
@@ -193,16 +212,14 @@ async function run() {
const listUnknownSku = await store.ledger.listBySku(googleAuthId, 'UNKNOWN-SKU'); const listUnknownSku = await store.ledger.listBySku(googleAuthId, 'UNKNOWN-SKU');
assert.equal(listUnknownSku.length, 0); assert.equal(listUnknownSku.length, 0);
// 7. Test validation & company authorization // 8. Test article history created from ledger
console.log('6. Testing authorization & validation errors...'); console.log('7. Testing article history entries from ledger rows...');
await assert.rejects( const history = await store.article.getHistoryBySku(googleAuthId, article.sku);
() => store.ledger.listByManifestarticleId(googleAuthId, 999999), const historyTexts = history.map((h) => h.text);
/Manifest article not found/, console.log('Article history texts:', historyTexts);
); assert(historyTexts.includes('Initial delivery movement'), 'History should include initial delivery movement');
await assert.rejects( assert(historyTexts.includes('Damaged item written off'), 'History should include damaged item written off');
() => store.ledger.listBySku(googleAuthId, ''), assert(historyTexts.includes('Second line goods receipt'), 'History should include second line goods receipt');
/Invalid SKU/,
);
console.log('✓ All ledger tests passed successfully!'); console.log('✓ All ledger tests passed successfully!');
} finally { } finally {

View File

@@ -1,10 +1,24 @@
export const CHECK_MANIFESTARTICLE_ACCESS = ` export const CHECK_MANIFESTARTICLE_ACCESS = `
SELECT ma.id SELECT ma.id, ma.sku
FROM manifestarticle ma FROM manifestarticle ma
INNER JOIN manifest m ON ma.manifest_id = m.id INNER JOIN manifest m ON ma.manifest_id = m.id
WHERE ma.id = @manifestarticleId AND m.company_id = @companyId; 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 = ` export const GET_LATEST_ROW = `
SELECT TOP 1 SELECT TOP 1
id, id,

View File

@@ -1,10 +1,24 @@
export const CHECK_MANIFESTARTICLE_ACCESS = ` export const CHECK_MANIFESTARTICLE_ACCESS = `
SELECT ma.id SELECT ma.id, ma.sku
FROM manifestarticle ma FROM manifestarticle ma
INNER JOIN manifest m ON ma.manifest_id = m.id INNER JOIN manifest m ON ma.manifest_id = m.id
WHERE ma.id = @manifestarticleId AND m.company_id = @companyId; 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 = ` export const GET_LATEST_ROW = `
SELECT SELECT
id, id,

View File

@@ -105,14 +105,15 @@ export default function add(provider) {
const transactionText = String(rawTransactionText); const transactionText = String(rawTransactionText);
const doAdd = async (runner) => { 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, { const check = await runner.query(queries.CHECK_MANIFESTARTICLE_ACCESS, {
manifestarticleId, manifestarticleId,
companyId, companyId,
}); });
if (!check.rows[0]) { const manifestArticle = check.rows[0];
if (!manifestArticle) {
throw new Error('Manifest article not found'); throw new Error('Manifest article not found');
} }
@@ -134,6 +135,22 @@ export default function add(provider) {
throw new Error('Failed to insert ledger entry'); 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); return shape(row);
}; };