Skip to content

Commit

Permalink
perf: omit cost center details REST calls for lists
Browse files Browse the repository at this point in the history
* use the link list attributes to map the data for cost center lists
  • Loading branch information
SGrueber committed Dec 4, 2024
1 parent 58f1aec commit 65e50a7
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -24,10 +25,9 @@ export class CostCentersService {
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))
)
this.apiService
.get<Link[]>(`customers/${this.apiService.encodeResourceId(customer.customerNo)}/costcenters`)
.pipe(map(CostCenterMapper.fromListData))
)
);
}
Expand Down
212 changes: 212 additions & 0 deletions src/app/core/models/cost-center/cost-center.mapper.spec.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
22 changes: 22 additions & 0 deletions src/app/core/models/cost-center/cost-center.mapper.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 65e50a7

Please sign in to comment.