Skip to content

Commit

Permalink
fix(dashboard): attendance week starts from Monday correctly (#2089)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon <simon@aam-digital.com>
  • Loading branch information
sleidig and TheSlimvReal authored Nov 21, 2023
1 parent eea545c commit 7b0d434
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ActivityAttendance } from "../../model/activity-attendance";
import { AttendanceService } from "../../attendance.service";
import { RecurringActivity } from "../../model/recurring-activity";
import moment from "moment";
import * as MockDate from "mockdate";

describe("AttendanceWeekDashboardComponent", () => {
let component: AttendanceWeekDashboardComponent;
Expand Down Expand Up @@ -90,28 +91,38 @@ describe("AttendanceWeekDashboardComponent", () => {
]);
});

it("should correctly use the offset", () => {
// default case: last week monday till saturday
const mondayLastWeek = moment().startOf("isoWeek").subtract(7, "days");
const saturdayLastWeek = mondayLastWeek.clone().add("5", "days");
function expectTimePeriodCalled(from: moment.Moment, to: moment.Moment) {
mockAttendanceService.getAllActivityAttendancesForPeriod.calls.reset();

component.ngOnInit();

expect(
mockAttendanceService.getAllActivityAttendancesForPeriod,
).toHaveBeenCalledWith(mondayLastWeek.toDate(), saturdayLastWeek.toDate());
).toHaveBeenCalledWith(from.toDate(), to.toDate());
}

// with offset: this week monday till saturday
const mondayThisWeek = moment().startOf("isoWeek");
const saturdayThisWeek = mondayThisWeek.clone().add(5, "days");
mockAttendanceService.getAllActivityAttendancesForPeriod.calls.reset();
it("should correctly use the offset", () => {
// default case: last week monday till saturday

// on Monday, that's the first day of the current period
MockDate.set(moment("2023-11-20").toDate());
const mondayLastWeek = moment("2023-11-13");
const saturdayLastWeek = moment("2023-11-18");
expectTimePeriodCalled(mondayLastWeek, saturdayLastWeek);

// on Sunday, that's the still the last day of the currently ending period
MockDate.set(moment("2023-11-26").toDate());
const mondayLastWeek2 = moment("2023-11-13");
const saturdayLastWeek2 = moment("2023-11-18");
expectTimePeriodCalled(mondayLastWeek2, saturdayLastWeek2);

// with offset: this week monday till saturday
MockDate.set(moment("2023-11-20").toDate());
const mondayThisWeek = moment("2023-11-20");
const saturdayThisWeek = moment("2023-11-25");
component.daysOffset = 7;
component.ngOnInit();
expectTimePeriodCalled(mondayThisWeek, saturdayThisWeek);

expect(
mockAttendanceService.getAllActivityAttendancesForPeriod,
).toHaveBeenCalledWith(mondayThisWeek.toDate(), saturdayThisWeek.toDate());
MockDate.reset();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,16 @@ export class AttendanceWeekDashboardComponent implements OnInit, AfterViewInit {
}

private async loadAttendanceOfAbsentees() {
const today = new Date();
const previousMonday = new Date(
today.getFullYear(),
today.getMonth(),
today.getDate() - (6 - today.getDay() + 7) + this.daysOffset,
);
const previousSaturday = new Date(
previousMonday.getFullYear(),
previousMonday.getMonth(),
previousMonday.getDate() + 5,
);
const previousMonday = moment()
.startOf("isoWeek")
.subtract(1, "week")
.add(this.daysOffset, "days");
const previousSaturday = moment(previousMonday).add(5, "days");

const activityAttendances =
await this.attendanceService.getAllActivityAttendancesForPeriod(
previousMonday,
previousSaturday,
previousMonday.toDate(),
previousSaturday.toDate(),
);
const lowAttendanceCases = new Set<string>();
const records: AttendanceWeekRow[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe("EntityFormService", () => {

schema.defaultValue = PLACEHOLDERS.NOW;
form = service.createFormGroup([{ id: "test" }], new Entity());
expect(form.get("test")).toHaveValue(new Date());
expect(form.get("test").value).toEqual(new Date());

schema.defaultValue = PLACEHOLDERS.CURRENT_USER;
form = service.createFormGroup([{ id: "test" }], new Entity());
Expand Down

0 comments on commit 7b0d434

Please sign in to comment.