src/app/payments/payment-detail/payment-detail.vm.ts
        
| Properties | 
| Methods | 
| constructor(statusService?: PaymentStatusService, payment?: Payment, datePipe?: DatePipe, template?: Template) | |||||||||||||||
| 
                                                Parameters :
                                                 
 | 
| Private expirationDateCachePayment | ||||||
| expirationDateCachePayment(datePipe: DatePipe) | ||||||
| 
                                                Parameters :
                                                 
 
                                            Returns :      Date | 
| isCancelled | 
| isCancelled() | 
| 
                                            Returns :      boolean | 
| isDone | 
| isDone() | 
| 
                                            Returns :      boolean | 
| isFailedAttempts | 
| isFailedAttempts() | 
| 
                                            Returns :      boolean | 
| isHandlingError | 
| isHandlingError() | 
| 
                                            Returns :      boolean | 
| isNotApplicable | 
| isNotApplicable() | 
| 
                                            Returns :      boolean | 
| isPaymentInFlight | 
| isPaymentInFlight() | 
| 
                                            Returns :      boolean | 
| isPmtExpired | 
| isPmtExpired() | 
| 
                                            Returns :      boolean | 
| isProcessing | 
| isProcessing() | 
| 
                                            Returns :      boolean | 
| isWaiting | 
| isWaiting() | 
| 
                                            Returns :      boolean | 
| Private parseExpirationDate | 
| parseExpirationDate(date: string, datePipe: DatePipe) | 
| 
                                            Returns :      Date | 
| Private parsePaymentDate | ||||||
| parsePaymentDate(datePipe: DatePipe) | ||||||
| 
                                                Parameters :
                                                 
 
                                            Returns :      any | 
| Private parseTodayDate | ||||||
| parseTodayDate(datePipe: DatePipe) | ||||||
| 
                                                Parameters :
                                                 
 
                                            Returns :      Date | 
| setDaysLeft | ||||||
| setDaysLeft(days: number) | ||||||
| 
                                                Parameters :
                                                 
 
                                            Returns :      string | 
| contactMethod | 
| contactMethod:      | 
| Type : string | 
| daysLeft | 
| daysLeft:      | 
| Type : string | 
| expirationDate | 
| expirationDate:      | 
| Type : Date | 
| isExpired | 
| isExpired:      | 
| Default value : false | 
| isLoading | 
| isLoading:      | 
| Default value : false | 
| loaderHeader | 
| loaderHeader:      | 
| Type : string | 
| Default value : '' | 
| message | 
| message:      | 
| Type : string | 
| Default value : '' | 
| p2PExpiresOn | 
| p2PExpiresOn:      | 
| Type : Date | 
| payment | 
| payment:      | 
| Type : Payment | 
| paymentDate | 
| paymentDate:      | 
| Type : Date | 
import { PaymentState } from '../../enums';
import { PaymentStatusService } from '../../services';
import { Payment, Template } from '../../models';
import { DatePipe } from '@angular/common';
const states = PaymentState;
const DAYS_TILL_EXPIRE = 10;
const ONE_DAY = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
export class PaymentDetailViewModel {
  payment: Payment;
  paymentDate: Date;
  contactMethod: string;
  expirationDate: Date;
  p2PExpiresOn: Date;
  daysLeft: string;
  isLoading = false;
  isExpired = false;
  loaderHeader = '';
  message = '';
  constructor(
    private statusService?: PaymentStatusService,
    payment?: Payment,
    datePipe?: DatePipe,
    template?: Template
  ) {
    // tslint:disable-next-line:curly
    if (statusService === null || statusService === undefined) return;
    // tslint:disable-next-line:curly
    if (payment === null || payment === undefined) return;
    this.payment = payment;
    const phoneOrNull =
      this.payment.p2PPayToPhoneNumber && this.payment.p2PPayToPhoneNumber.length > 0
        ? this.payment.p2PPayToPhoneNumber
        : null;
    this.contactMethod =
      this.payment.p2PPayToEmailAddress && this.payment.p2PPayToEmailAddress.length > 0
        ? this.payment.p2PPayToEmailAddress
        : phoneOrNull;
    if (this.isPaymentInFlight() && !this.contactMethod && template) {
      this.contactMethod =
        template.defaultContactMethod.toLowerCase() === 'email'
          ? template.p2PPayToEmailAddress
          : template.p2PPayToPhoneNumber;
    }
    this.paymentDate = this.parsePaymentDate(datePipe);
    if (this.payment.p2PExpiresOn === null || this.payment.p2PExpiresOn === undefined) {
      this.expirationDate = this.expirationDateCachePayment(datePipe);
    } else {
      this.expirationDate = this.parseExpirationDate(this.payment.p2PExpiresOn, datePipe);
    }
    const today = this.parseTodayDate(datePipe);
    const diff = Math.floor((this.expirationDate.getTime() - today.getTime()) / ONE_DAY);
    this.setDaysLeft(diff);
  }
  private parsePaymentDate(datePipe: DatePipe) {
    const paymentLongDate = new Date(this.payment.paymentDate);
    return new Date(paymentLongDate);
  }
  private parseTodayDate(datePipe: DatePipe): Date {
    const todayLongDate = new Date();
    const todayShortDate = datePipe.transform(todayLongDate, 'shortDate');
    return new Date(todayShortDate);
  }
  private expirationDateCachePayment(datePipe: DatePipe): Date {
    const startDateCachePayment = new Date(this.payment.paymentDate);
    const expireDateCachePayment = new Date(this.payment.paymentDate);
    expireDateCachePayment.setDate(startDateCachePayment.getDate() + DAYS_TILL_EXPIRE);
    const shortDateCachePayment = datePipe.transform(expireDateCachePayment, 'shortDate');
    return new Date(shortDateCachePayment);
  }
  private parseExpirationDate(date: string, datePipe: DatePipe): Date {
    const oringalDate = new Date(date);
    const shortDate = datePipe.transform(oringalDate, 'shortDate');
    return new Date(shortDate);
  }
  isWaiting(): boolean {
    // tslint:disable-next-line:curly
    if (this.statusService === null || this.statusService === undefined) return false;
    return this.statusService.getState(this.payment) === PaymentState.WAITING;
  }
  isPaymentInFlight(): boolean {
    // tslint:disable-next-line:curly
    if (this.statusService === null || this.statusService === undefined) return false;
    return this.statusService.getState(this.payment) === PaymentState.INFLIGHT;
  }
  isPmtExpired(): boolean {
    // tslint:disable-next-line:curly
    if (this.statusService === null || this.statusService === undefined) return false;
    return this.statusService.getState(this.payment) === PaymentState.EXPIRED;
  }
  isFailedAttempts(): boolean {
    // tslint:disable-next-line:curly
    if (this.statusService === null || this.statusService === undefined) return false;
    return this.statusService.getState(this.payment) === PaymentState.FAILED_MAX_ATTEMPTS;
  }
  isNotApplicable(): boolean {
    // tslint:disable-next-line:curly
    if (this.statusService === null || this.statusService === undefined) return false;
    return this.statusService.getState(this.payment) === PaymentState.NOTAPPLICABLE;
  }
  isProcessing(): boolean {
    // tslint:disable-next-line:curly
    if (this.statusService === null || this.statusService === undefined) return false;
    return this.statusService.getState(this.payment) === PaymentState.PROCESSING;
  }
  isCancelled(): boolean {
    // tslint:disable-next-line:curly
    if (this.statusService === null || this.statusService === undefined) return false;
    return this.statusService.getState(this.payment) === PaymentState.CANCELLED;
  }
  isDone(): boolean {
    // tslint:disable-next-line:curly
    if (this.statusService === null || this.statusService === undefined) return false;
    return this.statusService.getState(this.payment) === PaymentState.DONE;
  }
  isHandlingError(): boolean {
    // tslint:disable-next-line:curly
    if (this.statusService === null || this.statusService === undefined) return false;
    return this.statusService.getState(this.payment) === PaymentState.ERROR;
  }
  setDaysLeft(days: number) {
    if (days === 0) {
      return (this.daysLeft = 'LESS THAN 1');
    } else if (days > 0) {
      this.daysLeft = `${days}`;
    } else {
      this.daysLeft = 'EXPIRED';
      this.isExpired = true;
    }
  }
}