File

src/app/payments/payment-detail/payment-detail.vm.ts

Index

Properties
Methods

Constructor

constructor(statusService?: PaymentStatusService, payment?: Payment, datePipe?: DatePipe, template?: Template)
Parameters :
Name Type Optional
statusService PaymentStatusService yes
payment Payment yes
datePipe DatePipe yes
template Template yes

Methods

Private expirationDateCachePayment
expirationDateCachePayment(datePipe: DatePipe)
Parameters :
Name Type Optional
datePipe DatePipe no
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)
Parameters :
Name Type Optional
date string no
datePipe DatePipe no
Returns : Date
Private parsePaymentDate
parsePaymentDate(datePipe: DatePipe)
Parameters :
Name Type Optional
datePipe DatePipe no
Returns : any
Private parseTodayDate
parseTodayDate(datePipe: DatePipe)
Parameters :
Name Type Optional
datePipe DatePipe no
Returns : Date
setDaysLeft
setDaysLeft(days: number)
Parameters :
Name Type Optional
days number no
Returns : string

Properties

contactMethod
contactMethod: string
Type : string
daysLeft
daysLeft: string
Type : string
expirationDate
expirationDate: Date
Type : Date
isExpired
isExpired:
Default value : false
isLoading
isLoading:
Default value : false
loaderHeader
loaderHeader: string
Type : string
Default value : ''
message
message: string
Type : string
Default value : ''
p2PExpiresOn
p2PExpiresOn: Date
Type : Date
payment
payment: Payment
Type : Payment
paymentDate
paymentDate: Date
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;
    }
  }
}

results matching ""

    No results matching ""