Skip to content

Commit

Permalink
Use beginning of week for recent timelogs
Browse files Browse the repository at this point in the history
  • Loading branch information
DSIW committed Oct 19, 2021
1 parent 6f4c025 commit 7d9f735
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
5 changes: 2 additions & 3 deletions lib/application/TimeLogApplicationService.ts
@@ -1,7 +1,6 @@
import TimeLog from "../domain/TimeLog";
import TimeLogRepository from "../infrastructure/TimeLogRepository";
import { subWeeks } from "date-fns";
import { WEEK_LIMIT } from "../domain/time-constants";
import { startOfLastWeeks, WEEK_LIMIT } from "../domain/time-constants";

export default class TimeLogApplicationService {
private readonly timeLogRepository: TimeLogRepository;
Expand All @@ -15,7 +14,7 @@ export default class TimeLogApplicationService {
}

async getAllRecentTimeLogs() {
const recentLimitDate = subWeeks(new Date(), WEEK_LIMIT);
const recentLimitDate = startOfLastWeeks(WEEK_LIMIT);
return await this.timeLogRepository.allBefore(recentLimitDate);
}

Expand Down
25 changes: 24 additions & 1 deletion lib/domain/time-constants.test.ts
@@ -1,4 +1,4 @@
import { todayWorkdayEnd } from "./time-constants";
import { startOfLastWeeks, todayWorkdayEnd } from "./time-constants";

describe("time-constants", () => {
describe("#todayWorkdayEnd", () => {
Expand All @@ -9,4 +9,27 @@ describe("time-constants", () => {
expect(todayWorkdayEnd().getSeconds()).toEqual(0);
});
});

describe("#startOfLastWeeks", () => {
it("starts one week before", () => {
const someMonday = new Date(2021, 1, 1);
expect(startOfLastWeeks(1, someMonday)).toEqual(
new Date(2021, 0, 25, 0, 0, 0)
);
});

it("starts with same day on Monday", () => {
const someMonday = new Date(2021, 1, 1);
expect(startOfLastWeeks(0, someMonday)).toEqual(
new Date(2021, 1, 1, 0, 0, 0)
);
});

it("starts with same weeks Monday on Friday", () => {
const someFriday = new Date(2021, 1, 5);
expect(startOfLastWeeks(0, someFriday)).toEqual(
new Date(2021, 1, 1, 0, 0, 0)
);
});
});
});
7 changes: 7 additions & 0 deletions lib/domain/time-constants.ts
Expand Up @@ -6,6 +6,7 @@ import {
setMilliseconds,
setMinutes,
setSeconds,
startOfWeek,
subDays,
subWeeks,
} from "date-fns";
Expand All @@ -26,6 +27,12 @@ export function lastSunday() {
return lastWeekday(0, nextSunday);
}

export function startOfLastWeeks(weeks: number, today = TODAY): Date {
const recentLimitDate = subWeeks(today, weeks);
const MONDAY = 1;
return startOfWeek(recentLimitDate, { weekStartsOn: MONDAY });
}

export function todayWorkdayEnd() {
return _setTime(new Date(), "17:00:00");
}
Expand Down

0 comments on commit 7d9f735

Please sign in to comment.