All files / src/app/core/store/customer/addresses addresses.effects.ts

100% Statements 35/35
66.66% Branches 12/18
100% Functions 19/19
100% Lines 33/33

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 10724x 24x 24x 24x   24x 24x 24x 24x   24x                               24x 14x   14x 14x     4x 4x                   14x 14x     7x 7x   7x 4x                             14x 14x     5x 5x   5x 2x                         14x 14x     7x 7x   7x 4x                    
import { Injectable } from '@angular/core';
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { concatMap, filter, map, mergeMap, switchMap } from 'rxjs/operators';
 
import { AddressService } from 'ish-core/services/address/address.service';
import { displaySuccessMessage } from 'ish-core/store/core/messages';
import { getLoggedInCustomer } from 'ish-core/store/customer/user';
import { mapErrorToAction, mapToPayloadProperty } from 'ish-core/utils/operators';
 
import {
  createCustomerAddress,
  createCustomerAddressFail,
  createCustomerAddressSuccess,
  deleteCustomerAddress,
  deleteCustomerAddressFail,
  deleteCustomerAddressSuccess,
  loadAddresses,
  loadAddressesFail,
  loadAddressesSuccess,
  updateCustomerAddress,
  updateCustomerAddressFail,
  updateCustomerAddressSuccess,
} from './addresses.actions';
 
@Injectable()
export class AddressesEffects {
  constructor(private actions$: Actions, private addressService: AddressService, private store: Store) {}
 
  loadAddresses$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadAddresses),
      switchMap(() =>
        this.addressService.getCustomerAddresses().pipe(
          map(addresses => loadAddressesSuccess({ addresses })),
          mapErrorToAction(loadAddressesFail)
        )
      )
    )
  );
 
  /**
   * Creates a new customer address.
   */
  createCustomerAddress$ = createEffect(() =>
    this.actions$.pipe(
      ofType(createCustomerAddress),
      mapToPayloadProperty('address'),
      concatLatestFrom(() => this.store.pipe(select(getLoggedInCustomer))),
      filter(([address, customer]) => !!address || !!customer),
      concatMap(([address, customer]) =>
        this.addressService.createCustomerAddress(customer.customerNo, address).pipe(
          mergeMap(newAddress => [
            createCustomerAddressSuccess({ address: newAddress }),
            displaySuccessMessage({
              message: 'account.addresses.new_address_created.message',
            }),
          ]),
          mapErrorToAction(createCustomerAddressFail)
        )
      )
    )
  );
 
  /**
   * Updates a customer address.
   */
  updateCustomerAddress$ = createEffect(() =>
    this.actions$.pipe(
      ofType(updateCustomerAddress),
      mapToPayloadProperty('address'),
      concatLatestFrom(() => this.store.pipe(select(getLoggedInCustomer))),
      filter(([address, customer]) => !!address || !!customer),
      mergeMap(([address]) =>
        this.addressService.updateCustomerAddress('-', address).pipe(
          mergeMap(address => [
            updateCustomerAddressSuccess({ address }),
            displaySuccessMessage({ message: 'account.addresses.address_updated.message' }),
          ]),
          mapErrorToAction(updateCustomerAddressFail)
        )
      )
    )
  );
 
  /**
   * Deletes a customer address.
   */
  deleteCustomerAddress$ = createEffect(() =>
    this.actions$.pipe(
      ofType(deleteCustomerAddress),
      mapToPayloadProperty('addressId'),
      concatLatestFrom(() => this.store.pipe(select(getLoggedInCustomer))),
      filter(([addressId, customer]) => !!addressId || !!customer),
      mergeMap(([addressId, customer]) =>
        this.addressService.deleteCustomerAddress(customer.customerNo, addressId).pipe(
          mergeMap(id => [
            deleteCustomerAddressSuccess({ addressId: id }),
            displaySuccessMessage({ message: 'account.addresses.new_address_deleted.message' }),
          ]),
          mapErrorToAction(deleteCustomerAddressFail)
        )
      )
    )
  );
}