File

projects/prestations-ng/src/gesdem-action-recovery/gesdem-action-recovery.service.ts

Index

Properties
Methods
Accessors

Constructor

constructor(http: HttpClient, growlService: GrowlBrokerService, gesdemService: GesdemHandlerService, route: ActivatedRoute)
Parameters :
Name Type Optional
http HttpClient No
growlService GrowlBrokerService No
gesdemService GesdemHandlerService No
route ActivatedRoute No

Methods

checkRecoveryPassword
checkRecoveryPassword(reference: string, otp: string)
Parameters :
Name Type Optional
reference string No
otp string No
Returns : Observable<boolean>
getDefaultRecoveryValue
getDefaultRecoveryValue(reference: string)
Parameters :
Name Type Optional
reference string No
hasCaptchaToken
hasCaptchaToken()
Returns : boolean
registerForRecovery
registerForRecovery(reference: string, info: RepriseInfo)
Parameters :
Name Type Optional
reference string No
info RepriseInfo No
startRecovery
startRecovery(reference: string)
Parameters :
Name Type Optional
reference string No
Returns : Observable<string>

Properties

_prefix
Type : string

Accessors

prefix
setprefix(prefix: string)
Parameters :
Name Type Optional
prefix string No
Returns : void
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Observable, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';

import { GrowlBrokerService } from '../foehn-growl/growl-broker.service';
import { GrowlType } from '../foehn-growl/growl-types';
import { FormError } from '../form-error';
import { GesdemHandlerService } from '../gesdem/gesdem-handler.service';
import { RepriseInfo } from './reprise-info';

/**
 * @param value where space will be removed
 * @returns string
 */
const removeSpace = (value: string): string => {
    if (!value) {
        return null;
    }

    return value.replace(/\s/g, '');
};

@Injectable({
    providedIn: 'root'
})
export class GesdemActionRecoveryService {
    _prefix: string;

    constructor(
        private http: HttpClient,
        private growlService: GrowlBrokerService,
        private gesdemService: GesdemHandlerService,
        private route: ActivatedRoute
    ) {}

    set prefix(prefix: string) {
        this._prefix = prefix;
    }

    registerForRecovery(
        reference: string,
        info: RepriseInfo
    ): Observable<FormError[]> {
        let url = 'api';
        if (this._prefix) {
            url = `api/${this._prefix}`;
        }
        url += `/registerForRecovery/${reference}`;

        const normalizedRepriseInfo: RepriseInfo = {
            email: removeSpace(info.email),
            mobile: removeSpace(info.mobile),
            referenceInterne: info.referenceInterne
        };

        return this.http.post<FormError[]>(url, normalizedRepriseInfo).pipe(
            catchError((e: unknown) => {
                this.growlService.addWithType(
                    GrowlType.DANGER,
                    'Une erreur est survenue, veuillez réessayer plus tard.'
                );
                return throwError(() => e);
            })
        );
    }

    getDefaultRecoveryValue(reference: string): Observable<RepriseInfo> {
        let url = 'api';
        if (this._prefix) {
            url = `api/${this._prefix}`;
        }
        url += `/defaultRecoveryValue/${reference}`;

        return this.http
            .get<RepriseInfo>(url)
            .pipe(catchError((e: unknown) => throwError(() => e)));
    }

    startRecovery(reference: string): Observable<string> {
        let url = 'api';
        if (this._prefix) {
            url = `api/${this._prefix}`;
        }
        url += `/startRecovery/${reference}`;

        return this.http.put(url, null, {
            responseType: 'text'
        }) as Observable<string>;
    }

    checkRecoveryPassword(reference: string, otp: string): Observable<boolean> {
        let url = 'api';
        if (this._prefix) {
            url = `api/${this._prefix}`;
        }
        url += `/checkRecoveryPassword/${reference}/otp/${otp}/check`;

        let params = new HttpParams();
        if (!!this.route.snapshot.queryParams.reCaptchaByPassUUID) {
            params = params.set(
                'reCaptchaByPassUUID',
                this.route.snapshot.queryParams.reCaptchaByPassUUID
            );
        }
        if (this.gesdemService.getCaptchaToken()) {
            params = params.set(
                'reCaptchaToken',
                this.gesdemService.getCaptchaToken()
            );
        }

        return this.http.get(url, { responseType: 'text', params }).pipe(
            map(result => result === 'true'),
            catchError(() => {
                this.growlService.addWithType(
                    GrowlType.DANGER,
                    'Impossible de vérifier le mot de passe, veuillez réessayer plus tard.'
                );
                return throwError(() => true);
            })
        );
    }

    hasCaptchaToken(): boolean {
        return !!this.gesdemService.getCaptchaToken()?.length;
    }
}

results matching ""

    No results matching ""