import { format } from 'date-fns'; import { Base } from './base'; import { FirstDeliveryDateMembershipResponse } from './types/print-info'; import { DeliveryVoucherDates } from './models/delivery-voucher-dates'; import { VoucherDates } from './types/print-info'; export class PrintInfo extends Base { private membershipApi = this.config.get('membershipApi'); private printInfoApiKey = this.config.get('printInfoApiKey'); /** * This function is the public method for retrieving the first possible delivery date from the requested date. * @param printProductCode Type of print product code: n5d, n6d, nwe. Does not accept bundle codes. * @param countryCode Three letter ISO 3166-1 country code i.e. GBR * @param postcode User's postcode * @param option Home delivery, paper voucher or digital voucher. * @param startDate Optional. The date selected for delivery. Format yyyy-MM-dd. * @returns { productCode, countryCode, postcode, firstDeliveryDate }. FirstDeliveryDate is the closest date of delivery to the selected date. */ public async getFirstDeliveryDate(printProductCode: string, countryCode: string, postcode: string, option: string, startDate?: string): Promise { const requiredUrl = `${this.membershipApi}/newspaper/info/first-delivery-date?productCode=${printProductCode}&countryCode=${countryCode}&postCode=${postcode}&option=${option}`; const url = startDate ? requiredUrl + `&startDate=${startDate}` : requiredUrl; return this.requestGet({ url, key: this.printInfoApiKey }); } /** * This function is the public method for retrieving the first possible suspension date from the requested date. * @param countryCode Three letter ISO 3166-1 country code i.e. GBR * @param fromDate The date selected for suspension to start. Format yyyy-MM-dd. * @returns { firstSuspensionDate }. FirstSuspensionDate is the closest date to that selected that a suspension can start. */ public async getFirstSuspensionDate(countryCode: string, fromDate: string): Promise<{ firstSuspensionDate: string }> { const url = `${this.membershipApi}/newspaper/info/first-suspension-date?countryCode=${countryCode}&fromDate=${fromDate}`; return this.requestGet({ url, key: this.printInfoApiKey }); } /** * This function is the public method for retrieving a paper voucher delivery schedule. * @param term Period of time for delivery e.g P1Y (yearly), P1M (monthly). * @param startDate The date you want to see all the delivery schedules from. Format yyyy-MM-dd. Defaults to today's date. * @returns voucher delivery schedule */ public async getVoucherDeliverySchedule(term: string, startDate: string = format(new Date(), 'yyyy-MM-dd')): Promise> { const url = `${this.membershipApi}/newspaper/info/vouchers/schedules?startDate=${startDate}&term=${term}`; const { schedule } = await this.requestGet({ url, key: this.printInfoApiKey }); return schedule.map((deliveryDate: VoucherDates) => new DeliveryVoucherDates(deliveryDate)); } /** * Context, as explained by the Membership Team: "Today we generate delivery data for tomorrow. So, if you end your suspension immediately today, tomorrow you will still not receive it since we’ve already generated that delivery data. This gets more tricky with weekends and non-publishing days." * This function is the public method for retrieving Last Delivery Date (LDD) for which data has already been generated. * @param checkDate Given this date, the API will return its LDD. * @param region The region for which we want to run the query, as different countries have different bank holidays. * @returns { region, lastDeliveryDate }. LastDeliveryDate is the day we've already generated delivery data for given the region. */ public async getLastDeliveryDateGenerated(checkDate: string, region: string): Promise<{ region: string, lastDeliveryDate: string }> { const url = `${this.membershipApi}/newspaper/info/delivery-files/last-date?date=${checkDate}®ion=${region}`; return this.requestGet({ url, key: this.printInfoApiKey }); } /** * This function is the public method for retrieving Last Delivery Date (LDD) for which data has already been generated using Country. * @param checkDate Given this date, the API will return its LDD. * @param countryCode Three letter ISO 3166-1 country code i.e. GBR * @returns { region, lastDeliveryDate }. LastDeliveryDate is the day we've already generated delivery data for given the region. */ public async getLastDeliveryDateGeneratedByCountry(checkDate: string, countryCode: string): Promise<{ region: string, lastDeliveryDate: string }> { const url = `${this.membershipApi}/newspaper/info/delivery-files/last-date?date=${checkDate}&countryCode=${countryCode}`; return this.requestGet({ url, key: this.printInfoApiKey }); } }