import { isAfter, max, startOfDay } from 'date-fns'; import { DeliveryAddressResponse } from '../types/delivery-address'; export class DeliveryAddress { public id: string; public userId: string; public subscriptionNumber: string; public line1: string; public line2: string; public line3: string; public townCity: string; public county: string; public postcode: string; public country: string; public validDateFrom: string; public validDateTo: string | null; public deliveryNotes: string; public securityNotes: string; public addressType: string; public isCurrentDefaultAddress: boolean; public isCurrentWeekendAddress: boolean; public validDateFromDateFormat: Date; public subscriptionStartDate: Date; public company: string | null; public poBox: boolean; private todaysDate: Date ; private pointOfMeasure: Date; constructor(address: DeliveryAddressResponse, subscriptionStartDate: string) { this.id = address.id; this.userId = address.userId; this.subscriptionNumber = address.subscriptionNumber; this.line1 = address.line1; this.line2 = address.line2; this.line3 = address.line3; this.townCity = address.townCity; this.county = address.county; this.postcode = address.postcode; this.country = address.country; this.validDateFrom = address.validDateFrom; this.validDateTo = address.validDateTo; this.deliveryNotes = address.deliveryNotes; this.securityNotes = address.securityNotes; this.addressType = address.addressType; this.isCurrentDefaultAddress = false; this.isCurrentWeekendAddress = false; this.validDateFromDateFormat = startOfDay(new Date(this.validDateFrom)); this.subscriptionStartDate = startOfDay(new Date(subscriptionStartDate)); this.company = address.company; this.poBox = address.poBox; this.todaysDate = startOfDay(new Date()); // pointOfMeasure is determined by which date occurs later, todays date or the subscription start date. // If the subscription has not started yet it we can't measure a possible future date based on todays date, // it will have to be on the subscription date. this.pointOfMeasure = max([this.todaysDate, this.subscriptionStartDate]); } /** * Determines whether the delivery address is a default address. * @returns true|false */ public get isDefault(): boolean { return (this.addressType || '').toUpperCase() === 'DEFAULT'; } /** * Determines whether the delivery address is a weekend address. * @returns true|false */ public get isWeekend(): boolean { return (this.addressType || '').toUpperCase() === 'WEEKEND'; } /** * Determines whether the delivery address's valid start date is still to happen in the future. * @returns true|false */ public get isFutureAddress(): boolean { return this.pointOfMeasure < this.validDateFromDateFormat; } /** * Determines whether the delivery address's valid start date has already past. * @returns true|false */ public get isPastAddress(): boolean { return this.pointOfMeasure >= this.validDateFromDateFormat; } /** * Determines whether the delivery address is a default address and the valid start date is in the future. * @returns true|false */ public get isFutureDefaultAddress(): boolean { return this.isDefault && this.isFutureAddress; } /** * Determines whether the delivery address is a default address and the valid start date has already past. * @returns true|false */ public get isPastDefaultAddress(): boolean { return this.isDefault && this.isPastAddress; } /** * Determines whether the delivery address is a weekend address and the valid start date is in the future. * @returns true|false */ public get isFutureWeekendAddress(): boolean { return this.isWeekend && this.isFutureAddress; } /** * Determines whether the delivery address is a weekend address and the valid start date is in the past. * @returns true|false */ public get isPastWeekendAddress(): boolean { return this.isWeekend && this.isPastAddress; } /** * Determines which address - this address or the reigningMostCurrentAddress - is closer to today's date. * If there is no reigningMostCurrentAddress it will return this address. * @param reigningMostCurrentAddress a past address that is currently the closest to today's date. * @returns the most current delivery address (the closest address out of the two to today's Date). */ public getMostCurrentAddress(reigningMostCurrentAddress: DeliveryAddress | null): DeliveryAddress { if (!reigningMostCurrentAddress) { return this; } const isThisAddressMoreCurrent = isAfter( this.validDateFromDateFormat, new Date(reigningMostCurrentAddress.validDateFromDateFormat) ); if (isThisAddressMoreCurrent) { return this; } else { return reigningMostCurrentAddress; } } }