availability serf reference;

fix copying last month;
This commit is contained in:
Dobromir Popov
2024-03-28 22:55:35 +02:00
parent a6b7c62768
commit a26dd954c0
11 changed files with 163 additions and 90 deletions

View File

@ -1,6 +1,6 @@
import { getToken } from "next-auth/jwt";
import { NextApiRequest, NextApiResponse } from 'next'
import { DayOfWeek } from '@prisma/client';
import { DayOfWeek, AvailabilityType } from '@prisma/client';
const common = require('../../src/helpers/common');
const data = require('../../src/helpers/data');
const subq = require('../../prisma/bl/subqueries');
@ -221,7 +221,7 @@ export default async function handler(req, res) {
// isFromPreviousMonth: true
// }
// });
let oldpubs = await prisma.publisher.findMany({
let outdatedPubs = await prisma.publisher.findMany({
where: {
availabilities: {
none: {
@ -238,28 +238,55 @@ export default async function handler(req, res) {
availabilities: true
}
});
oldpubs.forEach(async pub => {
outdatedPubs.forEach(async pub => {
// avail.startTime >= monthInfo.firstMonday
//get prev month date:
let prevMonth = new Date(monthInfo.firstMonday);
prevMonth.setMonth(prevMonth.getMonth() - 1);
let prevMonthInfo = common.getMonthDatesInfo(prevMonth);
pub.availabilities = pub.availabilities.filter(avail => avail.startTime > prevMonthInfo.firstMonday);
console.log("" + pub.firstName + " " + pub.lastName + " copying " + pub.availabilities.length + " availabilities from previous months.");
pub.availabilities.forEach(async avail => {
//get the new date based on the day of week and week of month
let newStart = common.getDateFromWeekNrAndDayOfWeek(avail.weekNr, avail.dayofweek, avail.startTime);
if (!avail.weekOfMonth) {
avail.weekOfMonth = common.getWeekOfMonth(avail.startTime)
}
let origMonthInfo = common.getMonthDatesInfo(avail.startTime);
let newStart = common.getDateFromWeekNrAndDayOfWeek(monthInfo.firstMonday, avail.weekOfMonth, avail.dayofweek, avail.startTime);
//ToDo: fix double check. also check if we're in 5th week and the month has 4 weeks
// const availability = await data.findPublisherAvailability(publisher.id, newStart);
// if (availability) {
// return;
// }
let newEnd = new Date(newStart.getTime());
newEnd.setHours(avail.endTime.getHours(), avail.endTime.getMinutes(), 0, 0);
await prisma.availability.create({
data: {
publisherId: pub.id,
dayOfMonth: null,
dayofweek: avail.dayofweek || common.getDayOfWeekNameEnEnum(avail.startTime),
weekOfMonth: avail.weekofMonth || common.getWeekOfMonth(avail.startTime),
dateOfEntry: new Date(), //avail.dateOfEntry || avail.startTime,
startTime: avail.startTime,
endTime: avail.endTime,
weekNr: avail.weekNr,
type: 3,
isFromPreviousMonth: true,
name: avail.name || "старо предпочитание",
}
});
let data = {
publisherId: pub.id,
dayOfMonth: null,
dayofweek: avail.dayofweek || common.getDayOfWeekNameEnEnum(avail.startTime),
weekOfMonth: avail.weekofMonth || common.getWeekOfMonth(avail.startTime),
dateOfEntry: new Date(), //avail.dateOfEntry || avail.startTime,
startTime: newStart,
endTime: newEnd,
type: AvailabilityType.Monthly,
isFromPreviousMonth: true,
name: avail.name || "старо предпочитание",
parentAvailabilityId: avail.id
}
await prisma.availability.create({ data: data });
//if month has 5 weeks and the monthInfo has 4 weeks copy the availabilities also from the 1st week to the 5th week
if (monthInfo.nrOfWeeks == 5 && avail.weekOfMonth == 1 && origMonthInfo.nrOfWeeks == 4) {
newStart = common.getDateFromWeekNrAndDayOfWeek(monthInfo.firstMonday, 5, avail.dayofweek, avail.startTime);
newEnd = new Date(newStart.getTime());
newEnd.setHours(avail.endTime.getHours(), avail.endTime.getMinutes(), 0, 0);
data.weekOfMonth = 5;
data.startTime = newStart;
data.endTime = newEnd;
await prisma.availability.create({ data: data });
}
});
});
@ -531,8 +558,8 @@ export async function filterPublishers(selectFields, searchText, filterDate, fet
// also, permanent weekly availabilities will have dayOfMonth = null and type = 0
// for 0 we will match by dayOfWeekEnum and times
// for 1 we will match by exact date and times
// for 2 we will match by dayofweek, weeknr and times
// for 3 we will match by dayofweek, weeknr and times - this is the same as 2, but we will not count them as availabilities for the current month
// for 2 we will match by dayofweek, weekOfMonth and times
// for 3 we will match by dayofweek, weekOfMonth and times - this is the same as 2, but we will not count them as availabilities for the current month
// generaion of schedule:
@ -562,7 +589,7 @@ export async function filterPublishers(selectFields, searchText, filterDate, fet
{
dayOfMonth: null,
dayofweek: dayOfWeekEnum,
// ToDo: and weekNr
// ToDo: and weekOfMonth
//startTime: { gte: currentMonthStart },
}
]
@ -614,7 +641,7 @@ export async function filterPublishers(selectFields, searchText, filterDate, fet
// dayOfMonth: true,
// startTime: true,
// endTime: true,
// weekNr: true,
// weekOfMonth: true,
// type: true
// },
// where: {