import { startOfDay } from 'date-fns'; import { MembershipSuspensionResponse } from '../types/suspensions'; export class Suspension { public id: string; public subscriptionNumber: string; public startDate: string; public endDate: string; public credits: number; public editions: number; public originUser: string; private todaysDate: Date; private endDateStartOfDay: Date; private startDateStartOfDay: Date; constructor(suspension: MembershipSuspensionResponse) { this.id = suspension.suspensionId; this.subscriptionNumber = suspension.subscriptionNumber; this.startDate = suspension.startDate; this.endDate = suspension.endDate; this.credits = suspension.credits; this.editions = suspension.editions; this.originUser = suspension.originUser; this.todaysDate = startOfDay(new Date()); this.endDateStartOfDay = startOfDay(new Date(this.endDate)); this.startDateStartOfDay = startOfDay(new Date(this.startDate)); } /** * Is suspension active * @returns true if today's date falls within the user's suspension period. */ public get isActive(): boolean { /* To be an active suspension both of the following need to be true: - today's date needs to before or on the suspension endDate - today's date needs to be after or on the suspension start date */ return this.todaysDate <= this.endDateStartOfDay && this.todaysDate >= this.startDateStartOfDay; } /** * Is there a suspension in the future * @returns true if today's date is before the suspension start date */ public get isFuture(): boolean { return this.todaysDate < this.startDateStartOfDay; } /** * Has there been a suspension in the past * @returns true if today's date is after the suspension end date */ public get hasPast(): boolean { return this.todaysDate > this.endDateStartOfDay; } /** * Can the ACTIVE suspension be ended * @param firstSuspensionDate this is the result from the /newspaper/info/first-suspension-date end point of the newspaper info service. * It provides us with more accurate results as it takes into account non-working days. * @returns true if the suspension is active and today's date is at least 2 working days before the suspension endDate. */ public canEndActiveSuspension(firstSuspensionDate: string): boolean { if (!this.isActive) { return false; } const suspensionDateStartOfDay = startOfDay(new Date(firstSuspensionDate)); return suspensionDateStartOfDay < this.endDateStartOfDay; } /** * Can the future suspension be deleted * @param firstSuspensionDate this is the result from the /newspaper/info/first-suspension-date end point of the newspaper info service. * It provides us with more accurate results as it takes into account non-working days. * @returns true if there is a future suspension and if today's date is at least 2 working days before the suspension startDate. */ public canDeleteFutureSuspension(firstSuspensionDate: string): boolean { if (!this.isFuture) { return false; } const suspensionDateStartOfDay = startOfDay(new Date(firstSuspensionDate)); return suspensionDateStartOfDay <= this.startDateStartOfDay; } /** * Can the FUTURE suspension be ended (a suspension that starts tomorrow is NOT active according to the SDK, but it can't be deleted anymore. However, it CAN be ended) * @param firstSuspensionDate this is the result from the /newspaper/info/first-suspension-date end point of the newspaper info service. * It provides us with more accurate results as it takes into account non-working days. * @returns true if the future suspension can be ended */ public canEndFutureSuspension(firstSuspensionDate: string): boolean { const firstSuspensionDateBeforeEndDate = startOfDay(new Date(firstSuspensionDate)) < this.endDateStartOfDay; const canBeDeleted = this.canDeleteFutureSuspension(firstSuspensionDate); return !canBeDeleted && firstSuspensionDateBeforeEndDate; } }