fix filters
This commit is contained in:
@ -233,6 +233,11 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
|
|||||||
const prisma = common.getPrismaClient();
|
const prisma = common.getPrismaClient();
|
||||||
filterDate = new Date(filterDate); // Convert to date object if not already
|
filterDate = new Date(filterDate); // Convert to date object if not already
|
||||||
|
|
||||||
|
const monthInfo = common.getMonthDatesInfo(filterDate);
|
||||||
|
let prevMnt = new Date(filterDate)
|
||||||
|
prevMnt.setMonth(prevMnt.getMonth() - 1);
|
||||||
|
const prevMonthInfo = common.getMonthDatesInfo(prevMnt);
|
||||||
|
|
||||||
// Only attempt to split if selectFields is a string; otherwise, use it as it is.
|
// Only attempt to split if selectFields is a string; otherwise, use it as it is.
|
||||||
selectFields = typeof selectFields === 'string' ? selectFields.split(",") : selectFields;
|
selectFields = typeof selectFields === 'string' ? selectFields.split(",") : selectFields;
|
||||||
|
|
||||||
@ -241,6 +246,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
|
|||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
|
|
||||||
selectBase.assignments = {
|
selectBase.assignments = {
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
@ -255,7 +261,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
|
|||||||
where: {
|
where: {
|
||||||
shift: {
|
shift: {
|
||||||
startTime: {
|
startTime: {
|
||||||
gte: filterDate,
|
gte: prevMnt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -266,7 +272,6 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
|
|||||||
let isDayFilter = true;
|
let isDayFilter = true;
|
||||||
let whereClause = {};
|
let whereClause = {};
|
||||||
|
|
||||||
var monthInfo = common.getMonthDatesInfo(filterDate);
|
|
||||||
if (isForTheMonth) {
|
if (isForTheMonth) {
|
||||||
var weekNr = common.getWeekOfMonth(filterDate); //getWeekNumber
|
var weekNr = common.getWeekOfMonth(filterDate); //getWeekNumber
|
||||||
|
|
||||||
@ -371,20 +376,11 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
let currentWeekStart, currentWeekEnd,
|
let currentWeekStart, currentWeekEnd;
|
||||||
currentMonthStart, currentMonthEnd,
|
|
||||||
previousMonthStart, previousMonthEnd;
|
|
||||||
|
|
||||||
if (isWithStats) {
|
if (isWithStats) {
|
||||||
currentWeekStart = common.getStartOfWeek(filterDate);
|
currentWeekStart = common.getStartOfWeek(filterDate);
|
||||||
currentWeekEnd = common.getEndOfWeek(filterDate);
|
currentWeekEnd = common.getEndOfWeek(filterDate);
|
||||||
currentMonthStart = monthInfo.firstMonday;
|
|
||||||
currentMonthEnd = monthInfo.lastSunday;
|
|
||||||
let prevMnt = new Date(filterDate)
|
|
||||||
prevMnt.setMonth(prevMnt.getMonth() - 1);
|
|
||||||
monthInfo = common.getMonthDatesInfo(prevMnt);
|
|
||||||
previousMonthStart = monthInfo.firstMonday;
|
|
||||||
previousMonthEnd = monthInfo.lastSunday;
|
|
||||||
|
|
||||||
//get if publisher has assignments for current weekday, week, current month, previous month
|
//get if publisher has assignments for current weekday, week, current month, previous month
|
||||||
publishers.forEach(pub => {
|
publishers.forEach(pub => {
|
||||||
@ -401,12 +397,12 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
|
|||||||
|
|
||||||
// Filter assignments for current month
|
// Filter assignments for current month
|
||||||
pub.currentMonthAssignments = pub.assignments?.filter(assignment => {
|
pub.currentMonthAssignments = pub.assignments?.filter(assignment => {
|
||||||
return assignment.shift.startTime >= currentMonthStart && (noEndDateFilter || assignment.shift.startTime <= currentMonthEnd);
|
return assignment.shift.startTime >= monthInfo.firstMonday && (noEndDateFilter || assignment.shift.startTime <= monthInfo.lastSunday);
|
||||||
}).length;
|
}).length;
|
||||||
|
|
||||||
// Filter assignments for previous month
|
// Filter assignments for previous month
|
||||||
pub.previousMonthAssignments = pub.assignments?.filter(assignment => {
|
pub.previousMonthAssignments = pub.assignments?.filter(assignment => {
|
||||||
return assignment.shift.startTime >= previousMonthStart && assignment.shift.startTime <= previousMonthEnd;
|
return assignment.shift.startTime >= prevMonthInfo.firstMonday && assignment.shift.startTime <= prevMonthInfo.lastSunday;
|
||||||
}).length;
|
}).length;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -419,8 +415,8 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
|
|||||||
publishers.forEach(pub => {
|
publishers.forEach(pub => {
|
||||||
if (isWithStats) {
|
if (isWithStats) {
|
||||||
pub.currentMonthAvailability = pub.availabilities?.filter(avail => {
|
pub.currentMonthAvailability = pub.availabilities?.filter(avail => {
|
||||||
// return avail.dayOfMonth != null && avail.startTime >= currentMonthStart && avail.startTime <= currentMonthEnd;
|
// return avail.dayOfMonth != null && avail.startTime >= currentMonthStart && avail.startTime <= monthInfo.lastSunday;
|
||||||
return avail.startTime >= currentMonthStart && (noEndDateFilter || avail.startTime <= currentMonthEnd);
|
return avail.startTime >= monthInfo.firstMonday && (noEndDateFilter || avail.startTime <= monthInfo.lastSunday);
|
||||||
})
|
})
|
||||||
pub.currentMonthAvailabilityDaysCount = pub.currentMonthAvailability.length || 0;
|
pub.currentMonthAvailabilityDaysCount = pub.currentMonthAvailability.length || 0;
|
||||||
// pub.currentMonthAvailabilityDaysCount += pub.availabilities.filter(avail => {
|
// pub.currentMonthAvailabilityDaysCount += pub.availabilities.filter(avail => {
|
||||||
@ -431,7 +427,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
|
|||||||
}, 0);
|
}, 0);
|
||||||
//if pub has up-to-date availabilities (with dayOfMonth) for the current month
|
//if pub has up-to-date availabilities (with dayOfMonth) for the current month
|
||||||
pub.hasUpToDateAvailabilities = pub.availabilities?.some(avail => {
|
pub.hasUpToDateAvailabilities = pub.availabilities?.some(avail => {
|
||||||
return avail.dayOfMonth != null && avail.startTime >= currentMonthStart; // && avail.startTime <= currentMonthEnd;
|
return avail.dayOfMonth != null && avail.startTime >= monthInfo.firstMonday; // && avail.startTime <= monthInfo.lastSunday;
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user