All files / src/app/extensions/captcha/facades captcha.facade.ts

40% Statements 6/15
37.5% Branches 3/8
0% Functions 0/7
40% Lines 6/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 603x 3x   3x   3x 3x                     3x                                                                                    
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';
import { filter, map, switchMap } from 'rxjs/operators';
 
import { getServerConfigParameter } from 'ish-core/store/core/server-config';
import { whenTruthy } from 'ish-core/utils/operators';
 
export type CaptchaTopic =
  | 'contactUs'
  | 'emailShoppingCart'
  | 'forgotPassword'
  | 'redemptionOfGiftCardsAndCertificates'
  | 'register';
 
/* eslint-disable @typescript-eslint/member-ordering */
@Injectable({ providedIn: 'root' })
export class CaptchaFacade {
  constructor(private store: Store) {}
 
  captchaVersion$: Observable<2 | 3 | undefined> = this.store.pipe(
    select(
      getServerConfigParameter<{
        ReCaptchaV2ServiceDefinition: { runnable: boolean };
        ReCaptchaV3ServiceDefinition: { runnable: boolean };
      }>('services')
    ),
    whenTruthy(),
    map(services =>
      services.ReCaptchaV3ServiceDefinition?.runnable
        ? 3
        : services.ReCaptchaV2ServiceDefinition?.runnable
        ? 2
        : undefined
    )
  );
 
  captchaSiteKey$ = this.captchaVersion$.pipe(
    whenTruthy(),
    switchMap(version =>
      this.store.pipe(
        select(getServerConfigParameter<string>(`services.ReCaptchaV${version}ServiceDefinition.SiteKey`)),
        whenTruthy()
      )
    )
  );
 
  /**
   * @param key feature name according to the captcha ICM configuration, e.g. register, forgotPassword, contactUs
   */
  captchaActive$(key: CaptchaTopic): Observable<boolean> {
    return this.store.pipe(
      filter(() => !!key),
      switchMap(() => this.captchaVersion$),
      whenTruthy(),
      switchMap(() => this.store.pipe(select(getServerConfigParameter<boolean>(`captcha.${key}`))))
    );
  }
}