/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { isNil } from 'lodash' import { keepNumbers } from '../../../utils/string' import { LocationCountry, PaymentMethods, SmartFitCollaboratorPlans, SmartFitPlan } from '../enums' import { SmartFitPlanUtils } from '../utils' import { StateHelper } from '../helpers/state.helper' export const PersonStatuses = ['active', 'blocked', 'cancelled'] as const export type PersonStatus = typeof PersonStatuses[number] export type AddonInstallment = { addonCode: string installments: number } export class Person { id: number email: string name: string phone: string status: PersonStatus birthdate: Date plan: SmartFitPlan | SmartFitCollaboratorPlans photoUrl: string gender: string passwordRecoveryUrl: string paymentMethod: PaymentMethods mainPurchaseId: number mainPurchaseExpiredAt: Date turnstileStatus?: string membershipDueDate?: number remainingGuests?: number limitGuests?: number purchaseConfirmedAt?: Date blackBenefits?: boolean locationId: number locationAcronym?: string locationCountry?: LocationCountry lastEmailMessageDeliverAt?: Date welsher?: boolean canPurchaseAddon: boolean address?: { complement?: string number: string region: string street: string zip: string zone: string city: string state: string uf: string } smartphoneConfigs?: [] authToken: string addonsInstallments: Array lastDigitsCardNumber: string constructor(args?: Partial) { Object.assign(this, args) } static build(person: any): Person { return new Person({ id: person.id, email: person.email, name: person.name, phone: `+${keepNumbers(person.full_phone)}`, status: person.status, birthdate: new Date(person.birthdate), plan: person.plan, photoUrl: person.photo_url, gender: person.gender, passwordRecoveryUrl: person.password_recovery_url, paymentMethod: person.payment_method, mainPurchaseId: person.main_purchase_id, mainPurchaseExpiredAt: person.main_purchase_expired_at ? new Date(person.main_purchase_expired_at) : null, canPurchaseAddon: person.can_purchase_addon, locationId: person.location_id, locationAcronym: person.location_acronym, locationCountry: person.location_country, addonsInstallments: this.getMappedAddonInstallments(person.addons_installments), lastDigitsCardNumber: person.card_number, ...(!isNil(person.auth_token) ? { authToken: person.auth_token } : {}), address: { ...person.address, uf: StateHelper.nameToUF(person.address.state) }, }) } get isCollaborator(): boolean { return SmartFitPlanUtils.isCollaboratorPlanId(this.plan) } get isStudent(): boolean { return !this.isCollaborator } get isActive(): boolean { return isNil(this.mainPurchaseExpiredAt) } get isBlocked(): boolean { return !this.isActive } get isCancelled(): boolean { return this.isStudent && this.hasPurchaseExpired } get isActiveOrBlocked(): boolean { return this.isActive || this.isBlocked } get hasPurchaseExpired(): boolean { return this.isStudent // && // !!( // !isNil(this.mainPurchaseExpiredAt) && // DateUtils.momentWithZone(this.mainPurchaseExpiredAt) // .startOf('day') // .isBefore(DateUtils.momentWithZone().startOf('day')) // ) } get inCancellation(): boolean { return this.isStudent // && // !!( // !isNil(this.mainPurchaseExpiredAt) && // DateUtils.momentWithZone(this.mainPurchaseExpiredAt) // .startOf('day') // .isSameOrAfter(DateUtils.momentWithZone().startOf('day')) // ) } get hasAllowedPaymentMethod(): boolean { if (this.isCollaborator) return true return this.paymentMethod !== PaymentMethods.money } get allowPurchaseAddons(): boolean { return this.canPurchaseAddon } get allowSubscribe(): boolean { return this.isStudent ? this.allowPurchaseAddons : this.isActiveOrBlocked } getInstallmentsMap(): Record { return this.addonsInstallments.reduce( (previous, current) => ({ ...previous, [current.addonCode]: current.installments, }), {}, ) } private static getMappedAddonInstallments(addons_installments: Array): Array { return addons_installments?.length > 0 ? addons_installments.map(({ addon_code, installments }) => ({ addonCode: addon_code, installments, })) : [] } }