Fas 1b punkt 6: First Scan Coach + aktiveringsmätning

This commit is contained in:
Sven (AAMOS AI)
2026-08-06 04:14:49 +07:00
parent 2f3cdf8016
commit bd8df59138
29 changed files with 9378 additions and 29 deletions
+22
View File
@@ -200,6 +200,28 @@ export async function adminAnalyticsRoutes(app: FastifyInstance) {
const rows = Array.isArray(result) ? result : result.rows;
return { counts: rows };
});
/** Household activation metrics (Fas 1b). */
app.get("/admin/v1/analytics/activation", admin, async () => {
const result = await app.db.execute(sql`
SELECT
COUNT(*) FILTER (WHERE first_scan_completed_at IS NOT NULL) AS scanned,
COUNT(*) FILTER (WHERE activated_at IS NOT NULL) AS activated,
COUNT(*) FILTER (WHERE value_confirmed_at IS NOT NULL) AS value_confirmed,
COUNT(*) AS total
FROM households
`);
const rows = Array.isArray(result) ? result : result.rows;
const r = rows[0] as Record<string, number>;
return {
totalHouseholds: Number(r.total ?? 0),
scanned: Number(r.scanned ?? 0),
activated: Number(r.activated ?? 0),
valueConfirmed: Number(r.value_confirmed ?? 0),
activationRate: r.total ? Number(r.activated) / Number(r.total) : 0,
valueConfirmationRate: r.total ? Number(r.value_confirmed) / Number(r.total) : 0,
};
});
}
/** Escape a string/date parameter for raw SQL by wrapping it in single quotes.