skill issue
This commit is contained in:
@@ -39,8 +39,8 @@ const WINDOW_DAYS = 56;
|
||||
* Local rather than UTC because a shift belongs to the day it was worked in the
|
||||
* place it was worked, and `periodRange` windows on local day boundaries too.
|
||||
*/
|
||||
function daysAgo(n, hour = 9, minute = 0) {
|
||||
const d = new Date();
|
||||
function daysAgo(n, hour = 9, minute = 0, anchor = new Date()) {
|
||||
const d = new Date(anchor.getTime());
|
||||
d.setDate(d.getDate() - n);
|
||||
d.setHours(hour, minute, 0, 0);
|
||||
return d;
|
||||
@@ -154,10 +154,10 @@ const BEHAVIOUR = {
|
||||
};
|
||||
|
||||
/** Every shift date for one worker, most recent first. */
|
||||
function shiftOffsets(weekdays) {
|
||||
function shiftOffsets(weekdays, anchor) {
|
||||
const offsets = [];
|
||||
for (let offset = 0; offset <= WINDOW_DAYS; offset += 1) {
|
||||
const day = daysAgo(offset).getDay();
|
||||
const day = daysAgo(offset, 9, 0, anchor).getDay();
|
||||
if (weekdays.includes(day)) offsets.push(offset);
|
||||
}
|
||||
return offsets;
|
||||
@@ -166,14 +166,19 @@ function shiftOffsets(weekdays) {
|
||||
const pad = (n) => String(n).padStart(2, '0');
|
||||
const localDate = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
|
||||
|
||||
function buildShifts() {
|
||||
/**
|
||||
* @param {Date} [anchor] the day to count back from. Defaults to now, which is
|
||||
* the point of this collection; a caller passes one only to hold the window
|
||||
* still — see buildShiftsAt.
|
||||
*/
|
||||
function buildShifts(anchor = new Date()) {
|
||||
const records = [];
|
||||
|
||||
for (const worker of ROSTER) {
|
||||
const offsets = shiftOffsets(worker.weekdays);
|
||||
const offsets = shiftOffsets(worker.weekdays, anchor);
|
||||
|
||||
offsets.forEach((offset, i) => {
|
||||
const scheduledStart = daysAgo(offset, worker.startHour);
|
||||
const scheduledStart = daysAgo(offset, worker.startHour, 0, anchor);
|
||||
const weekday = scheduledStart.getDay();
|
||||
const scheduledEnd = new Date(scheduledStart.getTime() + worker.scheduledHours * HOUR);
|
||||
|
||||
@@ -231,3 +236,21 @@ function buildShifts() {
|
||||
}
|
||||
|
||||
export const SHIFT_RECORDS = buildShifts();
|
||||
|
||||
/**
|
||||
* The same shifts, counted back from a day you choose.
|
||||
*
|
||||
* The distribution is deterministic given its anchor but not across anchors:
|
||||
* which shifts fall in a complete week depends on where the anchor sits in one,
|
||||
* so an assertion about the seeded overtime climb held on a Thursday and failed
|
||||
* on a Friday. That is a test that reports the calendar, not the code.
|
||||
*
|
||||
* The app keeps `SHIFT_RECORDS` above, anchored to now, because attendance
|
||||
* "last week" has to mean last week on the day somebody looks. Pin the anchor
|
||||
* here instead, and pass the SAME date as `now` to whatever reads the result —
|
||||
* shifts built around one day and bucketed around another describe two
|
||||
* different windows.
|
||||
*/
|
||||
export function buildShiftsAt(anchor) {
|
||||
return buildShifts(anchor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user