Skip to content

Commit fca4018

Browse files
committed
#795 Simplify getTeamFromTaskAssignees logic
1 parent 728c51c commit fca4018

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

src/backend/src/services/notifications.services.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ export default class NotificationsService {
7171
)
7272
.join('\n\n');
7373

74-
sendMessage(slackId, messageBlock);
74+
// messageBlock will be empty if there are tasks with no assignees
75+
if (messageBlock != '') sendMessage(slackId, messageBlock);
7576
});
7677
}
7778
}

src/backend/src/utils/notifications.utils.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,32 @@ export const usersToSlackPings = (users: UserWithSettings[]) => {
2525
* @returns the team assigned to the task
2626
*/
2727
export 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

Comments
 (0)