new features
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user