function refreshSchedule()

in use-cases/employee-scheduling/src/main/resources/META-INF/resources/app.js [127:246]


function refreshSchedule() {
    $.getJSON("/schedule", function (schedule) {
        refreshSolvingButtons(schedule.solverStatus != null && schedule.solverStatus !== "NOT_SOLVING");
        $("#score").text("Score: " + (schedule.score == null ? "?" : schedule.score));

        const unassignedShifts = $("#unassignedShifts");
        const groups = [];
        const availabilityMap = new Map();

        // Show only first 7 days of draft
        const scheduleStart = schedule.scheduleState.firstDraftDate;
        const scheduleEnd = JSJoda.LocalDate.parse(scheduleStart).plusDays(7).toString();

        windowStart = scheduleStart;
        windowEnd = scheduleEnd;

        unassignedShifts.children().remove();
        let unassignedShiftsCount = 0;
        byEmployeeGroupDataSet.clear();
        byLocationGroupDataSet.clear();

        byEmployeeItemDataSet.clear();
        byLocationItemDataSet.clear();

        byEmployeeTimeline.setCustomTime(schedule.scheduleState.lastHistoricDate, 'published');
        byEmployeeTimeline.setCustomTime(schedule.scheduleState.firstDraftDate, 'draft');

        byLocationTimeline.setCustomTime(schedule.scheduleState.lastHistoricDate, 'published');
        byLocationTimeline.setCustomTime(schedule.scheduleState.firstDraftDate, 'draft');

        schedule.availabilityList.forEach((availability, index) => {
            const availabilityDate = JSJoda.LocalDate.parse(availability.date);
            const start = availabilityDate.atStartOfDay().toString();
            const end = availabilityDate.plusDays(1).atStartOfDay().toString();
            const byEmployeeShiftElement = $(`<div/>`)
                    .append($(`<h5 class="card-title mb-1"/>`).text(availability.availabilityType));
            const mapKey = availability.employee.name + '-' + availabilityDate.toString();
            availabilityMap.set(mapKey, availability.availabilityType);
            byEmployeeItemDataSet.add({
                id : 'availability-' + index, group: availability.employee.name,
                content: byEmployeeShiftElement.html(),
                start: start, end: end,
                type: "background",
                style: "opacity: 0.5; background-color: " + getAvailabilityColor(availability.availabilityType),
            });
        });


        schedule.employeeList.forEach((employee, index) => {
            const employeeGroupElement = $('<div class="card-body p-2"/>')
                    .append($(`<h5 class="card-title mb-2"/>)`)
                            .append(employee.name))
                    .append($('<div/>')
                            .append($(employee.skillSet.map(skill => `<span class="badge mr-1 mt-1" style="background-color:#d3d7cf">${skill}</span>`).join(''))));
            byEmployeeGroupDataSet.add({id : employee.name, content: employeeGroupElement.html()});
        });

        schedule.shiftList.forEach((shift, index) => {
            if (groups.indexOf(shift.location) === -1) {
                groups.push(shift.location);
                byLocationGroupDataSet.add({
                    id : shift.location,
                    content: shift.location,
                });
            }

            if (shift.employee == null) {
                unassignedShiftsCount++;

                const byLocationShiftElement = $('<div class="card-body p-2"/>')
                        .append($(`<h5 class="card-title mb-2"/>)`)
                                .append("Unassigned"))
                        .append($('<div/>')
                                .append($(`<span class="badge mr-1 mt-1" style="background-color:#d3d7cf">${shift.requiredSkill}</span>`)));

                byLocationItemDataSet.add({
                    id : 'shift-' + index, group: shift.location,
                    content: byLocationShiftElement.html(),
                    start: shift.start, end: shift.end,
                    style: "background-color: #EF292999"
                });
            } else {
                const skillColor = (shift.employee.skillSet.indexOf(shift.requiredSkill) === -1? '#ef2929' : '#8ae234');
                const byEmployeeShiftElement = $('<div class="card-body p-2"/>')
                        .append($(`<h5 class="card-title mb-2"/>)`)
                                .append(shift.location))
                        .append($('<div/>')
                                .append($(`<span class="badge mr-1 mt-1" style="background-color:${skillColor}">${shift.requiredSkill}</span>`)));
                const byLocationShiftElement = $('<div class="card-body p-2"/>')
                        .append($(`<h5 class="card-title mb-2"/>)`)
                                .append(shift.employee.name))
                        .append($('<div/>')
                                .append($(`<span class="badge mr-1 mt-1" style="background-color:${skillColor}">${shift.requiredSkill}</span>`)));

                const shiftColor =  getShiftColor(shift, availabilityMap);
                byEmployeeItemDataSet.add({
                    id : 'shift-' + index, group: shift.employee.name,
                    content: byEmployeeShiftElement.html(),
                    start: shift.start, end: shift.end,
                    style: "background-color: " + shiftColor
                });
                byLocationItemDataSet.add({
                    id : 'shift-' + index, group: shift.location,
                    content: byLocationShiftElement.html(),
                    start: shift.start, end: shift.end,
                    style: "background-color: " + shiftColor
                });
            }
        });


        if (unassignedShiftsCount === 0) {
            unassignedShifts.append($(`<p/>`).text(`There are no unassigned shifts.`));
        } else {
            unassignedShifts.append($(`<p/>`).text(`There are ${unassignedShiftsCount} unassigned shifts.`));
        }
        byEmployeeTimeline.setWindow(scheduleStart, scheduleEnd);
        byLocationTimeline.setWindow(scheduleStart, scheduleEnd);
    });
}