fix statistics;

rewrite availability filters;
fix availability filters;
ProtectedRoute.IsInRole helper
This commit is contained in:
Dobromir Popov
2024-04-27 15:31:28 +03:00
parent 029a9af390
commit 58ac046ee5
12 changed files with 267 additions and 138 deletions

View File

@ -78,7 +78,7 @@ exports.getPrismaClient = function getPrismaClient() {
if (!prisma) {
logger.debug("getPrismaClient: process.env.DATABASE = ", process.env.DATABASE);
prisma = new PrismaClient({
// Optional: Enable logging
// Optional: Enable Prisma logging
//log: ['query', 'info', 'warn', 'error'],
datasources: { db: { url: process.env.DATABASE } },
});
@ -757,6 +757,11 @@ exports.getInitials = function (names) {
const parts = names.split(' ');
return parts.map(part => part[0] + ".").join('');
}
// ? import { addMinutes } from 'date-fns';
exports.addMinutes = function (date, minutes) {
return new Date(date.getTime() + minutes * 60000); // 60000 milliseconds in a minute
}
// exports.getInitials = function (names) {
// const parts = names.split(' '); // Split the full name into parts
// if (parts.length === 0) {

View File

@ -260,110 +260,174 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
}
};
var monthInfo = common.getMonthDatesInfo(filterDate);
var weekNr = common.getWeekOfMonth(filterDate); //getWeekNumber
let dayOfWeekEnum = common.getDayOfWeekNameEnEnumForDate(filterDate);
if (!isExactTime) {
filterDate.setHours(0, 0, 0, 0); // Set to midnight
}
const filterDateEnd = new Date(filterDate);
filterDateEnd.setHours(23, 59, 59, 999);
let filterTimeFrom = new Date(filterDate)
let filterTimeTo = new Date(filterDate);
let isDayFilter = true;
let whereClause = {};
//if full day, match by date only
if (!isExactTime) { // Check only by date without considering time ( Assignments on specific days without time)
whereClause["availabilities"] = {
some: {
OR: [
{
startTime: { gte: filterDate },
endTime: { lte: filterDateEnd },
}
,
// Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
// This includes availabilities from previous assignments but not with preference
{
dayOfMonth: null, // includes monthly and weekly repeats
dayofweek: dayOfWeekEnum,
// ToDo: and weekOfMonth
startTime: { lte: filterDate },
AND: [
{
OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
{ endDate: { gte: filterDate } },
{ endDate: null }
]
}
]
}
]
}
};
}
//if not full day, match by date and time
else {
//match exact time (should be same as data.findPublisherAvailability())
whereClause["availabilities"] = {
some: {
OR: [
// Check if dayOfMonth is set and filterDate is between start and end dates (Assignments on specific days AND time)
{
// dayOfMonth: filterDate.getDate(),
startTime: { lte: filterDate },
endTime: { gte: filterDate }
},
// Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
{
dayOfMonth: null,
dayofweek: dayOfWeekEnum,
startTime: { gte: filterDate },
AND: [
{
OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
{ endDate: { gte: filterDate } },
{ endDate: null }
]
}
]
}
]
}
};
}
// if (!isExactTime) { // Check only by date without considering time ( Assignments on specific days without time)
// whereClause["availabilities"] = {
// some: {
// OR: [
// {
// dayOfMonth: { not: null },
// startTime: { gte: filterDate },
// // endTime: { lte: filterDateEnd },
// }
// ,
// // Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
// // This includes availabilities from previous assignments but not with preference
// {
// dayOfMonth: null, // includes monthly and weekly repeats
// dayofweek: dayOfWeekEnum,
// // ToDo: and weekOfMonth
// startTime: { lte: filterDate },
// AND: [
// {
// OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
// { endDate: { gte: filterDate } },
// { endDate: null }
// ]
// }
// ]
// }
// ]
// }
// };
// }
// //if not full day, match by date and time
// else {
// //match exact time (should be same as data.findPublisherAvailability())
// whereClause["availabilities"] = {
// some: {
// OR: [
// // Check if dayOfMonth is set and filterDate is between start and end dates (Assignments on specific days AND time)
// {
// // dayOfMonth: filterDate.getDate(),
// startTime: { gte: filterDate },
// // endTime: { lte: filterDate }
// },
// // Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
// {
// dayOfMonth: null,
// dayofweek: dayOfWeekEnum,
// startTime: { gte: filterDate },
// AND: [
// {
// OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
// { endDate: { gte: filterDate } },
// { endDate: null }
// ]
// }
// ]
// }
// ]
// }
// };
// }
// if (isForTheMonth) {
// // If no filter date, return all publishers's availabilities for currentMonthStart
// whereClause["availabilities"] = {
// some: {
// OR: [
// // Check if dayOfMonth is not null and startTime is after monthInfo.firstMonday (Assignments on specific days AND time)
// {
// dayOfMonth: { not: null },
// startTime: { gte: monthInfo.firstMonday },
// // endTime: { lte: monthInfo.lastSunday }
// },
// // Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
// {
// dayOfMonth: null,
// AND: [
// {
// OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
// { endDate: { gte: filterDate } },
// { endDate: null }
// ]
// }
// ]
// }
// ]
// }
// };
// }
var monthInfo = common.getMonthDatesInfo(filterDate);
if (isForTheMonth) {
// If no filter date, return all publishers's availabilities for currentMonthStart
var weekNr = common.getWeekOfMonth(filterDate); //getWeekNumber
whereClause["availabilities"] = {
some: {
OR: [
// Check if dayOfMonth is not null and startTime is after monthInfo.firstMonday (Assignments on specific days AND time)
{
dayOfMonth: { not: null },
startTime: { gte: monthInfo.firstMonday },
// endTime: { lte: monthInfo.lastSunday }
},
// Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
{
dayOfMonth: null,
AND: [
{
OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
{ endDate: { gte: filterDate } },
{ endDate: null }
]
}
]
}
]
filterTimeFrom = monthInfo.firstMonday;
filterTimeTo = monthInfo.lastSunday;
isDayFilter = false;
}
if (isExactTime) {
//add +- 90 minutes to the filterDate ToDo: should be "shift duration"
// filterTimeFrom.setMinutes(filterTimeFrom.getMinutes() - 90);
filterTimeTo.setMinutes(filterTimeTo.getMinutes() + 90);
}
else {
filterTimeFrom.setHours(0, 0, 0, 0);
filterTimeTo.setHours(23, 59, 59, 999);
}
whereClause["availabilities"] = {
some: {
OR: [
// Check if dayOfMonth is not null and startTime is after monthInfo.firstMonday (Assignments on specific days AND time)
{
//dayOfMonth: { not: null },
startTime: { gte: filterTimeFrom },
// endTime: { lte: monthInfo.lastSunday }
},
// Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
{
dayOfMonth: null,
AND: [
{
OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
{ endDate: { gte: filterTimeFrom } }, // endDate included
{ endDate: null }
]
}
]
}
]
}
};
/* FILTERS
1. exact time
2. exact date
3. the month
4. from start date only
*/
if (noEndDateFilter) {
isDayFilter = false;
}
else {
whereClause["availabilities"].some.OR[0].endTime = { lte: filterTimeTo };
if (isForTheMonth) {
// no dayofweek or time filters here
}
else {
let dayOfWeekEnum = common.getDayOfWeekNameEnEnumForDate(filterDate);
whereClause["availabilities"].some.OR[1].dayofweek = dayOfWeekEnum;
//NOTE: we filter by date after we calculate the correct dates post query
if (isExactTime) {
//if exact time we need the availability to be starting on or before start of the shift and ending on or after the end of the shift
whereClause["availabilities"].some.OR[0].startTime = { lte: filterTimeFrom };
whereClause["availabilities"].some.OR[0].endTime = { gte: filterTimeTo };
}
else {
}
};
if (!noEndDateFilter) { // Check if we need to apply the endTime filter
whereClause["availabilities"].some.OR[0].endTime = { lte: monthInfo.lastSunday };
}
}
console.log(`getting publishers for date: ${filterDate}, isExactTime: ${isExactTime}, isForTheMonth: ${isForTheMonth}`);
//include availabilities if flag is true
const prisma = common.getPrismaClient(); //why we need to get it again?
@ -377,7 +441,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
console.log(`publishers: ${publishers.length}, WhereClause: ${JSON.stringify(whereClause)}`);
// convert matching weekly availabilities to availabilities for the day to make furter processing easier on the client.
// convert matching weekly availabilities to availabilities for the day to make further processing easier on the client.
// we trust that the filtering was OK, so we use the dateFilter as date.
publishers.forEach(pub => {
pub.availabilities = pub.availabilities.map(avail => {
@ -415,10 +479,11 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
//get if publisher has assignments for current weekday, week, current month, previous month
publishers.forEach(pub => {
// Filter assignments for current day
pub.currentDayAssignments = pub.assignments?.filter(assignment => {
return assignment.shift.startTime >= filterDate && assignment.shift.startTime <= filterDateEnd;
}).length;
if (isDayFilter) {
pub.currentDayAssignments = pub.assignments?.filter(assignment => {
return assignment.shift.startTime >= filterDate && assignment.shift.startTime <= filterTimeTo;
}).length;
}
// Filter assignments for current week
pub.currentWeekAssignments = pub.assignments?.filter(assignment => {
return assignment.shift.startTime >= currentWeekStart && assignment.shift.startTime <= currentWeekEnd;
@ -426,7 +491,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
// Filter assignments for current month
pub.currentMonthAssignments = pub.assignments?.filter(assignment => {
return assignment.shift.startTime >= currentMonthStart && assignment.shift.startTime <= currentMonthEnd;
return assignment.shift.startTime >= currentMonthStart && (noEndDateFilter || assignment.shift.startTime <= currentMonthEnd);
}).length;
// Filter assignments for previous month
@ -437,7 +502,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
}
//get the availabilities for the day. Calcullate:
//get the availabilities for the day. Calculate:
//1. how many days the publisher is available for the current month - only with dayOfMonth
//2. how many days the publisher is available without dayOfMonth (previous months count)
//3. how many hours in total the publisher is available for the current month
@ -445,7 +510,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
if (isWithStats) {
pub.currentMonthAvailability = pub.availabilities?.filter(avail => {
// return avail.dayOfMonth != null && avail.startTime >= currentMonthStart && avail.startTime <= currentMonthEnd;
return avail.startTime >= currentMonthStart && avail.startTime <= currentMonthEnd;
return avail.startTime >= currentMonthStart && (noEndDateFilter || avail.startTime <= currentMonthEnd);
})
pub.currentMonthAvailabilityDaysCount = pub.currentMonthAvailability.length || 0;
// pub.currentMonthAvailabilityDaysCount += pub.availabilities.filter(avail => {
@ -465,37 +530,47 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
pub.hasEverFilledForm = pub.availabilities?.some(avail => {
return avail.isFromPreviousAssignments == false;
});
//if pub has availabilities for the current day
pub.hasAvailabilityForCurrentDay = pub.availabilities?.some(avail => {
return avail.startTime >= filterDate && avail.startTime <= filterDateEnd;
});
if (isDayFilter) {
//if pub has availabilities for the current day
pub.hasAvailabilityForCurrentDay = pub.availabilities?.some(avail => {
return avail.startTime >= filterDate && avail.startTime <= filterTimeTo;
});
}
});
if (isExactTime) {
// Post filter for time if dayOfMonth is null as we can't only by time for multiple dates in SQL
// Modify the availabilities array of the filtered publishers
//HERE WE FILTER by time for repeating availabilities. We can't do that if we don't have
// whereClause["availabilities"].some.OR[1].startTime = { gte: filterTimeFrom };
// whereClause["availabilities"].some.OR[1].endTime = { gte: filterTimeTo }
publishers.forEach(pub => {
pub.availabilities = pub.availabilities?.filter(avail => matchesAvailability(avail, filterDate));
pub.availabilities.filter(a => a.startTime > filterTimeFrom && a.endTime < filterTimeTo)
});
publishers.filter(pub => pub.availabilities.length > 0);
}
// if (isExactTime) {
// // Post filter for time if dayOfMonth is null as we can't only by time for multiple dates in SQL
// // Modify the availabilities array of the filtered publishers
// publishers.forEach(pub => {
// pub.availabilities = pub.availabilities?.filter(avail => matchesAvailability(avail, filterDate));
// });
// }
return publishers;
}
function matchesAvailability(avail, filterDate) {
// Setting the start and end time of the filterDate
filterDate.setHours(0, 0, 0, 0);
const filterDateEnd = new Date(filterDate);
filterDateEnd.setHours(23, 59, 59, 999);
// function matchesAvailability(avail, filterDate) {
// // Setting the start and end time of the filterDate
// filterDate.setHours(0, 0, 0, 0);
// const filterDateEnd = new Date(filterDate);
// filterDateEnd.setHours(23, 59, 59, 999);
// Return true if avail.startTime is between filterDate and filterDateEnd
return avail.startTime >= filterDate && avail.startTime <= filterDateEnd;
}
// // Return true if avail.startTime is between filterDate and filterDateEnd
// return avail.startTime >= filterDate && avail.startTime <= filterDateEnd;
// }
const fs = require('fs');
const path = require('path');