new features

This commit is contained in:
Matthias Hochmeister
2026-03-23 16:54:09 +01:00
parent 690f260b71
commit 948b211f70
6 changed files with 115 additions and 26 deletions

View File

@@ -174,21 +174,38 @@ async function getOrderById(id: number) {
}
}
async function createOrder(data: { bezeichnung: string; lieferant_id?: number; besteller_id?: string; notizen?: string; budget?: number }, userId: string) {
async function createOrder(data: { bezeichnung: string; lieferant_id?: number; besteller_id?: string; notizen?: string; budget?: number; positionen?: Array<{ bezeichnung: string; menge: number; einheit?: string }> }, userId: string) {
const client = await pool.connect();
try {
await client.query('BEGIN');
const bestellerId = data.besteller_id && data.besteller_id.trim() ? data.besteller_id.trim() : null;
const result = await pool.query(
const result = await client.query(
`INSERT INTO bestellungen (bezeichnung, lieferant_id, besteller_id, notizen, budget, erstellt_von)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *`,
[data.bezeichnung, data.lieferant_id || null, bestellerId, data.notizen || null, data.budget || null, userId]
);
const order = result.rows[0];
if (data.positionen && data.positionen.length > 0) {
for (const pos of data.positionen) {
await client.query(
`INSERT INTO bestellpositionen (bestellung_id, bezeichnung, menge, einheit)
VALUES ($1, $2, $3, $4)`,
[order.id, pos.bezeichnung, pos.menge, pos.einheit || 'Stk']
);
}
}
await logAction(order.id, 'Bestellung erstellt', `Bestellung "${data.bezeichnung}" erstellt`, userId);
await client.query('COMMIT');
return order;
} catch (error) {
await client.query('ROLLBACK');
logger.error('BestellungService.createOrder failed', { error });
throw new Error('Bestellung konnte nicht erstellt werden');
} finally {
client.release();
}
}