mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
fix(devices): auto-upsert APNS NotificationChannel on registration
The dispatcher's APNS branch only fires when a NotificationChannel of type APNS exists for the user, but no production code path created one. Every iOS install needed a manual settings toggle that doesn't exist, making the v1.4.23 W3 APNs cascade unreachable in real traffic. When `POST /api/devices` accepts an apnsToken, eagerly upsert the channel row with an encrypted empty config — the per-device token and environment live on the Device row by design, so the channel only exists to flip the dispatcher's gate. Mirrors the legacy Telegram on-first-dispatch auto-migration but eager so the very first reminder after registration routes through APNs. Adds an integration test that registers a device with apnsToken and asserts the channel row exists, registers without one and asserts absence, and re-registers the same apnsToken and asserts idempotency (channel count stays at one). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
54865075a5
commit
13977bcc70
2 files changed
+171
No files matched your search
@@ -28,6 +28,7 @@ import { apiError, apiSuccess, safeJson } from "@/lib/api-response";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { auditLog } from "@/lib/auth/audit";
|
||||
import { encrypt } from "@/lib/crypto";
|
||||
|
||||
const deviceSchema = z
|
||||
.object({
|
||||
@@ -157,6 +158,28 @@ export const POST = apiHandler(async (request: NextRequest) => {
|
||||
id = created.id;
|
||||
}
|
||||
|
||||
// Auto-create the APNS NotificationChannel row when the device
|
||||
// registers with an apnsToken. Without this row the dispatcher's APNS
|
||||
// branch (`dispatcher.ts:41-49`) never fires for the user — every
|
||||
// production iOS install would otherwise need a manual settings
|
||||
// toggle that doesn't exist. Mirrors the legacy Telegram on-first-
|
||||
// dispatch auto-migration, but eager so the very first reminder
|
||||
// after device registration already routes through APNs. Config is
|
||||
// an encrypted empty record by design — the per-device token +
|
||||
// environment live on the Device row.
|
||||
if (apnsToken) {
|
||||
await prisma.notificationChannel.upsert({
|
||||
where: { userId_type: { userId: user.id, type: "APNS" } },
|
||||
create: {
|
||||
userId: user.id,
|
||||
type: "APNS",
|
||||
enabled: true,
|
||||
config: encrypt("{}"),
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
}
|
||||
|
||||
await auditLog("devices.register", {
|
||||
userId: user.id,
|
||||
details: {
|
||||
|
||||
Reference in new issue
Block a user