projects/prestations-ng/src/gesdem/gesdem-handler.service.ts
Properties |
|
Methods |
|
Accessors |
constructor(http: HttpClient, growlService: GrowlBrokerService, validationHandlerService: ValidationHandlerService, route: ActivatedRoute, errorHandlerService: GesdemErrorHandlerService, sessionInfo: SessionInfo, dictionaryService: SdkDictionaryService)
|
||||||||||||||||||||||||
Parameters :
|
Private emitResponse | ||||||||||||
emitResponse(response: FormPostResponse<any>, shouldDispatchErrors: boolean)
|
||||||||||||
Parameters :
Returns :
void
|
getCaptchaToken |
getCaptchaToken()
|
Returns :
string
|
Private handleGesdemError | ||||||||||||
handleGesdemError(requestedReference: string, reCaptchaByPassUUID: string, response: HttpErrorResponse | any)
|
||||||||||||
Parameters :
Returns :
Observable<never>
|
isPendingPayment |
isPendingPayment()
|
Returns :
boolean
|
pdf(form: any)
|
||||||
Parameters :
Returns :
Observable<any>
|
Private postToUrl |
postToUrl(form: any, url: string, displaySuccessMessage: boolean)
|
Returns :
Observable<any>
|
require | ||||||||||||||||||||
require(reference?: string, reCaptchaByPassUUID?: string, forceRefreshIfNew?: boolean, byPassEmitResponse?: boolean)
|
||||||||||||||||||||
Parameters :
Returns :
Observable<any>
Observable |
retrieve | ||||||||||||||||
retrieve(reference: any, reCaptchaByPassUUID: string, shouldDispatchErrors: boolean)
|
||||||||||||||||
Parameters :
Returns :
Observable<any>
|
retrieveActionStatut | ||||||
retrieveActionStatut(reference: any)
|
||||||
Parameters :
Returns :
Observable<ActionStatut>
|
save | ||||||||||||||||
save(form: any, canCreateDemande: boolean, displaySuccessMessage: boolean)
|
||||||||||||||||
Parameters :
Returns :
Observable<any>
|
setCaptchaToken | ||||||
setCaptchaToken(token: string)
|
||||||
Parameters :
Returns :
void
|
transmit |
transmit()
|
Returns :
Observable<any>
|
validate | ||||||
validate(form: any)
|
||||||
Parameters :
Returns :
Observable<any>
|
Private _captchaToken |
Type : string
|
Private _lastResponse |
Type : FormPostResponse<any>
|
_prefix |
Type : string
|
Private _updateFormDataSubject |
Type : Subject<any>
|
Default value : new Subject<any>()
|
lastResponse |
getlastResponse()
|
updateFormDataSubject |
getupdateFormDataSubject()
|
prefix | ||||||
getprefix()
|
||||||
setprefix(prefix: string)
|
||||||
Parameters :
Returns :
void
|
import {
HttpClient,
HttpErrorResponse,
HttpParams
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { forkJoin, Observable, of, Subject } from 'rxjs';
import { catchError, first, map, switchMap, tap } from 'rxjs/operators';
import { GrowlBrokerService } from '../foehn-growl/growl-broker.service';
import { GrowlType } from '../foehn-growl/growl-types';
import { FormPostResponse } from '../form-post-response';
import { ActionStatut, GesdemStatutUtils } from '../gesdem/gesdem-statut-utils';
import { I18nForm } from '../i18n-form';
import { SdkDictionaryService } from '../sdk-dictionary/sdk-dictionary.service';
import { SessionInfo } from '../sdk-session-info/session-info.service';
import { ValidationHandlerService } from '../validation/validation-handler.service';
import { GesdemErrorHandlerService } from './gesdem-error-handler.service';
@Injectable({
providedIn: 'root'
})
export class GesdemHandlerService {
_prefix: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _updateFormDataSubject: Subject<any> = new Subject<any>();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _lastResponse: FormPostResponse<any>;
private _captchaToken: string;
constructor(
private http: HttpClient,
private growlService: GrowlBrokerService,
private validationHandlerService: ValidationHandlerService,
private route: ActivatedRoute,
private errorHandlerService: GesdemErrorHandlerService,
private sessionInfo: SessionInfo,
private dictionaryService: SdkDictionaryService
) {}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
get lastResponse(): FormPostResponse<any> {
return this._lastResponse;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
get updateFormDataSubject(): Observable<any> {
return this._updateFormDataSubject.asObservable();
}
get prefix(): string {
return this._prefix;
}
set prefix(prefix: string) {
this._prefix = prefix;
}
isPendingPayment(): boolean {
return GesdemStatutUtils.isPendingPayment(
ActionStatut[this._lastResponse?.meta?.statut as ActionStatut]
);
}
save(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
form: any,
canCreateDemande: boolean = false,
displaySuccessMessage: boolean = true
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Observable<any> {
let url = 'api/validated';
if (this.prefix) {
url = `api/${this.prefix}/validated`;
}
if (canCreateDemande) {
url = `${url}?updateOnly=false`;
}
return this.postToUrl(form, url, displaySuccessMessage);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
pdf(form: any): Observable<any> {
let url = 'api/pdf';
if (this.prefix) {
url = `api/${this.prefix}/pdf`;
}
return this.postToUrl(form, url, false);
}
setCaptchaToken(token: string): void {
this._captchaToken = token;
}
getCaptchaToken(): string {
return this._captchaToken;
}
/**
*
* @param reference de la demande
* @param reCaptchaByPassUUID
* @param forceRefreshIfNew allows to force the cleaning if reference is null
* @param byPassEmitResponse boolean used to emit or not gesdem last response (default is false)
* @returns Observable
*/
require(
reference?: string,
reCaptchaByPassUUID?: string,
forceRefreshIfNew?: boolean,
byPassEmitResponse?: boolean
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Observable<any> {
const isNewReference =
!!reference &&
!!this._lastResponse &&
!!this._lastResponse.meta &&
this._lastResponse.meta.reference !== reference;
const refreshOnNoReference = !!forceRefreshIfNew && !reference;
if (isNewReference || refreshOnNoReference) {
this._lastResponse = null;
}
if (reference && !this._lastResponse) {
return this.retrieve(reference, reCaptchaByPassUUID);
}
if (this._lastResponse && !byPassEmitResponse) {
this.emitResponse(this._lastResponse);
}
return of(true);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
transmit(): Observable<any> {
let url = 'api/transmission';
if (this.prefix) {
url = `api/${this.prefix}/transmission`;
}
if (this._lastResponse) {
url += `?reference=${this._lastResponse.meta.reference}`;
}
return this.http.post(url, null).pipe(
tap(this.emitResponse.bind(this)),
map(() => true),
catchError((response: unknown) =>
this.handleGesdemError(null, null, response)
)
);
}
retrieve(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reference: any,
reCaptchaByPassUUID: string,
shouldDispatchErrors: boolean = true
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Observable<any> {
let url = 'api';
if (this.prefix) {
url = `api/${this.prefix}`;
}
url = `${url}/validated/${reference}`;
return this.http.get(url).pipe(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tap((resp: FormPostResponse<any>) =>
this.emitResponse(resp, shouldDispatchErrors)
),
map(() => true),
catchError((response: unknown) =>
this.handleGesdemError(reference, reCaptchaByPassUUID, response)
)
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
retrieveActionStatut(reference: any): Observable<ActionStatut> {
let url = 'api';
if (this.prefix) {
url = `api/${this.prefix}`;
}
url = `${url}/${reference}/action-statut`;
return this.http.get(url).pipe(
map(status => status as ActionStatut),
catchError((response: unknown) =>
this.handleGesdemError(reference, null, response)
)
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
validate(form: any): Observable<any> {
let url = 'api/validated-only';
if (this.prefix) {
url = `api/${this.prefix}/validated-only`;
}
return this.postToUrl(form, url, false);
}
private postToUrl(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
form: any,
url: string,
displaySuccessMessage: boolean
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Observable<any> {
const reCaptchaByPassUUID =
this.route.snapshot.queryParams.reCaptchaByPassUUID;
let params = new HttpParams();
if (!!reCaptchaByPassUUID) {
params = params.set('reCaptchaByPassUUID', reCaptchaByPassUUID);
}
if (!!this._lastResponse?.meta?.reference) {
params = params.set('reference', this._lastResponse.meta.reference);
}
if (this._captchaToken) {
params = params.set('reCaptchaToken', this._captchaToken);
}
return this.http.post(url, form, { params }).pipe(
tap(this.emitResponse.bind(this)),
catchError((response: unknown) =>
this.handleGesdemError(null, reCaptchaByPassUUID, response)
),
switchMap((resp: FormPostResponse<I18nForm>) =>
forkJoin([
this.sessionInfo.data.pipe(
first(),
map(data => !!data)
),
of(resp)
])
),
tap(([isConnected, resp]) => {
// If the user is connected, display a message to make clear that
// nothing is lost!
if (
isConnected &&
displaySuccessMessage &&
!!resp?.meta?.reference?.length
) {
this.growlService.addWithType(
GrowlType.SUCCESS,
this.dictionaryService.getKeySync(
'gesdem-handler.save-confirmation.label'
)
);
}
}),
map(() => true)
);
}
private emitResponse(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response: FormPostResponse<any>,
shouldDispatchErrors: boolean = true
): void {
if (!response.meta.critical) {
this._lastResponse = response;
this._updateFormDataSubject.next(response.form);
} else {
this._lastResponse = new FormPostResponse();
this._lastResponse.meta.critical = true;
}
this.validationHandlerService.updateErrors(
shouldDispatchErrors ? response.errors : []
);
}
private handleGesdemError(
requestedReference: string,
reCaptchaByPassUUID: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response: HttpErrorResponse | any
): Observable<never> {
const reference = this._lastResponse
? this._lastResponse.meta.reference
: requestedReference;
return this.errorHandlerService.handleGesdemError(
this.prefix,
reference,
reCaptchaByPassUUID,
response
);
}
}