@@ -25,28 +25,32 @@ export const usersToSlackPings = (users: UserWithSettings[]) => {
2525 * @returns the team assigned to the task
2626 */
2727export const getTeamFromTaskAssignees = ( users : UserWithTeams [ ] ) : Team => {
28- const allTeams = users . map ( ( user ) => {
28+ const usersTeams = getTeamsFromUsers ( users ) ;
29+
30+ firstUsersTeams: for ( const team of usersTeams [ 0 ] ) {
31+ for ( let i = 0 ; i < usersTeams . length ; i ++ ) {
32+ // If the team is not found in the current user's teams, continue to the next team
33+ if ( ! usersTeams [ i ] . some ( ( t ) => t . teamId === team . teamId ) ) continue firstUsersTeams;
34+ }
35+ return team ;
36+ }
37+
38+ throw new HttpException ( 400 , 'All of the users do not share a team!' ) ;
39+ } ;
40+
41+ /**
42+ * Gets the teams from a list of users
43+ * @param users the users to get the teams from
44+ * @returns an array of the teams each user is in
45+ */
46+ export const getTeamsFromUsers = ( users : UserWithTeams [ ] ) : Team [ ] [ ] => {
47+ return users . map ( ( user ) => {
2948 const teams = [ ] ;
3049 if ( user . teamAsHead ) teams . push ( user . teamAsHead ) ;
3150 if ( user . teamsAsLead ) teams . push ( ...user . teamsAsLead ) ;
3251 if ( user . teamsAsMember ) teams . push ( ...user . teamsAsMember ) ;
3352 return teams ;
3453 } ) ;
35-
36- // Find common teams across all users
37- const commonTeams = allTeams . reduce ( ( acc , teams , index ) => {
38- if ( index === 0 ) {
39- return teams ;
40- }
41- return acc . filter ( ( team ) => teams . some ( ( t ) => t . teamId === team . teamId ) ) ;
42- } , allTeams [ 0 ] || [ ] ) ;
43-
44- // Assuming we return the Slack ID of the first common team if there are any
45- if ( commonTeams . length > 0 ) {
46- return commonTeams [ 0 ] ; // Return the first common team
47- }
48-
49- throw new HttpException ( 400 , 'All of the users do not share a team!' ) ;
5054} ;
5155
5256/**
0 commit comments