From 824da5d30d789a391e8d008342851a2584d52304 Mon Sep 17 00:00:00 2001 From: Eivind Dalholt Date: Wed, 27 Sep 2023 19:02:57 +0200 Subject: [PATCH] fix: improve votation fetch performance --- backend/controllers/votation.ts | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/backend/controllers/votation.ts b/backend/controllers/votation.ts index 5ff0079..7cda9cb 100644 --- a/backend/controllers/votation.ts +++ b/backend/controllers/votation.ts @@ -50,20 +50,7 @@ export async function getAllVotations(req: RequestWithNtnuiNo, res: Response) { continue; } - const optionList: OptionType[] = []; - - for (const optionID of vote.options) { - const id = optionID; - const option = await Option.findById(id); - if (option) { - const newOption = new Option({ - _id: id, - title: option.title, - voteCount: option.voteCount, - }); - optionList.push(newOption); - } - } + const optionList = await Option.find({ _id: { $in: vote.options } }); let isActive = false; if (assembly.currentVotation) { @@ -142,19 +129,9 @@ export async function getCurrentVotation( return res.status(200).json(null); } - const optionList: LimitedOptionType[] = []; - - for (const optionID of vote.options) { - const id = optionID; - const option = await Option.findById(id); - if (option) { - const newOption: LimitedOptionType = { - _id: id, - title: option.title, - }; - optionList.push(newOption); - } - } + const optionList: LimitedOptionType[] = await Option.find({ + _id: { $in: vote.options }, + }).select("_id title"); const votationResponse: LimitedVoteResponseType = { _id: assembly.currentVotation,