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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 7x 7x 7x 7x 7x 7x 7x 7x 7x 9x 9x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Injectable } from '@angular/core'; import { Store, select } from '@ngrx/store'; import { Observable, forkJoin, throwError } from 'rxjs'; import { concatMap, map, switchMap, take } from 'rxjs/operators'; import { CostCenterData } from 'ish-core/models/cost-center/cost-center.interface'; import { CostCenterMapper } from 'ish-core/models/cost-center/cost-center.mapper'; import { CostCenter, CostCenterBase, CostCenterBuyer } from 'ish-core/models/cost-center/cost-center.model'; import { ApiService } from 'ish-core/services/api/api.service'; import { getLoggedInCustomer } from 'ish-core/store/customer/user'; import { whenTruthy } from 'ish-core/utils/operators'; @Injectable({ providedIn: 'root' }) export class CostCentersService { constructor(private apiService: ApiService, private store: Store) {} private currentCustomer$ = this.store.pipe(select(getLoggedInCustomer), whenTruthy(), take(1)); /** * Get all cost centers of a customer. The current user is expected to have permission APP_B2B_VIEW_COSTCENTER. * * @returns All cost centers of the customer. */ getCostCenters(): Observable<CostCenter[]> { return this.currentCustomer$.pipe( switchMap(customer => this.apiService.get(`customers/${this.apiService.encodeResourceId(customer.customerNo)}/costcenters`).pipe( this.apiService.resolveLinks<CostCenterData>(), map(ccData => ccData.map(CostCenterMapper.fromData)) ) ) ); } /** * Get a cost center of a customer. The current user is expected to have permission APP_B2B_VIEW_COSTCENTER. * * @param costCenterId The costCenterId of the cost center. * @returns The requested cost center of the customer. */ getCostCenter(costCenterId: string): Observable<CostCenter> { Iif (!costCenterId) { return throwError(() => new Error('getCostCenter() called without required costCenterId')); } return this.currentCustomer$.pipe( switchMap(customer => this.apiService .get<CostCenterData>( `customers/${this.apiService.encodeResourceId( customer.customerNo )}/costcenters/${this.apiService.encodeResourceId(costCenterId)}` ) .pipe(map(CostCenterMapper.fromData)) ) ); } /** * Creates a cost center of a customer. The current user is expected to have permission APP_B2B_MANAGE_COSTCENTER. * * @param costCenter The costCenter for the cost center creation. * @returns The new cost center. */ addCostCenter(costCenter: CostCenterBase): Observable<CostCenter> { Iif (!costCenter) { return throwError(() => new Error('addCostCenter() called without required costCenter')); } return this.currentCustomer$.pipe( switchMap(customer => this.apiService .post<CostCenterData>( `customers/${this.apiService.encodeResourceId(customer.customerNo)}/costcenters`, costCenter ) .pipe(concatMap(() => this.getCostCenter(costCenter.costCenterId))) ) ); } /** * Updates a cost center of a customer. The current user is expected to have permission APP_B2B_MANAGE_COSTCENTER. * * @param costCenter The costCenter for the cost center update. * @returns The updated cost center. */ updateCostCenter(costCenter: CostCenterBase): Observable<CostCenter> { Iif (!costCenter) { return throwError(() => new Error('updateCostCenter() called without required costCenter')); } return this.currentCustomer$.pipe( switchMap(customer => this.apiService .patch<CostCenterData>( `customers/${this.apiService.encodeResourceId( customer.customerNo )}/costcenters/${this.apiService.encodeResourceId(costCenter.id)}`, costCenter ) .pipe(map(CostCenterMapper.fromData)) ) ); } /** * Deletes a cost center of a customer. The current user is expected to have permission APP_B2B_MANAGE_COSTCENTER. * * @param id The id of the costcenter that is to be deleted. */ deleteCostCenter(id: string) { Iif (!id) { return throwError(() => new Error('deleteCostCenter() called without required id')); } return this.currentCustomer$.pipe( switchMap(customer => this.apiService.delete( `customers/${this.apiService.encodeResourceId( customer.customerNo )}/costcenters/${this.apiService.encodeResourceId(id)}` ) ) ); } /** * Adds buyers with their budgets to the cost center. The current user is expected to have permission APP_B2B_MANAGE_COSTCENTER. * * @param costCenterId The id of the costcenter. * @param buyers An array of buyers and budgets. * @returns The changed cost center. */ addCostCenterBuyers(costCenterId: string, buyers: CostCenterBuyer[]): Observable<CostCenter> { Iif (!costCenterId) { return throwError(() => new Error('addCostCenterBuyers() called without required costCenterId')); } Iif (!buyers?.length) { return throwError(() => new Error('addCostCenterBuyers() called without required buyers')); } return this.currentCustomer$.pipe( concatMap(customer => forkJoin( buyers.map(buyer => this.apiService.post( `customers/${this.apiService.encodeResourceId( customer.customerNo )}/costcenters/${this.apiService.encodeResourceId(costCenterId)}/buyers`, buyer ) ) ).pipe(concatMap(() => this.getCostCenter(costCenterId))) ) ); } /** * Updates a cost center buyer budget. The current user is expected to have permission APP_B2B_MANAGE_COSTCENTER. * * @param costCenterId The id of the costcenter. * @param buyer The changed buyer budget of the costcenter. * @returns The updated cost center. */ updateCostCenterBuyer(costCenterId: string, buyer: CostCenterBuyer): Observable<CostCenter> { Iif (!costCenterId) { return throwError(() => new Error('updateCostCenterBuyer() called without required costCenterId')); } Iif (!buyer) { return throwError(() => new Error('updateCostCenterBuyer() called without required buyer')); } return this.currentCustomer$.pipe( switchMap(customer => this.apiService .patch( `customers/${this.apiService.encodeResourceId( customer.customerNo )}/costcenters/${this.apiService.encodeResourceId(costCenterId)}/buyers/${this.apiService.encodeResourceId( buyer.login )}`, buyer ) .pipe(concatMap(() => this.getCostCenter(costCenterId))) ) ); } /** * Un-assigns a buyer from a cost center. The current user is expected to have permission APP_B2B_MANAGE_COSTCENTER. * * @param costCenterId The id of the costcenter. * @param login The login of the buyer. * @returns The changed cost center. */ deleteCostCenterBuyer(costCenterId: string, login: string): Observable<CostCenter> { Iif (!costCenterId) { return throwError(() => new Error('deleteCostCenterBuyer() called without required costCenterId')); } Iif (!login) { return throwError(() => new Error('deleteCostCenterBuyer() called without required login')); } return this.currentCustomer$.pipe( switchMap(customer => this.apiService .delete( `customers/${this.apiService.encodeResourceId( customer.customerNo )}/costcenters/${this.apiService.encodeResourceId(costCenterId)}/buyers/${this.apiService.encodeResourceId( login )}` ) .pipe(concatMap(() => this.getCostCenter(costCenterId))) ) ); } } |