From f51009032339f175580c488462a47127d7abc5d2 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Sat, 13 Apr 2024 20:29:47 +0300 Subject: [PATCH] upgrade stats page --- pages/cart/publishers/stats.tsx | 152 +++++++++++++++++++++++++++----- src/helpers/data.js | 1 + 2 files changed, 129 insertions(+), 24 deletions(-) diff --git a/pages/cart/publishers/stats.tsx b/pages/cart/publishers/stats.tsx index 0a0b738..5c55a68 100644 --- a/pages/cart/publishers/stats.tsx +++ b/pages/cart/publishers/stats.tsx @@ -1,7 +1,7 @@ import { useState } from 'react'; import Layout from "../../../components/layout"; import ProtectedRoute from '../../../components/protectedRoute'; -import { UserRole } from '@prisma/client'; +import { Prisma, UserRole } from '@prisma/client'; import axiosServer from '../../../src/axiosServer'; import common from '../../../src/helpers/common'; // import { filterPublishers, /* other functions */ } from '../../api/index'; @@ -9,10 +9,10 @@ import data from '../../../src/helpers/data'; // const data = require('../../src/helpers/data'); -function ContactsPage({ publishers }) { +function ContactsPage({ publishers, allPublishers }) { const [searchQuery, setSearchQuery] = useState(''); - const filteredPublishers = publishers.filter((publisher) => + const filteredPublishers = allPublishers.filter((publisher) => publisher.firstName.toLowerCase().includes(searchQuery.toLowerCase()) || publisher.lastName.toLowerCase().includes(searchQuery.toLowerCase()) || publisher.email.toLowerCase().includes(searchQuery.toLowerCase()) || @@ -24,7 +24,7 @@ function ContactsPage({ publishers }) {

Статистика

-
{publishers.length} участника с предпочитания
+
{publishers.length} участника с предпочитания (от {allPublishers.length} )
- {filteredPublishers.map((pub) => ( - - {pub.firstName} {pub.lastName} - - - {pub.currentMonthAvailabilityDaysCount || 0} | {pub.currentMonthAvailabilityHoursCount || 0} - - - -
-
- {pub.currentWeekAssignments || 0} - {pub.currentMonthAssignments || 0} - {pub.previousMonthAssignments || 0} - -
-
- - - ))} + {filteredPublishers.map((allPub) => { + // Find the publisher in the publishers collection to access statistics + const pub = publishers.find(publisher => publisher.id === allPub.id); + + return ( + + {allPub.firstName} {allPub.lastName} + {/* Display statistics if publisher is found */} + {pub ? ( + <> + + + {pub.currentMonthAvailabilityDaysCount || 0} | {pub.currentMonthAvailabilityHoursCount || 0} + + + +
+
+ {pub.currentWeekAssignments || 0} + {pub.currentMonthAssignments || 0} + {pub.previousMonthAssignments || 0} + +
+
+ + + ) : ( + <> + + + + +
+
+ + {allPub.currentMonthAssignments || 0} + + + {allPub.previousMonthAssignments || 0} + +
+
+ + {/* Empty cell for alignment */} + + )} + + ); + })}
@@ -74,7 +103,26 @@ function ContactsPage({ publishers }) { export default ContactsPage; + +// Helper functions ToDo: move them to common and replace all implementations with the common ones +function countAssignments(assignments, startTime, endTime) { + return assignments.filter(assignment => + assignment.shift.startTime >= startTime && assignment.shift.startTime <= endTime + ).length; +} + +function convertShiftDates(assignments) { + assignments.forEach(assignment => { + if (assignment.shift && assignment.shift.startTime) { + assignment.shift.startTime = new Date(assignment.shift.startTime).toISOString(); + assignment.shift.endTime = new Date(assignment.shift.endTime).toISOString(); + } + }); +} + export const getServerSideProps = async (context) => { + + const prisma = common.getPrismaClient(); const dateStr = new Date().toISOString().split('T')[0]; let publishers = await data.filterPublishersNew('id,firstName,lastName,email,isActive,desiredShiftsPerMonth', dateStr, false, true, true); @@ -118,9 +166,65 @@ export const getServerSideProps = async (context) => { //remove publishers without availabilities publishers = publishers.filter(publisher => publisher.availabilities.length > 0); + let allPublishers = await prisma.publisher.findMany({ + select: { + id: true, + firstName: true, + lastName: true, + email: true, + phone: true, + isActive: true, + desiredShiftsPerMonth: true, + assignments: { + select: { + id: true, + shift: { + select: { + startTime: true, + endTime: true, + }, + }, + }, + }, + }, + }); + + + + + let monthInfo, + currentMonthStart, currentMonthEnd, + previousMonthStart, previousMonthEnd; + + monthInfo = common.getMonthDatesInfo(new Date()); + currentMonthStart = monthInfo.firstMonday; + currentMonthEnd = monthInfo.lastSunday; + let prevMnt = new Date(); + prevMnt.setMonth(prevMnt.getMonth() - 1); + monthInfo = common.getMonthDatesInfo(prevMnt); + previousMonthStart = monthInfo.firstMonday; + previousMonthEnd = monthInfo.lastSunday; + + + allPublishers.forEach(publisher => { + // Use helper functions to calculate and assign assignment counts + publisher.currentMonthAssignments = countAssignments(publisher.assignments, currentMonthStart, currentMonthEnd); + publisher.previousMonthAssignments = countAssignments(publisher.assignments, previousMonthStart, previousMonthEnd); + + // Convert date formats within the same iteration + convertShiftDates(publisher.assignments); + }); + + // Optionally, if you need a transformed list or additional properties, map the publishers + allPublishers = allPublishers.map(publisher => ({ + ...publisher, + // Potentially add more computed properties or transformations here if needed + })); + return { props: { publishers, + allPublishers, }, }; }; diff --git a/src/helpers/data.js b/src/helpers/data.js index f2e6316..4d1a4fc 100644 --- a/src/helpers/data.js +++ b/src/helpers/data.js @@ -482,6 +482,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false } + function matchesAvailability(avail, filterDate) { // Setting the start and end time of the filterDate filterDate.setHours(0, 0, 0, 0);