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.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
const delivery = await store.delivery.create(googleAuthId, {
externalNumber: 'DEL-1',
@@ -193,16 +212,14 @@ async function run() {
const listUnknownSku = await store.ledger.listBySku(googleAuthId, 'UNKNOWN-SKU');
assert.equal(listUnknownSku.length, 0);
// 7. Test validation & company authorization
console.log('6. Testing authorization & validation errors...');
await assert.rejects(
() => store.ledger.listByManifestarticleId(googleAuthId, 999999),
/Manifest article not found/,
);
await assert.rejects(
() => store.ledger.listBySku(googleAuthId, ''),
/Invalid SKU/,
);
// 8. Test article history created from ledger
console.log('7. Testing article history entries from ledger rows...');
const history = await store.article.getHistoryBySku(googleAuthId, article.sku);
const historyTexts = history.map((h) => h.text);
console.log('Article history texts:', historyTexts);
assert(historyTexts.includes('Initial delivery movement'), 'History should include initial delivery movement');
assert(historyTexts.includes('Damaged item written off'), 'History should include damaged item written off');
assert(historyTexts.includes('Second line goods receipt'), 'History should include second line goods receipt');
console.log('✓ All ledger tests passed successfully!');
} finally {

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);
};