src/app/providers/stores/app-state.store.ts
Properties |
|
Methods |
|
Accessors |
constructor(sessionStore: SessionStore, limitsService: LimitsService)
|
|||||||||
Parameters :
|
clear |
clear()
|
Returns :
void
|
onChange |
onChange(property: string, value: any)
|
Returns :
void
|
Private templatesRequireMfa |
templatesRequireMfa()
|
Returns :
boolean
|
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()
|
useMfaModal | ||||||
getuseMfaModal()
|
||||||
setuseMfaModal(value: boolean)
|
||||||
Parameters :
Returns :
void
|
loadingIsLimits | ||||||
getloadingIsLimits()
|
||||||
setloadingIsLimits(result: boolean)
|
||||||
Parameters :
Returns :
void
|
limitsResult | ||||||
getlimitsResult()
|
||||||
setlimitsResult(result: LimitsResult)
|
||||||
Parameters :
Returns :
void
|
hasPassedMfa | ||||||
gethasPassedMfa()
|
||||||
sethasPassedMfa(value: boolean)
|
||||||
Parameters :
Returns :
void
|
acknowledgeDuplicate | ||||||
getacknowledgeDuplicate()
|
||||||
setacknowledgeDuplicate(value: boolean)
|
||||||
Parameters :
Returns :
void
|
duplicateInfo | ||||||
getduplicateInfo()
|
||||||
setduplicateInfo(value: DuplicateInfo)
|
||||||
Parameters :
Returns :
void
|
requireMfa |
getrequireMfa()
|
isNew | ||||||
getisNew()
|
||||||
setisNew(value: boolean)
|
||||||
Parameters :
Returns :
void
|
amount | ||||||
getamount()
|
||||||
setamount(amount: number)
|
||||||
Parameters :
Returns :
void
|
memo | ||||||
getmemo()
|
||||||
setmemo(memo: string)
|
||||||
Parameters :
Returns :
void
|
debitTemplate | ||||||
getdebitTemplate()
|
||||||
setdebitTemplate(template: Template)
|
||||||
Parameters :
Returns :
void
|
creditTemplate | ||||||
getcreditTemplate()
|
||||||
setcreditTemplate(template: Template)
|
||||||
Parameters :
Returns :
void
|
payment | ||||||
getpayment()
|
||||||
setpayment(payment: Payment)
|
||||||
Parameters :
Returns :
void
|
template | ||||||
gettemplate()
|
||||||
settemplate(template: Template)
|
||||||
Parameters :
Returns :
void
|
multiFactorModel | ||||||
getmultiFactorModel()
|
||||||
setmultiFactorModel(multiFactorModel: MultiFactorModel)
|
||||||
Parameters :
Returns :
void
|
selectedMethod | ||||||
getselectedMethod()
|
||||||
setselectedMethod(selectedMethod: SelectedMultiFactorMethod)
|
||||||
Parameters :
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
);
}
}