import { Expose, Transform, Type } from 'class-transformer' import { isNil } from 'lodash' import { parseDate } from '../utils/date' import { BaseModel } from './base.model' import { CancellationsReasons, PaymentProviders } from './enums' import { SubscriptionInstallment } from './subscription_installment.model' import { SubscriptionSmartfit } from './subscription_smartfit.model' import { SubscriptionStatus } from './types' export class Subscription extends BaseModel { id: string token?: string @Expose({ name: 'payment_provider' }) paymentProvider: PaymentProviders @Expose({ name: 'original_transaction' }) originalTransaction?: string @Expose({ name: 'patient_id' }) patientId: string @Expose({ name: 'plan_id' }) planId: string @Transform(value => parseDate(value), { toClassOnly: true }) @Expose({ name: 'subscribed_at' }) subscribedAt: Date @Transform(value => parseDate(value), { toClassOnly: true }) @Expose({ name: 'created_at' }) createdAt: Date @Transform(value => parseDate(value), { toClassOnly: true }) @Expose({ name: 'trial_expires_at' }) trialExpiresAt?: Date @Transform(value => parseDate(value), { toClassOnly: true }) @Expose({ name: 'updated_at' }) updatedAt?: Date @Transform(value => parseDate(value), { toClassOnly: true }) @Expose({ name: 'expires_at' }) expiresAt?: Date @Transform(value => parseDate(value), { toClassOnly: true }) @Expose({ name: 'suspended_at' }) suspendedAt?: Date @Transform(value => parseDate(value), { toClassOnly: true }) @Expose({ name: 'cancellation_request_at' }) cancellationRequestAt?: Date @Transform(value => parseDate(value), { toClassOnly: true }) @Expose({ name: 'cancellation_requested_by_patient_at' }) cancellationRequestedByPatientAt?: Date @Transform(value => parseDate(value), { toClassOnly: true }) @Expose({ name: 'cancellation_at' }) cancellationAt?: Date @Expose({ name: 'cancellation_reason_id' }) cancellationReasonId?: CancellationsReasons @Expose({ name: 'cancellation_other_reason' }) cancellationOtherReason?: string @Expose({ name: 'cancellation_by_id' }) cancellationById?: string @Expose({ name: 'cancellation_by_type' }) cancellationByType?: string @Expose({ name: 'teleconsultations_credit' }) teleconsultationsCredit?: number @Transform(value => parseDate(value), { toClassOnly: true }) @Expose({ name: 'teleconsultation_renews_at' }) teleconsultationRenewsAt?: Date @Expose({ name: 'contract_url' }) contractURL?: string @Expose({ name: 'card_number' }) cardNumber?: string @Expose({ name: 'status', groups: ['json'] }) get status(): SubscriptionStatus { if (!isNil(this.cancellationAt)) return 'cancelled' if (!isNil(this.cancellationRequestAt)) return 'in_cancellation' return 'active' } @Expose({ name: 'omie_contract_code' }) omieContractCode?: string @Expose({ name: 'omie_os_code' }) omieOSCode?: string @Expose({ name: 'discount_coupon_id' }) discountCouponId?: string @Type(() => SubscriptionInstallment) installments?: SubscriptionInstallment[] data?: Record get isCancelled(): boolean { return this.status === 'cancelled' } get isTrial(): boolean { return this.status === 'active' && !isNil(this.trialExpiresAt) && this.trialExpiresAt >= new Date() } fillCancellationDataFromSmartfit(infoAddon: SubscriptionSmartfit): void { this.cancellationRequestAt = this.cancellationRequestAt || infoAddon.cancellationRequestedAt this.cancellationAt = this.cancellationAt || infoAddon.cancelledAt this.trialExpiresAt = this.trialExpiresAt || infoAddon.trialExpiresAt } uncancel(): void { this.cancellationAt = null this.cancellationById = null this.cancellationByType = null this.cancellationOtherReason = null this.cancellationReasonId = null this.cancellationRequestAt = null } }