Skip to content

Commit 728c51c

Browse files
committed
#795 Edit endpoint names and change to POST request
1 parent a51c511 commit 728c51c

7 files changed

Lines changed: 51 additions & 30 deletions

File tree

.github/workflows/task-deadline-notifications.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ jobs:
1010
steps:
1111
- name: Send notifications
1212
run: |
13-
curl -X GET https://api.finishlinebyner.com/deadline-notifications/sendTaskDeadlineSlackNotifications \
13+
curl -X POST https://api.finishlinebyner.com/notifications/task-deadlines \
1414
-H 'Authorization: ${{ secrets.NOTIFICATION_ENDPOINT_SECRET }}'

src/backend/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import descriptionBulletsRouter from './src/routes/description-bullets.routes';
1212
import tasksRouter from './src/routes/tasks.routes';
1313
import reimbursementRequestsRouter from './src/routes/reimbursement-requests.routes';
1414
import designReviewRouter from './src/routes/design-review.routes';
15-
import deadlineNotificationsRouter from './src/routes/deadline-notifications.routes';
15+
import notificationsRouter from './src/routes/notifications.routes';
1616

1717
const app = express();
1818
const port = process.env.PORT || 3001;
@@ -55,7 +55,7 @@ app.use('/description-bullets', descriptionBulletsRouter);
5555
app.use('/tasks', tasksRouter);
5656
app.use('/reimbursement-requests', reimbursementRequestsRouter);
5757
app.use('/design-reviews', designReviewRouter);
58-
app.use('/deadline-notifications', deadlineNotificationsRouter);
58+
app.use('/notifications', notificationsRouter);
5959
app.use('/', (_req, res) => {
6060
res.json('Welcome to FinishLine');
6161
});

src/backend/src/controllers/deadline-notifications.controllers.ts renamed to src/backend/src/controllers/notifications.controllers.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { NextFunction, Request, Response } from 'express';
2-
import DeadlineNotificationsService from '../services/deadline-notifications.services';
2+
import NotificationsService from '../services/notifications.services';
33

4-
export default class DeadlineNotificationsController {
4+
export default class NotificationsController {
55
static async sendTaskDeadlineSlackNotifications(_req: Request, res: Response, next: NextFunction) {
66
try {
7-
const tomorrow = new Date(new Date().setHours(24, 0, 0, 0));
8-
await DeadlineNotificationsService.sendTaskDeadlineSlackNotifications(tomorrow);
7+
await NotificationsService.sendTaskDeadlineSlackNotifications();
98

109
res.status(200).json({ message: 'Successfully sent task deadline notifications!' });
1110
} catch (error: unknown) {

src/backend/src/routes/deadline-notifications.routes.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import express from 'express';
2+
import NotificationsController from '../controllers/notifications.controllers';
3+
4+
const notificationsRouter = express.Router();
5+
6+
notificationsRouter.post('/task-deadlines', NotificationsController.sendTaskDeadlineSlackNotifications);
7+
8+
export default notificationsRouter;

src/backend/src/services/deadline-notifications.services.ts renamed to src/backend/src/services/notifications.services.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import prisma from '../prisma/prisma';
2-
import { TaskWithAssignees, getTeamFromTaskAssignees, usersToSlackPings } from '../utils/deadline-notifications.utils';
2+
import {
3+
TaskWithAssignees,
4+
endOfDayTomorrow,
5+
getTeamFromTaskAssignees,
6+
startOfDayTomorrow,
7+
usersToSlackPings
8+
} from '../utils/notifications.utils';
39
import { sendMessage } from '../integrations/slack';
410

5-
export default class DeadlineNotificationsService {
11+
export default class NotificationsService {
612
/**
7-
* Sends the task deadline slack notifications for all tasks with a deadline of the given date
8-
* @param deadline the beginning of the deadline date (at 12am)
13+
* Sends the task deadline slack notifications for all tasks with a deadline of tomorrow
914
*/
10-
static async sendTaskDeadlineSlackNotifications(deadline: Date) {
11-
const startOfDay = deadline;
12-
const endOfDay = new Date(startOfDay);
13-
endOfDay.setDate(startOfDay.getDate() + 1);
15+
static async sendTaskDeadlineSlackNotifications() {
16+
const startOfDay = startOfDayTomorrow();
17+
const endOfDay = endOfDayTomorrow();
1418

1519
const tasks = await prisma.task.findMany({
1620
where: {
@@ -43,7 +47,7 @@ export default class DeadlineNotificationsService {
4347

4448
// group tasks due by team in a map
4549
tasks.forEach((task) => {
46-
const teamSlackId = getTeamFromTaskAssignees(task.assignees);
50+
const teamSlackId = getTeamFromTaskAssignees(task.assignees).slackId;
4751

4852
const currentTasks = teamTaskMap.get(teamSlackId);
4953
if (currentTasks) {
@@ -57,6 +61,8 @@ export default class DeadlineNotificationsService {
5761
// send the notifications to each team for their respective tasks
5862
teamTaskMap.forEach((tasks, slackId) => {
5963
const messageBlock = tasks
64+
// ensures that the task has assignees to send a reminder for
65+
.filter((task) => task.assignees)
6066
.map(
6167
(task) =>
6268
`${usersToSlackPings(task.assignees ?? [])} Reminder: ${task.title} due tomorrow in project ${

src/backend/src/utils/deadline-notifications.utils.ts renamed to src/backend/src/utils/notifications.utils.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export const usersToSlackPings = (users: UserWithSettings[]) => {
2222
* Gets the team of a task's assignees.
2323
* Assumes all assigness share a team
2424
* @param users the users of the task
25-
* @returns the slack id of the team assigned to the task
25+
* @returns the team assigned to the task
2626
*/
27-
export const getTeamFromTaskAssignees = (users: UserWithTeams[]): string => {
27+
export const getTeamFromTaskAssignees = (users: UserWithTeams[]): Team => {
2828
const allTeams = users.map((user) => {
2929
const teams = [];
3030
if (user.teamAsHead) teams.push(user.teamAsHead);
@@ -43,8 +43,27 @@ export const getTeamFromTaskAssignees = (users: UserWithTeams[]): string => {
4343

4444
// Assuming we return the Slack ID of the first common team if there are any
4545
if (commonTeams.length > 0) {
46-
return commonTeams[0].slackId; // Return the slackId of the first common team
46+
return commonTeams[0]; // Return the first common team
4747
}
4848

4949
throw new HttpException(400, 'All of the users do not share a team!');
5050
};
51+
52+
/**
53+
* Gets the beginning of the day tomorrow
54+
* @returns the beginning of the day tomorrow (at 12am)
55+
*/
56+
export const startOfDayTomorrow = () => {
57+
return new Date(new Date().setHours(24, 0, 0, 0));
58+
};
59+
60+
/**
61+
* Gets the end of the day tomorrow
62+
* @returns the end of the day tomorrow (i.e. 12am of the following day)
63+
*/
64+
export const endOfDayTomorrow = () => {
65+
const startOfDay = startOfDayTomorrow();
66+
const endOfDay = new Date(startOfDay);
67+
endOfDay.setDate(startOfDay.getDate() + 1);
68+
return endOfDay;
69+
};

0 commit comments

Comments
 (0)