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

@ -147,14 +147,16 @@ export default async function handler(req, res) {
res.status(200).json(events);
case "getPublisherInfo":
let pubs = await filterPublishers("id,firstName,lastName,email".split(","), "", null, req.query.assignments || true, req.query.availabilities || true, false, req.query.id);
// let pubs = await filterPublishers("id,firstName,lastName,email".split(","), "", null, req.query.assignments || true, req.query.availabilities || true, false, req.query.id);
let pubs = await dataHelper.filterPublishersNew("id,firstName,lastName,email,isActive,assignments,availabilities", day, false, true, false, true, false, req.query.id, true);
let pub = pubs[0] || {};
if (pub) {
let dayOfWeekQuery = common.getDayOfWeek(day);
pub.availabilities = pub.availabilities.map(avail => {
if (avail.dayOfMonth == null) {
let dayOfWeek = common.getDayOfWeek(avail.startTime);
let dayOfWeek = common.getDayOfWeekIndex(avail.dayofweek);
let newStart = new Date(day);
newStart = addDays(newStart, dayOfWeek - dayOfWeekQuery);
newStart.setHours(avail.startTime.getHours(), avail.startTime.getMinutes(), 0, 0);

View File

@ -996,9 +996,9 @@ async function RankPublishersForShiftWeighted(publishers, scheduledPubsPerDayAnd
// Log the scores and penalties of the top publisher
if (ranked.length > 0) {
console.log(`Top Publisher: ${ranked[0].name}`);
console.log(`Score: ${ranked[0].score}`);
console.log(`Penalties:`, ranked[0].penalties);
console.log(`Top Publisher: ${ranked[0].firstName} ${ranked[0].lastName}`,
` Score: ${ranked[0].score}`, "last score: ", ranked[ranked.length - 1].score,
` Penalties: `, ranked[0].penalties);
}
return ranked;

View File

@ -866,7 +866,7 @@ export default function CalendarPage({ initialEvents, initialShifts }) {
const hasAssignment = (shiftId) => {
// return publisher.assignments.some(ass => ass.shift.id == shiftId);
return publisher.assignments?.some(ass => {
console.log(`Comparing: ${ass.shift.id} to ${shiftId}: ${ass.shift.id === shiftId}`);
//console.log(`Comparing: ${ass.shift.id} to ${shiftId}: ${ass.shift.id === shiftId}`);
return ass.shift.id === shiftId;
});
};

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`);