From 65e50a72d1a2640cfeb653763e6c3e1ce8a49f74 Mon Sep 17 00:00:00 2001 From: Silke Date: Wed, 4 Dec 2024 12:33:10 +0100 Subject: [PATCH] perf: omit cost center details REST calls for lists * use the link list attributes to map the data for cost center lists --- .../cost-centers/cost-centers.service.spec.ts | 1 - .../cost-centers/cost-centers.service.ts | 8 +- .../cost-center/cost-center.mapper.spec.ts | 212 ++++++++++++++++++ .../models/cost-center/cost-center.mapper.ts | 22 ++ 4 files changed, 238 insertions(+), 5 deletions(-) diff --git a/projects/organization-management/src/app/services/cost-centers/cost-centers.service.spec.ts b/projects/organization-management/src/app/services/cost-centers/cost-centers.service.spec.ts index e5b4f74aba..620a44d27b 100644 --- a/projects/organization-management/src/app/services/cost-centers/cost-centers.service.spec.ts +++ b/projects/organization-management/src/app/services/cost-centers/cost-centers.service.spec.ts @@ -22,7 +22,6 @@ describe('Cost Centers Service', () => { when(apiService.post(anything(), anything())).thenReturn(of({})); when(apiService.patch(anything(), anything())).thenReturn(of({})); when(apiService.delete(anything())).thenReturn(of({})); - when(apiService.resolveLinks()).thenReturn(() => of([])); when(apiService.encodeResourceId(anything())).thenCall(id => id); TestBed.configureTestingModule({ diff --git a/projects/organization-management/src/app/services/cost-centers/cost-centers.service.ts b/projects/organization-management/src/app/services/cost-centers/cost-centers.service.ts index 5fbabf2eba..b1ba9f537b 100644 --- a/projects/organization-management/src/app/services/cost-centers/cost-centers.service.ts +++ b/projects/organization-management/src/app/services/cost-centers/cost-centers.service.ts @@ -6,6 +6,7 @@ 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 { Link } from 'ish-core/models/link/link.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'; @@ -24,10 +25,9 @@ export class CostCentersService { getCostCenters(): Observable { return this.currentCustomer$.pipe( switchMap(customer => - this.apiService.get(`customers/${this.apiService.encodeResourceId(customer.customerNo)}/costcenters`).pipe( - this.apiService.resolveLinks(), - map(ccData => ccData.map(CostCenterMapper.fromData)) - ) + this.apiService + .get(`customers/${this.apiService.encodeResourceId(customer.customerNo)}/costcenters`) + .pipe(map(CostCenterMapper.fromListData)) ) ); } diff --git a/src/app/core/models/cost-center/cost-center.mapper.spec.ts b/src/app/core/models/cost-center/cost-center.mapper.spec.ts index c4b111f83d..1bb9d50e7a 100644 --- a/src/app/core/models/cost-center/cost-center.mapper.spec.ts +++ b/src/app/core/models/cost-center/cost-center.mapper.spec.ts @@ -1,7 +1,219 @@ +import { Link } from 'ish-core/models/link/link.model'; + import { CostCenterData } from './cost-center.interface'; import { CostCenterMapper } from './cost-center.mapper'; describe('Cost Center Mapper', () => { + describe('fromListData', () => { + it(`should return costCenters when getting CostCenterLinks`, () => { + const costCenterListData = [ + { + attributes: [ + { + name: 'name', + value: 'Oil Corp Headquarter', + }, + { + name: 'costCenterId', + value: '100400', + }, + { + name: 'budgetPeriod', + value: 'monthly', + }, + { + name: 'active', + value: true, + }, + { + name: 'budget', + value: { + type: 'Money', + value: 20000.0, + currencyMnemonic: 'USD', + currency: 'USD', + }, + }, + { + name: 'pendingOrders', + value: 0, + }, + { + name: 'approvedOrders', + value: 0, + }, + { + name: 'costCenterOwner', + value: { + email: 'jlink@test.intershop.de', + firstName: 'Jack', + lastName: 'Link', + login: 'jlink@test.intershop.de', + }, + }, + { + name: 'spentBudget', + value: { + type: 'Money', + value: 0.0, + currencyMnemonic: 'USD', + currency: 'USD', + }, + }, + { + name: 'remainingBudget', + value: { + type: 'Money', + value: 20000.0, + currencyMnemonic: 'USD', + currency: 'USD', + }, + }, + ], + itemId: 'LmpA8AVnGzEAAAGRxuAADVoP', + }, + { + attributes: [ + { + name: 'name', + value: 'Oil Corp Subsidiary 1', + }, + { + name: 'costCenterId', + value: '100401', + }, + { + name: 'budgetPeriod', + value: 'monthly', + }, + { + name: 'active', + value: true, + }, + { + name: 'budget', + value: { + type: 'Money', + value: 5000.0, + currencyMnemonic: 'USD', + currency: 'USD', + }, + }, + { + name: 'pendingOrders', + value: 1, + }, + { + name: 'approvedOrders', + value: 4, + }, + { + name: 'costCenterOwner', + value: { + email: 'bboldner@test.intershop.de', + firstName: 'Bernhard', + lastName: 'Boldner', + login: 'bboldner@test.intershop.de', + }, + }, + { + name: 'spentBudget', + value: { + type: 'Money', + value: 156759.02, + currencyMnemonic: 'USD', + currency: 'USD', + }, + }, + { + name: 'remainingBudget', + value: { + type: 'Money', + value: -151759.02, + currencyMnemonic: 'USD', + currency: 'USD', + }, + }, + ], + itemId: 'XopA8AVnEKUAAAGR0OAADVoP', + }, + ] as Link[]; + const costCenters = CostCenterMapper.fromListData(costCenterListData); + + expect(costCenters).toBeTruthy(); + expect(costCenters).toMatchInlineSnapshot(` + [ + { + "active": true, + "approvedOrders": 0, + "budget": { + "currency": "USD", + "currencyMnemonic": "USD", + "type": "Money", + "value": 20000, + }, + "budgetPeriod": "monthly", + "costCenterId": "100400", + "costCenterOwner": { + "email": "jlink@test.intershop.de", + "firstName": "Jack", + "lastName": "Link", + "login": "jlink@test.intershop.de", + }, + "id": "LmpA8AVnGzEAAAGRxuAADVoP", + "name": "Oil Corp Headquarter", + "pendingOrders": 0, + "remainingBudget": { + "currency": "USD", + "currencyMnemonic": "USD", + "type": "Money", + "value": 20000, + }, + "spentBudget": { + "currency": "USD", + "currencyMnemonic": "USD", + "type": "Money", + "value": 0, + }, + }, + { + "active": true, + "approvedOrders": 4, + "budget": { + "currency": "USD", + "currencyMnemonic": "USD", + "type": "Money", + "value": 5000, + }, + "budgetPeriod": "monthly", + "costCenterId": "100401", + "costCenterOwner": { + "email": "bboldner@test.intershop.de", + "firstName": "Bernhard", + "lastName": "Boldner", + "login": "bboldner@test.intershop.de", + }, + "id": "XopA8AVnEKUAAAGR0OAADVoP", + "name": "Oil Corp Subsidiary 1", + "pendingOrders": 1, + "remainingBudget": { + "currency": "USD", + "currencyMnemonic": "USD", + "type": "Money", + "value": -151759.02, + }, + "spentBudget": { + "currency": "USD", + "currencyMnemonic": "USD", + "type": "Money", + "value": 156759.02, + }, + }, + ] + `); + }); + }); + describe('fromData', () => { it(`should return a CostCenter when getting CostCenterData`, () => { const costCenterData = { diff --git a/src/app/core/models/cost-center/cost-center.mapper.ts b/src/app/core/models/cost-center/cost-center.mapper.ts index 95c51fad6c..02efa12d36 100644 --- a/src/app/core/models/cost-center/cost-center.mapper.ts +++ b/src/app/core/models/cost-center/cost-center.mapper.ts @@ -1,9 +1,31 @@ import { AttributeHelper } from 'ish-core/models/attribute/attribute.helper'; +import { Link } from 'ish-core/models/link/link.model'; import { CostCenterData } from './cost-center.interface'; import { CostCenter } from './cost-center.model'; export class CostCenterMapper { + /* map cost center data from link list attributes, + some data are missing like orders or buyer assignments that have to be fetched with the details call */ + static fromListData(data: Link[]): CostCenter[] { + if (!data?.length) { + return []; + } + return data.map(cc => ({ + id: cc.itemId, + costCenterId: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'costCenterId'), + name: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'name'), + active: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'active'), + costCenterOwner: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'costCenterOwner'), + budget: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'budget'), + budgetPeriod: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'budgetPeriod'), + pendingOrders: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'pendingOrders'), + approvedOrders: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'approvedOrders'), + spentBudget: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'spentBudget'), + remainingBudget: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'remainingBudget'), + })); + } + static fromData(data: CostCenterData): CostCenter { if (data) { return {