All files / src/app/core/services/newsletter newsletter.service.ts

91.66% Statements 22/24
83.33% Branches 10/12
90.9% Functions 10/11
95.23% Lines 20/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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 8125x 25x 25x   25x 25x           25x 5x   5x                   4x 3x   1x 1x                                   3x   3x 1x     2x                 1x             1x             1x   1x      
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable, catchError, map, of, switchMap, take, throwError } from 'rxjs';
 
import { ApiService } from 'ish-core/services/api/api.service';
import { getNewsletterSubscriptionStatus } from 'ish-core/store/customer/user';
 
/**
 * The Newsletter Service handles the newsletter related interaction with the 'subscriptions' REST API.
 */
@Injectable({ providedIn: 'root' })
export class NewsletterService {
  constructor(private apiService: ApiService, private store: Store) {}
 
  private newsletterSubscriptionStatus$ = this.store.pipe(select(getNewsletterSubscriptionStatus), take(1));
 
  /**
   * Gets the current newsletter subscription status of the user.
   *
   * @param userEmail    The user email.
   * @returns            The current newsletter subscription status.
   *                     Returns 'false' when a 404-error is thrown, which is the APIs response for "no subscription found".
   */
  getSubscription(userEmail: string): Observable<boolean> {
    return this.apiService.get(`subscriptions/${this.apiService.encodeResourceId(userEmail)}`).pipe(
      map((params: { active: boolean }) => params.active),
      catchError(error => {
        if (error.status === 404) {
          return of(false);
        }
        return throwError(() => error);
      })
    );
  }
 
  /**
   * Updates the newsletter subscription status of the user.
   * Doesn't make a REST call when newStatus and currentStatus are the same.
   *
   * @param newStatus        The new newsletter subscription status of the user.
   * @param userEmail        The user e-mail.
   * @returns                The new newsletter subscription status.
   *                         Returns the current status when newStatus and currentStatus are the same.
   */
  updateNewsletterSubscriptionStatus(newStatus: boolean, userEmail: string): Observable<boolean> {
    // only make a REST-call when the status has changed
    return this.newsletterSubscriptionStatus$.pipe(
      switchMap(currentStatus => {
        if (currentStatus === newStatus) {
          return of(currentStatus);
        }
 
        return newStatus ? this.subscribeToNewsletter(userEmail) : this.unsubscribeFromNewsletter(userEmail);
      })
    );
  }
 
  /**
   * always returns 'true'
   */
  private subscribeToNewsletter(userEmail: string): Observable<boolean> {
    const requestBody = {
      name: 'Newsletter',
      type: 'Subscription',
      active: true,
      recipient: userEmail,
    };
 
    return this.apiService.post(`subscriptions`, requestBody).pipe(map(() => true));
  }
 
  /**
   * always returns 'false'
   */
  private unsubscribeFromNewsletter(userEmail: string): Observable<boolean> {
    return this.apiService
      .delete(`subscriptions/${this.apiService.encodeResourceId(userEmail)}`)
      .pipe(map(() => false));
  }
}