new features
This commit is contained in:
@@ -202,11 +202,21 @@ async function createRequest(
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
|
||||
// Get next bestell_nummer for the current year
|
||||
const currentYear = new Date().getFullYear();
|
||||
const maxResult = await client.query(
|
||||
`SELECT COALESCE(MAX(bestell_nummer), 0) + 1 AS next_nr
|
||||
FROM shop_anfragen
|
||||
WHERE bestell_jahr = $1`,
|
||||
[currentYear],
|
||||
);
|
||||
const nextNr = maxResult.rows[0].next_nr;
|
||||
|
||||
const anfrageResult = await client.query(
|
||||
`INSERT INTO shop_anfragen (anfrager_id, notizen)
|
||||
VALUES ($1, $2)
|
||||
`INSERT INTO shop_anfragen (anfrager_id, notizen, bestell_nummer, bestell_jahr)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING *`,
|
||||
[userId, notizen || null],
|
||||
[userId, notizen || null, nextNr, currentYear],
|
||||
);
|
||||
const anfrage = anfrageResult.rows[0];
|
||||
|
||||
@@ -296,6 +306,42 @@ async function getLinkedOrders(anfrageId: number) {
|
||||
return result.rows;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Overview (aggregated)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function getOverview() {
|
||||
const aggregated = await pool.query(
|
||||
`SELECT p.bezeichnung,
|
||||
SUM(p.menge)::int AS total_menge,
|
||||
COUNT(DISTINCT p.anfrage_id)::int AS anfrage_count
|
||||
FROM shop_anfrage_positionen p
|
||||
JOIN shop_anfragen a ON a.id = p.anfrage_id
|
||||
WHERE a.status IN ('offen', 'genehmigt')
|
||||
GROUP BY p.bezeichnung
|
||||
ORDER BY total_menge DESC, p.bezeichnung`,
|
||||
);
|
||||
|
||||
const counts = await pool.query(
|
||||
`SELECT
|
||||
COUNT(*) FILTER (WHERE status = 'offen')::int AS pending_count,
|
||||
COUNT(*) FILTER (WHERE status = 'genehmigt')::int AS approved_count,
|
||||
COALESCE(SUM(sub.total), 0)::int AS total_items
|
||||
FROM shop_anfragen a
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT SUM(p.menge) AS total
|
||||
FROM shop_anfrage_positionen p
|
||||
WHERE p.anfrage_id = a.id
|
||||
) sub ON true
|
||||
WHERE a.status IN ('offen', 'genehmigt')`,
|
||||
);
|
||||
|
||||
return {
|
||||
items: aggregated.rows,
|
||||
...counts.rows[0],
|
||||
};
|
||||
}
|
||||
|
||||
export default {
|
||||
getItems,
|
||||
getItemById,
|
||||
@@ -312,4 +358,5 @@ export default {
|
||||
linkToOrder,
|
||||
unlinkFromOrder,
|
||||
getLinkedOrders,
|
||||
getOverview,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user