Skip to content

Commit

Permalink
refactor: remove promise all from service
Browse files Browse the repository at this point in the history
  • Loading branch information
DeboraSerra committed Dec 19, 2024
1 parent be5b35c commit b737cee
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions backend/src/service/statistics.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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}`);
}
}
}

Expand Down

0 comments on commit b737cee

Please sign in to comment.