All files / src/app/core/store/customer/user user-newsletter.effects.ts

100% Statements 23/23
81.25% Branches 13/16
100% Functions 10/10
100% Lines 21/21

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 60 61 62 63 64 6524x 24x 24x 24x   24x 24x   24x 24x     24x 9x           9x 9x     2x         2x   2x                             9x 9x     4x 4x   4x       4x                
import { Injectable } from '@angular/core';
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { concatMap, filter, map, take } from 'rxjs/operators';
 
import { NewsletterService } from 'ish-core/services/newsletter/newsletter.service';
import { mapErrorToAction, mapToPayload, whenTruthy } from 'ish-core/utils/operators';
 
import { userNewsletterActions, userNewsletterApiActions } from './user.actions';
import { getLoggedInUser } from './user.selectors';
 
@Injectable()
export class UserNewsletterEffects {
  constructor(private actions$: Actions, private store: Store, private newsletterService: NewsletterService) {}
 
  /**
   * The effect has to wait for the getLoggedInUser-selector because it is used in a page-guard that is called before
   * the user is ready in the store
   */
  loadUserNewsletterSubscription$ = createEffect(() =>
    this.actions$.pipe(
      ofType(userNewsletterActions.loadUserNewsletterSubscription),
      concatMap(() =>
        this.store.pipe(
          select(getLoggedInUser),
          whenTruthy(),
          take(1),
          concatMap(user =>
            this.newsletterService.getSubscription(user.email).pipe(
              map(subscriptionStatus =>
                userNewsletterApiActions.loadUserNewsletterSubscriptionSuccess({ subscribed: subscriptionStatus })
              ),
              mapErrorToAction(userNewsletterApiActions.loadUserNewsletterSubscriptionFail)
            )
          )
        )
      )
    )
  );
 
  /**
   * The user-email has to be provided when setting the subscription during the registration process
   * because the user is not logged in yet.
   * If no user-email is passed in, the email of the logged-in user is used.
   */
  updateNewsletterSubscription$ = createEffect(() =>
    this.actions$.pipe(
      ofType(userNewsletterActions.updateUserNewsletterSubscription),
      mapToPayload(),
      concatLatestFrom(() => this.store.pipe(select(getLoggedInUser))),
      filter(([payload, user]) => !!payload.userEmail || !!user?.email),
      concatMap(([payload, user]) =>
        this.newsletterService
          .updateNewsletterSubscriptionStatus(payload.subscriptionStatus, payload.userEmail || user.email)
          .pipe(
            map(subscriptionStatus =>
              userNewsletterApiActions.updateUserNewsletterSubscriptionSuccess({ subscriptionStatus })
            ),
            mapErrorToAction(userNewsletterApiActions.updateUserNewsletterSubscriptionFail)
          )
      )
    )
  );
}