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

@ -248,6 +248,33 @@ exports.getWeekOfMonth = function (inputDate) {
return weekNumber;
};
exports.getDateFromWeekNrAndDayOfWeek = function (firstMonday, weekNr, dayOfWeekEnum, startTime) {
firstMonday = new Date(firstMonday);
startTime = new Date(startTime);
if (!weekNr || weekNr < 1 || weekNr > 5) {
weekNr = this.getWeekOfMonth(startTime);
}
//get int from dayOfWeekEnum
let dayOfWeekNr = this.getDayOfWeekIndex(dayOfWeekEnum);
if (dayOfWeekNr < 0 || dayOfWeekNr > 6) {
dayOfWeekNr = 0;
}
// Calculate the day offset from the first Monday of the month
// Note: Assuming dayOfWeekEnum starts from 0 (Monday) to 6 (Sunday)
const daysFromFirstMonday = (weekNr - 1) * 7 + dayOfWeekNr;
// Calculate the new date
let newStart = new Date(firstMonday);
newStart.setDate(firstMonday.getDate() + daysFromFirstMonday);
// Extract time from startTime and apply it to newStart
const time = new Date(startTime);
newStart.setHours(time.getHours(), time.getMinutes(), time.getSeconds());
return newStart;
}
exports.getMonthDatesInfo = function (date) {
// get first day of the month
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
@ -285,6 +312,9 @@ exports.getMonthDatesInfo = function (date) {
// lastSunday.setDate(firstDayNextMonth.getDate() - firstDayNextMonth.getDay());
//logger.debug("Last Sunday: ", lastSunday);
const diffInDays = (lastSunday - firstMonday) / (1000 * 60 * 60 * 24);
// Calculate number of weeks, rounding up for partial weeks
const nrOfWeeks = Math.ceil((diffInDays + 1) / 7);
return {
firstDay: firstDay,
@ -295,7 +325,7 @@ exports.getMonthDatesInfo = function (date) {
date: date,
monthName: monthName,
year: date.getFullYear(),
nrOfWeeks: Math.ceil((lastMonday.getDate() - firstMonday.getDate()) / 7)
nrOfWeeks: nrOfWeeks
};
};
exports.getMonthInfo = exports.getMonthDatesInfo;

View File

@ -79,10 +79,7 @@ async function findPublisher(names, email, select, getAll = false) {
}
async function findPublisherAvailability(publisherId, date) {
const prisma = common.getPrismaClient();
const dayOfWeek = common.getDayOfWeekNameEnEnum(date); // Assuming common.getDayOfWeekNameEnEnum returns the day of week
//const weekOfMonth = common.getWeekOfMonth(date); // Assuming common.getWeekOfMonth returns the week of month
date = new Date(date); // Convert to date object if not already
const hours = date.getHours();
const minutes = date.getMinutes();
@ -90,32 +87,24 @@ async function findPublisherAvailability(publisherId, date) {
const potentialAvailabilities = await prisma.availability.findMany({
where: {
publisherId: publisherId,
OR: [
AND: [ // Ensure both conditions must be met
{
// Exact date match
startTime: {
gte: new Date(date.setHours(0, 0, 0, 0)),
lt: new Date(date.setHours(23, 59, 59, 999))
}
lte: new Date(date), // startTime is less than or equal to the date
},
},
{
// Correct day of week and before the date, with endDate consideration
dayofweek: dayOfWeek,
OR: [
{
endDate: null
},
{
endDate: {
gt: date
}
}
]
}
]
endTime: {
gte: new Date(date), // endTime is greater than or equal to the date
},
},
],
}
});
if (potentialAvailabilities.length === 0) {
return null; // No availability found
}
// Filter the results based on time and other criteria when not exact date match
const availability = potentialAvailabilities.find(avail => {
const availStartHours = avail.startTime.getHours();

View File

@ -481,23 +481,22 @@ exports.processEvents = async function (events, year, monthNumber, progressCallb
// create availability with the same date as the event.
//ToDo: add parameter to control if we want to create availability for each event. can be done whe we import previous shifts.
// if (createAvailabilities) {
// const dayofWeek = common.getDayOfWeekNameEnEnum(date);
// const availability = await prisma.availability.create({
// data: {
// publisherId: publisher.id,
// //date: date,
// dayofweek: dayofWeek,
// startTime: startTime,
// endTime: endTime,
// name: `от предишен график, ${publisher.firstName} ${publisher.lastName}`,
// isFromPreviousAssignment: true,
// isActive: true,
// },
// });
// console.log(`Created WEEKLY availability with ID ${availability.id} for date '${date.toDateString()}' and publisher '${publisher.firstName} ${publisher.lastName}'`);
// }
// const personResponse = await axiosInstance.post("/publishers", manualPub);
if (createAvailabilities) {
const dayofWeek = common.getDayOfWeekNameEnEnum(date);
const availability = await prisma.availability.create({
data: {
publisherId: publisher.id,
dayofweek: dayofWeek,
startTime: startTime,
endTime: endTime,
name: `от график, ${publisher.firstName} ${publisher.lastName}`,
isFromPreviousAssignment: true,
isActive: true,
},
});
console.log(`Created WEEKLY availability with ID ${availability.id} for date '${date.toDateString()}' and publisher '${publisher.firstName} ${publisher.lastName}'`);
}
const personResponse = await axiosInstance.post("/publishers", manualPub);
// let personId = personResponse.data.id;
} catch (e) {
@ -533,23 +532,26 @@ exports.processEvents = async function (events, year, monthNumber, progressCallb
});
//ToDo: fix findPublisherAvailability and creation of availabilities
// check if there is an availability for this publisher on this date, and if not, create one
// const availability = await data.findPublisherAvailability(publisher.id, start);
// if (!availability && createAvailabilities) {
// const dayofWeek = common.getDayOfWeekNameEnEnum(date);
// const availability = await prisma.availability.create({
// data: {
// publisherId: publisher.id,
// //date: date,
// dayofweek: dayofWeek,
// //weekOfMonth: common.getWeekOfMonth(date),
// startTime: start,
// endTime: end,
// name: `от предишен график, ${publisher.firstName} ${publisher.lastName}`,
// isFromPreviousAssignment: true,
// },
// });
// console.log(`Created WEEKLY availability with ID ${availability.id} for date '${date.toDateString()}' and publisher '${publisher.firstName} ${publisher.lastName}'`);
// }
//ToDo: check if that works
const availability = await data.findPublisherAvailability(publisher.id, start);
if (!availability && createAvailabilities) {
const dayofWeek = common.getDayOfWeekNameEnEnum(date);
const availability = await prisma.availability.create({
data: {
publisherId: publisher.id,
//date: date,
dayofweek: dayofWeek,
//weekOfMonth: common.getWeekOfMonth(date),
startTime: start,
endTime: end,
name: `от предишен график, ${publisher.firstName} ${publisher.lastName}`,
isFromPreviousAssignment: true,
isWithTransportIn: isWithTransport && event.shiftNr == 1,
isWithTransportOut: isWithTransport && event.shiftNr > 1,
},
});
console.log(`Created SYSTEM availability with ID ${availability.id} for date '${date.toDateString()}' and publisher '${publisher.firstName} ${publisher.lastName}'`);
}
console.log(`Created assignment with ID ${assignment.id} for date '${date.toDateString()}' and location '${event.placeOfEvent}'. publisher: ${publisher.firstName} ${publisher.lastName}}`);
}

View File

@ -1,4 +0,0 @@
//??? can we consolidate all imports into one file?
import ProtectedRoute from '../../../components/protectedRoute';
import axiosInstance from '../../../src/axiosSecure';
import Layout from "../../../components/layout";