-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(feo): add service tiles interceptor
- Loading branch information
1 parent
c228b19
commit cd2c515
Showing
4 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/config-utils/src/feo/service-tiles-interceptor.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { FrontendCRD, ServiceCategory } from './feo-types'; | ||
import serviceTilesInterceptor from './service-tiles-interceptor'; | ||
|
||
describe('Service tiles interceptor', () => { | ||
it('should replace service tiles with the ones from the frontendCRD', () => { | ||
const frontendName = 'frontendName'; | ||
const frontendCrd: FrontendCRD = { | ||
objects: [ | ||
{ | ||
metadata: { | ||
name: frontendName, | ||
}, | ||
spec: { | ||
module: { | ||
manifestLocation: 'location', | ||
}, | ||
serviceTiles: [ | ||
{ | ||
section: 'section-1', | ||
group: 'group-1', | ||
id: 'id-1', | ||
frontendRef: frontendName, | ||
}, | ||
{ | ||
section: 'section-1', | ||
group: 'group-1', | ||
id: 'id-2', | ||
frontendRef: frontendName, | ||
}, | ||
{ | ||
section: 'section-2', | ||
group: 'group-1', | ||
id: 'id-3', | ||
frontendRef: frontendName, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}; | ||
const remoteServiceTiles: ServiceCategory[] = [ | ||
{ | ||
id: 'section-1', | ||
groups: [ | ||
{ | ||
id: 'group-1', | ||
tiles: [ | ||
{ | ||
section: 'section-1', | ||
group: 'group-1', | ||
id: 'otherFrontend', | ||
frontendRef: 'otherFrontend', | ||
}, | ||
{ | ||
section: 'section-1', | ||
group: 'group-1', | ||
id: 'id-2', | ||
frontendRef: frontendName, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'section-2', | ||
groups: [ | ||
{ | ||
id: 'group-1', | ||
tiles: [ | ||
{ | ||
section: 'section-2', | ||
group: 'group-1', | ||
id: 'otherFrontend', | ||
frontendRef: 'otherFrontend', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]; | ||
const expectedServiceTiles: ServiceCategory[] = [ | ||
{ | ||
id: 'section-1', | ||
groups: [ | ||
{ | ||
id: 'group-1', | ||
tiles: [ | ||
remoteServiceTiles[0].groups[0].tiles[0], | ||
{ | ||
section: 'section-1', | ||
group: 'group-1', | ||
id: 'id-1', | ||
frontendRef: frontendName, | ||
}, | ||
{ | ||
section: 'section-1', | ||
group: 'group-1', | ||
id: 'id-2', | ||
frontendRef: frontendName, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'section-2', | ||
groups: [ | ||
{ | ||
id: 'group-1', | ||
tiles: [ | ||
remoteServiceTiles[1].groups[0].tiles[0], | ||
{ | ||
section: 'section-2', | ||
group: 'group-1', | ||
id: 'id-3', | ||
frontendRef: frontendName, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const result = serviceTilesInterceptor(remoteServiceTiles, frontendCrd); | ||
expect(result).toEqual(expectedServiceTiles); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
packages/config-utils/src/feo/service-tiles-interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { FrontendCRD, ServiceCategory, ServiceTile } from './feo-types'; | ||
|
||
function serviceTilesInterceptor(serviceCategories: ServiceCategory[], frontendCrd: FrontendCRD): ServiceCategory[] { | ||
const frontendRef = frontendCrd.objects[0].metadata.name; | ||
let result = [...serviceCategories]; | ||
|
||
const frontendCategories = | ||
frontendCrd.objects[0].spec.serviceTiles?.reduce<{ | ||
[section: string]: { [group: string]: ServiceTile[] }; | ||
}>((acc, tile) => { | ||
const section = tile.section; | ||
const group = tile.group; | ||
if (!acc[section]) { | ||
acc[section] = {}; | ||
} | ||
|
||
if (!acc[section][group]) { | ||
acc[section][group] = []; | ||
} | ||
|
||
acc[section][group].push({ ...tile }); | ||
return acc; | ||
}, {}) ?? {}; | ||
|
||
result = result.map((category) => { | ||
const newGroups = category.groups.map((group) => { | ||
const newTiles = group.tiles.filter((tile) => tile.frontendRef !== frontendRef); | ||
return { | ||
...group, | ||
tiles: [...newTiles, ...(frontendCategories[category.id]?.[group.id] ?? [])], | ||
}; | ||
}); | ||
return { | ||
...category, | ||
groups: newGroups, | ||
}; | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
export default serviceTilesInterceptor; |