This commit is contained in:
seb
2026-10-13 01:30:11 +02:00
parent 4b2ca7ce0e
commit 2ef2c0edbf
4 changed files with 416 additions and 25 deletions

View File

@@ -0,0 +1,205 @@
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import LibDashStore from '../src/index.js';
import { BetterSqlite3Provider } from '../src/providers/sqlite3/index.js';
async function run() {
console.log('--- Testing Order Filtering & Paging in dashdbschema ---');
const tempDir = mkdtempSync(join(tmpdir(), 'test-order-filters-'));
const dbFile = join(tempDir, 'test.db');
const storageDir = join(tempDir, 'storage');
const provider = new BetterSqlite3Provider({
filename: dbFile,
storageDir,
});
await provider.initialize();
const store = await new LibDashStore(provider);
try {
const googleAuthId = await store.google_auth.add('filter-test@example.com');
const company = await store.company.create({
name: 'Filter Test Corp',
iso639: 'de',
taxrate: 19,
reducedtaxrate: 7,
googleAuthId,
});
await store.google_auth.setSelectedCompany(googleAuthId, company.id);
const warehouse = await store.warehouse.create(googleAuthId, { name: 'Hauptlager' });
await store.unit.upsert(googleAuthId, {
si: 'stk',
basesi: 'stk',
basesifactor: 1,
symbol: 'Stk',
name: 'Stück',
});
const groups = await store.customergroup.list(googleAuthId);
const customerAlice = await store.entity.create(googleAuthId, {
customergroupId: groups[0].id,
firstName: 'Alice',
name: 'Wonderland',
});
const customerBob = await store.entity.create(googleAuthId, {
customergroupId: groups[0].id,
firstName: 'Bob',
name: 'Builder',
});
const art = await store.article.createUnderParent({
googleAuthId,
parentId: company.rootCategoryId,
name: 'Widget',
track_stock: 1,
});
await store.stock.adjust(googleAuthId, {
sku: art.sku,
warehouseId: warehouse.id,
amount: 100,
text: 'Stock in',
});
// Account for payments
const account = await store.account.create(googleAuthId, {
name: 'Bank',
type: 'bank',
});
// Create 3 orders
// Order 1: Alice, O-1001, €119 (1 * 100 + 19% tax), Date: 2 months ago (2026-08-01)
// Paid in full, Unshipped
const order1 = await store.order.create(googleAuthId, {
number: 'O-1001',
date: '2026-08-01T10:00:00Z',
entityId: customerAlice.id,
lines: [{ sku: art.sku, name: 'Widget', amount: 1, net: 100, taxrate: 19 }],
});
await store.payment.create(googleAuthId, {
accountId: account.id,
manifestId: order1.id,
type: 'bank_transfer',
amount: 119,
});
// Order 2: Bob, O-1002, €238 (2 * 100 + 19% tax), Date: 10 days ago (2026-10-03)
// Unpaid, Shipped in full with shipping 'shipped'
const order2 = await store.order.create(googleAuthId, {
number: 'O-1002',
date: '2026-10-03T10:00:00Z',
entityId: customerBob.id,
lines: [{ sku: art.sku, name: 'Widget', amount: 2, net: 100, taxrate: 19 }],
});
const shipment2 = await store.shipment.create(googleAuthId, {
orderId: order2.id,
lines: [{ sku: art.sku, amount: 2 }],
});
await store.shipping.create(googleAuthId, {
manifestId: shipment2.id,
type: 'parcel',
state: 'shipped',
});
// Order 3: Alice, O-1003, €59.50, Date: today (2026-10-13)
// Unpaid, Undelivered
const order3 = await store.order.create(googleAuthId, {
number: 'O-1003',
date: '2026-10-13T10:00:00Z',
entityId: customerAlice.id,
lines: [{ sku: art.sku, name: 'Widget', amount: 1, net: 50, taxrate: 19 }],
});
// Order 4: Bob, O-1004, €119 (1 * 100 + 19% tax), Date: today (2026-10-13)
// Overpaid (paid €150), Delivered
const order4 = await store.order.create(googleAuthId, {
number: 'O-1004',
date: '2026-10-13T10:00:00Z',
entityId: customerBob.id,
lines: [{ sku: art.sku, name: 'Widget', amount: 1, net: 100, taxrate: 19 }],
});
await store.payment.create(googleAuthId, {
accountId: account.id,
manifestId: order4.id,
type: 'bank_transfer',
amount: 150, // Overpaid!
});
const shipment4 = await store.shipment.create(googleAuthId, {
orderId: order4.id,
lines: [{ sku: art.sku, amount: 1 }],
});
await store.shipping.create(googleAuthId, {
manifestId: shipment4.id,
type: 'parcel',
state: 'shipped',
});
console.log('Orders created successfully.');
// 1. Search Alice
const resAlice = await store.order.list(googleAuthId, { search: 'Alice' });
assert.equal(resAlice.total, 2, 'Should find 2 orders for Alice');
assert.deepEqual(resAlice.orders.map((o) => o.number), ['O-1003', 'O-1001']);
// 2. Search Bob
const resBob = await store.order.list(googleAuthId, { search: 'Bob' });
assert.equal(resBob.total, 2, 'Should find 2 orders for Bob');
assert.deepEqual(resBob.orders.map((o) => o.number), ['O-1004', 'O-1002']);
// 3. Search 1002
const res1002 = await store.order.list(googleAuthId, { search: '1002' });
assert.equal(res1002.total, 1, 'Should find 1 order for 1002');
assert.equal(res1002.orders[0].number, 'O-1002');
// 4. onlyUnpaid (O-1004 is overpaid, so it also counts as unpaid!)
const resUnpaid = await store.order.list(googleAuthId, { onlyUnpaid: true });
assert.equal(resUnpaid.total, 3, 'O-1004 (overpaid), O-1003 (unpaid), O-1002 (unpaid) are unpaid');
assert.deepEqual(resUnpaid.orders.map((o) => o.number), ['O-1004', 'O-1003', 'O-1002']);
// 5. onlyUndelivered
const resUndelivered = await store.order.list(googleAuthId, { onlyUndelivered: true });
assert.equal(resUndelivered.total, 2, 'O-1001 and O-1003 are undelivered');
assert.deepEqual(resUndelivered.orders.map((o) => o.number), ['O-1003', 'O-1001']);
// 6. onlyUnpaid AND onlyUndelivered
const resBoth = await store.order.list(googleAuthId, { onlyUnpaid: true, onlyUndelivered: true });
assert.equal(resBoth.total, 1, 'Only O-1003 is both unpaid and undelivered');
assert.equal(resBoth.orders[0].number, 'O-1003');
// 7. seekMonths -1 (orders older than 1 month)
const resSeek = await store.order.list(googleAuthId, { seekMonths: -1 });
assert.equal(resSeek.total, 1, 'Only O-1001 is older than 1 month');
assert.equal(resSeek.orders[0].number, 'O-1001');
// 8. Paging
const resPaged = await store.order.list(googleAuthId, { offset: 0, limit: 1 });
assert.equal(resPaged.total, 4);
assert.equal(resPaged.orders.length, 1);
assert.equal(resPaged.orders[0].number, 'O-1004');
const resPaged2 = await store.order.list(googleAuthId, { offset: 1, limit: 1 });
assert.equal(resPaged2.total, 4);
assert.equal(resPaged2.orders.length, 1);
assert.equal(resPaged2.orders[0].number, 'O-1003');
// 9. Unpaged backwards-compatibility
const resAll = await store.order.list(googleAuthId);
assert(Array.isArray(resAll));
assert.equal(resAll.length, 4);
console.log('✓ All filter and pagination tests passed via store.order.list!');
} finally {
try {
rmSync(tempDir, { recursive: true, force: true });
} catch {}
}
}
run().catch((err) => {
console.error(err);
process.exit(1);
});