rework internal order system

This commit is contained in:
Matthias Hochmeister
2026-03-24 10:22:31 +01:00
parent 3ce8adfa07
commit 2b77ae5724
4 changed files with 151 additions and 58 deletions

View File

@@ -393,21 +393,36 @@ async function createRequest(
await client.query('BEGIN');
const currentYear = new Date().getFullYear();
const maxResult = await client.query(
`SELECT COALESCE(MAX(bestell_nummer), 0) + 1 AS next_nr
FROM ausruestung_anfragen
WHERE bestell_jahr = $1`,
[currentYear],
);
const nextNr = maxResult.rows[0].next_nr;
const anfrageResult = await client.query(
`INSERT INTO ausruestung_anfragen (anfrager_id, notizen, bezeichnung, bestell_nummer, bestell_jahr)
VALUES ($1, $2, $3, $4, $5)
RETURNING *`,
[userId, notizen || null, bezeichnung || null, nextNr, currentYear],
);
const anfrage = anfrageResult.rows[0];
// Try with bestell_nummer/bestell_jahr (migration 050), fallback without
let anfrage: Record<string, unknown>;
try {
await client.query('SAVEPOINT sp_bestell_nr');
const maxResult = await client.query(
`SELECT COALESCE(MAX(bestell_nummer), 0) + 1 AS next_nr
FROM ausruestung_anfragen
WHERE bestell_jahr = $1`,
[currentYear],
);
const nextNr = maxResult.rows[0].next_nr;
const anfrageResult = await client.query(
`INSERT INTO ausruestung_anfragen (anfrager_id, notizen, bezeichnung, bestell_nummer, bestell_jahr)
VALUES ($1, $2, $3, $4, $5)
RETURNING *`,
[userId, notizen || null, bezeichnung || null, nextNr, currentYear],
);
await client.query('RELEASE SAVEPOINT sp_bestell_nr');
anfrage = anfrageResult.rows[0];
} catch {
await client.query('ROLLBACK TO SAVEPOINT sp_bestell_nr');
const anfrageResult = await client.query(
`INSERT INTO ausruestung_anfragen (anfrager_id, notizen, bezeichnung)
VALUES ($1, $2, $3)
RETURNING *`,
[userId, notizen || null, bezeichnung || null],
);
anfrage = anfrageResult.rows[0];
}
for (const item of items) {
let itemBezeichnung = item.bezeichnung;
@@ -566,17 +581,34 @@ async function updateRequestStatus(
adminNotizen?: string,
bearbeitetVon?: string,
) {
const result = await pool.query(
`UPDATE ausruestung_anfragen
SET status = $1,
admin_notizen = COALESCE($2, admin_notizen),
bearbeitet_von = COALESCE($3, bearbeitet_von),
bearbeitet_am = NOW()
WHERE id = $4
RETURNING *`,
[status, adminNotizen || null, bearbeitetVon || null, id],
);
return result.rows[0] || null;
// Use aktualisiert_am (always exists) + try bearbeitet_am (added in migration 050)
try {
const result = await pool.query(
`UPDATE ausruestung_anfragen
SET status = $1,
admin_notizen = COALESCE($2, admin_notizen),
bearbeitet_von = COALESCE($3, bearbeitet_von),
bearbeitet_am = NOW(),
aktualisiert_am = NOW()
WHERE id = $4
RETURNING *`,
[status, adminNotizen || null, bearbeitetVon || null, id],
);
return result.rows[0] || null;
} catch {
// Fallback if bearbeitet_am column doesn't exist yet (migration 050 not run)
const result = await pool.query(
`UPDATE ausruestung_anfragen
SET status = $1,
admin_notizen = COALESCE($2, admin_notizen),
bearbeitet_von = COALESCE($3, bearbeitet_von),
aktualisiert_am = NOW()
WHERE id = $4
RETURNING *`,
[status, adminNotizen || null, bearbeitetVon || null, id],
);
return result.rows[0] || null;
}
}
async function deleteRequest(id: number) {