From c27dba5860360a341242cf2a91d668fa2a2913ed Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Wed, 6 Mar 2024 01:44:05 +0200 Subject: [PATCH] restore deleted by accident source --- pages/cart/calendar/index.tsx | 86 ++++++++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 12 deletions(-) diff --git a/pages/cart/calendar/index.tsx b/pages/cart/calendar/index.tsx index 77a02a4..e853d4b 100644 --- a/pages/cart/calendar/index.tsx +++ b/pages/cart/calendar/index.tsx @@ -14,12 +14,9 @@ const common = require('src/helpers/common'); import { toast } from 'react-toastify'; import ProtectedRoute from '../../../components/protectedRoute'; import ConfirmationModal from '../../../components/ConfirmationModal'; -import axiosServer from '../../../src/axiosServer'; - - -// import { FaPlus, FaCogs, FaTrashAlt, FaSpinner } from 'react-icons/fa'; // Import FontAwesome icons import LocalShippingIcon from '@mui/icons-material/LocalShipping'; +// import { FaPlus, FaCogs, FaTrashAlt, FaSpinner } from 'react-icons/fa'; // Import FontAwesome icons @@ -112,12 +109,6 @@ export default function CalendarPage({ initialEvents, initialShifts }) { const { data: shiftsForDate } = await axiosInstance.get(`/api/?action=getShiftsForDay&date=${dateStr}`); setShifts(shiftsForDate); let { data: availablePubsForDate } = await axiosInstance.get(`/api/?action=filterPublishers&assignments=true&availabilities=true&date=${dateStr}&select=id,firstName,lastName,isactive,desiredShiftsPerMonth`); - availablePubsForDate.forEach(pub => { - pub.canTransport = pub.availabilities.some(av => - av.isWithTransportIn || av.isWithTransportOut - ); - }); - //remove availabilities that are isFromPreviousAssignment or from previous month for each publisher // availablePubsForDate = availablePubsForDate.map(pub => { // pub.availabilities = pub.availabilities.filter(avail => avail.isFromPreviousAssignment == false); @@ -150,6 +141,7 @@ export default function CalendarPage({ initialEvents, initialShifts }) { ); if (av) { pub.isAvailableForShift = true; + pub.canTransport = av.isWithTransportIn || av.isWithTransportOut; } // const isAvailableForShift = pub.availabilities.some(avail => @@ -335,7 +327,7 @@ export default function CalendarPage({ initialEvents, initialShifts }) { var classname = ""; if (events == null) { - return
{" - "}
; + return
{" "}
; } const event = events.find((event) => { @@ -682,7 +674,7 @@ export default function CalendarPage({ initialEvents, initialShifts }) { > {pub.firstName} {pub.lastName} - {pub.canTransport && ()} + {pub.canTransport && ()}
@@ -713,6 +705,10 @@ export default function CalendarPage({ initialEvents, initialShifts }) {
+ +
+ {/* */} +
{isModalOpen && setIsModalOpen(false)} />} @@ -859,3 +855,69 @@ export default function CalendarPage({ initialEvents, initialShifts }) { } } } + +import axiosServer from '../../../src/axiosServer'; +import { start } from 'repl'; +export const getServerSideProps = async (context) => { + const axios = await axiosServer(context); + const baseUrl = common.getBaseUrl(); + console.log('runtime BaseUrl: ' + baseUrl); + console.log('runtime NEXTAUTH_URL: ' + process.env.NEXTAUTH_URL); + console.log('Runtime Axios Base URL:', axios.defaults.baseURL); + + const currentDate = new Date(); + const firstDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() - 3, 1); + const lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0); // 0th day of the next month gives the last day of the current month + + const url = `/api/data/shifts?where={"startTime":{"$and":[{"$gte":"${common.getISODateOnly(firstDayOfMonth)}","$lt":"${common.getISODateOnly(lastDayOfMonth)}"}]}}`; + + const prismaClient = common.getPrismaClient(); + // let events = await prismaClient.cartEvent.findMany({ where: { isactive: true } }); + // events = events.map(event => ({ + // ...event, + // // Convert Date objects to ISO strings + // startTime: event.startTime.toISOString(), + // endTime: event.endTime.toISOString(), + // })); + const { data: events } = await axios.get(`/api/data/cartevents?where={"isactive":true}`); + //const { data: shifts } = await axios.get(url); + + // get all shifts for the month, including assigments + let shifts = await prismaClient.shift.findMany({ + where: { + isactive: true, + startTime: { + gte: firstDayOfMonth, + //lt: lastDayOfMonth + } + }, + include: { + assignments: { + include: { + publisher: { + select: { + id: true, + } + } + } + } + } + }); + + //calculate assCount for each shift + shifts = shifts.map(shift => ({ + ...shift, + assignedCount: shift.assignments.length, + startTime: shift.startTime.toISOString(), + endTime: shift.endTime.toISOString(), + })); + + + return { + props: { + initialEvents: events, + initialShifts: shifts, + }, + }; + +} \ No newline at end of file