Skip to content

Commit 3e42ffa

Browse files
committed
#795 Move some logic to team utils
1 parent 63a496d commit 3e42ffa

3 files changed

Lines changed: 22 additions & 23 deletions

File tree

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,15 @@ export default class NotificationsService {
6161
// send the notifications to each team for their respective tasks
6262
teamTaskMap.forEach((tasks, slackId) => {
6363
const messageBlock = tasks
64-
// ensures that the task has assignees to send a reminder for
65-
.filter((task) => task.assignees)
6664
.map(
6765
(task) =>
68-
`${usersToSlackPings(task.assignees ?? [])} Reminder: ${task.title} due tomorrow in project ${
69-
task.wbsElement?.name
70-
}`
66+
`${usersToSlackPings(task.assignees ?? [])} ${task.title} due tomorrow in project ${task.wbsElement?.name}`
7167
)
7268
.join('\n\n');
7369

7470
// messageBlock will be empty if there are tasks with no assignees
75-
if (messageBlock !== '') sendMessage(slackId, messageBlock);
71+
if (messageBlock !== '')
72+
sendMessage(slackId, ':sparkles: :pepe-coop: UPCOMING TASK DEADLINES :pepe-coop: :sparkles: \n\n\n' + messageBlock);
7673
});
7774
}
7875
}

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Team, Task as Prisma_Task, WBS_Element } from '@prisma/client';
22
import { UserWithSettings } from './auth.utils';
33
import { HttpException } from './errors.utils';
4+
import { getTeamsFromUsers } from './teams.utils';
45

56
export type UserWithTeams = UserWithSettings & {
67
teamAsHead: Team | null;
@@ -28,7 +29,7 @@ export const getTeamFromTaskAssignees = (users: UserWithTeams[]): Team => {
2829
const usersTeams = getTeamsFromUsers(users);
2930

3031
firstUsersTeams: for (const team of usersTeams[0]) {
31-
for (let i = 0; i < usersTeams.length; i++) {
32+
for (let i = 1; i < usersTeams.length; i++) {
3233
// If the team is not found in the current user's teams, continue to the next team
3334
if (!usersTeams[i].some((t) => t.teamId === team.teamId)) continue firstUsersTeams;
3435
}
@@ -38,21 +39,6 @@ export const getTeamFromTaskAssignees = (users: UserWithTeams[]): Team => {
3839
throw new HttpException(400, 'All of the users do not share a team!');
3940
};
4041

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) => {
48-
const teams = [];
49-
if (user.teamAsHead) teams.push(user.teamAsHead);
50-
if (user.teamsAsLead) teams.push(...user.teamsAsLead);
51-
if (user.teamsAsMember) teams.push(...user.teamsAsMember);
52-
return teams;
53-
});
54-
};
55-
5642
/**
5743
* Gets the beginning of the day tomorrow
5844
* @returns the beginning of the day tomorrow (at 12am)

src/backend/src/utils/teams.utils.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Prisma, User } from '@prisma/client';
1+
import { Prisma, User, Team } from '@prisma/client';
2+
import { UserWithTeams } from './notifications.utils';
23

34
const teamQueryArgsMembersOnly = Prisma.validator<Prisma.TeamArgs>()({
45
include: {
@@ -50,3 +51,18 @@ export const areUsersPartOfTeams = (teams: Prisma.TeamGetPayload<typeof teamQuer
5051
export const isUserPartOfTeams = (teams: Prisma.TeamGetPayload<typeof teamQueryArgsMembersOnly>[], user: User) => {
5152
return teams.some((team) => isUserOnTeam(team, user));
5253
};
54+
55+
/**
56+
* Gets the teams from a list of users
57+
* @param users the users to get the teams from
58+
* @returns an array of the teams each user is in
59+
*/
60+
export const getTeamsFromUsers = (users: UserWithTeams[]): Team[][] => {
61+
return users.map((user) => {
62+
const teams = [];
63+
if (user.teamAsHead) teams.push(user.teamAsHead);
64+
if (user.teamsAsLead) teams.push(...user.teamsAsLead);
65+
if (user.teamsAsMember) teams.push(...user.teamsAsMember);
66+
return teams;
67+
});
68+
};

0 commit comments

Comments
 (0)