Mobile_PIbd-31_Balberova_D.N./server/reportRouter.js

69 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2023-12-15 19:49:41 +04:00
module.exports = (req, res, next) => {
const isReportRequest = req.url.startsWith('/report') && req.method === 'GET';
if (!isReportRequest) {
next();
return;
}
try {
const { startDate, endDate } = req.query;
const { cinemas, sessions, orders } = require('./data.json');
2023-12-15 19:49:41 +04:00
const start = new Date(startDate);
const end = new Date(endDate);
// Фильтруем сеансы по периоду
const filteredSessions = sessions.filter(session => {
const sessionDate = new Date(session.dateTime.replace(/(\d{2}).(\d{2}).(\d{4}) (\d{2}):(\d{2})/, '$3-$2-$1T$4:$5'));
// обнуление времени с учетом зоны времени
sessionDate.setHours(4, 0, 0, 0);
return sessionDate >= start && sessionDate <= end;
});
2023-12-22 15:18:00 +04:00
const report = { "report_data": [],
"total_revenue": 0.0,
"total_purchased_tickets": 0 }
2023-12-15 19:49:41 +04:00
// Обрабатываем отфильтрованные сеансы для аналитики
const reportData = filteredSessions.map(session => {
// берем заказ, где сеанс только текущий
const relevantOrders = orders
.map(orderWithOneSession => ({
...orderWithOneSession,
sessions: orderWithOneSession.sessions.filter(orderSession => orderSession.id === session.id &&
orderSession.cinemaId === session.cinemaId && orderSession.dateTime === session.dateTime)
})).filter(order => order.sessions.length > 0);
const { totalTicketsSold, revenue } = relevantOrders.reduce((accumulator, order) => {
const session = order.sessions[0];
const tickets = session.count;
const sessionRevenue = tickets * session.frozenPrice;
return {
totalTicketsSold: accumulator.totalTicketsSold + tickets,
revenue: accumulator.revenue + sessionRevenue
};
}, { totalTicketsSold: 0, revenue: 0 });
const cinema = cinemas.find(cinema => cinema.id === session.cinemaId)
2023-12-22 15:18:00 +04:00
report.total_revenue += revenue
report.total_purchased_tickets += totalTicketsSold
2023-12-15 19:49:41 +04:00
return {
cinema_name: cinema ? cinema.name : "Неизвестно",
2023-12-15 19:49:41 +04:00
current_ticket_date_time: session.dateTime,
current_ticket_price: session.price,
max_ticket_quantity: session.maxCount,
purchased_tickets: totalTicketsSold,
revenue: revenue
};
});
2023-12-22 15:18:00 +04:00
report.report_data = reportData.sort((a, b) => b.revenue - a.revenue)
res.json(report);
2023-12-15 19:49:41 +04:00
} catch (error) {
console.error('Error processing report: ', error);
res.status(500).json({ message: 'Internal Server Error' });
}
};