⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(reminders): calculate deadlines from the end of the due day (#1762)
Tin Severf04972042f94
diff --git a/apps/api/src/scheduler/due-date-reminders.ts b/apps/api/src/scheduler/due-date-reminders.tsindex f50f6e99b..48c6e0878 100644--- a/apps/api/src/scheduler/due-date-reminders.ts+++ b/apps/api/src/scheduler/due-date-reminders.ts@@ -9,24 +9,29 @@ import {workspaceUserTable,} from "../database/schema";import createNotification from "../notification/controllers/create-notification";-import { REMINDER_WINDOW_MINUTES } from "./reminder-timing";+import {+ DUE_DATE_DURATION_MS,+ REMINDER_WINDOW_MINUTES,+} from "./reminder-timing";type ReminderType = "configured_before" | "overdue";const MINUTE_MS = 60 * 1000;function buildWindows(now: Date) {- const nowMs = now.getTime();+ // Shift the window to stored day-start timestamps, preserving indexed lookups.+ const nowMs = now.getTime() - DUE_DATE_DURATION_MS;+ const windowEnd = new Date(nowMs);return {upcoming: {start: new Date(nowMs - REMINDER_WINDOW_MINUTES * MINUTE_MS),- end: now,+ end: windowEnd,type: "configured_before" as ReminderType,notificationType: "due_date_reminder" as const,},overdue: {- end: now,+ end: windowEnd,start: new Date(nowMs - 10 * MINUTE_MS),type: "overdue" as ReminderType,notificationType: "task_overdue" as const,diff --git a/apps/api/src/scheduler/project-webhook-reminders.ts b/apps/api/src/scheduler/project-webhook-reminders.tsindex 5dbc8162d..12a4a1f11 100644--- a/apps/api/src/scheduler/project-webhook-reminders.ts+++ b/apps/api/src/scheduler/project-webhook-reminders.ts@@ -11,7 +11,10 @@ import {normalizeGenericWebhookConfig,} from "../plugins/generic-webhook/config";import { sendDueDateReminder } from "../plugins/generic-webhook/events";-import { REMINDER_WINDOW_MINUTES } from "./reminder-timing";+import {+ DUE_DATE_DURATION_MS,+ REMINDER_WINDOW_MINUTES,+} from "./reminder-timing";const MINUTE_MS = 60 * 1000;@@ -42,7 +45,10 @@ export async function checkProjectWebhookReminders(): Promise<{if (!config.events?.dueDateReminder) continue;const leadTimeMinutes = config.dueDateReminderLeadTimeMinutes ?? 1440;- const windowEnd = new Date(now.getTime() + leadTimeMinutes * MINUTE_MS);+ // Count back from expiration while keeping the indexed due date unmodified.+ const windowEnd = new Date(+ now.getTime() + leadTimeMinutes * MINUTE_MS - DUE_DATE_DURATION_MS,+ );const windowStart = new Date(windowEnd.getTime() - REMINDER_WINDOW_MINUTES * MINUTE_MS,);diff --git a/apps/api/src/scheduler/reminder-timing.ts b/apps/api/src/scheduler/reminder-timing.tsindex 1ece6b69c..2bfaf0f94 100644--- a/apps/api/src/scheduler/reminder-timing.ts+++ b/apps/api/src/scheduler/reminder-timing.ts@@ -1,4 +1,7 @@const MINUTE_MS = 60 * 1000;+// Stored due dates lack IANA timezone information, so 24 hours approximates a+// calendar day; DST days can last 23 or 25 hours. Track exact semantics in #1783.+export const DUE_DATE_DURATION_MS = 24 * 60 * MINUTE_MS;export const REMINDER_WINDOW_MINUTES = 10;@@ -11,7 +14,9 @@ export function isReminderDue({leadTimeMinutes: number;now: Date;}) {- const targetTime = dueDate.getTime() - leadTimeMinutes * MINUTE_MS;+ // Due dates represent a full day; reminders count back from its expiration.+ const targetTime =+ dueDate.getTime() + DUE_DATE_DURATION_MS - leadTimeMinutes * MINUTE_MS;const elapsedSinceTarget = now.getTime() - targetTime;return (diff --git a/tests/api-integration/date-only-reminders.test.ts b/tests/api-integration/date-only-reminders.test.tsnew file mode 100644index 000000000..52acc4934--- /dev/null+++ b/tests/api-integration/date-only-reminders.test.ts@@ -0,0 +1,200 @@+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import db, { schema } from "../../apps/api/src/database";+import { taskReminderSentTable } from "../../apps/api/src/database/schema";+import createNotification from "../../apps/api/src/notification/controllers/create-notification";+import { sendDueDateReminder } from "../../apps/api/src/plugins/generic-webhook/events";+import { checkDueDateReminders } from "../../apps/api/src/scheduler/due-date-reminders";+import { checkProjectWebhookReminders } from "../../apps/api/src/scheduler/project-webhook-reminders";+import { resetTestDatabase } from "./helpers/database";+import {+ createProjectFixture,+ createWorkspaceMember,+} from "./helpers/fixtures";++vi.mock(+ "../../apps/api/src/notification/controllers/create-notification",+ () => ({+ default: vi.fn().mockResolvedValue(undefined),+ }),+);+vi.mock("../../apps/api/src/plugins/generic-webhook/events", () => ({+ sendDueDateReminder: vi.fn().mockResolvedValue(true),+}));++async function seedReminder({+ dueDate = "2026-09-23T00:00:00.000Z",+ leadTimeMinutes = 14 * 60,+ completed = false,+ enabled = true,+}: {+ dueDate?: string;+ leadTimeMinutes?: number | null;+ completed?: boolean;+ enabled?: boolean;+} = {}) {+ const { user, workspace } = await createWorkspaceMember();+ const { project, columns } = await createProjectFixture({+ workspaceId: workspace.id,+ });+ const column = completed ? columns.done : columns.todo;+ const [task] = await db+ .insert(schema.taskTable)+ .values({+ title: "Due all day",+ projectId: project.id,+ userId: user.id,+ columnId: column.id,+ status: column.slug,+ number: 1,+ dueDate: new Date(dueDate),+ })+ .returning();++ if (leadTimeMinutes !== null) {+ await db.insert(schema.userNotificationPreferenceTable).values({+ userId: user.id,+ dueDateReminderLeadTimeMinutes: leadTimeMinutes,+ dueDateReminderEnabled: enabled,+ });+ }+ await db.insert(schema.integrationTable).values({+ projectId: project.id,+ type: "generic-webhook",+ config: JSON.stringify({+ url: "https://example.com/reminders",+ events: { dueDateReminder: enabled },+ ...(leadTimeMinutes === null+ ? {}+ : { dueDateReminderLeadTimeMinutes: leadTimeMinutes }),+ }),+ });+ return task;+}++async function checkAllReminders() {+ await checkDueDateReminders();+ await checkProjectWebhookReminders();+}++function expectNoDelivery() {+ expect(createNotification).not.toHaveBeenCalled();+ expect(sendDueDateReminder).not.toHaveBeenCalled();+}++describe("API integration: date-only reminders", () => {+ beforeEach(async () => {+ vi.clearAllMocks();+ await resetTestDatabase();+ vi.useFakeTimers({ toFake: ["Date"] });+ });++ afterEach(() => {+ vi.useRealTimers();+ });++ it.each([+ [+ "2026-09-23T00:00:00.000Z",+ "2026-09-22T10:00:00.000Z",+ "2026-09-23T10:00:00.000Z",+ ],+ [+ "2026-09-23T00:00:00+02:00",+ "2026-09-22T10:00:00+02:00",+ "2026-09-23T10:00:00+02:00",+ ],+ [+ "2026-09-23T00:00:00-07:00",+ "2026-09-22T10:00:00-07:00",+ "2026-09-23T10:00:00-07:00",+ ],+ ])(+ "sends fourteen-hour reminders on the due day for %s",+ async (dueDate, early, expected) => {+ const task = await seedReminder({ dueDate });+ vi.setSystemTime(new Date(early));+ await checkAllReminders();+ expectNoDelivery();+ expect(await db.select().from(taskReminderSentTable)).toHaveLength(0);++ vi.setSystemTime(new Date(expected));+ await checkAllReminders();+ expect(createNotification).toHaveBeenCalledExactlyOnceWith(+ expect.objectContaining({+ type: "due_date_reminder",+ resourceId: task.id,+ eventData: expect.objectContaining({+ dueDate: task.dueDate?.toISOString(),+ leadTimeMinutes: 840,+ }),+ }),+ );+ expect(sendDueDateReminder).toHaveBeenCalledExactlyOnceWith(+ expect.any(Object),+ task.id,+ task.projectId,+ 840,+ task.dueDate,+ );++ vi.setSystemTime(new Date(new Date(expected).getTime() + 5 * 60 * 1000));+ await checkAllReminders();+ expect(createNotification).toHaveBeenCalledTimes(1);+ expect(sendDueDateReminder).toHaveBeenCalledTimes(1);+ },+ );++ it("does not mark a task overdue until the due day ends", async () => {+ const task = await seedReminder();+ for (const now of ["2026-09-23T00:00:00Z", "2026-09-23T23:59:59.999Z"]) {+ vi.setSystemTime(new Date(now));+ await checkDueDateReminders();+ expectNoDelivery();+ }+ vi.setSystemTime(new Date("2026-09-24T00:00:00Z"));+ await checkDueDateReminders();+ expect(createNotification).toHaveBeenCalledExactlyOnceWith(+ expect.objectContaining({+ type: "task_overdue",+ resourceId: task.id,+ }),+ );+ });++ it("uses expiration for the default one-day lead time without preferences", async () => {+ await seedReminder({ leadTimeMinutes: null });+ vi.setSystemTime(new Date("2026-09-22T00:00:00Z"));+ await checkAllReminders();+ expectNoDelivery();+ vi.setSystemTime(new Date("2026-09-23T00:00:00Z"));+ await checkAllReminders();+ expect(createNotification).toHaveBeenCalledExactlyOnceWith(+ expect.objectContaining({ type: "due_date_reminder" }),+ );+ expect(sendDueDateReminder).toHaveBeenCalledTimes(1);+ });++ it.each([+ ["2026-09-23T09:59:59.999Z", 0],+ ["2026-09-23T10:10:00.000Z", 1],+ ["2026-09-23T10:10:00.001Z", 0],+ ])("respects the delivery window at %s", async (now, count) => {+ await seedReminder();+ vi.setSystemTime(new Date(now));+ await checkAllReminders();+ expect(createNotification).toHaveBeenCalledTimes(count);+ expect(sendDueDateReminder).toHaveBeenCalledTimes(count);+ });++ it.each([{ completed: true }, { enabled: false }])(+ "skips reminders when %j",+ async (options) => {+ await seedReminder(options);+ for (const now of ["2026-09-23T10:00:00Z", "2026-09-24T00:00:00Z"]) {+ vi.setSystemTime(new Date(now));+ await checkAllReminders();+ }+ expectNoDelivery();+ },+ );+});diff --git a/tests/api-integration/due-date-reminders.test.ts b/tests/api-integration/due-date-reminders.test.tsindex 4ae6190ef..d8fdaefe0 100644--- a/tests/api-integration/due-date-reminders.test.ts+++ b/tests/api-integration/due-date-reminders.test.ts@@ -31,6 +31,9 @@ const { checkDueDateReminders } = await import(const { checkProjectWebhookReminders } = await import("../../apps/api/src/scheduler/project-webhook-reminders");+const { DUE_DATE_DURATION_MS } = await import(+ "../../apps/api/src/scheduler/reminder-timing"+);const { resetTestDatabase } = await import("./helpers/database");const { createProjectFixture, createWorkspaceMember } = await import("./helpers/fixtures"@@ -39,11 +42,15 @@ const { createProjectFixture, createWorkspaceMember } = await import(const MINUTE_MS = 60 * 1000;const DEFAULT_LEAD_TIME_MINUTES = 1440;-// Both schedulers fire when `dueDate - leadTime` lands in the trailing+// Both schedulers fire when `dueDate + duration - leadTime` lands in the trailing// REMINDER_WINDOW_MINUTES. Sitting five minutes inside keeps the fixture off// both edges of that window regardless of how long the suite takes to run.function dueDateInsideReminderWindow() {- return new Date(Date.now() + (DEFAULT_LEAD_TIME_MINUTES - 5) * MINUTE_MS);+ return new Date(+ Date.now() ++ (DEFAULT_LEAD_TIME_MINUTES - 5) * MINUTE_MS -+ DUE_DATE_DURATION_MS,+ );}type Scene = Awaited<ReturnType<typeof seedScene>>;diff --git a/tests/api-integration/notification-access.test.ts b/tests/api-integration/notification-access.test.tsindex 4fa1f9cf4..01cf5e67c 100644--- a/tests/api-integration/notification-access.test.ts+++ b/tests/api-integration/notification-access.test.ts@@ -12,6 +12,7 @@ const { deliverNotification } = await vi.importActual<>("../../apps/api/src/notification-preferences/delivery");import { checkDueDateReminders } from "../../apps/api/src/scheduler/due-date-reminders";+import { DUE_DATE_DURATION_MS } from "../../apps/api/src/scheduler/reminder-timing";import bulkUpdateTasks from "../../apps/api/src/task/controllers/bulk-update-tasks";import createTask from "../../apps/api/src/task/controllers/create-task";import importTasks from "../../apps/api/src/task/controllers/import-tasks";@@ -113,7 +114,7 @@ describe("notification recipient boundaries", () => {);await db.update(schema.taskTable)- .set({ dueDate: new Date(Date.now() - 60_000) })+ .set({ dueDate: new Date(Date.now() - DUE_DATE_DURATION_MS - 60_000) }).where(eq(schema.taskTable.id, task.id));await createComment(task.id, actor.user.id, "Private comment");await checkDueDateReminders();@@ -126,7 +127,7 @@ describe("notification recipient boundaries", () => {const { member, task } = await fixture();await db.update(schema.taskTable)- .set({ dueDate: new Date(Date.now() - 60_000) })+ .set({ dueDate: new Date(Date.now() - DUE_DATE_DURATION_MS - 60_000) }).where(eq(schema.taskTable.id, task.id));await checkDueDateReminders();expect(await getNotifications(member.user.id)).toEqual([diff --git a/tests/api/scheduler/reminder-timing.test.ts b/tests/api/scheduler/reminder-timing.test.tsindex eea41f57d..8b20392ef 100644--- a/tests/api/scheduler/reminder-timing.test.ts+++ b/tests/api/scheduler/reminder-timing.test.ts@@ -2,42 +2,56 @@ import { describe, expect, it } from "vitest";import { isReminderDue } from "../../../apps/api/src/scheduler/reminder-timing";describe("isReminderDue", () => {- const now = new Date("2026-07-12T10:00:00.000Z");+ const dueDate = new Date("2026-09-23T00:00:00.000Z");- it("matches a configured lead time", () => {+ it.each([+ [+ "the previous day's incorrect reminder",+ "2026-09-22T10:00:00.000Z",+ false,+ ],+ ["before the reminder", "2026-09-23T09:59:59.999Z", false],+ [+ "exactly fourteen hours before expiration",+ "2026-09-23T10:00:00.000Z",+ true,+ ],+ ["a delayed scheduler run", "2026-09-23T10:05:00.000Z", true],+ ["the end of the delivery window", "2026-09-23T10:10:00.000Z", true],+ ["after the delivery window", "2026-09-23T10:10:00.001Z", false],+ ])("handles %s", (_description, now, expected) => {expect(- isReminderDue({- dueDate: new Date("2026-07-14T10:00:00.000Z"),- leadTimeMinutes: 2 * 24 * 60,- now,- }),- ).toBe(true);+ isReminderDue({ dueDate, leadTimeMinutes: 14 * 60, now: new Date(now) }),+ ).toBe(expected);});- it("accepts scheduler runs within the ten minute delivery window", () => {+ it.each([+ ["2026-09-23T00:00:00.000Z", false],+ ["2026-09-23T23:59:59.999Z", false],+ ["2026-09-24T00:00:00.000Z", true],+ ])("uses next midnight for a zero lead time at %s", (now, expected) => {expect(- isReminderDue({- dueDate: new Date("2026-07-13T09:55:00.000Z"),- leadTimeMinutes: 24 * 60,- now,- }),- ).toBe(true);+ isReminderDue({ dueDate, leadTimeMinutes: 0, now: new Date(now) }),+ ).toBe(expected);});- it("does not send before or after the delivery window", () => {- expect(- isReminderDue({- dueDate: new Date("2026-07-13T10:05:00.000Z"),- leadTimeMinutes: 24 * 60,- now,- }),- ).toBe(false);- expect(- isReminderDue({- dueDate: new Date("2026-07-13T09:49:00.000Z"),- leadTimeMinutes: 24 * 60,- now,- }),- ).toBe(false);- });+ it.each([+ ["2026-09-23T00:00:00.000Z", 24 * 60, "2026-09-23T00:00:00.000Z"],+ ["2026-09-23T00:00:00.000Z", 2 * 24 * 60, "2026-09-22T00:00:00.000Z"],+ ["2026-12-31T00:00:00.000Z", 0, "2027-01-01T00:00:00.000Z"],+ ["2028-02-29T00:00:00.000Z", 0, "2028-03-01T00:00:00.000Z"],+ ["2026-09-23T00:00:00+02:00", 14 * 60, "2026-09-23T10:00:00+02:00"],+ ["2026-09-23T00:00:00-07:00", 14 * 60, "2026-09-23T10:00:00-07:00"],+ ])(+ "handles due date %s with %i minutes lead time",+ (due, leadTimeMinutes, now) => {+ expect(+ isReminderDue({+ dueDate: new Date(due),+ leadTimeMinutes,+ now: new Date(now),+ }),+ ).toBe(true);+ },+ );});