@@ -181,80 +181,7 @@ public async Task DeleteManyAsync(IEnumerable<Guid> organizationUserIds)
181181
182182 try
183183 {
184- await dbContext . UserBumpAccountRevisionDateByOrganizationUserIdsAsync ( targetOrganizationUserIds ) ;
185-
186- var organizationUsersToDelete = await dbContext . OrganizationUsers
187- . Where ( ou => targetOrganizationUserIds . Contains ( ou . Id ) )
188- . Include ( ou => ou . User )
189- . ToListAsync ( ) ;
190-
191- var collectionUsers = await dbContext . CollectionUsers
192- . Where ( cu => targetOrganizationUserIds . Contains ( cu . OrganizationUserId ) )
193- . ToListAsync ( ) ;
194-
195- var collectionIds = collectionUsers . Select ( cu => cu . CollectionId ) . Distinct ( ) . ToList ( ) ;
196-
197- var collections = await dbContext . Collections
198- . Where ( c => collectionIds . Contains ( c . Id ) )
199- . ToListAsync ( ) ;
200-
201- var collectionsToUpdate = collections
202- . Where ( c => c . Type == CollectionType . DefaultUserCollection )
203- . ToList ( ) ;
204-
205- var collectionUserLookup = collectionUsers . ToLookup ( cu => cu . CollectionId ) ;
206-
207- foreach ( var collection in collectionsToUpdate )
208- {
209- var collectionUser = collectionUserLookup [ collection . Id ] . FirstOrDefault ( ) ;
210- if ( collectionUser != null )
211- {
212- var orgUser = organizationUsersToDelete . FirstOrDefault ( ou => ou . Id == collectionUser . OrganizationUserId ) ;
213-
214- if ( orgUser ? . User != null )
215- {
216- if ( string . IsNullOrEmpty ( collection . DefaultUserCollectionEmail ) )
217- {
218- var emailToUse = ! string . IsNullOrEmpty ( orgUser . Email )
219- ? orgUser . Email
220- : orgUser . User . Email ;
221-
222- if ( ! string . IsNullOrEmpty ( emailToUse ) )
223- {
224- collection . DefaultUserCollectionEmail = emailToUse ;
225- }
226- }
227- collection . Type = CollectionType . SharedCollection ;
228- }
229- }
230- }
231-
232- await dbContext . CollectionUsers
233- . Where ( cu => targetOrganizationUserIds . Contains ( cu . OrganizationUserId ) )
234- . ExecuteDeleteAsync ( ) ;
235-
236- await dbContext . GroupUsers
237- . Where ( gu => targetOrganizationUserIds . Contains ( gu . OrganizationUserId ) )
238- . ExecuteDeleteAsync ( ) ;
239-
240- await dbContext . UserProjectAccessPolicy
241- . Where ( ap => targetOrganizationUserIds . Contains ( ap . OrganizationUserId ! . Value ) )
242- . ExecuteDeleteAsync ( ) ;
243-
244- await dbContext . UserServiceAccountAccessPolicy
245- . Where ( ap => targetOrganizationUserIds . Contains ( ap . OrganizationUserId ! . Value ) )
246- . ExecuteDeleteAsync ( ) ;
247-
248- await dbContext . UserSecretAccessPolicy
249- . Where ( ap => targetOrganizationUserIds . Contains ( ap . OrganizationUserId ! . Value ) )
250- . ExecuteDeleteAsync ( ) ;
251-
252- await dbContext . OrganizationSponsorships
253- . Where ( os => targetOrganizationUserIds . Contains ( os . SponsoringOrganizationUserId ) )
254- . ExecuteDeleteAsync ( ) ;
255-
256- await dbContext . OrganizationUsers
257- . Where ( ou => targetOrganizationUserIds . Contains ( ou . Id ) ) . ExecuteDeleteAsync ( ) ;
184+ await DeleteManyOrganizationUsersAndRelatedDataAsync ( dbContext , targetOrganizationUserIds ) ;
258185
259186 await dbContext . SaveChangesAsync ( ) ;
260187 await transaction . CommitAsync ( ) ;
@@ -1123,34 +1050,88 @@ public DatabaseTransactionAction RemoveForPublicKeyPairRegeneration(
11231050 using var scope = ServiceScopeFactory . CreateScope ( ) ;
11241051 var dbContext = GetTransactionalDatabaseContext ( scope , connection , transaction ) ;
11251052
1126- await dbContext . CollectionUsers
1127- . Where ( cu => ids . Contains ( cu . OrganizationUserId ) )
1128- . ExecuteDeleteAsync ( ) ;
1053+ await DeleteManyOrganizationUsersAndRelatedDataAsync ( dbContext , ids ) ;
1054+ } ;
1055+ }
1056+
1057+ private static async Task DeleteManyOrganizationUsersAndRelatedDataAsync (
1058+ DatabaseContext dbContext , List < Guid > organizationUserIds )
1059+ {
1060+ await dbContext . UserBumpAccountRevisionDateByOrganizationUserIdsAsync ( organizationUserIds ) ;
11291061
1130- await dbContext . GroupUsers
1131- . Where ( gu => ids . Contains ( gu . OrganizationUserId ) )
1132- . ExecuteDeleteAsync ( ) ;
1062+ var organizationUsersToDelete = await dbContext . OrganizationUsers
1063+ . Where ( ou => organizationUserIds . Contains ( ou . Id ) )
1064+ . Include ( ou => ou . User )
1065+ . ToListAsync ( ) ;
11331066
1134- await dbContext . UserProjectAccessPolicy
1135- . Where ( ap => ids . Contains ( ap . OrganizationUserId ! . Value ) )
1136- . ExecuteDeleteAsync ( ) ;
1067+ var collectionUsers = await dbContext . CollectionUsers
1068+ . Where ( cu => organizationUserIds . Contains ( cu . OrganizationUserId ) )
1069+ . ToListAsync ( ) ;
11371070
1138- await dbContext . UserServiceAccountAccessPolicy
1139- . Where ( ap => ids . Contains ( ap . OrganizationUserId ! . Value ) )
1140- . ExecuteDeleteAsync ( ) ;
1071+ var collectionIds = collectionUsers . Select ( cu => cu . CollectionId ) . Distinct ( ) . ToList ( ) ;
11411072
1142- await dbContext . UserSecretAccessPolicy
1143- . Where ( ap => ids . Contains ( ap . OrganizationUserId ! . Value ) )
1144- . ExecuteDeleteAsync ( ) ;
1073+ var collections = await dbContext . Collections
1074+ . Where ( c => collectionIds . Contains ( c . Id ) )
1075+ . ToListAsync ( ) ;
11451076
1146- await dbContext . OrganizationSponsorships
1147- . Where ( os => ids . Contains ( os . SponsoringOrganizationUserId ) )
1148- . ExecuteDeleteAsync ( ) ;
1077+ var collectionsToUpdate = collections
1078+ . Where ( c => c . Type == CollectionType . DefaultUserCollection )
1079+ . ToList ( ) ;
11491080
1150- await dbContext . OrganizationUsers
1151- . Where ( ou => ids . Contains ( ou . Id ) )
1152- . ExecuteDeleteAsync ( ) ;
1153- } ;
1081+ var collectionUserLookup = collectionUsers . ToLookup ( cu => cu . CollectionId ) ;
1082+
1083+ foreach ( var collection in collectionsToUpdate )
1084+ {
1085+ var collectionUser = collectionUserLookup [ collection . Id ] . FirstOrDefault ( ) ;
1086+ if ( collectionUser != null )
1087+ {
1088+ var orgUser = organizationUsersToDelete . FirstOrDefault ( ou => ou . Id == collectionUser . OrganizationUserId ) ;
1089+
1090+ if ( orgUser ? . User != null )
1091+ {
1092+ if ( string . IsNullOrEmpty ( collection . DefaultUserCollectionEmail ) )
1093+ {
1094+ var emailToUse = ! string . IsNullOrEmpty ( orgUser . Email )
1095+ ? orgUser . Email
1096+ : orgUser . User . Email ;
1097+
1098+ if ( ! string . IsNullOrEmpty ( emailToUse ) )
1099+ {
1100+ collection . DefaultUserCollectionEmail = emailToUse ;
1101+ }
1102+ }
1103+ collection . Type = CollectionType . SharedCollection ;
1104+ }
1105+ }
1106+ }
1107+
1108+ await dbContext . CollectionUsers
1109+ . Where ( cu => organizationUserIds . Contains ( cu . OrganizationUserId ) )
1110+ . ExecuteDeleteAsync ( ) ;
1111+
1112+ await dbContext . GroupUsers
1113+ . Where ( gu => organizationUserIds . Contains ( gu . OrganizationUserId ) )
1114+ . ExecuteDeleteAsync ( ) ;
1115+
1116+ await dbContext . UserProjectAccessPolicy
1117+ . Where ( ap => organizationUserIds . Contains ( ap . OrganizationUserId ! . Value ) )
1118+ . ExecuteDeleteAsync ( ) ;
1119+
1120+ await dbContext . UserServiceAccountAccessPolicy
1121+ . Where ( ap => organizationUserIds . Contains ( ap . OrganizationUserId ! . Value ) )
1122+ . ExecuteDeleteAsync ( ) ;
1123+
1124+ await dbContext . UserSecretAccessPolicy
1125+ . Where ( ap => organizationUserIds . Contains ( ap . OrganizationUserId ! . Value ) )
1126+ . ExecuteDeleteAsync ( ) ;
1127+
1128+ await dbContext . OrganizationSponsorships
1129+ . Where ( os => organizationUserIds . Contains ( os . SponsoringOrganizationUserId ) )
1130+ . ExecuteDeleteAsync ( ) ;
1131+
1132+ await dbContext . OrganizationUsers
1133+ . Where ( ou => organizationUserIds . Contains ( ou . Id ) )
1134+ . ExecuteDeleteAsync ( ) ;
11541135 }
11551136
11561137#nullable disable
0 commit comments