File

feature-libs/organization/administration/core/services/org-unit.service.ts

Index

Methods

Constructor

constructor(store: Store, userIdService: UserIdService)
Parameters :
Name Type Optional
store Store<StateWithOrganization | StateWithProcess> No
userIdService UserIdService No

Methods

assignApprover
assignApprover(orgUnitId: string, orgCustomerId: string, roleId: string)
Parameters :
Name Type Optional
orgUnitId string No
orgCustomerId string No
roleId string No
Returns : void
assignRole
assignRole(orgCustomerId: string, roleId: string)
Parameters :
Name Type Optional
orgCustomerId string No
roleId string No
Returns : void
clearAssignedUsersList
clearAssignedUsersList(orgUnitId: string, roleId: string, params: SearchConfig)
Parameters :
Name Type Optional
orgUnitId string No
roleId string No
params SearchConfig No
Returns : void
create
create(unit: B2BUnit)
Parameters :
Name Type Optional
unit B2BUnit No
Returns : void
createAddress
createAddress(orgUnitId: string, address: Address)
Parameters :
Name Type Optional
orgUnitId string No
address Address No
Returns : void
deleteAddress
deleteAddress(orgUnitId: string, addressId: string)
Parameters :
Name Type Optional
orgUnitId string No
addressId string No
Returns : void
Protected findUnitChildrenInTree
findUnitChildrenInTree(orginitId, unit: B2BUnitNode)
Parameters :
Name Type Optional
orginitId No
unit B2BUnitNode No
Returns : B2BUnitNode[]
get
get(orgUnitId: string)
Parameters :
Name Type Optional
orgUnitId string No
Returns : Observable<B2BUnit>
getActiveUnitList
getActiveUnitList()
Returns : Observable<B2BUnitNode[]>
getAddress
getAddress(orgUnitId: string, addressId: string)
Parameters :
Name Type Optional
orgUnitId string No
addressId string No
Returns : Observable<Address>
getAddresses
getAddresses(orgUnitId: string)
Parameters :
Name Type Optional
orgUnitId string No
Returns : Observable<EntitiesModel<Address>>
Private getAddressesState
getAddressesState(orgUnitId: string)
Parameters :
Name Type Optional
orgUnitId string No
Returns : Observable<StateUtils.LoaderState<EntitiesModel<Address>>>
getAddressLoadingStatus
getAddressLoadingStatus(addressId: string)
Parameters :
Name Type Optional
addressId string No
Private getAddressState
getAddressState(addressId: string)
Parameters :
Name Type Optional
addressId string No
Returns : Observable<StateUtils.LoaderState<Address>>
getApprovalProcesses
getApprovalProcesses()
Returns : Observable<B2BApprovalProcess[]>
Private getApprovalProcessesList
getApprovalProcessesList()
Returns : Observable<StateUtils.LoaderState<B2BApprovalProcess[]>>
Private getAssignedUsers
getAssignedUsers(orgUnitId: string, roleId: string, params: SearchConfig)
Parameters :
Name Type Optional
orgUnitId string No
roleId string No
params SearchConfig No
Returns : Observable<StateUtils.LoaderState<EntitiesModel<B2BUser>>>
getChildUnits
getChildUnits(orgUnitId: string)
Parameters :
Name Type Optional
orgUnitId string No
Returns : Observable<EntitiesModel<B2BUnitNode>>
getCostCenters
getCostCenters(orgUnitId: string)
Parameters :
Name Type Optional
orgUnitId string No
Returns : Observable<EntitiesModel<CostCenter>>
getErrorState
getErrorState(orgCustomerId)
Parameters :
Name Optional
orgCustomerId No
Returns : Observable<boolean>
getList
getList()
Returns : Observable<B2BUnitNode[]>
getLoadingStatus
getLoadingStatus(orgUnitId: string)
Parameters :
Name Type Optional
orgUnitId string No
Private getOrgUnit
getOrgUnit(orgUnitId: string)
Parameters :
Name Type Optional
orgUnitId string No
Returns : Observable<StateUtils.LoaderState<B2BUnit>>
Private getOrgUnitsList
getOrgUnitsList()
Returns : Observable<StateUtils.LoaderState<B2BUnitNode[]>>
Private getOrgUnitState
getOrgUnitState(orgUnitId: string)
Parameters :
Name Type Optional
orgUnitId string No
Returns : Observable<StateUtils.LoaderState<B2BUnit>>
Private getOrgUnitValue
getOrgUnitValue(orgUnitId: string)
Parameters :
Name Type Optional
orgUnitId string No
Returns : Observable<B2BUnit>
getTree
getTree()
Returns : Observable<B2BUnitNode>
Private getTreeState
getTreeState()
Returns : Observable<StateUtils.LoaderState<B2BUnitNode>>
getUsers
getUsers(orgUnitId: string, roleId: string, params: SearchConfig)
Parameters :
Name Type Optional
orgUnitId string No
roleId string No
params SearchConfig No
Returns : Observable<EntitiesModel<B2BUser>>
load
load(orgUnitId: string)
Parameters :
Name Type Optional
orgUnitId string No
Returns : void
loadAddresses
loadAddresses(orgUnitId: string)
Parameters :
Name Type Optional
orgUnitId string No
Returns : void
loadApprovalProcesses
loadApprovalProcesses()
Returns : void
loadList
loadList()
Returns : void
loadTree
loadTree()
Returns : void
loadUsers
loadUsers(orgUnitId: string, roleId: string, params: SearchConfig)
Parameters :
Name Type Optional
orgUnitId string No
roleId string No
params SearchConfig No
Returns : void
Protected sortUnitList
sortUnitList(a: B2BUnitNode, b: B2BUnitNode)
Parameters :
Name Type Optional
a B2BUnitNode No
b B2BUnitNode No
Returns : 1 | -1 | 0
unassignApprover
unassignApprover(orgUnitId: string, orgCustomerId: string, roleId: string)
Parameters :
Name Type Optional
orgUnitId string No
orgCustomerId string No
roleId string No
Returns : void
unassignRole
unassignRole(orgCustomerId: string, roleId: string)
Parameters :
Name Type Optional
orgCustomerId string No
roleId string No
Returns : void
update
update(unitCode: string, unit: B2BUnit)
Parameters :
Name Type Optional
unitCode string No
unit B2BUnit No
Returns : void
updateAddress
updateAddress(orgUnitId: string, addressId: string, address: Address)
Parameters :
Name Type Optional
orgUnitId string No
addressId string No
address Address No
Returns : void
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import {
  Address,
  B2BApprovalProcess,
  B2BUnit,
  B2BUser,
  CostCenter,
  EntitiesModel,
  SearchConfig,
  StateUtils,
  StateWithProcess,
  UserIdService,
} from '@spartacus/core';
import { Observable, queueScheduler, using } from 'rxjs';
import { auditTime, filter, map, observeOn, tap } from 'rxjs/operators';
import { OrganizationItemStatus } from '../model/organization-item-status';
import { B2BUnitNode } from '../model/unit-node.model';
import { OrgUnitActions } from '../store/actions/index';
import { StateWithOrganization } from '../store/organization-state';
import {
  getApprovalProcesses,
  getAssignedUsers,
  getB2BAddress,
  getB2BAddresses,
  getOrgUnit,
  getOrgUnitList,
  getOrgUnitState,
  getOrgUnitTree,
  getOrgUnitValue,
} from '../store/selectors/org-unit.selector';
import { getItemStatus } from '../utils/get-item-status';

@Injectable({ providedIn: 'root' })
export class OrgUnitService {
  constructor(
    protected store: Store<StateWithOrganization | StateWithProcess<void>>,
    protected userIdService: UserIdService
  ) {}

  clearAssignedUsersList(
    orgUnitId: string,
    roleId: string,
    params: SearchConfig
  ): void {
    this.store.dispatch(
      new OrgUnitActions.ClearAssignedUsers({ orgUnitId, roleId, params })
    );
  }

  load(orgUnitId: string): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.LoadOrgUnit({ userId, orgUnitId })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  loadList(): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(new OrgUnitActions.LoadOrgUnitNodes({ userId })),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  loadTree(): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) => this.store.dispatch(new OrgUnitActions.LoadTree({ userId })),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  loadApprovalProcesses(): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.LoadApprovalProcesses({ userId })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  loadUsers(orgUnitId: string, roleId: string, params: SearchConfig): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.LoadAssignedUsers({
            userId,
            orgUnitId,
            roleId,
            params,
          })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  loadAddresses(orgUnitId: string): void {
    // TODO: replace it after turn on loadAddresses$
    // this.store.dispatch(
    //   new OrgUnitActions.LoadAddresses({ userId, orgUnitId })
    // );
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.LoadOrgUnit({ userId, orgUnitId })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  private getOrgUnit(
    orgUnitId: string
  ): Observable<StateUtils.LoaderState<B2BUnit>> {
    return this.store.select(getOrgUnit(orgUnitId));
  }

  private getOrgUnitValue(orgUnitId: string): Observable<B2BUnit> {
    return this.store.select(getOrgUnitValue(orgUnitId)).pipe(filter(Boolean));
  }

  private getTreeState(): Observable<StateUtils.LoaderState<B2BUnitNode>> {
    return this.store.select(getOrgUnitTree());
  }

  private getOrgUnitsList(): Observable<StateUtils.LoaderState<B2BUnitNode[]>> {
    return this.store.select(getOrgUnitList());
  }

  private getAddressesState(
    orgUnitId: string
  ): Observable<StateUtils.LoaderState<EntitiesModel<Address>>> {
    return this.store.select(getB2BAddresses(orgUnitId, null));
  }

  private getAddressState(
    addressId: string
  ): Observable<StateUtils.LoaderState<Address>> {
    return this.store.select(getB2BAddress(addressId));
  }

  private getAssignedUsers(
    orgUnitId: string,
    roleId: string,
    params: SearchConfig
  ): Observable<StateUtils.LoaderState<EntitiesModel<B2BUser>>> {
    return this.store.select(getAssignedUsers(orgUnitId, roleId, params));
  }

  private getApprovalProcessesList(): Observable<
    StateUtils.LoaderState<B2BApprovalProcess[]>
  > {
    return this.store.select(getApprovalProcesses());
  }

  get(orgUnitId: string): Observable<B2BUnit> {
    const loading$ = this.getOrgUnit(orgUnitId).pipe(
      auditTime(0),
      tap((state) => {
        if (!(state.loading || state.success || state.error)) {
          this.load(orgUnitId);
        }
      })
    );

    return using(
      () => loading$.subscribe(),
      () => this.getOrgUnitValue(orgUnitId)
    );
  }

  getCostCenters(orgUnitId: string): Observable<EntitiesModel<CostCenter>> {
    return this.get(orgUnitId).pipe(
      map((orgUnit) => ({
        values: orgUnit.costCenters ?? [],
      }))
    );
  }

  protected findUnitChildrenInTree(
    orginitId,
    unit: B2BUnitNode
  ): B2BUnitNode[] {
    return unit.id === orginitId
      ? unit.children
      : unit.children.flatMap((child) =>
          this.findUnitChildrenInTree(orginitId, child)
        );
  }

  getChildUnits(orgUnitId: string): Observable<EntitiesModel<B2BUnitNode>> {
    return this.getTree().pipe(
      map((tree) => ({
        values: this.findUnitChildrenInTree(orgUnitId, tree),
      }))
    );
  }

  getTree(): Observable<B2BUnitNode> {
    return this.getTreeState().pipe(
      observeOn(queueScheduler),
      tap((process: StateUtils.LoaderState<B2BUnitNode>) => {
        if (!(process.loading || process.success || process.error)) {
          this.loadTree();
        }
      }),
      filter(
        (process: StateUtils.LoaderState<B2BUnitNode>) =>
          process.success || process.error
      ),
      map((result) => result.value)
    );
  }

  getApprovalProcesses(): Observable<B2BApprovalProcess[]> {
    return this.getApprovalProcessesList().pipe(
      observeOn(queueScheduler),
      tap((process: StateUtils.LoaderState<B2BApprovalProcess[]>) => {
        if (!(process.loading || process.success || process.error)) {
          this.loadApprovalProcesses();
        }
      }),
      filter(
        (process: StateUtils.LoaderState<B2BApprovalProcess[]>) =>
          process.success || process.error
      ),
      map((result) => result.value)
    );
  }

  getList(): Observable<B2BUnitNode[]> {
    return this.getOrgUnitsList().pipe(
      observeOn(queueScheduler),
      tap((process: StateUtils.LoaderState<B2BUnitNode[]>) => {
        if (!(process.loading || process.success || process.error)) {
          this.loadList();
        }
      }),
      filter(
        (process: StateUtils.LoaderState<B2BUnitNode[]>) =>
          process.success || process.error
      ),
      map((result) => result.value)
    );
  }

  getActiveUnitList(): Observable<B2BUnitNode[]> {
    return this.getList().pipe(
      map((units) => units.filter((unit) => unit.active)),
      map((units) => units.sort(this.sortUnitList))
    );
  }

  protected sortUnitList(a: B2BUnitNode, b: B2BUnitNode) {
    return a.id.toLowerCase() < b.id.toLowerCase()
      ? -1
      : a.id.toLowerCase() > b.id.toLowerCase()
      ? 1
      : 0;
  }

  getUsers(
    orgUnitId: string,
    roleId: string,
    params: SearchConfig
  ): Observable<EntitiesModel<B2BUser>> {
    return this.getAssignedUsers(orgUnitId, roleId, params).pipe(
      observeOn(queueScheduler),
      tap((process: StateUtils.LoaderState<EntitiesModel<B2BUser>>) => {
        if (!(process.loading || process.success || process.error)) {
          this.loadUsers(orgUnitId, roleId, params);
        }
      }),
      filter(
        (process: StateUtils.LoaderState<EntitiesModel<B2BUser>>) =>
          process.success || process.error
      ),
      map((result) => result.value)
    );
  }

  getErrorState(orgCustomerId): Observable<boolean> {
    return this.getOrgUnitState(orgCustomerId).pipe(
      map((state) => state.error)
    );
  }

  create(unit: B2BUnit): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(new OrgUnitActions.CreateUnit({ userId, unit })),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  update(unitCode: string, unit: B2BUnit): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.UpdateUnit({ userId, unitCode, unit })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  getLoadingStatus(
    orgUnitId: string
  ): Observable<OrganizationItemStatus<B2BUnit>> {
    return getItemStatus(this.getOrgUnit(orgUnitId));
  }

  assignRole(orgCustomerId: string, roleId: string): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.AssignRole({
            userId,
            orgCustomerId,
            roleId,
          })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  unassignRole(orgCustomerId: string, roleId: string): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.UnassignRole({
            userId,
            orgCustomerId,
            roleId,
          })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  assignApprover(
    orgUnitId: string,
    orgCustomerId: string,
    roleId: string
  ): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.AssignApprover({
            orgUnitId,
            userId,
            orgCustomerId,
            roleId,
          })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  unassignApprover(
    orgUnitId: string,
    orgCustomerId: string,
    roleId: string
  ): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.UnassignApprover({
            orgUnitId,
            userId,
            orgCustomerId,
            roleId,
          })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  createAddress(orgUnitId: string, address: Address): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.CreateAddress({
            userId,
            orgUnitId,
            address,
          })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  getAddresses(orgUnitId: string): Observable<EntitiesModel<Address>> {
    return this.getAddressesState(orgUnitId).pipe(
      observeOn(queueScheduler),
      tap((state) => {
        if (!(state.loading || state.success || state.error)) {
          this.loadAddresses(orgUnitId);
        }
      }),
      filter((state) => state.success || state.error),
      map((state) => state.value)
    );
  }

  getAddress(orgUnitId: string, addressId: string): Observable<Address> {
    return this.getAddressState(addressId).pipe(
      observeOn(queueScheduler),
      tap((state) => {
        if (!(state.loading || state.success || state.error)) {
          this.loadAddresses(orgUnitId);
        }
      }),
      filter((state) => state.success || state.error),
      map((state) => state.value)
    );
  }

  updateAddress(orgUnitId: string, addressId: string, address: Address): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.UpdateAddress({
            userId,
            orgUnitId,
            addressId,
            address,
          })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  getAddressLoadingStatus(
    addressId: string
  ): Observable<OrganizationItemStatus<Address>> {
    return getItemStatus(this.getAddressState(addressId));
  }

  deleteAddress(orgUnitId: string, addressId: string): void {
    this.userIdService.takeUserId(true).subscribe(
      (userId) =>
        this.store.dispatch(
          new OrgUnitActions.DeleteAddress({
            userId,
            orgUnitId,
            addressId,
          })
        ),
      () => {
        // TODO: for future releases, refactor this part to thrown errors
      }
    );
  }

  private getOrgUnitState(
    orgUnitId: string
  ): Observable<StateUtils.LoaderState<B2BUnit>> {
    return this.store.select(getOrgUnitState(orgUnitId));
  }
}

result-matching ""

    No results matching ""