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;

View File

@ -57,7 +57,8 @@ export default function CalendarPage({ initialEvents, initialShifts }) {
const [allShifts, setAllShifts] = useState(initialShifts);
const [isPublished, setIsPublished] = useState(() => initialShifts.some(shift => shift.isPublished)); const [value, onChange] = useState<Date>(new Date());
const [isPublished, setIsPublished] = useState(() => initialShifts.some(shift => shift.isPublished));
const [value, onChange] = useState<Date>(new Date());
const [shifts, setShifts] = React.useState([]);
const [error, setError] = React.useState(null);
const [availablePubs, setAvailablePubs] = React.useState([]);
@ -528,6 +529,10 @@ export default function CalendarPage({ initialEvents, initialShifts }) {
const [isMenuOpen, setIsMenuOpen] = useState(false);
const [isConfirmModalOpen, setConfirmModalOpen] = useState(false);
async function copyOldAvailabilities(event: MouseEvent<HTMLButtonElement, MouseEvent>): Promise<void> {
await axiosInstance.get(`/api/?action=copyOldAvailabilities&date=${common.getISODateOnly(value)}`);
}
return (
<>
<Layout>
@ -610,6 +615,7 @@ export default function CalendarPage({ initialEvents, initialShifts }) {
<button className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" onClick={fetchShifts}>
{isLoading('fetchShifts') ? (<i className="fas fa-sync-alt fa-spin mr-2"></i>) : (<i className="fas fa-sync-alt mr-2"></i>)} презареди</button>
<button className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" onClick={generateMonthlyStatistics}><i className="fas fa-chart-bar mr-2"></i> Генерирай статистика</button>
<button className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" onClick={copyOldAvailabilities}><i className="fas fa-copy mr-2"></i> Прехвърли предпочитанията</button>
</div>
</div>
)}

View File

@ -136,6 +136,7 @@ export default function ImportPage() {
const handleSave = async () => {
try {
common.logger.debug("handleSave to: " + common.getBaseUrl());
console.log("handleSave to: " + common.getBaseUrl());
const header = rawData[mode.headerRow];
for (let i = mode.headerRow + 1; i < rawData.length; i++) { //fullData.length; each publisher
//update status.info with current publisher