Skip to content

Commit f2346c4

Browse files
committed
off by 1 task and event times fix
1 parent 4568620 commit f2346c4

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import {
33
TaskWithAssignees,
44
endOfDayTomorrow,
55
startOfDayTomorrow,
6+
startOfTodayEST,
7+
startOfTomorrowEST,
68
usersToSlackPings,
79
EventWithAttendees
810
} from '../utils/notifications.utils.js';
911
import { sendMessage } from '../integrations/slack.js';
10-
import { daysBetween, startOfDay, wbsPipe, formatTimeForSlack } from 'shared';
12+
import { daysBetween, wbsPipe, formatTimeForSlack } from 'shared';
1113
import { buildDueString, sendThreadResponse } from '../utils/slack.utils.js';
1214
import WorkPackagesService from './work-packages.services.js';
1315
import { addWeeksToDate } from 'shared';
@@ -30,7 +32,7 @@ export default class NotificationsService {
3032
static async sendTaskDeadlineSlackNotifications() {
3133
const endOfDay = endOfDayTomorrow();
3234

33-
if (endOfDay.getDay() === 0 || endOfDay.getDay() === 2 || endOfDay.getDay() === 4) return;
35+
if (endOfDay.getUTCDay() === 0 || endOfDay.getUTCDay() === 2 || endOfDay.getUTCDay() === 4) return;
3436

3537
const tasks = await prisma.task.findMany({
3638
where: {
@@ -81,7 +83,8 @@ export default class NotificationsService {
8183
const messageBlock = tasks
8284
.map((task) => {
8385
// prisma call earlier allows the forced unwrap (deadline is guaranteed to be a non-null value)
84-
const daysUntilDeadline = daysBetween(task.deadline!, new Date());
86+
const todayMidnightUTC = new Date(new Date().setUTCHours(0, 0, 0, 0));
87+
const daysUntilDeadline = daysBetween(task.deadline!, todayMidnightUTC);
8588

8689
return `${usersToSlackPings(task.assignees ?? [])} <https://finishlinebyner.com/projects/${wbsPipe(
8790
task.wbsElement
@@ -121,8 +124,8 @@ export default class NotificationsService {
121124
* Sends Slack notifications for all events scheduled for today whose event type has sendSlackNotifications enabled
122125
*/
123126
static async sendEventSlackNotifications() {
124-
const endOfToday = startOfDayTomorrow();
125-
const startOfToday = startOfDay(new Date());
127+
const endOfToday = startOfTomorrowEST();
128+
const startOfToday = startOfTodayEST();
126129

127130
const events = await prisma.event.findMany({
128131
where: {
@@ -231,7 +234,7 @@ export default class NotificationsService {
231234
* Sends the sponsor task slack notifications for all tasks with a notify date of today
232235
*/
233236
static async sendSponsorTaskNotifications() {
234-
const startOfToday = startOfDay(new Date());
237+
const startOfToday = new Date(new Date().setUTCHours(0, 0, 0, 0));
235238
const endOfToday = startOfDayTomorrow();
236239

237240
const sponsorTasks = await prisma.sponsor_Task.findMany({

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export const userToSlackPing = (user: UserWithSettings) => {
3131
* @returns the beginning of the day tomorrow (at 12am)
3232
*/
3333
export const startOfDayTomorrow = () => {
34-
return new Date(new Date().setHours(24, 0, 0, 0));
34+
const tomorrow = new Date();
35+
tomorrow.setUTCDate(tomorrow.getUTCDate() + 1);
36+
tomorrow.setUTCHours(0, 0, 0, 0);
37+
return tomorrow;
3538
};
3639

3740
/**
@@ -41,6 +44,36 @@ export const startOfDayTomorrow = () => {
4144
export const endOfDayTomorrow = () => {
4245
const startOfDay = startOfDayTomorrow();
4346
const endOfDay = new Date(startOfDay);
44-
endOfDay.setDate(startOfDay.getDate() + 1);
47+
endOfDay.setUTCDate(startOfDay.getUTCDate() + 1);
4548
return endOfDay;
4649
};
50+
51+
const EST_OFFSET_MS = 5 * 60 * 60 * 1000;
52+
53+
/**
54+
* Given a UTC Date, returns the start of that calendar day in EST (UTC-5), expressed as a UTC Date.
55+
* EST is always treated as UTC-5 (no DST adjustment).
56+
* @returns midnight EST of the given date as a UTC Date
57+
*/
58+
export const startOfDateEST = (date: Date): Date => {
59+
const dateInEST = new Date(date.getTime() - EST_OFFSET_MS);
60+
return new Date(Date.UTC(dateInEST.getUTCFullYear(), dateInEST.getUTCMonth(), dateInEST.getUTCDate(), 5, 0, 0, 0));
61+
};
62+
63+
/**
64+
* Gets the start of today in EST (UTC-5), expressed as a UTC Date.
65+
* EST is always treated as UTC-5 (no DST adjustment).
66+
* @returns midnight EST today as a UTC Date
67+
*/
68+
export const startOfTodayEST = (): Date => startOfDateEST(new Date());
69+
70+
/**
71+
* Gets the start of tomorrow in EST (UTC-5), expressed as a UTC Date.
72+
* EST is always treated as UTC-5 (no DST adjustment).
73+
* @returns midnight EST tomorrow as a UTC Date
74+
*/
75+
export const startOfTomorrowEST = (): Date => {
76+
const start = startOfTodayEST();
77+
start.setUTCDate(start.getUTCDate() + 1);
78+
return start;
79+
};

0 commit comments

Comments
 (0)