src/app/modules/ach-setup/providers/ach-setup.service.ts
Properties |
|
Methods |
constructor(http: HttpClient, errorStore: ErrorStore)
|
|||||||||
Parameters :
|
clearState |
clearState()
|
Returns :
void
|
create |
create()
|
Returns :
any
|
fetchSetupForDomain | ||||||
fetchSetupForDomain(domain: string)
|
||||||
Parameters :
Returns :
any
|
Private handleError | |||||||||
handleError(error: HttpErrorResponse, message: string)
|
|||||||||
Parameters :
Returns :
any
|
loadFinancialInstitutions |
loadFinancialInstitutions()
|
Returns :
any
|
set | ||||||
set(state: IAchConfiguration)
|
||||||
Parameters :
Returns :
void
|
Public fis$ |
fis$:
|
Default value : this.fisSubject.asObservable()
|
Private fisSubject |
fisSubject:
|
Default value : new BehaviorSubject<FinancialInstitution[]>(null)
|
Private state |
state:
|
Type : AchConfiguration
|
Default value : new AchConfiguration()
|
Public state$ |
state$:
|
Default value : this.stateSubject.asObservable()
|
Private stateSubject |
stateSubject:
|
Default value : new BehaviorSubject<AchConfiguration>(null)
|
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BehaviorSubject, of, throwError } from 'rxjs';
import { catchError, filter, map, tap } from 'rxjs/operators';
import { FinancialInstitution } from 'src/app/ach-setup/models/financial-institution';
import { API_ROOT } from '../../../app.constants';
import { ErrorStore } from '../../../providers/stores';
import { AchConfiguration, IAchConfiguration } from '../models/ach-configuration';
@Injectable({
providedIn: 'root'
})
export class AchSetupService {
private state: AchConfiguration = new AchConfiguration();
private stateSubject = new BehaviorSubject<AchConfiguration>(null);
public state$ = this.stateSubject.asObservable();
private fisSubject = new BehaviorSubject<FinancialInstitution[]>(null);
public fis$ = this.fisSubject.asObservable();
constructor(private http: HttpClient, private errorStore: ErrorStore) {
this.loadFinancialInstitutions()
.pipe(
filter(fis => fis !== null && fis !== undefined),
map(fis => {
return fis
.filter(fi => {
return fi !== null && fi !== undefined && fi.domain !== null && fi.domain !== undefined;
})
.filter(fi => {
// remove domains marked as DONOTUSE
return fi.domain.toLowerCase() !== 'donotuse';
})
.sort((a, b) => {
if (a.domain > b.domain) {
return 1;
}
if (a.domain < b.domain) {
return -1;
}
return 0;
});
})
)
.subscribe(fis => {
this.fisSubject.next(fis);
});
}
set(state: IAchConfiguration) {
this.state = new AchConfiguration(state);
this.stateSubject.next(this.state);
}
clearState() {
this.state = null;
this.stateSubject.next(this.state);
}
create() {
return this.http.post<boolean>(`${API_ROOT}/ach/setup`, this.state).pipe(
map(() => {
return true;
}),
catchError(error => {
return this.handleError(error, 'Failed to create ach configuration');
})
);
}
fetchSetupForDomain(domain: string) {
domain = domain ? domain : '';
return this.http.get<AchConfiguration | null>(`${API_ROOT}/ach/setup?domain=${domain}`).pipe(
tap(config => console.log({ config })),
catchError(error => {
console.log(error);
if (error.status === 404) {
return of(null);
}
return throwError('This ach setup has an issue. Please contact development to resolve');
})
);
}
loadFinancialInstitutions() {
return this.http.get<FinancialInstitution[]>(`${API_ROOT}/ach/setup/fis`).pipe(
catchError(error => {
return this.handleError(error, 'Failed to fetch minified financial institutions');
})
);
}
private handleError(error: HttpErrorResponse, message: string) {
const title = 'Ach Configuration Error';
// todo
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.error('An error occurred:', error.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.error(`Backend returned code ${error.status}, ` + `body was: ${error.error}`);
}
// return an ErrorObservable with a user-facing error message
return throwError(
`There was a problem creating the ACH entries. Error code: ${error.status}. Error message: ${
error.error
}`
);
}
}