fix shidfs popup, maybe found a working do-it-all publishers data API functoin (ToDo: implement separate and simple fns if it does not work)

This commit is contained in:
Dobromir Popov
2024-05-28 16:44:10 +03:00
parent 9ad77bec38
commit 59dd7cd7b4
4 changed files with 33 additions and 17 deletions

View File

@ -210,6 +210,7 @@ async function getAvailabilities(userId) {
* Filters publishers based on various criteria including exact times, monthly duration,
* and whether or not to include statistics about publishers' availabilities and assignments.
* This function heavily relies on the `prisma` client to query and manipulate data related to publishers.
* (ToDo: implement separate and simple fns if it does not work)
*
* @param {Array|string} selectFields - Fields to select from the publishers data. Can be an array of field names or a comma-separated string of field names.
* @param {string|Date|null} filterDate - The reference date for filtering. Can be a date string, a Date object, or null. Used to determine relevant time frames like current month, previous month, etc.
@ -221,7 +222,8 @@ async function getAvailabilities(userId) {
*
* @returns {Promise<Array>} Returns a promise that resolves to an array of publishers with filtered data according to the specified criteria.
*/
async function filterPublishersNew(selectFields, filterDate, isExactTime = false, isForTheMonth = false, noEndDateFilter = false, isWithStats = true, includeOldAvailabilities = false, id = null) {
// (ToDo: implement separate and simple fns if it does not work)
async function filterPublishersNew(selectFields, filterDate, isExactTime = false, isForTheMonth = false, noEndDateFilter = false, isWithStats = true, includeOldAvailabilities = false, id = null, filterAvailabilitiesByDate = true) {
const prisma = common.getPrismaClient();
@ -327,9 +329,9 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
// startTime: { gte: filterTimeFrom },
AND: [
{ dayOfMonth: null },
{ dayofweek: dayOfWeekEnum },
//{ dayofweek: dayOfWeekEnum }, // we want all days of the week for now
// moved down to conditional filters
{ startTime: { lte: filterTimeTo } }, // startTime included
// { startTime: { lte: filterTimeTo } }, // we ignore startTime as it will be filtered later only by the time and not by date.
{
OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
{ endDate: { gte: filterTimeFrom } }, // endDate included
@ -360,7 +362,9 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
}
else {
//moved to upper qery as it is now also dependant on date filter
whereClause["availabilities"].some.OR[1].dayofweek = dayOfWeekEnum;
if ((isDayFilter || filterAvailabilitiesByDate) && !isForTheMonth) {
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
@ -391,10 +395,12 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
publishers.forEach(pub => {
pub.availabilities = pub.availabilities.map(avail => {
if (avail.dayOfMonth == null) {
// filter out repeating availabilities when on other day of week
if (filterTimeFrom) {
if (avail.dayofweek != dayOfWeekEnum) {
return null;
if (filterAvailabilitiesByDate && !isForTheMonth) {
// filter out repeating availabilities when on other day of week
if (filterTimeFrom) {
if (avail.dayofweek != dayOfWeekEnum) {
return null;
}
}
}
let newStart = new Date(filterDate);
@ -407,7 +413,15 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
endTime: newEnd
}
}
return avail;
else {
if (filterAvailabilitiesByDate && !isForTheMonth) {
if (avail.startTime >= filterTimeFrom && avail.startTime <= filterTimeTo) {
return avail;
}
return null;
}
return avail;
}
})
.filter(avail => avail !== null);
});
@ -454,7 +468,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
pub.currentMonthAvailability = pub.availabilities?.filter(avail => {
// return avail.dayOfMonth != null && avail.startTime >= currentMonthStart && avail.startTime <= monthInfo.lastSunday;
return (avail.startTime >= monthInfo.firstMonday && (noEndDateFilter || avail.startTime <= monthInfo.lastSunday))
|| (avail.dayOfMonth == null); // include repeating availabilities
|| (avail.dayOfMonth == null && avail.startTime <= monthInfo.firstMonday); // include repeating availabilities
})
pub.currentMonthAvailabilityDaysCount = pub.currentMonthAvailability.length;
// pub.currentMonthAvailabilityDaysCount += pub.availabilities.filter(avail => {
@ -512,7 +526,7 @@ async function getAllPublishersWithStatisticsMonth(filterDateDuringMonth, noEndD
const monthInfo = common.getMonthDatesInfo(new Date(filterDateDuringMonth));
const dateStr = new Date(monthInfo.firstMonday).toISOString().split('T')[0];
let publishers = await filterPublishersNew('id,firstName,lastName,email,isActive,desiredShiftsPerMonth,lastLogin,type', dateStr, false, true, noEndDateFilter, true, true);
let publishers = await filterPublishersNew('id,firstName,lastName,email,isActive,desiredShiftsPerMonth,lastLogin,type', dateStr, false, true, noEndDateFilter, true, true, null, false);
// const axios = await axiosServer(context);
// const { data: publishers } = await axios.get(`api/?action=filterPublishers&assignments=true&availabilities=true&date=${dateStr}&select=id,firstName,lastName,isActive,desiredShiftsPerMonth`);