revert data api and fix repeating availabilities
This commit is contained in:
@ -6,7 +6,7 @@ import { DayOfWeek, AvailabilityType, UserRole, EventLogType } from '@prisma/cli
|
|||||||
const common = require('../../src/helpers/common');
|
const common = require('../../src/helpers/common');
|
||||||
const dataHelper = require('../../src/helpers/data');
|
const dataHelper = require('../../src/helpers/data');
|
||||||
const subq = require('../../prisma/bl/subqueries');
|
const subq = require('../../prisma/bl/subqueries');
|
||||||
import { addMinutes } from 'date-fns';
|
import { set, format, addMinutes, addDays } from 'date-fns';
|
||||||
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
@ -147,9 +147,30 @@ export default async function handler(req, res) {
|
|||||||
res.status(200).json(events);
|
res.status(200).json(events);
|
||||||
|
|
||||||
case "getPublisherInfo":
|
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);
|
let pub = pubs[0] || {};
|
||||||
res.status(200).json(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 newStart = new Date(day);
|
||||||
|
newStart = addDays(newStart, dayOfWeek - dayOfWeekQuery);
|
||||||
|
newStart.setHours(avail.startTime.getHours(), avail.startTime.getMinutes(), 0, 0);
|
||||||
|
let newEnd = new Date(newStart);
|
||||||
|
newEnd.setHours(avail.endTime.getHours(), avail.endTime.getMinutes(), 0, 0);
|
||||||
|
return {
|
||||||
|
...avail,
|
||||||
|
startTime: newStart,
|
||||||
|
endTime: newEnd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return avail;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
//let pubs = await dataHelper.filterPublishersNew("id,firstName,lastName,email,isActive,assignments,availabilities", day, false, true, false, true, false, req.query.id);
|
||||||
|
res.status(200).json(pub);
|
||||||
break;
|
break;
|
||||||
case "filterPublishersNew":
|
case "filterPublishersNew":
|
||||||
let includeOldAvailabilities = common.parseBool(req.query.includeOldAvailabilities);
|
let includeOldAvailabilities = common.parseBool(req.query.includeOldAvailabilities);
|
||||||
|
@ -956,13 +956,15 @@ async function RankPublishersForShiftWeighted(publishers, scheduledPubsPerDayAnd
|
|||||||
p.desiredCompletion = p.currentMonthAssignments / p.desiredShiftsPerMonth;
|
p.desiredCompletion = p.currentMonthAssignments / p.desiredShiftsPerMonth;
|
||||||
});
|
});
|
||||||
|
|
||||||
const calculateScore = (p) => {
|
const calculateScoreAndPenalties = (p) => {
|
||||||
let score = (p.isMale ? weights.gender : 0) -
|
let score = (p.isMale ? weights.gender : 0) -
|
||||||
(p.desiredCompletion * weights.desiredCompletion) +
|
(p.desiredCompletion * weights.desiredCompletion) +
|
||||||
((1 - p.currentMonthAvailabilityHoursCount / 24) * weights.availability) +
|
((1 - p.currentMonthAvailabilityHoursCount / 24) * weights.availability) +
|
||||||
(p.currentMonthAssignments * weights.lastMonthCompletion) -
|
(p.currentMonthAssignments * weights.lastMonthCompletion) -
|
||||||
(p.currentMonthAssignments * weights.currentAssignments);
|
(p.currentMonthAssignments * weights.currentAssignments);
|
||||||
|
|
||||||
|
let penalties = [];
|
||||||
|
|
||||||
// Apply penalties based on proximity to current day
|
// Apply penalties based on proximity to current day
|
||||||
for (let i = 1; i <= 6; i++) {
|
for (let i = 1; i <= 6; i++) {
|
||||||
const previousDayKey = common.getISODateOnly(addDays(currentDay, -i));
|
const previousDayKey = common.getISODateOnly(addDays(currentDay, -i));
|
||||||
@ -972,21 +974,33 @@ async function RankPublishersForShiftWeighted(publishers, scheduledPubsPerDayAnd
|
|||||||
|
|
||||||
if (flattenRegistry(previousDayKey).includes(p.id)) {
|
if (flattenRegistry(previousDayKey).includes(p.id)) {
|
||||||
score *= penalty;
|
score *= penalty;
|
||||||
|
penalties.push({ day: previousDayKey, penalty });
|
||||||
}
|
}
|
||||||
if (flattenRegistry(nextDayKey).includes(p.id)) {
|
if (flattenRegistry(nextDayKey).includes(p.id)) {
|
||||||
score *= penalty;
|
score *= penalty;
|
||||||
|
penalties.push({ day: nextDayKey, penalty });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return score;
|
return { score, penalties };
|
||||||
};
|
};
|
||||||
|
|
||||||
let ranked = publishers.sort((a, b) => {
|
// Calculate scores and penalties for each publisher
|
||||||
const scoreA = calculateScore(a);
|
publishers.forEach(p => {
|
||||||
const scoreB = calculateScore(b);
|
const result = calculateScoreAndPenalties(p);
|
||||||
|
p.score = result.score;
|
||||||
return scoreB - scoreA; // Sort descending by score
|
p.penalties = result.penalties;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Sort publishers based on score
|
||||||
|
let ranked = publishers.sort((a, b) => b.score - a.score);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
return ranked;
|
return ranked;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,6 +110,9 @@ Date.prototype.getDayEuropean = function () {
|
|||||||
return (day === 0) ? 6 : day - 1; // Convert 0 (Sunday) to 6, and decrement other days by 1
|
return (day === 0) ? 6 : day - 1; // Convert 0 (Sunday) to 6, and decrement other days by 1
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.getDayOfWeek = function (date) {
|
||||||
|
return date.getDayEuropean();
|
||||||
|
};
|
||||||
// Helper function to convert month name to 0-based index
|
// Helper function to convert month name to 0-based index
|
||||||
exports.getMonthNames = function () {
|
exports.getMonthNames = function () {
|
||||||
return ["януари", "февруари", "март", "април", "май", "юни", "юли", "август", "септември", "октомври", "ноември", "декември"];
|
return ["януари", "февруари", "март", "април", "май", "юни", "юли", "август", "септември", "октомври", "ноември", "декември"];
|
||||||
|
Reference in New Issue
Block a user