fix(notifications): rest a channel that had nobody to send to

A send with no browser subscribed, no iPhone paired or a transport the
operator has not set up no longer counts as a failure, but nothing else
replaced it. The mood reminder and the daily briefing retry every
minute until delivered, so such a channel wrote a skipped row each
minute, for good; the dispatch still counted as an attempt, so the
watchdog and status alerts took it as announced; and the status screen
said "active" with no hint why nothing arrived.

The channel now rests for an hour after such a send, with the reason on
its status, and the send does not count as an attempt. Subscribing a
browser or pairing an iPhone ends the rest at once.
This commit is contained in:
Marc-André Bombeck committed 2026-11-12 14:12:45 +01:00
1 parent 95e2ba5477
commit 4ff3e4e1b7
7 files changed
+97 -7

No files matched your search

@@ -17,6 +17,7 @@ vi.mock("@/lib/db", () => ({
$transaction: vi.fn(),
notificationChannel: {
upsert: vi.fn(),
updateMany: vi.fn(),
},
auditLog: {
create: vi.fn(),
@@ -89,6 +90,9 @@ beforeEach(() => {
vi.mocked(prisma.notificationChannel.upsert).mockResolvedValue({
id: "ch-1",
} as never);
vi.mocked(prisma.notificationChannel.updateMany).mockResolvedValue({
count: 0,
});
vi.mocked(prisma.auditLog.create).mockResolvedValue({} as never);
// `resetAllMocks` strips the top-level `.mockResolvedValue(...)`
// declaration too, so re-arm the rate-limit success path here.
@@ -219,6 +223,29 @@ describe("POST /api/devices", () => {
expect(args.data.apnsEnvironment).toBe("sandbox");
});
it("ends a pause that was waiting for a paired iPhone", async () => {
// A reminder with no iPhone paired rests the APNs channel for an hour;
// pairing one must not leave the next reminder waiting it out.
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
const res = await POST(
req({
token: "abcd1234efgh5678",
bundleId: "io.healthlog.app",
apnsToken: "deadbeef".repeat(8),
apnsEnvironment: "sandbox",
}),
);
expect(res.status).toBe(201);
expect(prisma.notificationChannel.updateMany).toHaveBeenCalledWith({
where: {
userId: "user-1",
type: "APNS",
lastFailureReason: "apns_no_devices",
},
data: { nextRetryAt: null, lastFailureReason: null },
});
});
it("rejects re-registration of an apnsToken owned by another user with 409", async () => {
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
vi.mocked(prisma.device.findFirst).mockResolvedValue({
+9
View File
@@ -272,6 +272,15 @@ export const POST = apiHandler(async (request: NextRequest) => {
},
update: {},
});
// The iPhone a no-device pause was waiting for: deliver at once.
await tx.notificationChannel.updateMany({
where: {
userId: user.id,
type: "APNS",
lastFailureReason: "apns_no_devices",
},
data: { nextRetryAt: null, lastFailureReason: null },
});
}
return { id: device.id };