diff --git a/pages/api/index.ts b/pages/api/index.ts index 6979920..f5e5d14 100644 --- a/pages/api/index.ts +++ b/pages/api/index.ts @@ -7,6 +7,7 @@ const subq = require('../../prisma/bl/subqueries'); import fs from 'fs'; import path from 'path'; +import { all } from "axios"; /** * @@ -29,7 +30,7 @@ export default async function handler(req, res) { var action = req.query.action; var filter = req.query.filter; - let date: Date; + let date: Date, monthInfo: any; if (req.query.date) { date = new Date(req.query.date); //date.setDate(date.getDate()); // Subtract one day to get the correct date, as calendar sends wrong date (one day ahead) @@ -77,7 +78,7 @@ export default async function handler(req, res) { //gets publisher by names with availabilities and assignments case "deleteAvailabilityForPublisher": let publisherId = req.query.publisherId; - let dateFor, monthInfo; + let dateFor; if (req.query.date) { dateFor = new Date(req.query.date); //get month info from date @@ -212,11 +213,31 @@ export default async function handler(req, res) { case "replaceInAssignment": const { oldPublisherId, newPublisherId, shiftId } = req.method === "POST" ? req.body : req.query; - const prisma = common.getPrismaClient(); + const result = await replaceInAssignment(oldPublisherId, newPublisherId, shiftId); res.status(200).json(result); break; + case "updateShifts": + //get all shifts for the month and publish them (we pass date ) + let monthInfo = common.getMonthDatesInfo(date); + let isPublished = common.parseBool(req.query.isPublished); + let updated = await prisma.shift.updateMany({ + where: { + startTime: { + gte: new Date(monthInfo.firstMonday.getFullYear(), monthInfo.firstMonday.getMonth(), 1), + lt: new Date(monthInfo.lastSunday.getFullYear(), monthInfo.lastSunday.getMonth() + 1, 1), + } + }, + data: { + isPublished: isPublished + } + }); + console.log("Updated shifts: " + updated.count); + res.status(200).json({ "message": "ok" }); + + break; + default: res.status(200).json({ "message": "no action '" + action + "' found" diff --git a/pages/api/schedule.ts b/pages/api/schedule.ts index e322568..087ce81 100644 --- a/pages/api/schedule.ts +++ b/pages/api/schedule.ts @@ -90,6 +90,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const shifts = await prisma.shift.findMany({ where: { isActive: true, + isPublished: true, startTime: { gte: fromDate, lt: toDate, diff --git a/pages/cart/calendar/index.tsx b/pages/cart/calendar/index.tsx index 77b2d4a..f183eb5 100644 --- a/pages/cart/calendar/index.tsx +++ b/pages/cart/calendar/index.tsx @@ -57,7 +57,7 @@ export default function CalendarPage({ initialEvents, initialShifts }) { const [allShifts, setAllShifts] = useState(initialShifts); - const [value, onChange] = useState(new Date()); + const [isPublished, setIsPublished] = useState(() => initialShifts.some(shift => shift.isPublished)); const [value, onChange] = useState(new Date()); const [shifts, setShifts] = React.useState([]); const [error, setError] = React.useState(null); const [availablePubs, setAvailablePubs] = React.useState([]); @@ -108,6 +108,7 @@ export default function CalendarPage({ initialEvents, initialShifts }) { try { const { data: shiftsForDate } = await axiosInstance.get(`/api/?action=getShiftsForDay&date=${dateStr}`); setShifts(shiftsForDate); + setIsPublished(shiftsForDate.some(shift => shift.isPublished)); let { data: availablePubsForDate } = await axiosInstance.get(`/api/?action=filterPublishers&assignments=true&availabilities=true&date=${dateStr}&select=id,firstName,lastName,isActive,desiredShiftsPerMonth`); availablePubsForDate.forEach(pub => { @@ -511,6 +512,18 @@ export default function CalendarPage({ initialEvents, initialShifts }) { } } + const togglePublished = async () => { + try { + const publishState = !isPublished; // Toggle the state + const isPublishedParam = publishState ? 'true' : 'fasle'; + await axiosInstance.get(`/api/?action=updateShifts&isPublished=${isPublishedParam}&date=${common.getISODateOnly(value)}`); + setIsPublished(publishState); // Update state based on the action + + } catch (error) { + console.log(error); + } + } + const [isMenuOpen, setIsMenuOpen] = useState(false); @@ -550,6 +563,12 @@ export default function CalendarPage({ initialEvents, initialShifts }) { }} message="Това ще изпрати имейли до всички участници за смените им през избрания месец. Сигурни ли сте?" /> +