import { endOfDay, isAfter, isBefore, isWithinInterval, startOfDay } from 'date-fns'; import { VoucherDates } from '../types/print-info'; export class DeliveryVoucherDates { public voucherStartDate: string; public voucherEndDate: string; public fileDeliveryDate: string; public startDate: Date; public endDate: Date; public deliveryDate: Date; private todaysDate: Date; constructor (voucherDates: VoucherDates) { this.voucherStartDate = voucherDates.voucherStartDate; this.startDate = startOfDay(new Date(this.voucherStartDate)); this.voucherEndDate = voucherDates.voucherEndDate; this.endDate = endOfDay(new Date(this.voucherEndDate)); this.fileDeliveryDate = voucherDates.fileDeliveryDate; this.deliveryDate = startOfDay(new Date(this.fileDeliveryDate)); this.todaysDate = startOfDay(new Date()); } /** * Does today's date fall between this vouchers start and end date. * @returns true|false */ public isCurrentDeliveryDate (): boolean { return isWithinInterval(this.todaysDate, { start: this.startDate, end: this.endDate }); } /** * Does today's date come before this vouchers start date. * @returns true|false */ public isFutureDeliveryDate (): boolean { return isBefore(this.todaysDate, this.startDate); } /** * Does today's date come after this vouchers end date. * @returns true|false */ public isPastDeliveryDate (): boolean { return isAfter(this.todaysDate, this.endDate); } }