@@ -18,7 +18,10 @@ import {
1818 SystemImportanceType ,
1919 SystemPatch ,
2020 SystemPost ,
21+ type CatalogueItem ,
2122} from './api.types' ;
23+ import { getCatalogueItem } from './catalogueItems' ;
24+ import { getItems } from './items' ;
2225
2326/** Utility for turning an importance into an MUI palette colour to display */
2427export const getSystemImportanceColour = (
@@ -87,23 +90,66 @@ export const useGetSystem = (
8790 return useQuery ( getSystemQuery ( id ) ) ;
8891} ;
8992
90- export interface SystemTree extends System {
93+ export interface SystemTree extends Partial < System > {
94+ catalogueItems : ( CatalogueItem & { itemsQuantity : number } ) [ ] ;
9195 subsystems ?: SystemTree [ ] ;
9296}
9397
94- const getSystemTree = async ( parent_id ? : string ) : Promise < SystemTree [ ] > => {
98+ const getSystemTree = async ( parent_id : string ) : Promise < SystemTree [ ] > => {
9599 // Fetch the systems at the current level
100+
101+ const rootSystem = await getSystem ( parent_id ) ;
102+
96103 const systems = await getSystems ( parent_id || 'null' ) ;
97104
98- // Fetch subsystems for each system recursively
99- const systemsWithTree = await Promise . all (
105+ // Fetch subsystems and catalogue items for each system recursively
106+ const systemsWithTree : SystemTree [ ] = await Promise . all (
100107 systems . map ( async ( system ) => {
101- const subsystems = await getSystemTree ( system . id ) ; // Fetch subsystems
102- return { ...system , subsystems } ; // Attach subsystems
108+ // Fetch subsystems recursively
109+ const subsystems = await getSystemTree ( system . id ) ;
110+
111+ // Fetch all items for the current system
112+ const items = await getItems ( system . id ) ;
113+
114+ // Group items into catalogue categories and fetch catalogue item details
115+ const catalogueItemIdSet = new Set < string > (
116+ items . map ( ( item ) => item . catalogue_item_id )
117+ ) ;
118+
119+ const catalogueItems : SystemTree [ 'catalogueItems' ] = await Promise . all (
120+ Array . from ( catalogueItemIdSet ) . map ( async ( id ) => {
121+ const catalogueItem = await getCatalogueItem ( id ) ;
122+ const categoryItems = items . filter (
123+ ( item ) => item . catalogue_item_id === id
124+ ) ;
125+ return { ...catalogueItem , itemsQuantity : categoryItems . length } ;
126+ } )
127+ ) ;
128+
129+ return { ...system , subsystems, catalogueItems } ;
130+ } )
131+ ) ;
132+
133+ // Handle the case when there are no systems (leaf nodes or empty levels)
134+
135+ const items = await getItems ( parent_id ) ;
136+
137+ // Group items into catalogue categories and fetch catalogue item details
138+ const catalogueItemIdSet = new Set < string > (
139+ items . map ( ( item ) => item . catalogue_item_id )
140+ ) ;
141+
142+ const catalogueItems : SystemTree [ 'catalogueItems' ] = await Promise . all (
143+ Array . from ( catalogueItemIdSet ) . map ( async ( id ) => {
144+ const catalogueItem = await getCatalogueItem ( id ) ;
145+ const categoryItems = items . filter (
146+ ( item ) => item . catalogue_item_id === id
147+ ) ;
148+ return { ...catalogueItem , itemsQuantity : categoryItems . length } ;
103149 } )
104150 ) ;
105151
106- return systemsWithTree ;
152+ return [ { ... rootSystem , catalogueItems , subsystems : systemsWithTree } ] ;
107153} ;
108154
109155export const useGetSystemsTree = (
0 commit comments