import * as moment from 'moment'; import { END_MONTH, END_YEAR } from '../constants/age-can-valid-until'; import { DateAgeUntilParams } from '../interfaces/date-age-until.params'; import { TempValidUntilParams } from '../interfaces/temp-valid-until.params'; import { TicketDto } from '../interfaces/ticket/ticket.dto'; /** * When the phone is going offline while validateTicket() mutation, currentDate is usefull. * If user phone is offline when validation, we can't trust phone date * @param currentDate : the date when the validateTicket() has been launched (when going online) * @param validationDate : the date when the user attempted to launched the mutation */ export function getAdjustedValidationDate(validationDate: Date, userPhoneCurrentDate?: Date): Date { validationDate = new Date(validationDate ? validationDate : Date.now()); if (!userPhoneCurrentDate) { return validationDate; } // If no currentDate, we trust the validationDate let adjustedTimestamp = new Date(validationDate).getTime(); const userTimestamp = new Date(userPhoneCurrentDate).getTime(); // Difference between the user phone currentDate and the server current date const serverTimestamp = Date.now(); const diff = serverTimestamp - userTimestamp; adjustedTimestamp += diff; return new Date(adjustedTimestamp); } export function calculateValidationEndDate(ticket: TicketDto, validationCreationDate: Date): Date { const tempValidUntil = getValidationUntilDate({ createdAt: validationCreationDate, dateUntil: ticket.dateUntil || ticket.dateUntil, validationDuration: ticket.validationDuration, }); const dateAgeUntil = getAgeUntilDate({ ageUntil: ticket?.ageUntil, birthdate: ticket?.passengers[0]?.birthdate, ageCanValidUntil: ticket?.ageCanValidUntil, }); return dateAgeUntil ? moment.min(moment(tempValidUntil), moment(dateAgeUntil)).toDate() : tempValidUntil; } /** * Get the end date of the validation * @param createdAt : the date when the related ticket had been created * @param dateUntil : the date when the related ticket end its valid period (after this, the ticket is expired (ticket.dateTo)) * @param validationDuration : the period (in second) when a validation of the related ticket is valid. If -1, "ticket.dateTo" has to be specified */ function getValidationUntilDate(params: TempValidUntilParams): Date { const ticketEntityDateTo = params.dateUntil; if (!params.validationDuration || params.validationDuration === -1) { if (!ticketEntityDateTo) { throw new Error(); } return ticketEntityDateTo; } const calculateDateUntil = moment(params.createdAt).add(params.validationDuration, 'seconds'); return !ticketEntityDateTo ? calculateDateUntil.toDate() : moment.min(moment(ticketEntityDateTo), calculateDateUntil).toDate(); } /** * Get the until date with birthdate, age until and ageCanValidUntil * @param dateAgeUntilParams * @returns */ function getAgeUntilDate(dateAgeUntilParams: DateAgeUntilParams): Date | null { if ( !dateAgeUntilParams.ageUntil || dateAgeUntilParams.ageUntil === -1 || !dateAgeUntilParams?.birthdate || !dateAgeUntilParams?.ageCanValidUntil ) { return null; } const res = moment(dateAgeUntilParams.birthdate).add(dateAgeUntilParams.ageUntil + 1, 'years'); if (dateAgeUntilParams?.ageCanValidUntil === END_YEAR) { return res.endOf('year').toDate(); } if (dateAgeUntilParams?.ageCanValidUntil === END_MONTH) { return res.endOf('month').toDate(); } return res.startOf('day').toDate(); }