File

src/app/payments/payment-create/payment-create.component.ts

Implements

OnInit

Metadata

selector app-payment-create
styleUrls payment-create.component.css
templateUrl ./payment-create.component.html

Index

Properties
Methods

Constructor

constructor(templateService: TemplateService, paymentService: PaymentService, app: AppState, router: Router)
Parameters :
Name Type Optional
templateService TemplateService no
paymentService PaymentService no
app AppState no
router Router no

Methods

Private createPayment
createPayment()
Private getMemo
getMemo()
Returns : any
ngOnInit
ngOnInit()
Returns : void
Private onComplete
onComplete(wasCreated: )
Parameters :
Name Optional
wasCreated no
Returns : any
Private payAgain
payAgain()
Returns : void
Private payFirstTime
payFirstTime()
Returns : void
Private updateTemplateAndCreatePayment
updateTemplateAndCreatePayment(patch: UpdateTemplateRequest)
Parameters :
Name Type Optional
patch UpdateTemplateRequest no
Returns : any

Properties

action$
action$: Observable<any>
Type : Observable<any>
model
model:
Default value : new PaymentCreateVm()
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';
import { Payment, TemplateUpdate, UpdateTemplateRequest } from '../../models';
import { PaymentService, TemplateService } from '../../services';
import { AppState } from '../../stores/app-state.store';
import { PaymentCreateVm } from './payment-create.vm';

@Component({
  selector: 'app-payment-create',
  templateUrl: './payment-create.component.html',
  styleUrls: ['./payment-create.component.css'],
})
export class PaymentCreateComponent implements OnInit {
  action$: Observable<any>;
  model = new PaymentCreateVm();
  constructor(
    private templateService: TemplateService,
    private paymentService: PaymentService,
    private app: AppState,
    private router: Router
  ) {}

  ngOnInit() {
    // tslint:disable-next-line:curly
    if (this.app.isNew) return this.payFirstTime();
    return this.payAgain();
  }

  private payFirstTime() {
    const data = this.app.getCombinedInfo();
    this.action$ = this.paymentService.createPayment(this.app.payment, this.app.template).pipe(
      tap((wasCreated) => {
        if (!wasCreated) {
          return console.log('Failed to create payment!');
        }
        this.paymentService.cachePayment(this.app.payment);
        this.templateService.cacheTemplate(this.app.template);
        this.router.navigate(['/success-screen']);
      })
    );
    this.app.hasPassedMfa = false;
  }

  private getMemo() {
    const memo = this.app.payment.memo && this.app.payment.memo.length > 0 ? this.app.payment.memo : '';
    return memo;
  }

  private payAgain() {
    const memo = this.getMemo();
    const patch: UpdateTemplateRequest = {
      id: this.app.template.id,
      payToName: this.app.template.name,
      payFromAccountNumber: this.app.bankAccount.accountNumber,
      payFromRoutingNumber: this.app.bankAccount.routingNumber,
    };
    let update = false;
    if (this.app.secretWasSet) {
      update = true;
      patch.secret = this.app.template.secret;
    }
    if (memo !== this.app.template.memo) {
      update = true;
      this.app.template.memo = memo;
      patch.memoField = memo;
    }
    const action$ = update ? this.updateTemplateAndCreatePayment(patch) : this.createPayment();
    this.action$ = action$.pipe(tap((wasCreated) => this.onComplete(wasCreated)));
  }

  private updateTemplateAndCreatePayment(patch: UpdateTemplateRequest) {
    // secret was set update template with new secret
    return this.templateService.updateTemplate(patch).pipe(switchMap(() => this.createPayment()));
  }

  private createPayment(): Observable<Payment> {
    this.app.payment.paymentDate = this.model.paymentDate;
    return this.paymentService.createPayment(this.app.payment, this.app.template);
  }

  private onComplete(wasCreated) {
    if (!wasCreated) {
      return console.log('Failed to create payment!');
    }
    this.app.hasPassedMfa = false;
    this.paymentService.cachePayment(this.app.payment);
    this.router.navigate(['/success-screen']);
  }
}
<app-loader [header]="model.loaderHeader" [message]="model.message"></app-loader>
<ng-container *ngIf="action$ | async"></ng-container>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""