File

src/app/providers/stores/app-state.store.ts

Index

Properties
Methods
Accessors

Constructor

constructor(sessionStore: SessionStore, limitsService: LimitsService)
Parameters :
Name Type Optional
sessionStore SessionStore No
limitsService LimitsService No

Methods

clear
clear()
Returns : void
onChange
onChange(property: string, value: any)
Parameters :
Name Type Optional Default value
property string No
value any No null
Returns : void
Private templatesRequireMfa
templatesRequireMfa()
Returns : boolean

Properties

Private _state
Default value : new AppStateModel()
Private _stateSrc
Default value : new BehaviorSubject<AppStateModel>(this._state)
Private amountSubject
Type : Subject<number>
Default value : new Subject()
Private debitTemplateSubject
Type : Subject<Template>
Default value : new Subject()
Private request
Type : Subscription
state$
Default value : this._stateSrc.asObservable()

Accessors

useMfaModal
getuseMfaModal()
setuseMfaModal(value: boolean)
Parameters :
Name Type Optional
value boolean No
Returns : void
loadingIsLimits
getloadingIsLimits()
setloadingIsLimits(result: boolean)
Parameters :
Name Type Optional
result boolean No
Returns : void
limitsResult
getlimitsResult()
setlimitsResult(result: LimitsResult)
Parameters :
Name Type Optional
result LimitsResult No
Returns : void
hasPassedMfa
gethasPassedMfa()
sethasPassedMfa(value: boolean)
Parameters :
Name Type Optional
value boolean No
Returns : void
acknowledgeDuplicate
getacknowledgeDuplicate()
setacknowledgeDuplicate(value: boolean)
Parameters :
Name Type Optional
value boolean No
Returns : void
duplicateInfo
getduplicateInfo()
setduplicateInfo(value: DuplicateInfo)
Parameters :
Name Type Optional
value DuplicateInfo No
Returns : void
requireMfa
getrequireMfa()
isNew
getisNew()
setisNew(value: boolean)
Parameters :
Name Type Optional
value boolean No
Returns : void
amount
getamount()
setamount(amount: number)
Parameters :
Name Type Optional
amount number No
Returns : void
memo
getmemo()
setmemo(memo: string)
Parameters :
Name Type Optional
memo string No
Returns : void
debitTemplate
getdebitTemplate()
setdebitTemplate(template: Template)
Parameters :
Name Type Optional
template Template No
Returns : void
creditTemplate
getcreditTemplate()
setcreditTemplate(template: Template)
Parameters :
Name Type Optional
template Template No
Returns : void
payment
getpayment()
setpayment(payment: Payment)
Parameters :
Name Type Optional
payment Payment No
Returns : void
template
gettemplate()
settemplate(template: Template)
Parameters :
Name Type Optional
template Template No
Returns : void
multiFactorModel
getmultiFactorModel()
setmultiFactorModel(multiFactorModel: MultiFactorModel)
Parameters :
Name Type Optional
multiFactorModel MultiFactorModel No
Returns : void
selectedMethod
getselectedMethod()
setselectedMethod(selectedMethod: SelectedMultiFactorMethod)
Parameters :
Name Type Optional
selectedMethod SelectedMultiFactorMethod No
Returns : void
isValid
getisValid()
import { DuplicateInfo } from './../../models/duplicate-info';
import { Injectable } from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { debounceTime, filter, switchMap, tap } from 'rxjs/operators';
import { combineLatest } from 'rxjs/observable/combineLatest';
import { MultiFactorModel, Payment, SelectedMultiFactorMethod, Template } from '../../models';
import { AppStateModel } from '../../models/app-state.model';
import { LimitsResult } from '../../models/limits-result.model';
import { LimitsService } from '../services/limits.service';
import { SessionStore } from './session.store';

@Injectable()
export class AppStateStore {
  private _state = new AppStateModel();
  private _stateSrc = new BehaviorSubject<AppStateModel>(this._state);
  state$ = this._stateSrc.asObservable();
  private amountSubject: Subject<number> = new Subject();
  private debitTemplateSubject: Subject<Template> = new Subject();
  private request: Subscription;

  constructor(private sessionStore: SessionStore, private limitsService: LimitsService) {
    this.sessionStore.session$
      .filter((session) => (session ? true : false))
      .subscribe((session) => {
        this.multiFactorModel = new MultiFactorModel(session);
      });

    combineLatest(this.amountSubject.asObservable(), this.debitTemplateSubject.asObservable())
      .pipe(
        debounceTime(500),
        tap(() => {
          this.loadingIsLimits = false;
        }),
        filter(([amount, debitTemplate]) => amount > 0),
        filter(([amount, debitTemplate]) => !!debitTemplate),
        tap(() => {
          this.loadingIsLimits = true;
        }),
        switchMap(([amount, debitTemplate]) =>
          this.limitsService.verify(amount, debitTemplate)
        )
      )
      .subscribe((result) => {
        this.limitsResult = result;
        this.loadingIsLimits = false;
      });
  }

  onChange(property: string, value: any = null) {
    const message = `on app data change: ${property}`;
    value ? console.log(message, value) : console.log(message);

    this._stateSrc.next(this._state);
  }

  set useMfaModal(value: boolean) {
    this._state.useMfaModal = value;
    this.onChange('useMfaModal');
  }

  get useMfaModal(): boolean {
    return this._state.useMfaModal;
  }

  set loadingIsLimits(result: boolean) {
    this._state.loadingIsLimits = result;
    this.onChange('loadingIsLimits');
  }

  get loadingIsLimits(): boolean {
    return this._state.loadingIsLimits;
  }

  set limitsResult(result: LimitsResult) {
    this._state.limitsResult = result;
    this.onChange('limitsResult');
  }

  get limitsResult(): LimitsResult {
    return this._state.limitsResult;
  }

  set hasPassedMfa(value: boolean) {
    this._state.hasPassedMfa = value;
    this.onChange('hasPassedMfa');
  }

  get hasPassedMfa(): boolean {
    return this._state.hasPassedMfa;
  }

  set acknowledgeDuplicate(value: boolean) {
    this._state.acknowledgedDuplicate = value;
    this.onChange('userHasAcknowledgeDuplicatePayment');
  }

  get acknowledgeDuplicate(): boolean {
    return this._state.acknowledgedDuplicate;
  }

  set duplicateInfo(value: DuplicateInfo) {
    this._state.duplicateInfo = value;
    this.onChange('duplicateInfo');
  }

  get duplicateInfo(): DuplicateInfo {
    return this._state.duplicateInfo;
  }

  get requireMfa(): boolean {
    if (this.hasPassedMfa) {
      return false;
    }
    return this.templatesRequireMfa();
  }

  private templatesRequireMfa(): boolean {
    return (
      (this.creditTemplate && this.creditTemplate.requireMfaOnPaymentCreation) ||
      (this.debitTemplate && this.debitTemplate.requireMfaOnPaymentCreation)
    );
  }

  set isNew(value: boolean) {
    this._state.isNew = value;
    this.onChange('isNew');
  }

  get isNew(): boolean {
    return this._state.isNew;
  }

  set amount(amount: number) {
    this._state.amount = amount;
    const nextAmount = amount ? amount : null;
    this.amountSubject.next(nextAmount);
    this.onChange('amount');
  }

  get memo(): string {
    return this._state.memo;
  }

  set memo(memo: string) {
    this._state.memo = memo;
    this.onChange('memo');
  }

  get amount(): number {
    return this._state.amount;
  }

  set debitTemplate(template: Template) {
    this._state.debitTemplate = template;
    const nextDebitTemplate = this.debitTemplate ? this.debitTemplate : null;
    this.debitTemplateSubject.next(nextDebitTemplate);
    this.onChange('debitTemplate', template);
  }

  get debitTemplate(): Template {
    return this._state.debitTemplate;
  }

  set creditTemplate(template: Template) {
    this._state.creditTemplate = template;
    this.onChange('creditTemplate', template);
  }

  get payment(): Payment {
    return this._state.payment;
  }

  set payment(payment: Payment) {
    this._state.payment = payment;
    const nextDebitTemplate = this.debitTemplate ? this.debitTemplate : null;
    this.debitTemplateSubject.next(nextDebitTemplate);
    this.onChange('payment');
  }

  get template(): Template {
    return this._state.template;
  }

  set template(template: Template) {
    this._state.template = template;
    this.onChange('template');
  }

  get creditTemplate(): Template {
    return this._state.creditTemplate;
  }

  set multiFactorModel(multiFactorModel: MultiFactorModel) {
    this._state.multiFactorModel = multiFactorModel;
    this.onChange('multiFactorModel');
  }

  get multiFactorModel(): MultiFactorModel {
    return this._state.multiFactorModel;
  }

  set selectedMethod(selectedMethod: SelectedMultiFactorMethod) {
    this._state.selectedMethod = selectedMethod;
    this.onChange('selectedMethod');
  }

  get selectedMethod(): SelectedMultiFactorMethod {
    return this._state.selectedMethod;
  }

  clear() {
    const mfaModel = this._state.multiFactorModel;
    this._state = new AppStateModel();
    this._state.multiFactorModel = mfaModel;
    console.log(mfaModel);
    this.onChange('cleared all (except mfa model)!');
    this.debitTemplate = null;
    this.creditTemplate = null;
    this.acknowledgeDuplicate = false;
  }

  get isValid(): boolean {
    return (
      this.amount !== null &&
      this.amount !== undefined &&
      this.amount > 0 &&
      this.creditTemplate !== null &&
      this.creditTemplate !== undefined &&
      this.creditTemplate.name.length > 0 &&
      this.debitTemplate !== null &&
      this.debitTemplate !== undefined &&
      this.debitTemplate.name.length > 0 &&
      this.limitsResult !== null
      // this.limitsResult.isAllowed
    );
  }
}

results matching ""

    No results matching ""