@@ -18,11 +18,11 @@ import { ResponseType, Teams } from '@Common/Types'
1818import { getTeamListMin } from '@Common/Common.service'
1919import { get } from '@Common/Api'
2020import { ClusterType } from '@Shared/Services'
21- import { EnvironmentType , EnvListMinDTO } from '@Shared/types'
21+ import { EnvListMinDTO } from '@Shared/types'
2222import { EnvironmentTypeEnum } from '@Shared/constants'
2323import { ROUTES } from '@Common/Constants'
2424import { stringComparatorBySortOrder } from '@Shared/Helpers'
25- import { AppsGroupedByProjectsType , ClusterDTO } from './types'
25+ import { AppsGroupedByProjectsType , ClusterDTO , EnvironmentsGroupedByClustersType } from './types'
2626
2727export const getAppOptionsGroupedByProjects = async ( ) : Promise < AppsGroupedByProjectsType > => {
2828 const { result } = ( await get ( ROUTES . APP_LIST_MIN ) ) as ResponseType < AppsGroupedByProjectsType >
@@ -70,30 +70,47 @@ export const getClusterOptions = async (): Promise<ClusterType[]> => {
7070 . sort ( ( a , b ) => stringComparatorBySortOrder ( a . name , b . name ) )
7171}
7272
73- export const getEnvironmentOptions = async ( ) : Promise < EnvironmentType [ ] > => {
73+ export const getEnvironmentOptionsGroupedByClusters = async ( ) : Promise < EnvironmentsGroupedByClustersType > => {
7474 const { result } = ( await get ( ROUTES . ENVIRONMENT_LIST_MIN ) ) as ResponseType < EnvListMinDTO [ ] >
7575
7676 if ( ! result ) {
7777 return [ ]
7878 }
7979
80- return result
81- . map (
82- ( {
83- id,
84- environment_name : name ,
85- isVirtualEnvironment,
86- cluster_name : cluster ,
87- default : isDefault ,
88- namespace,
89- } ) => ( {
90- id,
91- name,
92- isVirtual : isVirtualEnvironment ?? false ,
93- cluster,
94- environmentType : isDefault ? EnvironmentTypeEnum . production : EnvironmentTypeEnum . nonProduction ,
95- namespace,
96- } ) ,
97- )
98- . sort ( ( a , b ) => stringComparatorBySortOrder ( a . name , b . name ) )
80+ const sortedEnvList = result . map (
81+ ( {
82+ id,
83+ environment_name : name ,
84+ isVirtualEnvironment,
85+ cluster_name : cluster ,
86+ default : isDefault ,
87+ namespace,
88+ } ) => ( {
89+ id,
90+ name,
91+ isVirtual : isVirtualEnvironment ?? false ,
92+ cluster,
93+ environmentType : isDefault ? EnvironmentTypeEnum . production : EnvironmentTypeEnum . nonProduction ,
94+ namespace,
95+ } ) ,
96+ )
97+
98+ const envGroupedByCluster = Object . values (
99+ sortedEnvList . reduce <
100+ Record < EnvironmentsGroupedByClustersType [ number ] [ 'clusterName' ] , EnvironmentsGroupedByClustersType [ number ] >
101+ > ( ( acc , env ) => {
102+ if ( ! acc [ env . cluster ] ) {
103+ acc [ env . cluster ] = {
104+ clusterName : env . cluster ,
105+ envList : [ ] ,
106+ }
107+ }
108+
109+ acc [ env . cluster ] . envList . push ( env )
110+
111+ return acc
112+ } , { } ) ,
113+ ) . sort ( ( a , b ) => stringComparatorBySortOrder ( a . clusterName , b . clusterName ) )
114+
115+ return envGroupedByCluster
99116}
0 commit comments