6
6
} from 'ramda' ;
7
7
import * as yup from 'yup' ;
8
8
9
- import { getSelectedReposForOrg } from '@/api/integrations/selected' ;
10
9
import { syncReposForOrg } from '@/api/internal/[org_id]/sync_repos' ;
11
10
import {
12
11
getOnBoardingState ,
@@ -22,8 +21,12 @@ import {
22
21
} from '@/constants/integrations' ;
23
22
import { getTeamV2Mock } from '@/mocks/teams' ;
24
23
import { BaseTeam } from '@/types/api/teams' ;
25
- import { OnboardingStep , ReqRepoWithProvider } from '@/types/resources' ;
26
- import { db , getFirstRow } from '@/utils/db' ;
24
+ import {
25
+ OnboardingStep ,
26
+ RepoWithMultipleWorkflows ,
27
+ ReqRepoWithProvider
28
+ } from '@/types/resources' ;
29
+ import { db , dbRaw , getFirstRow } from '@/utils/db' ;
27
30
import groupBy from '@/utils/objectArray' ;
28
31
29
32
const repoSchema = yup . object ( ) . shape ( {
@@ -84,17 +87,16 @@ endpoint.handle.GET(getSchema, async (req, res) => {
84
87
. orderBy ( 'name' , 'asc' ) ;
85
88
86
89
const teams = await getQuery ;
87
- const reposWithWorkflows = await getSelectedReposForOrg (
90
+ const teamReposMap = await getTeamReposMap (
88
91
org_id ,
89
92
providers ?. length
90
93
? ( providers as Integration [ ] )
91
94
: [ Integration . GITHUB , Integration . GITLAB ]
92
- ) . then ( ( res ) => res . flat ( ) ) ;
95
+ ) ;
93
96
94
97
res . send ( {
95
98
teams : teams ,
96
- teamReposMap : ramdaGroupBy ( prop ( 'team_id' ) , reposWithWorkflows ) ,
97
- reposWithWorkflows
99
+ teamReposMap
98
100
} ) ;
99
101
} ) ;
100
102
@@ -134,7 +136,7 @@ endpoint.handle.POST(postSchema, async (req, res) => {
134
136
135
137
const providers = Array . from ( new Set ( orgReposList . map ( ( r ) => r . provider ) ) ) ;
136
138
await updateReposWorkflows ( org_id , orgReposList ) ;
137
- const reposWithWorkflows = await getSelectedReposForOrg (
139
+ const teamReposMap = await getTeamReposMap (
138
140
org_id ,
139
141
providers as Integration [ ]
140
142
) ;
@@ -143,7 +145,7 @@ endpoint.handle.POST(postSchema, async (req, res) => {
143
145
144
146
res . send ( {
145
147
team,
146
- teamReposMap : ramdaGroupBy ( prop ( 'team_id' ) , reposWithWorkflows )
148
+ teamReposMap
147
149
} ) ;
148
150
} ) ;
149
151
@@ -177,14 +179,14 @@ endpoint.handle.PATCH(patchSchema, async (req, res) => {
177
179
178
180
const providers = Array . from ( new Set ( orgReposList . map ( ( r ) => r . provider ) ) ) ;
179
181
180
- const reposWithWorkflows = await getSelectedReposForOrg (
182
+ const teamReposMap = await getTeamReposMap (
181
183
org_id ,
182
184
providers as Integration [ ]
183
185
) ;
184
186
syncReposForOrg ( ) ;
185
187
res . send ( {
186
188
team,
187
- teamReposMap : ramdaGroupBy ( prop ( 'team_id' ) , reposWithWorkflows )
189
+ teamReposMap
188
190
} ) ;
189
191
} ) ;
190
192
@@ -193,12 +195,49 @@ endpoint.handle.DELETE(deleteSchema, async (req, res) => {
193
195
return res . send ( getTeamV2Mock ) ;
194
196
}
195
197
196
- const data = await db ( 'Team' )
197
- . update ( 'is_deleted' , true )
198
- . where ( 'id' , req . payload . id )
199
- . orderBy ( 'name' , 'asc' )
200
- . returning ( '*' )
201
- . then ( getFirstRow ) ;
198
+ const data = await dbRaw . transaction ( async ( trx ) => {
199
+ const deletedTeamRow = await trx ( 'Team' )
200
+ . update ( {
201
+ is_deleted : true ,
202
+ updated_at : new Date ( )
203
+ } )
204
+ . where ( 'id' , req . payload . id )
205
+ . orderBy ( 'name' , 'asc' )
206
+ . returning ( '*' )
207
+ . then ( getFirstRow ) ;
208
+
209
+ // 1. Mark inactive and Get Repo Ids of this deleted team. Ex: [ '123', '456', '789' ]
210
+ const deletedTeamRepoIds : string [ ] = await trx ( 'TeamRepos' )
211
+ . update ( {
212
+ is_active : false ,
213
+ updated_at : new Date ( )
214
+ } )
215
+ . where ( 'team_id' , req . payload . id )
216
+ . andWhere ( 'is_active' , true )
217
+ . returning ( 'org_repo_id' )
218
+ . then ( ( result ) => result . map ( ( row ) => row . org_repo_id ) ) ;
219
+
220
+ // 2. Get Repo Ids which are still used across other teams. Ex: [ '456', '789' ]
221
+ const activeRepoIds : string [ ] = await trx ( 'TeamRepos' )
222
+ . where ( 'is_active' , true )
223
+ . whereIn ( 'org_repo_id' , deletedTeamRepoIds )
224
+ . distinct ( 'org_repo_id' )
225
+ . then ( ( result ) => result . map ( ( row ) => row . org_repo_id ) ) ;
226
+
227
+ // 3. Repo Ids which are nowhere used. Ex: [ '123' ]
228
+ const orgRepoIdsToBeInactivate = deletedTeamRepoIds . filter (
229
+ ( id ) => ! activeRepoIds . includes ( id )
230
+ ) ;
231
+
232
+ await trx ( 'OrgRepo' )
233
+ . update ( {
234
+ is_active : false ,
235
+ updated_at : new Date ( )
236
+ } )
237
+ . whereIn ( 'id' , orgRepoIdsToBeInactivate ) ;
238
+
239
+ return deletedTeamRow ;
240
+ } ) ;
202
241
203
242
res . send ( data ) ;
204
243
} ) ;
@@ -302,3 +341,48 @@ const updateReposWorkflows = async (
302
341
}
303
342
}
304
343
} ;
344
+
345
+ const getTeamReposMap = async ( org_id : ID , providers : Integration [ ] ) => {
346
+ const baseQuery = db ( Table . OrgRepo )
347
+ . where ( 'org_id' , org_id )
348
+ . andWhere ( 'OrgRepo.is_active' , true )
349
+ . whereIn ( 'OrgRepo.provider' , providers ) ;
350
+
351
+ const teamJoin = baseQuery
352
+ . leftJoin ( { tr : Table . TeamRepos } , 'OrgRepo.id' , 'tr.org_repo_id' )
353
+ . andWhere ( 'tr.is_active' , true ) ;
354
+
355
+ const workflowJoin = teamJoin . leftJoin (
356
+ { rw : Table . RepoWorkflow } ,
357
+ function ( ) {
358
+ this . on ( 'OrgRepo.id' , 'rw.org_repo_id' ) . andOn (
359
+ 'rw.is_active' ,
360
+ 'tr.is_active'
361
+ ) ;
362
+ }
363
+ ) ;
364
+
365
+ const dbRepos : RepoWithMultipleWorkflows [ ] = await workflowJoin
366
+ . select ( 'OrgRepo.*' )
367
+ . select (
368
+ dbRaw . raw (
369
+ "COALESCE(json_agg(rw.*) FILTER (WHERE rw.id IS NOT NULL), '[]') as repo_workflows"
370
+ )
371
+ )
372
+ . select ( 'tr.deployment_type' , 'tr.team_id' )
373
+ . groupBy ( 'OrgRepo.id' , 'tr.deployment_type' , 'tr.team_id' ) ;
374
+
375
+ const repoWithWorkflows = dbRepos . map ( ( repo ) => {
376
+ const updatedWorkflows = repo . repo_workflows . map ( ( workflow ) => ( {
377
+ name : workflow . name ,
378
+ value : workflow . provider_workflow_id
379
+ } ) ) ;
380
+
381
+ return {
382
+ ...repo ,
383
+ repo_workflows : updatedWorkflows
384
+ } ;
385
+ } ) ;
386
+
387
+ return ramdaGroupBy ( prop ( 'team_id' ) , repoWithWorkflows ) ;
388
+ } ;
0 commit comments