src/app/providers/services/limits.service.ts
Methods |
constructor(sessionStore: SessionStore, http: HttpClient)
|
|||||||||
Parameters :
|
verify |
verify(amount: Number, debitTemplate: Template)
|
Returns :
Observable<LimitsResult>
|
import { Template } from './../../models/template.model';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { combineLatest } from 'rxjs/observable/combineLatest';
import { LimitsResult } from '../../models/limits-result.model';
import { SessionStore } from '../stores';
import { catchError, filter, map } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';
import { LIMITS_API } from '../../app.constants';
import { HttpClient } from '@angular/common/http';
import { AccountCategory } from '../../enums';
@Injectable()
export class LimitsService {
constructor(private sessionStore: SessionStore, private http: HttpClient) {}
verify(amount: Number, debitTemplate: Template): Observable<LimitsResult> {
if (!amount) {
return of(new LimitsResult());
}
if (!debitTemplate) {
throw new Error('debit template is required to check limits');
}
const limitType =
debitTemplate.accountCategory === AccountCategory.FiVerified ? 'DEBIT' : 'CREDIT';
const callable$ = this.http.get(`${LIMITS_API}/verify?amount=${amount}&type=${limitType}`);
const session$ = this.sessionStore.session$;
return combineLatest(session$, callable$).pipe(
filter(([session, response]) => session !== null && session !== undefined),
map(([session, response]) => {
const result = new LimitsResult();
const limits = session.limits;
if (limits.electronic.verify < amount) {
result.requiresMfa = true;
}
return result;
}),
catchError((response) => {
// The backedn returned an unsuccessful response code.
console.error(
`Backend returned code ${response.status}, ` + `body was: ${response.error}`
);
const result = new LimitsResult();
result.addError(response.error);
return of(result);
})
);
}
}