TanksApp/compose/server/reportRouter.js
2023-12-24 22:27:25 +04:00

126 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module.exports = (req, res, next) => {
if (req.url.startsWith('/report') && req.method === 'GET') {
const { startDate, endDate } = req.query;
try {
delete require.cache[require.resolve('./data.json')];
const data = require('./data.json');
const filteredPurchase = data.users_tanks.filter(purchase => {
const purchaseDate = new Date(purchase.date);
return purchaseDate >= new Date(startDate) && purchaseDate <= new Date(endDate);
});
//ПОИСК САМОГО ПОПУЛЯРНОГО ТАНКА ЗА ПЕРИОД
//список танков с кол-во их покупок
let tankList = [];
filteredPurchase.forEach(purchase => {
const tankId = purchase.tankId;
const tankInList = tankList.some(item => item.tankId === tankId);
if (tankInList) {
tankList[tankList.findIndex(index => index.tankId === tankId)].count++;
} else {
tankList.push(
{
tankId: tankId,
count: 1
}
)
}
});
tankList.sort((a, b) => b.count - a.count);
//нашли самый популярный танк
const supportTank = data.tanks.find(tank => tank.id === tankList[0].tankId);
const supportLevel = data.levels.find(level => level.id === supportTank.levelId).level;
const supportNation = data.nations.find(nation => nation.id === supportTank.nationId).nationName;
const popularTank = {
id: supportTank.id,
name: supportTank.name,
price: supportTank.price,
miniature: supportTank.miniature,
level: supportLevel,
nation: supportNation
};
//ПОИСК САМЫХ ПОПУЛЯРНЫХ УРОВНЯ И НАЦИИ ЗА ПЕРИОД
//список уроней и кол-во их покупок
let levelList = [];
//список уроней и кол-во их покупок
let nationList = [];
tankList.forEach(element => {
const tank = data.tanks.find(tank => tank.id === element.tankId);
//смотрим, сколько раз танк был куплен, чтобы сразу прибавить к соответствующему уровню текущее кол-во
const countCurrentTank = element.count;
const levelId = tank.levelId;
const nationId = tank.nationId;
const levelInList = levelList.some(item => item.levelId === levelId);
const nationInList = nationList.some(item => item.nationId === nationId);
if (levelInList) {
levelList[levelList.findIndex(index => index.levelId === levelId)].count += countCurrentTank;
} else {
levelList.push(
{
levelId: levelId,
count: countCurrentTank
}
)
}
if (nationInList) {
nationList[nationList.findIndex(index => index.nationId === nationId)].count += countCurrentTank;
} else {
nationList.push(
{
nationId: nationId,
count: countCurrentTank
}
)
}
});
levelList.sort((a, b) => b.count - a.count);
nationList.sort((a, b) => b.count - a.count);
//нашли самые популярные уровень и нацию
const popularLevel = data.levels.find(level => level.id === levelList[0].levelId)
const popularNation = data.nations.find(nation => nation.id === nationList[0].nationId)
finalDataReport = {
id: popularTank.id,
tankName: popularTank.name,
price: popularTank.price,
miniature: popularTank.miniature,
tankLevel: popularTank.level,
tankNation: popularTank.nation,
countTankPurchase: tankList[0].count,
level: popularLevel.level,
countLevelPurchase: levelList[0].count,
nationName: popularNation.nationName,
countNationPurchase: nationList[0].count
};
console.log(finalDataReport)
res.json(finalDataReport);
} catch (error) {
console.error('Error loading data:', error);
res.status(500).json({ message: 'Internal Server Error' });
}
} else {
next();
}
};