File

projects/prestations-ng/src/sdk-appinfo/application-info.service.ts

Index

Properties
Methods
Accessors

Constructor

constructor(http: HttpClient, foehnPageService: FoehnPageService)
Parameters :
Name Type Optional
http HttpClient No
foehnPageService FoehnPageService No

Methods

Private getPortailBaseUrl
getPortailBaseUrl(appInfo: ApplicationInfo)
Parameters :
Name Type Optional
appInfo ApplicationInfo No
Returns : string
getSafeCyberLoginUrl
getSafeCyberLoginUrl()
Returns : Observable<string>
getSafeEspacesSecuriseUrl
getSafeEspacesSecuriseUrl()
Returns : Observable<string>
getSafeSupportFormTitle
getSafeSupportFormTitle()
Returns : Observable<string>
getSafeSupportFormUrl
getSafeSupportFormUrl(reference?: string, errors?: FormError[])
Parameters :
Name Type Optional
reference string Yes
errors FormError[] Yes
Returns : Observable<string>
Private setApplicationInfo
setApplicationInfo(applicationInfo: ApplicationInfo)
Parameters :
Name Type Optional
applicationInfo ApplicationInfo No
Returns : void

Properties

Private applicationInfo
Type : ApplicationInfo
Default value : new ApplicationInfo()
Private currentEtapeInfo_
Type : Observable<EtapeInfo>
Private firstEtapeInfo_
Type : Observable<EtapeInfo>
Private pulling_
Type : Observable<ApplicationInfo>

Accessors

data
getdata()
firstEtapeInfo
getfirstEtapeInfo()
currentEtapeInfo
getcurrentEtapeInfo()
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { combineLatest, Observable } from 'rxjs';
import { filter, map, shareReplay, tap } from 'rxjs/operators';

import { FoehnPageService } from '../foehn-page/foehn-page.service';
import { FormError } from '../form-error';
import { ApplicationInfo, EtapeInfo } from './application-info';

export const APP_INFO_API_URL = 'api/applicationInfo';
export const PORTAIL_BASE_URL_INT = 'https://int-prestations.vd.ch/';
export const PORTAIL_BASE_URL_VAL = 'https://val-prestations.vd.ch/';
export const PORTAIL_BASE_URL_PROD = 'https://prestations.vd.ch/';
export const FORM_SUPPORT_CYBER_TITLE_FALLBACK =
    "Demander de l'aide pour l'utilisation du portail sécurisé";

@Injectable({
    providedIn: 'root'
})
export class ApplicationInfoService {
    private applicationInfo: ApplicationInfo = new ApplicationInfo();
    private pulling_: Observable<ApplicationInfo>;
    private firstEtapeInfo_: Observable<EtapeInfo>;
    private currentEtapeInfo_: Observable<EtapeInfo>;

    constructor(
        private http: HttpClient,
        private foehnPageService: FoehnPageService
    ) {
        this.pulling_ = this.http
            .get<ApplicationInfo>(APP_INFO_API_URL)
            .pipe(shareReplay(1));

        // In most prestation, we're interested in the first etape info.
        this.firstEtapeInfo_ = this.pulling_.pipe(
            filter(appInfo => !!appInfo && !!appInfo.etapeInfos),
            map(({ etapeInfos }) => {
                // Checks whether the server returns a first key, otherwise fails the
                // observable to ensure that client applications don't try to use this value
                // and are able to implement fallback strategies.
                const firstKey = Object.keys(etapeInfos)[0];

                if (!firstKey) {
                    throw new Error(
                        'There must be at least one key in the etapeInfos'
                    );
                }

                return etapeInfos[firstKey];
            })
        );

        this.currentEtapeInfo_ = combineLatest([
            this.foehnPageService.onEtapeIdChange(),
            this.pulling_
        ]).pipe(
            map(
                ([etapeId, applicationInfo]) =>
                    applicationInfo.etapeInfos[etapeId]
            ),
            shareReplay(1)
        );

        this.pulling_.subscribe((ai: ApplicationInfo) =>
            this.setApplicationInfo(ai)
        );
    }

    get data(): Observable<ApplicationInfo> {
        return this.pulling_.pipe(
            tap(appInfo => {
                if (!!appInfo.configuration.portail.baseVdChUrl?.length) {
                    // already set by the backend
                    return;
                }
                switch (appInfo.environment?.toUpperCase()) {
                    case 'IN':
                        appInfo.configuration.portail.baseVdChUrl =
                            'https://i2.vd.ch';
                        break;
                    case 'VA':
                        appInfo.configuration.portail.baseVdChUrl =
                            'https://valid.vd.ch';
                        break;
                    default:
                        appInfo.configuration.portail.baseVdChUrl =
                            'https://www.vd.ch';
                }
            })
        );
    }

    get firstEtapeInfo(): Observable<EtapeInfo> {
        return this.firstEtapeInfo_;
    }

    get currentEtapeInfo(): Observable<EtapeInfo> {
        return this.currentEtapeInfo_;
    }

    getSafeCyberLoginUrl(): Observable<string> {
        return this.data.pipe(
            map(
                appInfo =>
                    appInfo.configuration?.portail?.cyberLoginUrl ||
                    `${this.getPortailBaseUrl(appInfo)}100018/`
            )
        );
    }

    getSafeSupportFormUrl(
        reference?: string,
        errors?: FormError[]
    ): Observable<string> {
        return this.data.pipe(
            map(
                appInfo =>
                    appInfo.configuration?.portail?.supportFormUrl ||
                    `${this.getPortailBaseUrl(appInfo)}pub/101098/`
            ),
            map(link => {
                if (!!reference?.length) {
                    link += `?concernedReference=${reference}`;
                }
                if (!!errors?.length) {
                    link += !!reference?.length ? '&' : '?';
                    // Need following code to avoid browser Sanitizing error when using JSON.stringify
                    link += `errors=${errors
                        .map(error =>
                            Object.entries(error)
                                .map(value => `${value.join('=')}`)
                                .join(',')
                        )
                        .join(';')}`;
                }
                return link;
            })
        );
    }

    getSafeSupportFormTitle(): Observable<string> {
        return this.data.pipe(
            map(appInfo => {
                const title =
                    appInfo.configuration?.portail
                        ?.portailTitlesByPrestaCode?.[101098];
                return title && title.length
                    ? title
                    : FORM_SUPPORT_CYBER_TITLE_FALLBACK;
            })
        );
    }

    getSafeEspacesSecuriseUrl(): Observable<string> {
        return this.data.pipe(
            map(
                appInfo =>
                    appInfo.configuration?.portail?.espaceSecuriseUrl ||
                    `${this.getPortailBaseUrl(appInfo)}100002/`
            )
        );
    }

    private setApplicationInfo(applicationInfo: ApplicationInfo): void {
        this.applicationInfo = applicationInfo;
        if (this.applicationInfo) {
            switch (this.applicationInfo.environment) {
                case 'CO': {
                    this.applicationInfo.environmentLabel = 'développement';
                    break;
                }
                case 'IN': {
                    this.applicationInfo.environmentLabel = 'intégration';
                    break;
                }
                case 'VA': {
                    this.applicationInfo.environmentLabel = 'validation';
                    break;
                }
                default: {
                    // Nothing to do
                }
            }
        }
    }

    private getPortailBaseUrl(appInfo: ApplicationInfo): string {
        const baseUrl = appInfo.configuration?.portail?.baseUrl;
        if (!!baseUrl) {
            return baseUrl;
        }
        switch (appInfo.environment) {
            case 'CO':
            case 'IN':
                return PORTAIL_BASE_URL_INT;
            case 'VA':
                return PORTAIL_BASE_URL_VAL;
            default:
                return PORTAIL_BASE_URL_PROD;
        }
    }
}

results matching ""

    No results matching ""