new function - copyOldAvailabilities

This commit is contained in:
Dobromir Popov
2024-03-28 18:11:41 +02:00
parent 3ccd9ad106
commit e0dfbc51ec
3 changed files with 76 additions and 1 deletions

View File

@ -210,6 +210,74 @@ export default async function handler(req, res) {
res.status(200).json(shiftsForDate);
break;
case "copyOldAvailabilities":
//get all publishers that don't have availabilities for the current month
monthInfo = common.getMonthDatesInfo(date);
// await prisma.availability.deleteMany({
// where: {
// startTime: {
// gte: monthInfo.firstMonday,
// },
// isFromPreviousMonth: true
// }
// });
let oldpubs = await prisma.publisher.findMany({
where: {
availabilities: {
none: {
startTime: {
gte: monthInfo.firstMonday,
}
}
}
},
select: {
id: true,
firstName: true,
lastName: true,
availabilities: true
}
});
oldpubs.forEach(async pub => {
console.log("" + pub.firstName + " " + pub.lastName + " copying " + pub.availabilities.length + " availabilities from previous months.");
pub.availabilities.forEach(async avail => {
//get the new date based on the day of week and week of month
let newStart = common.getDateFromWeekNrAndDayOfWeek(avail.weekNr, avail.dayofweek, avail.startTime);
let newEnd = new Date(newStart.getTime());
newEnd.setHours(avail.endTime.getHours(), avail.endTime.getMinutes(), 0, 0);
await prisma.availability.create({
data: {
publisherId: pub.id,
dayOfMonth: null,
dayofweek: avail.dayofweek || common.getDayOfWeekNameEnEnum(avail.startTime),
weekOfMonth: avail.weekofMonth || common.getWeekOfMonth(avail.startTime),
dateOfEntry: new Date(), //avail.dateOfEntry || avail.startTime,
startTime: avail.startTime,
endTime: avail.endTime,
weekNr: avail.weekNr,
type: 3,
isFromPreviousMonth: true,
name: avail.name || "старо предпочитание",
}
});
});
});
//convert old assignments to availabilities
res.status(200).json({ "message": "ok" });
break;
case "deleteCopiedAvailabilities":
//delete all availabilities that are copied from previous months
monthInfo = common.getMonthDatesInfo(date);
await prisma.availability.deleteMany({
where: {
startTime: {
gte: monthInfo.firstMonday,
},
isFromPreviousMonth: true
}
});
case "replaceInAssignment":
const { oldPublisherId, newPublisherId, shiftId } = req.method === "POST" ? req.body : req.query;