@@ -1049,26 +1049,40 @@ export const deleteOrganization: ResolverFn = async (
1049
1049
return 'success' ;
1050
1050
} ;
1051
1051
1052
- const checkBulkProjectGroupAssociation = async ( oid , pid , projectsToMove , groupsToMove , projectsInOtherOrgs , groupsInOtherOrgs , sqlClientPool , models ) => {
1053
- const groupProjectIds = [ ] ;
1052
+ // recursive check of a project and the groups of that project
1053
+ const checkProjectGroups = async ( groupProjectIds , projectIds , projectsGroups , models , sqlClientPool , pid ) => {
1054
1054
const projectGroups = await groupHelpers ( sqlClientPool ) . selectGroupsByProjectId ( models , pid )
1055
- // get all the groups the requested project is in
1056
- for ( const group of projectGroups ) {
1057
- // for each group the project is in, get the list of projects that are also in this group
1058
- if ( R . prop ( 'lagoon-projects' , group . attributes ) ) {
1059
- const groupProjects = R . prop ( 'lagoon-projects' , group . attributes ) . toString ( ) . split ( ',' )
1060
- for ( const project of groupProjects ) {
1061
- groupProjectIds . push ( { group : group . name , project : project } )
1055
+ let index = projectIds . findIndex ( ( item ) => item === pid ) ;
1056
+ if ( index === - 1 ) {
1057
+ projectIds . push ( pid )
1058
+ // get all the groups the requested project is in
1059
+ for ( const group of projectGroups ) {
1060
+ const groupProjectIdss = await groupHelpers ( sqlClientPool ) . selectProjectIdsByGroupID ( group . id )
1061
+ // for each group the project is in, get the list of projects that are also in this group
1062
+ for ( const project of groupProjectIdss ) {
1063
+ let index = groupProjectIds . findIndex ( ( item ) => item . group === group . name ) ;
1064
+ if ( index === - 1 ) {
1065
+ groupProjectIds . push ( { group : group . name , project : project } )
1066
+ projectsGroups . push ( group )
1067
+ }
1068
+ // recurse the project
1069
+ await checkProjectGroups ( groupProjectIds , projectIds , projectsGroups , models , sqlClientPool , project )
1062
1070
}
1063
1071
}
1064
1072
}
1073
+ }
1074
+
1075
+ const checkBulkProjectGroupAssociation = async ( oid , pid , projectsToMove , groupsToMove , projectsInOtherOrgs , groupsInOtherOrgs , sqlClientPool , models ) => {
1076
+ const groupProjectIds = [ ] ;
1077
+ const projectIds = [ ] ;
1078
+ const projectsGroups = [ ]
1079
+ await checkProjectGroups ( groupProjectIds , projectIds , projectsGroups , models , sqlClientPool , pid )
1065
1080
1066
1081
// for all the projects in the first projects group, iterate through the projects and the groups attached
1067
1082
// to these projects and try to build out a map of all the groups and projects that are linked by the primary project
1068
1083
if ( groupProjectIds . length > 0 ) {
1069
1084
for ( const pGroup of groupProjectIds ) {
1070
1085
const project = await projectHelpers ( sqlClientPool ) . getProjectById ( pGroup . project )
1071
- const projectGroups = await groupHelpers ( sqlClientPool ) . selectGroupsByProjectId ( models , pid )
1072
1086
// check if the project is already in the requested organization
1073
1087
if ( project . organization != oid && project . organization == null ) {
1074
1088
let alreadyAdded = false
@@ -1096,32 +1110,32 @@ const checkBulkProjectGroupAssociation = async (oid, pid, projectsToMove, groups
1096
1110
}
1097
1111
}
1098
1112
}
1099
- for ( const group of projectGroups ) {
1100
- // for every group that the project is in, check if the group is already in the requested organization
1101
- if ( group . organization != oid && group . organization == null ) {
1113
+ }
1114
+ for ( const group of projectsGroups ) {
1115
+ // for every group that the project is in, check if the group is already in the requested organization
1116
+ if ( group . organization != oid && group . organization == null ) {
1117
+ let alreadyAdded = false
1118
+ for ( const f of groupsToMove ) {
1119
+ if ( f . id == group . id ) {
1120
+ alreadyAdded = true
1121
+ }
1122
+ }
1123
+ if ( ! alreadyAdded ) {
1124
+ // if it isn't already in the requested organization, add it to the list of groups that should be moved
1125
+ groupsToMove . push ( group )
1126
+ }
1127
+ } else {
1128
+ // if the group is in a completely different organization
1129
+ if ( group . organization != oid ) {
1102
1130
let alreadyAdded = false
1103
- for ( const f of groupsToMove ) {
1131
+ for ( const f of groupsInOtherOrgs ) {
1104
1132
if ( f . id == group . id ) {
1105
1133
alreadyAdded = true
1106
1134
}
1107
1135
}
1108
1136
if ( ! alreadyAdded ) {
1109
- // if it isn't already in the requested organization, add it to the list of groups that should be moved
1110
- groupsToMove . push ( group )
1111
- }
1112
- } else {
1113
- // if the group is in a completely different organization
1114
- if ( group . organization != oid ) {
1115
- let alreadyAdded = false
1116
- for ( const f of groupsInOtherOrgs ) {
1117
- if ( f . id == group . id ) {
1118
- alreadyAdded = true
1119
- }
1120
- }
1121
- if ( ! alreadyAdded ) {
1122
- // add it to the lsit of projects that will cause this check to fail
1123
- groupsInOtherOrgs . push ( group )
1124
- }
1137
+ // add it to the lsit of projects that will cause this check to fail
1138
+ groupsInOtherOrgs . push ( group )
1125
1139
}
1126
1140
}
1127
1141
}
0 commit comments