From b737ceebf49cb48fd87c50186d51c45dd3155ee0 Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Thu, 19 Dec 2024 14:29:42 -0800 Subject: [PATCH] refactor: remove promise all from service --- backend/src/service/statistics.service.js | 24 +++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/backend/src/service/statistics.service.js b/backend/src/service/statistics.service.js index e39dc024..d008e6d4 100644 --- a/backend/src/service/statistics.service.js +++ b/backend/src/service/statistics.service.js @@ -4,12 +4,13 @@ const GuideLog = db.GuideLog; class StatisticsService { async generateStatistics({ userId }) { - const now = new Date(); - const thisMonth = new Date(now.getFullYear(), now.getMonth(), 1); - const lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1); - const twoMonthsAgo = new Date(now.getFullYear(), now.getMonth() - 2, 1); - const views = await Promise.all( - Object.entries(GuideType).map(async ([guideName, guideType]) => { + try { + const now = new Date(); + const thisMonth = new Date(now.getFullYear(), now.getMonth(), 1); + const lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1); + const twoMonthsAgo = new Date(now.getFullYear(), now.getMonth() - 2, 1); + const views = []; + for (const [guideName, guideType] of Object.entries(GuideType)) { const logs = await GuideLog.findAll({ where: { guideType: Number(guideType), @@ -44,10 +45,13 @@ class StatisticsService { change: percentageDifference, guideType: guideName.toLowerCase(), }; - return result; - }) - ); - return views.sort((a, b) => b.views - a.views); + views.push(result); + } + return views.sort((a, b) => b.views - a.views); + } catch (error) { + console.log(error); + throw new Error(`Failed to generate statistics: ${error.message}`); + } } }