@@ -23,7 +23,7 @@ import {
23
23
sanitizeUserApprovalList ,
24
24
stringComparatorBySortOrder ,
25
25
} from '@Shared/Helpers'
26
- import { PolicyBlockInfo , RuntimeParamsAPIResponseType , RuntimePluginVariables } from '@Shared/types'
26
+ import { EnvListMinDTO , PolicyBlockInfo , RuntimeParamsAPIResponseType , RuntimePluginVariables } from '@Shared/types'
27
27
import { GitProviderType , ROUTES } from './Constants'
28
28
import { getUrlWithSearchParams , sortCallback } from './Helper'
29
29
import {
@@ -51,11 +51,14 @@ import {
51
51
GetAppsInfoForEnvProps ,
52
52
AppMeta ,
53
53
ApprovalRuntimeStateType ,
54
+ EnvironmentsGroupedByClustersType ,
55
+ AppsGroupedByProjectsType ,
54
56
} from './Types'
55
57
import { ApiResourceType , STAGE_MAP } from '../Pages'
56
58
import { RefVariableType , VariableTypeFormat } from './CIPipeline.Types'
57
59
import { get , post } from './API'
58
60
import { StatusType } from '@Shared/Components'
61
+ import { EnvironmentTypeEnum } from '@Shared/constants'
59
62
60
63
export const getTeamListMin = ( ) : Promise < TeamList > => {
61
64
// ignore active field
@@ -564,3 +567,67 @@ export const getAppsInfoForEnv = async ({ envId, appIds }: GetAppsInfoForEnvProp
564
567
} , [ ] ) ,
565
568
}
566
569
}
570
+
571
+
572
+ export const getAppOptionsGroupedByProjects = async ( ) : Promise < AppsGroupedByProjectsType > => {
573
+ const { result } = await get < AppsGroupedByProjectsType > ( ROUTES . APP_LIST_MIN )
574
+
575
+ if ( ! result ) {
576
+ return [ ]
577
+ }
578
+
579
+ return result
580
+ . map ( ( project ) => ( {
581
+ ...project ,
582
+ appList : project . appList . sort ( ( a , b ) => stringComparatorBySortOrder ( a . name , b . name ) ) ,
583
+ } ) )
584
+ . sort ( ( a , b ) => stringComparatorBySortOrder ( a . projectName , b . projectName ) )
585
+ }
586
+
587
+
588
+ export const getEnvironmentOptionsGroupedByClusters = async ( ) : Promise < EnvironmentsGroupedByClustersType > => {
589
+ const { result } = ( await get ( ROUTES . ENVIRONMENT_LIST_MIN ) ) as ResponseType < EnvListMinDTO [ ] >
590
+
591
+ if ( ! result ) {
592
+ return [ ]
593
+ }
594
+
595
+ const sortedEnvList = result
596
+ . map (
597
+ ( {
598
+ id,
599
+ environment_name : name ,
600
+ isVirtualEnvironment,
601
+ cluster_name : cluster ,
602
+ default : isDefault ,
603
+ namespace,
604
+ } ) => ( {
605
+ id,
606
+ name,
607
+ isVirtual : isVirtualEnvironment ?? false ,
608
+ cluster,
609
+ environmentType : isDefault ? EnvironmentTypeEnum . production : EnvironmentTypeEnum . nonProduction ,
610
+ namespace,
611
+ } ) ,
612
+ )
613
+ . sort ( ( a , b ) => stringComparatorBySortOrder ( a . name , b . name ) )
614
+
615
+ const envGroupedByCluster = Object . values (
616
+ sortedEnvList . reduce <
617
+ Record < EnvironmentsGroupedByClustersType [ number ] [ 'clusterName' ] , EnvironmentsGroupedByClustersType [ number ] >
618
+ > ( ( acc , env ) => {
619
+ if ( ! acc [ env . cluster ] ) {
620
+ acc [ env . cluster ] = {
621
+ clusterName : env . cluster ,
622
+ envList : [ ] ,
623
+ }
624
+ }
625
+
626
+ acc [ env . cluster ] . envList . push ( env )
627
+
628
+ return acc
629
+ } , { } ) ,
630
+ ) . sort ( ( a , b ) => stringComparatorBySortOrder ( a . clusterName , b . clusterName ) )
631
+
632
+ return envGroupedByCluster
633
+ }
0 commit comments