admin: fix timestamps, per-day stats, reset, delete drinks/bars
- store created_at as UTC ISO; display/group in Europe/Berlin - stats grouped by business day (sales night past midnight, 5am cutoff) - add "Statistik zurücksetzen" - allow deleting drinks/tresen (refused if referenced by sales) - CSV exports local wall-clock time
This commit is contained in:
parent
22577a0a65
commit
40cdc98f82
4 changed files with 210 additions and 9 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||
import type { DB } from '../db.js';
|
||||
import { businessDay, formatLocal, parseDbTime } from '../time.js';
|
||||
|
||||
const SESSION_COOKIE = 'wutz_admin';
|
||||
|
||||
|
|
@ -76,6 +77,24 @@ export function registerAdminRoutes(app: FastifyInstance, db: DB) {
|
|||
}
|
||||
);
|
||||
|
||||
app.delete<{ Params: { id: string } }>('/admin/api/drinks/:id', async (req, reply) => {
|
||||
if (!requireAuth(req, reply)) return;
|
||||
const id = Number(req.params.id);
|
||||
const sold = db
|
||||
.prepare('SELECT 1 FROM transaction_items WHERE drink_id = ? LIMIT 1')
|
||||
.get(id);
|
||||
if (sold) {
|
||||
return reply
|
||||
.code(409)
|
||||
.send({ error: 'Getränk wurde bereits verkauft — bitte archivieren statt löschen.' });
|
||||
}
|
||||
db.transaction(() => {
|
||||
db.prepare('DELETE FROM bar_drinks WHERE drink_id = ?').run(id);
|
||||
db.prepare('DELETE FROM drinks WHERE id = ?').run(id);
|
||||
})();
|
||||
return { ok: true };
|
||||
});
|
||||
|
||||
// ----- Bars -----
|
||||
app.get('/admin/api/bars', async (req, reply) => {
|
||||
if (!requireAuth(req, reply)) return;
|
||||
|
|
@ -145,6 +164,24 @@ export function registerAdminRoutes(app: FastifyInstance, db: DB) {
|
|||
}
|
||||
);
|
||||
|
||||
app.delete<{ Params: { id: string } }>('/admin/api/bars/:id', async (req, reply) => {
|
||||
if (!requireAuth(req, reply)) return;
|
||||
const id = Number(req.params.id);
|
||||
const used = db
|
||||
.prepare('SELECT 1 FROM transactions WHERE bar_id = ? LIMIT 1')
|
||||
.get(id);
|
||||
if (used) {
|
||||
return reply
|
||||
.code(409)
|
||||
.send({ error: 'Tresen hat Transaktionen — Löschen würde die Statistik verfälschen.' });
|
||||
}
|
||||
db.transaction(() => {
|
||||
db.prepare('DELETE FROM bar_drinks WHERE bar_id = ?').run(id);
|
||||
db.prepare('DELETE FROM bars WHERE id = ?').run(id);
|
||||
})();
|
||||
return { ok: true };
|
||||
});
|
||||
|
||||
// ----- Stats -----
|
||||
app.get('/admin/api/stats', async (req, reply) => {
|
||||
if (!requireAuth(req, reply)) return;
|
||||
|
|
@ -174,7 +211,36 @@ export function registerAdminRoutes(app: FastifyInstance, db: DB) {
|
|||
)
|
||||
.all();
|
||||
|
||||
return { totals, per_drink: perDrink };
|
||||
// Per business day (sales night runs past midnight — see time.ts).
|
||||
const txRows = db
|
||||
.prepare('SELECT created_at, total_cents, crew, pfand_returns FROM transactions')
|
||||
.all() as Array<{ created_at: string; total_cents: number; crew: number; pfand_returns: number }>;
|
||||
|
||||
const dayMap = new Map<string, { day: string; tx_count: number; paid_cents: number; crew_count: number; pfand_returns: number }>();
|
||||
for (const r of txRows) {
|
||||
const day = businessDay(parseDbTime(r.created_at));
|
||||
let agg = dayMap.get(day);
|
||||
if (!agg) {
|
||||
agg = { day, tx_count: 0, paid_cents: 0, crew_count: 0, pfand_returns: 0 };
|
||||
dayMap.set(day, agg);
|
||||
}
|
||||
agg.tx_count += 1;
|
||||
if (r.crew) agg.crew_count += 1;
|
||||
else agg.paid_cents += r.total_cents;
|
||||
agg.pfand_returns += r.pfand_returns;
|
||||
}
|
||||
const byDay = [...dayMap.values()].sort((a, b) => b.day.localeCompare(a.day));
|
||||
|
||||
return { totals, per_drink: perDrink, by_day: byDay };
|
||||
});
|
||||
|
||||
app.post('/admin/api/stats/reset', async (req, reply) => {
|
||||
if (!requireAuth(req, reply)) return;
|
||||
db.transaction(() => {
|
||||
db.prepare('DELETE FROM transaction_items').run();
|
||||
db.prepare('DELETE FROM transactions').run();
|
||||
})();
|
||||
return { ok: true };
|
||||
});
|
||||
|
||||
// ----- CSV export -----
|
||||
|
|
@ -211,6 +277,11 @@ export function registerAdminRoutes(app: FastifyInstance, db: DB) {
|
|||
.all() as any[];
|
||||
}
|
||||
|
||||
// created_at is stored as UTC — export it as local wall-clock time.
|
||||
for (const r of rows) {
|
||||
if (r.created_at) r.created_at = formatLocal(parseDbTime(r.created_at));
|
||||
}
|
||||
|
||||
const csv = [header.join(',')]
|
||||
.concat(rows.map(r => header.map(h => csvCell(r[h])).join(',')))
|
||||
.join('\n');
|
||||
|
|
|
|||
Loading…
Reference in a new issue