From 75942c027dee952add8a6a591409f8eec75d5555 Mon Sep 17 00:00:00 2001 From: Italo Date: Thu, 20 May 2021 19:33:55 -0300 Subject: [PATCH] feat(employees): check also for created date --- helpers/employee.ts | 18 +++++++++++++++--- store/reports/actions.ts | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/helpers/employee.ts b/helpers/employee.ts index 14c78fc7..b18748a6 100644 --- a/helpers/employee.ts +++ b/helpers/employee.ts @@ -1,8 +1,20 @@ -import isAfter from "date-fns/isAfter"; +import { isAfter, endOfDay } from "date-fns"; +// Check if employee was already created and yet active in a given date or time span export function checkEmployeeAvailability( employee: Employee, - compareDate: Date + startCompareDate: Date, + endCompareDate?: Date ) { - return !employee.endDate || isAfter(new Date(employee.endDate), compareDate); + const end = endCompareDate + ? endOfDay(endCompareDate) + : endOfDay(startCompareDate); + + if (end.getTime() < employee.created) { + return false; + } + + return ( + !employee.endDate || isAfter(new Date(employee.endDate), startCompareDate) + ); } diff --git a/store/reports/actions.ts b/store/reports/actions.ts index f98e1ba4..de02b9f3 100644 --- a/store/reports/actions.ts +++ b/store/reports/actions.ts @@ -51,7 +51,7 @@ const actions: ActionTree = { ); const activeEmployees = employees.filter((employee) => - checkEmployeeAvailability(employee, startDate) + checkEmployeeAvailability(employee, startDate, endDate) ); commit("setIsLoading", { isLoading: false });