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 store.order.createFromQuote in dashdbschema ---'); const tempDir = mkdtempSync(join(tmpdir(), 'test-order-create-from-quote-')); 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('order-from-quote-test@example.com'); const company = await store.company.create({ name: 'Order Quote Corp', iso639: 'de', taxrate: 19, reducedtaxrate: 7, googleAuthId, }); await store.google_auth.setSelectedCompany(googleAuthId, company.id); const groups = await store.customergroup.list(googleAuthId); const customer = await store.entity.create(googleAuthId, { customergroupId: groups[0].id, firstName: 'Alice', name: 'Wonderland', }); const art = await store.article.createUnderParent({ googleAuthId, parentId: company.rootCategoryId, name: 'Gadget', track_stock: 0, }); // 1. Create a quote const quote = await store.quote.create(googleAuthId, { number: 'Q-2026-001', date: '2026-09-01T10:00:00Z', entityId: customer.id, externalNumber: 'EXT-QUOTE-123', lines: [ { sku: art.sku, name: 'Gadget', pu: 1, amount: 5, net: 20, rebate: 10, taxrate: 19, comment: 'test item' }, ], }); assert.equal(quote.number, 'Q-2026-001'); assert.equal(quote.lines.length, 1); // 2. Create order from quote with default auto-allocated number & inherited fields const order1 = await store.order.createFromQuote(googleAuthId, quote.id); assert.ok(order1.id, 'Order must have an id'); assert.equal(order1.type, 'order'); assert.equal(order1.entityId, customer.id); assert.equal(order1.externalNumber, quote.number); assert.equal(order1.number, 'AUF-000001'); assert.equal(order1.lines.length, 1); assert.equal(order1.lines[0].sku, art.sku); assert.equal(order1.lines[0].amount, 5); assert.equal(order1.lines[0].net, 20); assert.equal(order1.lines[0].rebate, 10); assert.equal(order1.lines[0].taxrate, 19); assert.equal(order1.lines[0].comment, 'test item'); // Verify ordersummary row const summaryRows = provider.db.prepare('SELECT * FROM ordersummary WHERE manifest_id = ?').all(order1.id); assert.equal(summaryRows.length, 1); assert.equal(summaryRows[0].customer_name, 'Alice Wonderland'); assert.equal(summaryRows[0].positions, 1); assert.equal(summaryRows[0].ordered_quantity, 5); // 3. Create order from quote with custom overrides (number, externalNumber, date) const order2 = await store.order.createFromQuote(googleAuthId, quote.id, { number: 'ORD-CUSTOM-999', date: '2026-09-15T12:00:00Z', externalNumber: 'CUSTOM-EXT', }); assert.equal(order2.number, 'ORD-CUSTOM-999'); assert.equal(order2.externalNumber, 'CUSTOM-EXT'); assert.equal(order2.lines.length, 1); // 4. Non-existent quote should throw 'Quote not found' await assert.rejects( async () => { await store.order.createFromQuote(googleAuthId, 999999); }, { message: 'Quote not found' } ); // 5. Invalid quoteId should throw 'Quote not found' await assert.rejects( async () => { await store.order.createFromQuote(googleAuthId, 'invalid'); }, { message: 'Quote not found' } ); // 6. Quote without entity should throw 'No entity selected' if no entity passed const quoteWithoutEntity = await store.quote.create(googleAuthId, { number: 'Q-NO-ENT', date: '2026-09-01T10:00:00Z', lines: [{ sku: art.sku, name: 'Gadget', amount: 1, net: 10, taxrate: 19 }], }); assert.equal(quoteWithoutEntity.entityId, null); await assert.rejects( async () => { await store.order.createFromQuote(googleAuthId, quoteWithoutEntity.id); }, { message: 'No entity selected' } ); // 7. Explicitly overriding with entityId: null should also throw 'No entity selected' await assert.rejects( async () => { await store.order.createFromQuote(googleAuthId, quote.id, { entityId: null }); }, { message: 'No entity selected' } ); // 8. Passing entityId for quote without entity should succeed const orderFromNoEnt = await store.order.createFromQuote(googleAuthId, quoteWithoutEntity.id, { entityId: customer.id, }); assert.equal(orderFromNoEnt.entityId, customer.id); console.log('PASS: All order.createFromQuote tests passed successfully!'); } finally { try { rmSync(tempDir, { recursive: true, force: true }); } catch { // ignore cleanup errors } } } run().catch((err) => { console.error('FAIL: Test failed:', err); process.exit(1); });