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

100% Statements 20/20
58.33% Branches 7/12
100% Functions 11/11
100% Lines 19/19

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 107 108 10924x   24x   24x 24x     24x           24x 5x                 2x     2x     2x                           1x         1x     1x                           1x         1x     1x                                       1x     1x           1x          
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { concatMap, first, map } from 'rxjs/operators';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { AddressMapper } from 'ish-core/models/address/address.mapper';
import { Address } from 'ish-core/models/address/address.model';
import { Link } from 'ish-core/models/link/link.model';
import { ApiService, unpackEnvelope } from 'ish-core/services/api/api.service';
 
/**
 * The Address Service handles the interaction with the REST API concerning addresses.
 */
@Injectable({ providedIn: 'root' })
export class AddressService {
  constructor(private apiService: ApiService, private appFacade: AppFacade) {}
 
  /**
   * Gets the addresses for the given customer id. Falls back to '-' as customer id to get the addresses for the current user.
   *
   * @param customerId  The customer id.
   * @returns           The customer's addresses.
   */
  getCustomerAddresses(customerId: string = '-'): Observable<Address[]> {
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService.get(`${restResource}/${this.apiService.encodeResourceId(customerId)}/addresses`).pipe(
          unpackEnvelope<Link>(),
          this.apiService.resolveLinks<Address>(),
          map(addressesData => addressesData.map(AddressMapper.fromData))
        )
      )
    );
  }
 
  /**
   * Creates an address for the given customer id. Falls back to '-' as customer id if no customer id is given
   *
   * @param customerId  The customer id.
   * @param address     The address which should be created
   * @returns           The new customer's address.
   */
  createCustomerAddress(customerId: string = '-', address: Address): Observable<Address> {
    const customerAddress = {
      ...address,
      mainDivision: address.mainDivisionCode,
    };
 
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService
          .post(`${restResource}/${this.apiService.encodeResourceId(customerId)}/addresses`, customerAddress)
          .pipe(this.apiService.resolveLink<Address>(), map(AddressMapper.fromData))
      )
    );
  }
 
  /**
   * Updates an address for the given customer id. Falls back to '-' as customer id if no customer id is given
   *
   * @param customerId  The customer id.
   * @param address     The address
   */
  updateCustomerAddress(customerId: string = '-', address: Address): Observable<Address> {
    const customerAddress = {
      ...address,
      mainDivision: address.mainDivisionCode,
    };
 
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService
          .put(
            `${restResource}/${this.apiService.encodeResourceId(
              customerId
            )}/addresses/${this.apiService.encodeResourceId(address.id)}`,
            customerAddress
          )
          .pipe(map(AddressMapper.fromData))
      )
    );
  }
 
  /**
   * Deletes an address for the given customer id. Falls back to '-' as customer id if no customer id is given
   *
   * @param customerId  The customer id.
   * @param address     The address id
   * @returns           The id of the deleted address.
   */
  deleteCustomerAddress(customerId: string = '-', addressId: string): Observable<string> {
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService
          .delete(
            `${restResource}/${this.apiService.encodeResourceId(
              customerId
            )}/addresses/${this.apiService.encodeResourceId(addressId)}`
          )
          .pipe(map(() => addressId))
      )
    );
  }
}