(census)(admin) allow downloading previous years

This commit is contained in:
Andrea Vos 2025-06-21 15:06:45 +02:00
parent a04c412fc3
commit 0ba8cfaf4f

View File

@ -222,7 +222,7 @@ const calculateAggregate = (config: Aggregate, answer: Set<number | string>): bo
} }
}; };
router.get('/census/export', handleErrorAsync(async (req, res) => { router.get('/census/export/:edition?', handleErrorAsync(async (req, res) => {
if (!req.isGranted('census')) { if (!req.isGranted('census')) {
return res.status(401).json({ error: 'Unauthorised' }); return res.status(401).json({ error: 'Unauthorised' });
} }
@ -230,11 +230,13 @@ router.get('/census/export', handleErrorAsync(async (req, res) => {
const locale = getLocale(getH3Event(req)); const locale = getLocale(getH3Event(req));
const config = await loadConfig(locale); const config = await loadConfig(locale);
const edition = req.params.edition || config.census.edition;
const report = []; const report = [];
for (const { answers: answersRaw, writins: writinsRaw } of await req.db.all<Pick<CensusRow, 'answers' | 'writins'>>(SQL` for (const { answers: answersRaw, writins: writinsRaw } of await req.db.all<Pick<CensusRow, 'answers' | 'writins'>>(SQL`
SELECT answers, writins FROM census SELECT answers, writins FROM census
WHERE locale = ${config.locale} WHERE locale = ${config.locale}
AND edition = ${config.census.edition} AND edition = ${edition}
AND suspicious = 0 AND suspicious = 0
AND troll = 0 AND troll = 0
AND relevant = 1 AND relevant = 1