Repository: ephemeraHQ/convos-backend
Pull request #67: fix: Better push notification registering and sending
Author: login: thierryskoda
Created: 2025-05-13T22:28:34Z | Merged: 2025-05-14T03:20:41Z

### Implement XMTP push notification registration and delivery using xmtpInstallationId in IdentitiesOnDevice table
* Adds `xmtpInstallationId` column to `IdentitiesOnDevice` table through new migration in [migration.sql](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-aba0dc12ac32fc9a011ad06d8f1c17cec0664b22ab8e52b651d0c4c719f2f233) and updates [schema.prisma](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-5b443964f4f3a611682db8f7e02177b0a8c632b2039e2bd5e4dd7347815c565c)
* Rewrites notification handling in [handle-xmtp-notification.ts](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-5e68d80d2d4bcf38d79f23de529a3d6dd023eb809a0ffb452350874ec493e29e) to use `xmtpInstallationId` for device lookup with transaction-based updates and cleanup of failed installations
* Updates registration flow in [register-installation.ts](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-e832655c5ccce7cf8a4f8bb66174d34d4b8cb2b3df82b73532ce96f27b2b5dcd) to store `xmtpInstallationId`, `expoToken`, and `pushToken` with authentication checks
* Modifies unregistration in [unregister-installation.ts](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-a17b268f05d5b036f7fe7086d3910b0ecca003982a5b2c8ed2dbe6e016a18c5f) to nullify `xmtpInstallationId` and remove XMTP server registration

#### 📍Where to Start
Start with the database schema changes in [schema.prisma](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-5b443964f4f3a611682db8f7e02177b0a8c632b2039e2bd5e4dd7347815c565c) and [migration.sql](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-aba0dc12ac32fc9a011ad06d8f1c17cec0664b22ab8e52b651d0c4c719f2f233), then review the registration flow in [register-installation.ts](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-e832655c5ccce7cf8a4f8bb66174d34d4b8cb2b3df82b73532ce96f27b2b5dcd)

----

_[Macroscope](https://app.macroscope.com) summarized bfeb42c._

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
	- Added support for a unique installation identifier (xmtpInstallationId) for device and identity associations.
	- Introduced enhanced registration and unregistration flows with improved validation and ownership checks.
	- Expanded test coverage for notification registration, unregistration, subscription, and unsubscription workflows.

- **Bug Fixes**
	- Improved error handling and cleanup for push notification registrations and failed notifications.
	- Enhanced validation and authorization for installation registration and unregistration.

- **Refactor**
	- Updated registration and unregistration request formats for installations.
	- Streamlined notification sending and cleanup processes for better reliability and logging.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->


Diff:
diff --git a/prisma/migrations/20250513202135_add_xmtp_installation_id_on_identities_on_devices/migration.sql b/prisma/migrations/20250513202135_add_xmtp_installation_id_on_identities_on_devices/migration.sql
new file mode 100644
index 00000000..bde81cbb
--- /dev/null
+++ b/prisma/migrations/20250513202135_add_xmtp_installation_id_on_identities_on_devices/migration.sql
@@ -0,0 +1,11 @@
+/*
+  Warnings:
+
+  - A unique constraint covering the columns `[xmtpInstallationId]` on the table `IdentitiesOnDevice` will be added. If there are existing duplicate values, this will fail.
+
+*/
+-- AlterTable
+ALTER TABLE "IdentitiesOnDevice" ADD COLUMN     "xmtpInstallationId" TEXT;
+
+-- CreateIndex
+CREATE UNIQUE INDEX "IdentitiesOnDevice_xmtpInstallationId_key" ON "IdentitiesOnDevice"("xmtpInstallationId");
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index fe695980..3ec41eab 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -66,6 +66,7 @@ model IdentitiesOnDevice {
   device     Device         @relation(fields: [deviceId], references: [id])
   identityId String
   identity   DeviceIdentity @relation(fields: [identityId], references: [id])
+  xmtpInstallationId String?    @unique
 
   @@id([deviceId, identityId])
 }
diff --git a/src/api/v1/identities.ts b/src/api/v1/identities.ts
index 1b22b17c..b2148cff 100644
--- a/src/api/v1/identities.ts
+++ b/src/api/v1/identities.ts
@@ -182,11 +182,31 @@ identitiesRouter.post(
         return;
       }
 
+      // Check if an identity with this user ID and XMTP ID is already linked to this device
+      const existingLink = await prisma.identitiesOnDevice.findFirst({
+        where: {
+          deviceId: deviceId,
+          identity: {
+            userId: device.user.id,
+            xmtpId: xmtpId,
+          },
+        },
+      });
+
+      if (existingLink) {
+        res.status(409).json({
+          error:
+            "An identity for this user and XMTP ID is already associated with this device.",
+        });
+        return;
+      }
+
       const identity = await prisma.deviceIdentity.create({
         data: {
-          ...validatedData,
+          turnkeyAddress: validatedData.turnkeyAddress,
           xmtpId,
           userId: device.user.id,
+          // Create a new link between the identity and the device
           devices: {
             create: {
               deviceId,
diff --git a/src/api/v1/notifications/handlers/handle-xmtp-notification.ts b/src/api/v1/notifications/handlers/handle-xmtp-notification.ts
index e5a25a71..9327da87 100644
--- a/src/api/v1/notifications/handlers/handle-xmtp-notification.ts
+++ b/src/api/v1/notifications/handlers/handle-xmtp-notification.ts
@@ -1,10 +1,14 @@
 import { Expo, type ExpoPushMessage } from "expo-server-sdk";
 import type { Request, Response } from "express";
-import type { NotificationResponse } from "@/notifications/client";
+import {
+  createNotificationClient,
+  type NotificationResponse,
+} from "@/notifications/client";
 import { getHttpDeliveryNotificationAuthHeader } from "@/notifications/utils";
 import { prisma } from "@/utils/prisma";
 
 const expo = new Expo();
+const notificationClient = createNotificationClient();
 
 if (!process.env.XMTP_NOTIFICATION_SECRET) {
   throw new Error("XMTP_NOTIFICATION_SECRET is not set");
@@ -18,6 +22,11 @@ if (!process.env.XMTP_NOTIFICATION_SECRET) {
  * from the authorized XMTP server.
  */
 export async function handleXmtpNotification(req: Request, res: Response) {
+  let identityOnDeviceToCleanup: {
+    xmtpInstallationId: string | null;
+    deviceId: string;
+  } | null = null;
+
   try {
     const notification = req.body as NotificationResponse;
 
@@ -48,51 +57,69 @@ export async function handleXmtpNotification(req: Request, res: Response) {
       return;
     }
 
-    const device = await prisma.device.findFirst({
+    const identityOnDevice = await prisma.identitiesOnDevice.findUnique({
       where: {
-        pushToken: notification.installation.delivery_mechanism.token,
+        xmtpInstallationId: notification.installation.id,
       },
       include: {
-        identities: {
-          include: {
-            identity: true,
-          },
-        },
+        device: true,
+        identity: true,
       },
     });
 
-    if (!device) {
-      req.log.error(
-        `Device with push token ${notification.installation.delivery_mechanism.token} not found`,
+    if (!identityOnDevice || !identityOnDevice.xmtpInstallationId) {
+      req.log.warn(
+        `No active device/identity found for xmtpInstallationId ${notification.installation.id}. This installation might have been cleaned up already.`,
       );
-      res.status(404).end();
+      try {
+        await notificationClient.deleteInstallation({
+          installationId: notification.installation.id,
+        });
+        req.log.info(
+          `Requested deletion of orphaned xmtpInstallationId ${notification.installation.id} from XMTP server.`,
+        );
+      } catch (deleteError) {
+        req.log.error(
+          { error: deleteError },
+          `Failed to request deletion of orphaned xmtpInstallationId ${notification.installation.id}`,
+        );
+      }
+      res.status(200).end();
       return;
     }
 
+    identityOnDeviceToCleanup = {
+      xmtpInstallationId: identityOnDevice.xmtpInstallationId,
+      deviceId: identityOnDevice.deviceId,
+    };
+
+    const { device, identity } = identityOnDevice;
     const expoPushToken = device.expoToken;
-    const turnkeyAddress =
-      device.identities.length > 0
-        ? device.identities[0].identity.turnkeyAddress
-        : undefined;
+    const turnkeyAddress = identity.turnkeyAddress;
 
     if (!turnkeyAddress) {
       req.log.error(
-        `Device with push token ${notification.installation.delivery_mechanism.token} has no identity`,
+        `DeviceIdentity ${identity.id} for xmtpInstallationId ${notification.installation.id} has no turnkeyAddress`,
       );
       res.status(200).end();
       return;
     }
 
-    // Validate the Expo push token
-    if (!Expo.isExpoPushToken(expoPushToken)) {
-      req.log.error(
-        `Push token ${expoPushToken} is not a valid Expo push token`,
+    if (!expoPushToken || !Expo.isExpoPushToken(expoPushToken)) {
+      req.log.warn(
+        `Expo push token for Device ${device.id} (xmtpInstallationId ${notification.installation.id}) is invalid or missing: ${expoPushToken}. Cleaning up.`,
       );
+      if (identityOnDeviceToCleanup.xmtpInstallationId) {
+        await cleanupFailedInstallation({
+          xmtpInstallationId: identityOnDeviceToCleanup.xmtpInstallationId,
+          deviceId: identityOnDeviceToCleanup.deviceId,
+          req,
+        });
+      }
       res.status(200).end();
       return;
     }
 
-    // Prepare the base message data that's common for both silent and regular notifications
     const baseMessageData = {
       contentTopic: notification.message.content_topic,
       messageType: notification.message_context.message_type,
@@ -101,21 +128,15 @@ export async function handleXmtpNotification(req: Request, res: Response) {
       ethAddress: turnkeyAddress,
     };
 
-    // Create the message based on whether it's silent or regular
     const message: ExpoPushMessage = notification.subscription.is_silent
       ? {
-          // Silent notification configuration
           to: expoPushToken,
           data: baseMessageData,
-          // Required for iOS silent notifications
           _contentAvailable: true,
-          // Required for Android silent notifications
           priority: "normal",
-          // Ensure no visible alerts
           sound: undefined,
         }
       : {
-          // Regular notification configuration
           to: expoPushToken,
           sound: "default",
           body: "New message",
@@ -124,25 +145,93 @@ export async function handleXmtpNotification(req: Request, res: Response) {
           mutableContent: true,
         };
 
-    // Send the notification
     const chunks = expo.chunkPushNotifications([message]);
-    const sendPromises = chunks.map(async (chunk) => {
+
+    for (const chunk of chunks) {
       try {
-        const ticketChunk = await expo.sendPushNotificationsAsync(chunk);
-        req.log.info({ ticketChunk }, "push notification sent");
-        return ticketChunk;
+        const tickets = await expo.sendPushNotificationsAsync(chunk);
+
+        for (const ticket of tickets) {
+          const expoPushReceipt = ticket;
+
+          if (expoPushReceipt.status === "error") {
+            req.log.error(
+              {
+                details: expoPushReceipt.details,
+                xmtpInstallationId: notification.installation.id,
+              },
+              `Error sending push notification: ${expoPushReceipt.message}`,
+            );
+
+            if (
+              expoPushReceipt.details &&
+              expoPushReceipt.details.error === "DeviceNotRegistered"
+            ) {
+              req.log.info(
+                `DeviceNotRegistered error for token ${expoPushToken} (xmtpInstallationId ${notification.installation.id}). Initiating cleanup.`,
+              );
+
+              if (identityOnDeviceToCleanup.xmtpInstallationId) {
+                await cleanupFailedInstallation({
+                  xmtpInstallationId:
+                    identityOnDeviceToCleanup.xmtpInstallationId,
+                  deviceId: identityOnDeviceToCleanup.deviceId,
+                  req,
+                });
+              }
+            }
+          }
+        }
       } catch (error) {
-        req.log.error({ error }, "Error sending push notification:");
-        throw error;
+        req.log.error(
+          { error, xmtpInstallationId: notification.installation.id },
+          "Critical error sending push notifications chunk",
+        );
       }
-    });
-
-    await Promise.all(sendPromises);
+    }
 
-    // Respond with success
     res.status(200).end();
   } catch (error) {
-    console.error("Error processing notification:", error);
+    req.log.error({ error }, "Outer error processing notification");
     res.status(500).json({ error: "Internal server error" });
   }
 }
+
+async function cleanupFailedInstallation(args: {
+  xmtpInstallationId: string;
+  deviceId: string;
+  req: Request;
+}) {
+  const { xmtpInstallationId, deviceId, req } = args;
+
+  try {
+    req.log.info(
+      `Cleaning up installation: ${xmtpInstallationId} for device: ${deviceId}`,
+    );
+    await prisma.$transaction([
+      prisma.identitiesOnDevice.updateMany({
+        where: { xmtpInstallationId: xmtpInstallationId },
+        data: { xmtpInstallationId: null },
+      }),
+      prisma.device.update({
+        where: { id: deviceId },
+        data: { expoToken: null },
+      }),
+    ]);
+    req.log.info(
+      `Successfully cleaned xmtpInstallationId ${xmtpInstallationId} and expoToken for device ${deviceId} from local DB.`,
+    );
+
+    await notificationClient.deleteInstallation({
+      installationId: xmtpInstallationId,
+    });
+    req.log.info(
+      `Successfully requested deletion of xmtpInstallationId ${xmtpInstallationId} from XMTP server.`,
+    );
+  } catch (cleanupError) {
+    req.log.error(
+      { error: cleanupError, xmtpInstallationId, deviceId },
+      "Failed during cleanup of installation",
+    );
+  }
+}
diff --git a/src/api/v1/notifications/handlers/register-installation.ts b/src/api/v1/notifications/handlers/register-installation.ts
index 7c462003..da06cba5 100644
--- a/src/api/v1/notifications/handlers/register-installation.ts
+++ b/src/api/v1/notifications/handlers/register-installation.ts
@@ -1,16 +1,14 @@
 import type { Request, Response } from "express";
 import { z } from "zod";
 import { createNotificationClient } from "@/notifications/client";
+import { prisma } from "@/utils/prisma";
 
-// Schema for registration
 const registrationSchema = z.object({
-  installationId: z.string(),
-  deliveryMechanism: z.object({
-    deliveryMechanismType: z.object({
-      case: z.enum(["apnsDeviceToken", "firebaseDeviceToken", "customToken"]),
-      value: z.string(),
-    }),
-  }),
+  deviceId: z.string(),
+  identityId: z.string(),
+  xmtpInstallationId: z.string(),
+  expoToken: z.string(),
+  pushToken: z.string(),
 });
 
 type RegisterInstallationRequestBody = z.infer<typeof registrationSchema>;
@@ -22,11 +20,104 @@ export async function registerInstallation(
   res: Response,
 ) {
   try {
+    const authenticatedXmtpId = req.app.locals.xmtpId;
+
     const validatedData = registrationSchema.parse(req.body);
 
+    const [
+      deviceIdentityForAuthenticatedUser,
+      deviceOwnerCheck,
+      identityOwnerCheck,
+    ] = await Promise.all([
+      prisma.deviceIdentity.findFirst({
+        where: { xmtpId: authenticatedXmtpId },
+        select: { userId: true },
+      }),
+      prisma.device.findUnique({
+        where: { id: validatedData.deviceId },
+        select: { userId: true },
+      }),
+      prisma.deviceIdentity.findUnique({
+        where: { id: validatedData.identityId },
+        select: { userId: true },
+      }),
+    ]);
+
+    console.log(
+      "deviceIdentityForAuthenticatedUser:",
+      deviceIdentityForAuthenticatedUser,
+    );
+    console.log("deviceOwnerCheck:", deviceOwnerCheck);
+    console.log("identityOwnerCheck:", identityOwnerCheck);
+
+    if (!deviceIdentityForAuthenticatedUser) {
+      res.status(403).json({ error: "Forbidden: Device access denied" });
+      return;
+    }
+
+    // Check device ownership
+    if (
+      !deviceOwnerCheck ||
+      deviceOwnerCheck.userId !== deviceIdentityForAuthenticatedUser.userId
+    ) {
+      req.log.warn(
+        `User ${deviceIdentityForAuthenticatedUser.userId} attempt to register for unowned/unknown device ${validatedData.deviceId}`,
+      );
+      res.status(403).json({ error: "Forbidden: Device access denied" });
+      return;
+    }
+
+    // Check identity ownership
+    if (
+      !identityOwnerCheck ||
+      identityOwnerCheck.userId !== deviceIdentityForAuthenticatedUser.userId
+    ) {
+      req.log.warn(
+        `User ${deviceIdentityForAuthenticatedUser.userId} attempt to register for unowned/unknown identity ${validatedData.identityId}`,
+      );
+      res.status(403).json({ error: "Forbidden: Identity access denied" });
+      return;
+    }
+
+    await prisma.$transaction([
+      // 1. Update Device the device push tokens
+      prisma.device.update({
+        where: {
+          id: validatedData.deviceId,
+        },
+        data: {
+          expoToken: validatedData.expoToken,
+          pushToken: validatedData.pushToken,
+        },
+      }),
+      // 2. Update IdentitiesOnDevice with the xmtpInstallationId
+      prisma.identitiesOnDevice.upsert({
+        where: {
+          deviceId_identityId: {
+            deviceId: validatedData.deviceId,
+            identityId: validatedData.identityId,
+          },
+        },
+        create: {
+          deviceId: validatedData.deviceId,
+          identityId: validatedData.identityId,
+          xmtpInstallationId: validatedData.xmtpInstallationId,
+        },
+        update: {
+          xmtpInstallationId: validatedData.xmtpInstallationId,
+        },
+      }),
+    ]);
+
+    // 3. Register with the XMTP notification server
     const response = await notificationClient.registerInstallation({
-      installationId: validatedData.installationId,
-      deliveryMechanism: validatedData.deliveryMechanism,
+      installationId: validatedData.xmtpInstallationId,
+      deliveryMechanism: {
+        deliveryMechanismType: {
+          case: "customToken",
+          value: validatedData.expoToken,
+        },
+      },
     });
 
     res.status(201).json({
@@ -35,9 +126,10 @@ export async function registerInstallation(
     });
   } catch (error) {
     if (error instanceof z.ZodError) {
-      res.status(400).json({ error: "Invalid request body" });
+      res.status(400).json({ errors: error.errors });
       return;
     }
+    req.log.error({ error }, "Failed to register installation");
     res.status(500).json({ error: "Failed to register installation" });
   }
 }
diff --git a/src/api/v1/notifications/handlers/unregister-installation.ts b/src/api/v1/notifications/handlers/unregister-installation.ts
index 4cb6a1f9..2dc848f0 100644
--- a/src/api/v1/notifications/handlers/unregister-installation.ts
+++ b/src/api/v1/notifications/handlers/unregister-installation.ts
@@ -1,10 +1,11 @@
 import type { Request, Response } from "express";
 import { z } from "zod";
 import { createNotificationClient } from "@/notifications/client";
+import { prisma } from "@/utils/prisma";
 
 // Schema for unregistration
 const unregisterRequestParamsSchema = z.object({
-  installationId: z.string(),
+  xmtpInstallationId: z.string(),
 });
 
 type UnregisterRequestParams = z.infer<typeof unregisterRequestParamsSchema>;
@@ -16,14 +17,69 @@ export async function unregisterInstallation(
   res: Response,
 ) {
   try {
-    const validatedData = unregisterRequestParamsSchema.parse(req.params);
+    const authenticatedXmtpId = req.app.locals.xmtpId;
 
+    const { xmtpInstallationId } = unregisterRequestParamsSchema.parse(
+      req.params,
+    );
+
+    const [deviceIdentityForUser, identityOnDeviceForInstallation] =
+      await Promise.all([
+        prisma.deviceIdentity.findFirstOrThrow({
+          where: { xmtpId: authenticatedXmtpId },
+          select: { userId: true },
+        }),
+        prisma.identitiesOnDevice.findUnique({
+          where: {
+            xmtpInstallationId: xmtpInstallationId,
+          },
+          include: {
+            device: { select: { userId: true } },
+          },
+        }),
+      ]);
+
+    if (!identityOnDeviceForInstallation) {
+      res.status(404).json({ error: "Installation not found" });
+      return;
+    }
+
+    // Verify that the installation belongs to the authenticated user
+    if (
+      identityOnDeviceForInstallation.device.userId !==
+      deviceIdentityForUser.userId
+    ) {
+      req.log.warn(
+        `User ${deviceIdentityForUser.userId} attempt to unregister unowned installation ${xmtpInstallationId}`,
+      );
+      res.status(403).json({ error: "Forbidden: Installation access denied" });
+      return;
+    }
+
+    // 1. Nullify xmtpInstallationId in our DB
+    await prisma.$transaction([
+      prisma.identitiesOnDevice.update({
+        where: {
+          xmtpInstallationId: xmtpInstallationId,
+        },
+        data: {
+          xmtpInstallationId: null,
+        },
+      }),
+    ]);
+
+    // 2. Delete from XMTP notification server
     await notificationClient.deleteInstallation({
-      installationId: validatedData.installationId,
+      installationId: xmtpInstallationId,
     });
 
-    res.status(200).send();
-  } catch {
+    res.status(200).send("Installation unregistered successfully");
+  } catch (error) {
+    if (error instanceof z.ZodError) {
+      res.status(400).json({ errors: error.errors });
+      return;
+    }
+    req.log.error({ error }, "Failed to unregister installation");
     res.status(500).json({ error: "Failed to delete installation" });
   }
 }
diff --git a/src/api/v1/notifications/notifications.router.ts b/src/api/v1/notifications/notifications.router.ts
index 6df71d0d..94a9a49a 100644
--- a/src/api/v1/notifications/notifications.router.ts
+++ b/src/api/v1/notifications/notifications.router.ts
@@ -11,7 +11,7 @@ notificationsRouter.post("/register", registerInstallation);
 notificationsRouter.post("/subscribe", subscribeToTopics);
 notificationsRouter.post("/unsubscribe", unsubscribeFromTopics);
 notificationsRouter.delete(
-  "/unregister/:installationId",
+  "/unregister/:xmtpInstallationId",
   unregisterInstallation,
 );
 
diff --git a/tests/notifications.test.ts b/tests/notifications.test.ts
index 75eea357..9d42d846 100644
--- a/tests/notifications.test.ts
+++ b/tests/notifications.test.ts
@@ -1,8 +1,10 @@
 import type { Server } from "http";
+import { DeviceOS } from "@prisma/client";
 import {
   afterAll,
   afterEach,
   beforeAll,
+  beforeEach,
   describe,
   expect,
   test,
@@ -11,95 +13,217 @@ import express from "express";
 import { uint8ArrayToHex } from "uint8array-extras";
 import { notificationsRouter } from "@/api/v1/notifications/notifications.router";
 import { jsonMiddleware } from "@/middleware/json";
+import { pinoMiddleware } from "@/middleware/pino";
 import { createNotificationClient } from "@/notifications/client";
 import type { RegisterInstallationResponse } from "@/notifications/gen/notifications/v1/service_pb";
+import { prisma } from "@/utils/prisma";
 
 const app = express();
 app.use(jsonMiddleware);
+app.use(pinoMiddleware);
+
+const AUTH_XMTP_ID = "test-auth-xmtp-id";
+
+app.use((req, res, next) => {
+  req.app.locals.xmtpId = AUTH_XMTP_ID;
+  next();
+});
+
 app.use("/notifications", notificationsRouter);
 
 let server: Server;
-
-const testInstallationId = "test-installation-id";
 const notificationClient = createNotificationClient();
 
+const genericTestInstallationId1 = "test-install-id-1-for-subs";
+const genericTestInstallationId2 = "test-install-id-2-for-meta-subs";
+const genericTestInstallationId3 = "test-install-id-3-for-unsub";
+const genericTestInstallationId4 = "test-install-id-4-for-unsub-validate";
+
 beforeAll(() => {
   server = app.listen(3008);
 });
 
-afterAll(() => {
+afterAll(async () => {
+  await prisma.$disconnect();
   server.close();
 });
 
-afterEach(async () => {
-  await notificationClient.deleteInstallation({
-    installationId: testInstallationId,
+describe("/notifications API - Register/Unregister (Auth Required)", () => {
+  let testUserId: string;
+  let testDeviceId: string;
+  let testIdentityId: string;
+  const testUserTurnkeyId = "reg-unreg-turnkey-user-id";
+  const testDeviceName = "RegUnreg Test Device";
+  const testIdentityTurnkeyAddress = "reg-unreg-turnkey-address";
+
+  beforeEach(async () => {
+    await prisma.identitiesOnDevice.deleteMany();
+    await prisma.profile.deleteMany();
+    await prisma.conversationMetadata.deleteMany();
+    await prisma.deviceIdentity.deleteMany();
+    await prisma.device.deleteMany();
+    await prisma.user.deleteMany();
+
+    const user = await prisma.user.create({
+      data: { turnkeyUserId: testUserTurnkeyId },
+    });
+    testUserId = user.id;
+
+    const identity = await prisma.deviceIdentity.create({
+      data: {
+        userId: testUserId,
+        xmtpId: AUTH_XMTP_ID,
+        turnkeyAddress: testIdentityTurnkeyAddress,
+      },
+    });
+    testIdentityId = identity.id;
+
+    const device = await prisma.device.create({
+      data: { userId: testUserId, name: testDeviceName, os: DeviceOS.ios },
+    });
+    testDeviceId = device.id;
+
+    await prisma.identitiesOnDevice.create({
+      data: { deviceId: testDeviceId, identityId: testIdentityId },
+    });
+  });
+
+  afterEach(async () => {
+    try {
+      await notificationClient.deleteInstallation({
+        installationId: "test-installation-id-register",
+      });
+      await notificationClient.deleteInstallation({
+        installationId: "test-installation-id-unregister-setup",
+      });
+      await notificationClient.deleteInstallation({
+        installationId: "test-forbidden-device-install-id",
+      });
+    } catch {
+      /* ignore */
+    }
   });
-});
 
-describe("/notifications API", () => {
   test("POST /notifications/register creates a new installation", async () => {
+    const testXmtpInstallationId = "test-installation-id-register";
     const response = await fetch(
       "http://localhost:3008/notifications/register",
       {
         method: "POST",
-        headers: {
-          "Content-Type": "application/json",
-        },
+        headers: { "Content-Type": "application/json" },
         body: JSON.stringify({
-          installationId: testInstallationId,
-          deliveryMechanism: {
-            deliveryMechanismType: {
-              case: "apnsDeviceToken",
-              value: "test-device-token",
-            },
-          },
+          deviceId: testDeviceId,
+          identityId: testIdentityId,
+          xmtpInstallationId: testXmtpInstallationId,
+          expoToken: "test-expo-token-register",
+          pushToken: "test-push-token-register",
         }),
       },
     );
 
     expect(response.status).toBe(201);
     const data = (await response.json()) as RegisterInstallationResponse;
-    expect(data).toBeDefined();
-    expect(data.installationId).toBe(testInstallationId);
-    expect(data.validUntil).toBeGreaterThan(0);
+    expect(data.installationId).toBe(testXmtpInstallationId);
+
+    const iod = await prisma.identitiesOnDevice.findUnique({
+      where: {
+        deviceId_identityId: {
+          deviceId: testDeviceId,
+          identityId: testIdentityId,
+        },
+      },
+    });
+    expect(iod?.xmtpInstallationId).toBe(testXmtpInstallationId);
+    const device = await prisma.device.findUnique({
+      where: { id: testDeviceId },
+    });
+    expect(device?.expoToken).toBe("test-expo-token-register");
   });
 
-  test("POST /notifications/register validates request body", async () => {
+  test("POST /notifications/register validates request body (missing fields)", async () => {
     const response = await fetch(
       "http://localhost:3008/notifications/register",
       {
         method: "POST",
-        headers: {
-          "Content-Type": "application/json",
-        },
+        headers: { "Content-Type": "application/json" },
+        body: JSON.stringify({ deviceId: testDeviceId }),
+      },
+    );
+    expect(response.status).toBe(400);
+    const data = (await response.json()) as { errors: unknown };
+    expect(data.errors).toBeDefined();
+  });
+
+  test("POST /notifications/register returns 403 if device not owned by authenticated user", async () => {
+    const response = await fetch(
+      "http://localhost:3008/notifications/register",
+      {
+        method: "POST",
+        headers: { "Content-Type": "application/json" },
         body: JSON.stringify({
-          installationId: testInstallationId,
-          // Missing required deliveryMechanism
+          deviceId: testDeviceId,
+          identityId: "wrong-identity-id",
+          xmtpInstallationId: "test-installation-id-register",
+          expoToken: "test-expo-token-forbidden",
+          pushToken: "test-push-token-forbidden",
         }),
       },
     );
+    expect(response.status).toBe(403);
+  });
 
-    expect(response.status).toBe(400);
-    const data = (await response.json()) as { error: string };
-    expect(data.error).toBe("Invalid request body");
+  test("DELETE /notifications/unregister/:xmtpInstallationId deletes an installation", async () => {
+    const xmtpInstallationIdToTest = "test-installation-id-unregister-setup";
+    const regResponse = await fetch(
+      "http://localhost:3008/notifications/register",
+      {
+        method: "POST",
+        headers: { "Content-Type": "application/json" },
+        body: JSON.stringify({
+          deviceId: testDeviceId,
+          identityId: testIdentityId,
+          xmtpInstallationId: xmtpInstallationIdToTest,
+          expoToken: "token-for-unregister",
+          pushToken: "token-for-unregister-push",
+        }),
+      },
+    );
+
+    expect(regResponse.status).toBe(201);
+
+    const unregResponse = await fetch(
+      `http://localhost:3008/notifications/unregister/${xmtpInstallationIdToTest}`,
+      {
+        method: "DELETE",
+      },
+    );
+    expect(unregResponse.status).toBe(200);
+
+    const iod = await prisma.identitiesOnDevice.findUnique({
+      where: {
+        deviceId_identityId: {
+          deviceId: testDeviceId,
+          identityId: testIdentityId,
+        },
+      },
+    });
+    expect(iod?.xmtpInstallationId).toBeNull();
   });
+});
 
+describe("/notifications API - Subscribe/Unsubscribe (No local DB auth needed, using XMTP installationId)", () => {
   test("POST /notifications/subscribe handles simple topic subscription", async () => {
     const response = await fetch(
       "http://localhost:3008/notifications/subscribe",
       {
         method: "POST",
-        headers: {
-          "Content-Type": "application/json",
-        },
+        headers: { "Content-Type": "application/json" },
         body: JSON.stringify({
-          installationId: testInstallationId,
+          installationId: genericTestInstallationId1,
           topics: ["test-topic-1", "test-topic-2"],
         }),
       },
     );
-
     expect(response.status).toBe(200);
   });
 
@@ -108,11 +232,9 @@ describe("/notifications API", () => {
       "http://localhost:3008/notifications/subscribe",
       {
         method: "POST",
-        headers: {
-          "Content-Type": "application/json",
-        },
+        headers: { "Content-Type": "application/json" },
         body: JSON.stringify({
-          installationId: testInstallationId,
+          installationId: genericTestInstallationId2,
           subscriptions: [
             {
               topic: "test-topic-1",
@@ -138,7 +260,6 @@ describe("/notifications API", () => {
         }),
       },
     );
-
     expect(response.status).toBe(200);
   });
 
@@ -147,67 +268,47 @@ describe("/notifications API", () => {
       "http://localhost:3008/notifications/unsubscribe",
       {
         method: "POST",
-        headers: {
-          "Content-Type": "application/json",
-        },
+        headers: { "Content-Type": "application/json" },
         body: JSON.stringify({
-          installationId: testInstallationId,
+          installationId: genericTestInstallationId3,
           topics: ["test-topic-1", "test-topic-2"],
         }),
       },
     );
-
-    expect(response.status).toBe(200);
-  });
-
-  test("DELETE /notifications/unregister/:installationId deletes an installation", async () => {
-    const response = await fetch(
-      `http://localhost:3008/notifications/unregister/${testInstallationId}`,
-      {
-        method: "DELETE",
-      },
-    );
-
     expect(response.status).toBe(200);
   });
 
-  test("POST /notifications/subscribe validates request body", async () => {
+  test("POST /notifications/subscribe validates request body for missing installationId", async () => {
     const response = await fetch(
       "http://localhost:3008/notifications/subscribe",
       {
         method: "POST",
-        headers: {
-          "Content-Type": "application/json",
-        },
-        body: JSON.stringify({
-          // Missing required installationId
-          topics: ["test-topic"],
-        }),
+        headers: { "Content-Type": "application/json" },
+        body: JSON.stringify({ topics: ["test-topic"] }),
       },
     );
-
     expect(response.status).toBe(400);
-    const data = (await response.json()) as { error: string };
-    expect(data.error).toBe("Invalid request body");
+    const data = (await response.json()) as {
+      errors?: unknown;
+      error?: string;
+    };
+    expect(data.errors ?? data.error).toBeDefined();
   });
 
-  test("POST /notifications/unsubscribe validates request body", async () => {
+  test("POST /notifications/unsubscribe validates request body for missing topics", async () => {
     const response = await fetch(
       "http://localhost:3008/notifications/unsubscribe",
       {
         method: "POST",
-        headers: {
-          "Content-Type": "application/json",
-        },
-        body: JSON.stringify({
-          installationId: testInstallationId,
-          // Missing required topics array
-        }),
+        headers: { "Content-Type": "application/json" },
+        body: JSON.stringify({ installationId: genericTestInstallationId4 }),
       },
     );
-
     expect(response.status).toBe(400);
-    const data = (await response.json()) as { error: string };
-    expect(data.error).toBe("Invalid request body");
+    const data = (await response.json()) as {
+      errors?: unknown;
+      error?: string;
+    };
+    expect(data.errors ?? data.error).toBeDefined();
   });
 });


Reviews:
user: login: coderabbitai[bot]
body: **Actionable comments posted: 6**

<details>
<summary>🧹 Nitpick comments (7)</summary><blockquote>

<details>
<summary>prisma/migrations/20250513202135_add_xmtp_installation_id_on_identities_on_devices/migration.sql (1)</summary><blockquote>

`8-11`: **Add a down-migration or mention rollback strategy**

The migration only contains an `ALTER TABLE … ADD COLUMN` and `CREATE UNIQUE INDEX`. If you ever need to roll back, `prisma migrate reset` will drop the entire DB.  
For production environments that run reversible migrations, consider adding a companion `DROP INDEX` and `ALTER TABLE … DROP COLUMN` in the generated “down” step or documenting the rollback process in the repo.

</blockquote></details>
<details>
<summary>src/api/v1/notifications/handlers/register-installation.ts (2)</summary><blockquote>

`93-101`: **Push only the token type that matches the delivery mechanism**

`deliveryMechanismType.case: "customToken"` uses `expoToken`, but `pushToken` remains unused.  
If both tokens are stored, choose the correct one dynamically or document why only Expo is supported.

---

`109-113`: **Surface validation & DB errors distinctly**

You already special-case `ZodError`. Extending this to Prisma error codes (e.g. `P2002`, `P2025`) will give clients clearer feedback and reduce noisy logs.

</blockquote></details>
<details>
<summary>src/api/v1/notifications/handlers/unregister-installation.ts (3)</summary><blockquote>

`8-9`: **Tighten parameter validation early**  

`xmtpInstallationId` looks like a UUID v4. Validating that format up-front prevents malformed requests from hitting the DB:

```diff
-  xmtpInstallationId: z.string(),
+  xmtpInstallationId: z.string().uuid(),
```

---

`54-63`: **Unnecessary single-statement transaction**  

`prisma.$transaction` adds overhead when wrapping just one update. Unless you intend to batch more ops here, replace with a plain `update` call:

```diff
-await prisma.$transaction([
-  prisma.identitiesOnDevice.update({ … })
-]);
+await prisma.identitiesOnDevice.update({ … });
```

---

`66-71`: **Prefer a JSON response for consistency**  

Most endpoints return structured JSON. Responding with a bare string forces clients to special-case the handler.

```diff
-res.status(200).send("Installation unregistered successfully");
+res.status(200).json({ message: "Installation unregistered successfully" });
```

</blockquote></details>
<details>
<summary>src/api/v1/notifications/handlers/handle-xmtp-notification.ts (1)</summary><blockquote>

`146-188`: **Push-ticket loop is sequential; consider parallellising for latency**  

The `for … of` loop awaits `expo.sendPushNotificationsAsync` for each chunk serially. With high traffic, this can become a bottleneck. You can fire off all chunk promises and `await Promise.all()` instead:

```diff
- for (const chunk of chunks) {
-   try {
-     const tickets = await expo.sendPushNotificationsAsync(chunk);
-     …
-   } catch (error) {
-     …
-   }
- }
+ await Promise.all(
+   chunks.map(async (chunk) => {
+     try {
+       const tickets = await expo.sendPushNotificationsAsync(chunk);
+       …
+     } catch (error) {
+       …
+     }
+   }),
+ );
```

This maintains error handling while improving throughput.

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

**Configuration used: CodeRabbit UI**
**Review profile: CHILL**
**Plan: Pro**

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between cf5e88c3c112ead6ef81e4271c720708c56c93b5 and bfeb42c056e12606d171807ae34f9a89acfb1955.

</details>

<details>
<summary>📒 Files selected for processing (5)</summary>

* `prisma/migrations/20250513202135_add_xmtp_installation_id_on_identities_on_devices/migration.sql` (1 hunks)
* `prisma/schema.prisma` (1 hunks)
* `src/api/v1/notifications/handlers/handle-xmtp-notification.ts` (4 hunks)
* `src/api/v1/notifications/handlers/register-installation.ts` (3 hunks)
* `src/api/v1/notifications/handlers/unregister-installation.ts` (2 hunks)

</details>

<details>
<summary>🧰 Additional context used</summary>

<details>
<summary>🧬 Code Graph Analysis (2)</summary>

<details>
<summary>src/api/v1/notifications/handlers/register-installation.ts (1)</summary><blockquote>

<details>
<summary>src/utils/prisma.ts (1)</summary>

* `prisma` (3-3)

</details>

</blockquote></details>
<details>
<summary>src/api/v1/notifications/handlers/unregister-installation.ts (1)</summary><blockquote>

<details>
<summary>src/utils/prisma.ts (1)</summary>

* `prisma` (3-3)

</details>

</blockquote></details>

</details>

</details>

<details>
<summary>🔇 Additional comments (2)</summary><blockquote>

<details>
<summary>src/api/v1/notifications/handlers/handle-xmtp-notification.ts (2)</summary>

`58-67`: **Potential field-selection issue for `deviceId`**  

Later (line 90) the code accesses `identityOnDevice.deviceId`, but the `include` only brings in the nested `device` relation. Unless `deviceId` is a scalar field on the same record (and not shadowed by the relation), you may receive `undefined`.

```diff
include: {
  device: { select: { id: true, expoToken: true, userId: true } },
  identity: { select: { id: true, turnkeyAddress: true } },
},
```

Explicitly selecting what you need avoids accidental `undefined` and reduces payload size.

---

`225-233`: **`updateMany` may mask data-integrity issues**  

`xmtpInstallationId` is declared `@unique`; therefore at most one row is affected. Using `updateMany` hides the expectation that exactly one row should change. Switching to `update` will surface mistakes (e.g., if the row was already cleaned up) via an exception.

```diff
- prisma.identitiesOnDevice.updateMany({
-   where: { xmtpInstallationId },
-   data: { xmtpInstallationId: null },
- }),
+ prisma.identitiesOnDevice.update({
+   where: { xmtpInstallationId },
+   data: { xmtpInstallationId: null },
+ }),
```

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-13T22:34:59Z
user: login: neekolas
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-05-13T22:54:20Z
user: login: thierryskoda
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-13T23:01:02Z
user: login: neekolas
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-05-13T23:03:58Z
user: login: thierryskoda
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-14T02:26:38Z
user: login: coderabbitai[bot]
body: **Actionable comments posted: 0**

<details>
<summary>♻️ Duplicate comments (1)</summary><blockquote>

<details>
<summary>src/api/v1/notifications/handlers/unregister-installation.ts (1)</summary><blockquote>

`42-57`: **Robust installation validation and authorization.**

The authorization check now correctly compares the device's userId with the authenticated user's userId, fixing the previously identified issues from past reviews. Good job adding detailed logging and clear error messages.

</blockquote></details>

</blockquote></details>

<details>
<summary>🧹 Nitpick comments (2)</summary><blockquote>

<details>
<summary>src/api/v1/notifications/handlers/unregister-installation.ts (1)</summary><blockquote>

`59-69`: **Consider simplifying the transaction.**

The transaction currently contains only a single operation, which doesn't leverage transactional benefits. Unless additional operations will be added later, this could be simplified to a direct update.

```diff
-    // 1. Nullify xmtpInstallationId in our DB
-    await prisma.$transaction([
-      prisma.identitiesOnDevice.update({
-        where: {
-          xmtpInstallationId: xmtpInstallationId,
-        },
-        data: {
-          xmtpInstallationId: null,
-        },
-      }),
-    ]);
+    // 1. Nullify xmtpInstallationId in our DB
+    await prisma.identitiesOnDevice.update({
+      where: {
+        xmtpInstallationId: xmtpInstallationId,
+      },
+      data: {
+        xmtpInstallationId: null,
+      },
+    });
```

</blockquote></details>
<details>
<summary>src/api/v1/notifications/handlers/handle-xmtp-notification.ts (1)</summary><blockquote>

`150-191`: **Consider adding retry logic for failed notifications.**

While the error handling is robust, the code does not attempt to retry sending notifications that fail for transient reasons.

```diff
 for (const chunk of chunks) {
   try {
     const tickets = await expo.sendPushNotificationsAsync(chunk);

     for (const ticket of tickets) {
       const expoPushReceipt = ticket;

       if (expoPushReceipt.status === "error") {
         req.log.error(
           {
             details: expoPushReceipt.details,
             xmtpInstallationId: notification.installation.id,
           },
           `Error sending push notification: ${expoPushReceipt.message}`,
         );

+        // Skip retry for DeviceNotRegistered errors
         if (
           expoPushReceipt.details &&
           expoPushReceipt.details.error === "DeviceNotRegistered"
         ) {
           req.log.info(
             `DeviceNotRegistered error for token ${expoPushToken} (xmtpInstallationId ${notification.installation.id}). Initiating cleanup.`,
           );

           if (identityOnDeviceToCleanup.xmtpInstallationId) {
             await cleanupFailedInstallation({
               xmtpInstallationId:
                 identityOnDeviceToCleanup.xmtpInstallationId,
               deviceId: identityOnDeviceToCleanup.deviceId,
               req,
             });
           }
+        } else if (
+          expoPushReceipt.details &&
+          ["MessageTooBig", "MessageRateExceeded", "Unregistered"].includes(expoPushReceipt.details.error)
+        ) {
+          // These are transient errors that might succeed on retry
+          req.log.info(
+            `Transient error ${expoPushReceipt.details.error} for token ${expoPushToken}. Will retry.`,
+          );
+          // Implement retry logic here
         }
       }
     }
   } catch (error) {
     req.log.error(
       { error, xmtpInstallationId: notification.installation.id },
       "Critical error sending push notifications chunk",
     );
   }
 }
```

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

**Configuration used: CodeRabbit UI**
**Review profile: CHILL**
**Plan: Pro**

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between f70533dd8054c12f3b0822f50b3c9c64e13a67f2 and e27ccb13531f2cd18a2a04ecd2c402ab49e2f5f1.

</details>

<details>
<summary>📒 Files selected for processing (3)</summary>

* `src/api/v1/notifications/handlers/handle-xmtp-notification.ts` (4 hunks)
* `src/api/v1/notifications/handlers/register-installation.ts` (3 hunks)
* `src/api/v1/notifications/handlers/unregister-installation.ts` (2 hunks)

</details>

<details>
<summary>🚧 Files skipped from review as they are similar to previous changes (1)</summary>

* src/api/v1/notifications/handlers/register-installation.ts

</details>

<details>
<summary>🧰 Additional context used</summary>

<details>
<summary>🧬 Code Graph Analysis (2)</summary>

<details>
<summary>src/api/v1/notifications/handlers/unregister-installation.ts (1)</summary><blockquote>

<details>
<summary>src/utils/prisma.ts (1)</summary>

* `prisma` (3-3)

</details>

</blockquote></details>
<details>
<summary>src/api/v1/notifications/handlers/handle-xmtp-notification.ts (2)</summary><blockquote>

<details>
<summary>src/notifications/client.ts (1)</summary>

* `createNotificationClient` (12-21)

</details>
<details>
<summary>src/utils/prisma.ts (1)</summary>

* `prisma` (3-3)

</details>

</blockquote></details>

</details>

</details>

<details>
<summary>⏰ Context from checks skipped due to timeout of 90000ms (2)</summary>

* GitHub Check: push_to_registry
* GitHub Check: Typecheck, format check, lint

</details>

<details>
<summary>🔇 Additional comments (16)</summary><blockquote>

<details>
<summary>src/api/v1/notifications/handlers/unregister-installation.ts (6)</summary>

`4-4`: **Appropriate import for database access.**

Adding the Prisma client import is necessary for direct database interactions.

---

`8-8`: **Parameter name update aligns with system changes.**

Changing from `installationId` to `xmtpInstallationId` properly aligns with the new database schema and system design.

---

`20-24`: **Improved parameter handling with proper validation.**

The code now correctly retrieves the authenticated XMTP ID from request locals and validates input parameters using Zod schema parsing.

---

`26-40`: **Efficient concurrent database lookups with proper field selection.**

Using Promise.all for parallel database queries improves efficiency. The device identity query now correctly selects only the needed userId field, and the installation query properly includes the device's userId for authorization checking.

---

`71-74`: **Proper external service notification.**

Correctly notifies the XMTP notification server after updating the local database, with the parameter name matching the schema change.

---

`76-84`: **Enhanced error handling with proper status codes.**

The error handling now includes specific handling for validation errors with appropriate status codes and logging.

</details>
<details>
<summary>src/api/v1/notifications/handlers/handle-xmtp-notification.ts (10)</summary>

`3-6`: **Improved type safety with explicit imports.**

Explicitly importing the NotificationResponse type improves type safety and code clarity.

---

`11-11`: **Efficiently initialized notification client.**

The notification client is now correctly initialized at the module level for reuse.

---

`25-28`: **Well-structured tracking variable for cleanup.**

Good practice to declare this tracking variable outside the try block to ensure it's in scope for any cleanup operations that might need to happen in catch blocks or after error conditions.

---

`60-68`: **Efficient database query with xmtpInstallationId.**

The updated query correctly uses xmtpInstallationId as the lookup key and includes both device and identity information in a single query, improving efficiency.

---

`70-89`: **Robust handling for missing installations.**

The code now properly handles cases where the installation is not found in the database, attempting to clean up orphaned installations from the XMTP server with appropriate error handling and logging.

---

`91-99`: **Improved data extraction for potential cleanup.**

Good practice to store a reference to the installation for potential cleanup and to destructure the device and identity properties for cleaner code.

---

`108-121`: **Enhanced token validation with automatic cleanup.**

The code now properly validates Expo push tokens and initiates cleanup for invalid tokens, improving system reliability.

---

`149-191`: **More robust push notification handling with sequential processing.**

Changed from parallel promise mapping to sequential loop processing, with detailed error handling for each push ticket. The specific handling of "DeviceNotRegistered" errors with automatic cleanup is particularly valuable.

---

`200-237`: **Well-structured reusable cleanup function.**

The new cleanup function centralizes the logic for handling failed installations, using transactions for data consistency and including comprehensive logging and error handling.

---

`207-220`: <details>
<summary>✅ Verification successful</summary>

**Ensure transaction atomicity for database cleanup.**

The transaction is well-structured, but consider what happens if the second update fails - would it be better to rollback both operations or allow partial cleanup?

---

🏁 Script executed:

```shell
#!/bin/bash
# Description: Checking whether updateMany and update operations can fail independently within a transaction

# Check Prisma's transaction documentation
rg -A 3 -B 3 "\$transaction" --glob "*.{ts,md}" | grep -v "node_modules"
```

Length of output: 71

---

🌐 Web query:

```web
Does Prisma’s $transaction method guarantee atomicity by rolling back all operations if any query within the transaction fails?
```

💡 Result:

Yes, Prisma's `$transaction` method guarantees atomicity. This means that if any query within the transaction fails, all operations within that transaction are rolled back, ensuring that none of the partial changes are applied to the database. This atomic behavior ensures data consistency by treating the group of queries as a single unit of work—either all succeed or none take effect.

---



</details>

**Atomicity Confirmed with Prisma $transaction**

Prisma’s `$transaction` method is atomic: if any query (e.g., the second update) fails, all operations in the transaction are rolled back, preventing partial cleanup. No further changes are required here—just ensure you catch and handle any errors from the transaction as needed.

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-14T02:30:57Z
user: login: neekolas
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-05-14T02:38:03Z
user: login: neekolas
state: APPROVED
author_association: COLLABORATOR
submitted_at: 2025-05-14T02:38:13Z
user: login: coderabbitai[bot]
body: **Actionable comments posted: 1**

<details>
<summary>♻️ Duplicate comments (1)</summary><blockquote>

<details>
<summary>tests/notifications.test.ts (1)</summary><blockquote>

`116-118`: **Same unused-variable issue here**

Apply the same change as above to keep the test file lint-clean.

<details>
<summary>🧰 Tools</summary>

<details>
<summary>🪛 ESLint</summary>

[error] 116-116: 'error' is defined but never used.

(@typescript-eslint/no-unused-vars)

</details>
<details>
<summary>🪛 GitHub Actions: Validate code quality</summary>

[error] 116-116: ESLint: 'error' is defined but never used. (@typescript-eslint/no-unused-vars)

</details>

</details>

</blockquote></details>

</blockquote></details>

<details>
<summary>🧹 Nitpick comments (3)</summary><blockquote>

<details>
<summary>tests/notifications.test.ts (3)</summary><blockquote>

`25-29`: **Prefer `res.locals` for per-request data**

`app.locals` is a global object shared by every request, so in a concurrent test run (or if this helper gets copied into production code) requests could overwrite each other’s `xmtpId`. Using `res.locals` (or a dedicated request-scoped property) avoids that risk:

```diff
-app.use((req, res, next) => {
-  req.app.locals.xmtpId = AUTH_XMTP_ID;
+app.use((req, res, next) => {
+  res.locals.xmtpId = AUTH_XMTP_ID;
   next();
 });
```

---

`41-43`: **Avoid hard-coding port 3008 in tests**

Binding to a fixed port can cause flaky CI failures if the port is busy. Let the OS choose a free port and derive it for the fetch calls:

```ts
beforeAll(() => {
  server = app.listen(0);            // 0 ⇒ dynamic port
  baseUrl = `http://localhost:${(server.address() as AddressInfo).port}`;
});
```

Then replace hard-coded `http://localhost:3008` with `${baseUrl}` throughout the file (a small find-and-replace).  
This keeps the suite parallel-friendly.

---

`49-60`: **Minor: fire cleanup requests concurrently**

The four `deleteInstallation` calls are independent; wrapping them in `Promise.allSettled` shortens teardown time and keeps the code terse:

```diff
-try {
-  await notificationClient.deleteInstallation({ installationId: genericTestInstallationId1 });
-  await notificationClient.deleteInstallation({ installationId: genericTestInstallationId2 });
-  await notificationClient.deleteInstallation({ installationId: genericTestInstallationId3 });
-  await notificationClient.deleteInstallation({ installationId: genericTestInstallationId4 });
-} catch {
-  /* ignore */
-}
+await Promise.allSettled(
+  [genericTestInstallationId1, genericTestInstallationId2,
+   genericTestInstallationId3, genericTestInstallationId4].map((id) =>
+     notificationClient.deleteInstallation({ installationId: id }),
+  ),
+);
```
Purely a speed/readability tweak – optional.

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

**Configuration used: CodeRabbit UI**
**Review profile: CHILL**
**Plan: Pro**

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between e27ccb13531f2cd18a2a04ecd2c402ab49e2f5f1 and 7c9576313c007cfcd29be2854fa1bc295db113d7.

</details>

<details>
<summary>📒 Files selected for processing (3)</summary>

* `src/api/v1/notifications/handlers/register-installation.ts` (3 hunks)
* `src/api/v1/notifications/notifications.router.ts` (1 hunks)
* `tests/notifications.test.ts` (4 hunks)

</details>

<details>
<summary>🚧 Files skipped from review as they are similar to previous changes (1)</summary>

* src/api/v1/notifications/handlers/register-installation.ts

</details>

<details>
<summary>🧰 Additional context used</summary>

<details>
<summary>🧬 Code Graph Analysis (1)</summary>

<details>
<summary>tests/notifications.test.ts (5)</summary><blockquote>

<details>
<summary>src/middleware/json.ts (1)</summary>

* `jsonMiddleware` (3-3)

</details>
<details>
<summary>src/middleware/pino.ts (1)</summary>

* `pinoMiddleware` (5-24)

</details>
<details>
<summary>src/notifications/client.ts (1)</summary>

* `createNotificationClient` (12-21)

</details>
<details>
<summary>src/utils/prisma.ts (1)</summary>

* `prisma` (3-3)

</details>
<details>
<summary>src/notifications/gen/notifications/v1/service_pb.ts (1)</summary>

* `RegisterInstallationResponse` (81-91)

</details>

</blockquote></details>

</details><details>
<summary>🪛 ESLint</summary>

<details>
<summary>tests/notifications.test.ts</summary>

[error] 61-61: 'error' is defined but never used.

(@typescript-eslint/no-unused-vars)

---

[error] 116-116: 'error' is defined but never used.

(@typescript-eslint/no-unused-vars)

</details>

</details>
<details>
<summary>🪛 GitHub Actions: Validate code quality</summary>

<details>
<summary>tests/notifications.test.ts</summary>

[error] 61-61: ESLint: 'error' is defined but never used. (@typescript-eslint/no-unused-vars)

---

[error] 116-116: ESLint: 'error' is defined but never used. (@typescript-eslint/no-unused-vars)

</details>

</details>

</details>

<details>
<summary>⏰ Context from checks skipped due to timeout of 90000ms (1)</summary>

* GitHub Check: push_to_registry

</details>

<details>
<summary>🔇 Additional comments (1)</summary><blockquote>

<details>
<summary>src/api/v1/notifications/notifications.router.ts (1)</summary>

`14-15`: **Route-param rename looks good – double-check handler & docs**

Renaming the parameter to `:xmtpInstallationId` keeps the public contract consistent with the new schema.  Just make sure:
• `unregisterInstallation` reads the param under the same key (`req.params.xmtpInstallationId`).  
• Any API docs / client SDKs are updated to avoid breaking consumers.

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-14T03:05:06Z
user: login: macroscopeapp[bot]
state: COMMENTED
author_association: NONE
submitted_at: 2025-05-14T03:16:45Z
user: login: nplasterer
state: COMMENTED
author_association: NONE
submitted_at: 2025-05-14T14:15:05Z

Review comments:
diff_hunk: @@ -66,6 +66,7 @@ model IdentitiesOnDevice {
   device     Device         @relation(fields: [deviceId], references: [id])
   identityId String
   identity   DeviceIdentity @relation(fields: [identityId], references: [id])
+  xmtpInstallationId String?    @unique
 
path: prisma/schema.prisma
user: login: coderabbitai[bot]
body: _💡 Verification agent_

<details>
<summary>🧩 Analysis chain</summary>

**Uniqueness on a nullable column can surface unexpected runtime conflicts**

`xmtpInstallationId` is declared `String? @unique`, which means:

* Multiple `NULL`s are allowed (PostgreSQL semantics), but **any non-null value must be globally unique** across the join table.
* `register-installation.ts` updates this field without first checking whether it is already in use by another row. A concurrent request (or a hijacked account) that tries to reuse the same installation ID will raise a `P2002` (unique-constraint) error and surface to the caller as a 500.

Consider adding an explicit pre-check (or catching `P2002` and returning 409/422) so the API communicates a deterministic, client-actionable error instead of “internal server error”.

---

I’ll locate and inspect the `register-installation.ts` implementation to confirm whether it safely handles unique-constraint errors on `xmtpInstallationId`.

---

🏁 Script executed:

```shell
#!/bin/bash
# Locate register-installation.ts and inspect its handling of xmtpInstallationId and P2002
FILE=$(fd register-installation.ts)
echo "Found file: $FILE"
rg -n "xmtpInstallationId" "$FILE" || echo "No xmtpInstallationId occurrences"
rg -n "update" "$FILE" || echo "No update operations found"
rg -n "catch" "$FILE" || echo "No catch blocks found"
rg -n "P2002" "$FILE" || echo "No P2002 handling found"
```

Length of output: 1880

---

Let’s target only the register handler and look for any Prisma write + error handling around `xmtpInstallationId`:

---

🏁 Script executed:

```shell
#!/bin/bash
# Find the exact register-installation.ts file(s)
FILES=$(fd --exact-name register-installation.ts)

for FILE in $FILES; do
  echo "== Inspecting $FILE =="
  # Look for any create/update/upsert operations
  rg -n "prisma\.[a-zA-Z_]\+\.\(create\|update\|upsert\)" "$FILE" || echo "  → No Prisma write ops found"
  # Check for any try/catch blocks
  rg -n "try" "$FILE" && rg -n "catch" "$FILE" || echo "  → No try/catch around writes"
  # Check for explicit P2002 handling
  rg -n "P2002" "$FILE" || echo "  → No P2002 error handling"
done
```

Length of output: 264

---

Let’s retry locating and inspecting the register handler with a more robust script:

---

🏁 Script executed:

```shell
#!/bin/bash
# Find all register-installation.ts files and inspect Prisma writes and error handling
fd register-installation.ts | tr ' ' '\n' | while read -r FILE; do
  echo "== Inspecting $FILE =="
  # Prisma write operations
  rg -n "prisma\.\w+\.(create|update|upsert)" "$FILE" || echo "  → No Prisma write ops found"
  # Any try/catch blocks
  rg -n "try\s*{" "$FILE" || echo "  → No try blocks found"
  rg -n "catch\s*\(" "$FILE" || echo "  → No catch blocks found"
  # Explicit P2002 error handling
  rg -n "P2002" "$FILE" || echo "  → No P2002 handling found"
done
```

Length of output: 1659

---

Let’s confirm whether `register-installation.ts` actually touches `xmtpInstallationId` and see the exact Prisma operations:

---

🏁 Script executed:

```shell
#!/bin/bash
# 1. Global search for xmtpInstallationId references
echo "== All occurrences of xmtpInstallationId =="
rg -n "xmtpInstallationId" .

# 2. Inspect key Prisma writes in register-installation.ts
FILE="src/api/v1/notifications/handlers/register-installation.ts"
echo -e "\n== Upsert on identitiesOnDevice =="
rg -n -C5 "prisma\.identitiesOnDevice\.upsert" "$FILE"
echo -e "\n== Update on device =="
rg -n -C5 "prisma\.device\.update" "$FILE"
echo -e "\n== Catch block around writes =="
rg -n -C5 "catch\s*\(" "$FILE"
```

Length of output: 8528

---



</details>

**Handle unique-constraint errors on xmtpInstallationId in register-installation.ts**

The `prisma/schema.prisma` file defines:

- `xmtpInstallationId String? @unique`  
  → Any non-null value must be globally unique; concurrent writes of the same ID will trigger a `P2002` error.

In `src/api/v1/notifications/handlers/register-installation.ts`:

- Around lines 74–93, you call  
  ```ts
  prisma.identitiesOnDevice.upsert({
    where: { deviceId_identityId: { deviceId, identityId } },
    create: { …, xmtpInstallationId: validatedData.xmtpInstallationId },
    update: { …, xmtpInstallationId: validatedData.xmtpInstallationId },
  })
  ```
- There is a catch block at lines 108–113, but it only handles `ZodError` and returns 500 for all other errors.

Recommendation:
- Wrap your Prisma write in a dedicated `try/catch`, detect `error.code === 'P2002'` for the `xmtpInstallationId` constraint, and return a 409 or 422 with a clear message.
- (Optional) Before upserting, perform a `findUnique` on `identitiesOnDevice` for the given `xmtpInstallationId` to surface conflicts proactively.

This will prevent leaking database errors as 500s and give clients a deterministic, actionable response.

<!-- This is an auto-generated comment by CodeRabbit -->
created_at: 2025-05-13T22:34:58Z
author_association: CONTRIBUTOR
diff_hunk: @@ -22,11 +20,85 @@ export async function registerInstallation(
   res: Response,
 ) {
   try {
+    const authenticatedXmtpId = req.app.locals.xmtpId;
+
+    const authenticatedUser = await prisma.deviceIdentity.findFirstOrThrow({
+      where: { xmtpId: authenticatedXmtpId },
+    });
path: src/api/v1/notifications/handlers/register-installation.ts
user: login: coderabbitai[bot]
body: _⚠️ Potential issue_

**Ownership check compares to the wrong field – always yields “Forbidden”**

`deviceOwnerCheck.userId` and `identityOwnerCheck.userId` are compared against `authenticatedUser.id`, but `authenticatedUser` is a `DeviceIdentity`.  
The correct comparison is with `authenticatedUser.userId`.

```diff
-const authenticatedUser = await prisma.deviceIdentity.findFirstOrThrow({
-  where: { xmtpId: authenticatedXmtpId },
-});
+const authenticatedUser = await prisma.deviceIdentity.findFirstOrThrow({
+  where: { xmtpId: authenticatedXmtpId },
+  select: { id: true, userId: true },
+});

…

-if (!deviceOwnerCheck || deviceOwnerCheck.userId !== authenticatedUser.id) {
+if (!deviceOwnerCheck || deviceOwnerCheck.userId !== authenticatedUser.userId) {

…

-!identityOwnerCheck || identityOwnerCheck.userId !== authenticatedUser.id
+!identityOwnerCheck || identityOwnerCheck.userId !== authenticatedUser.userId
```

Without this fix, every legitimate call is rejected with HTTP 403.  



Also applies to: 42-54

<!-- This is an auto-generated comment by CodeRabbit -->
created_at: 2025-05-13T22:34:58Z
author_association: CONTRIBUTOR
diff_hunk: @@ -22,11 +20,85 @@
   res: Response,
 ) {
   try {
+    const authenticatedXmtpId = req.app.locals.xmtpId;
+
+    const authenticatedUser = await prisma.deviceIdentity.findFirstOrThrow({
+      where: { xmtpId: authenticatedXmtpId },
+    });
+
     const validatedData = registrationSchema.parse(req.body);
 
+    const [deviceOwnerCheck, identityOwnerCheck] = await Promise.all([
+      prisma.device.findUnique({
+        where: { id: validatedData.deviceId },
+        select: { userId: true },
+      }),
+      prisma.deviceIdentity.findUnique({
+        where: { id: validatedData.identityId },
+        select: { userId: true },
+      }),
+    ]);
+
+    // Check device ownership
+    if (!deviceOwnerCheck || deviceOwnerCheck.userId !== authenticatedUser.id) {
+      req.log.warn(
+        `User ${authenticatedUser.id} attempt to register for unowned/unknown device ${validatedData.deviceId}`,
+      );
+      return res.status(403).json({ error: "Forbidden: Device access denied" });
+    }
+
+    // Check identity ownership
+    if (
+      !identityOwnerCheck ||
+      identityOwnerCheck.userId !== authenticatedUser.id
+    ) {
+      req.log.warn(
+        `User ${authenticatedUser.id} attempt to register for unowned/unknown identity ${validatedData.identityId}`,
+      );
+      return res
+        .status(403)
+        .json({ error: "Forbidden: Identity access denied" });
+    }
+
+    await prisma.$transaction([
+      // 1. Update Device the device push tokens
+      prisma.device.update({
+        where: {
+          id: validatedData.deviceId,
+        },
+        data: {
+          expoToken: validatedData.expoToken,
+          pushToken: validatedData.pushToken,
+        },
+      }),
+      // 2. Update IdentitiesOnDevice with the xmtpInstallationId
+      prisma.identitiesOnDevice.upsert({
+        where: {
+          deviceId_identityId: {
+            deviceId: validatedData.deviceId,
+            identityId: validatedData.identityId,
+          },
+        },
+        create: {
+          deviceId: validatedData.deviceId,
+          identityId: validatedData.identityId,
+          xmtpInstallationId: validatedData.xmtpInstallationId,
+        },
+        update: {
+          xmtpInstallationId: validatedData.xmtpInstallationId,
+        },
+      }),
+    ]);
path: src/api/v1/notifications/handlers/register-installation.ts
user: login: coderabbitai[bot]
body: _🛠️ Refactor suggestion_

**Handle duplicate `xmtpInstallationId` gracefully**

The `upsert` path may throw a `P2002` when two devices race to claim the same installation ID (unique column). Catch this error specifically and reply with `409 Conflict` so clients can react.

```typescript
catch (error) {
  if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
    return res.status(409).json({ error: "Installation ID already registered" });
  }
  …
}
```

Also consider **moving the XMTP registration inside the transaction** or rolling back when XMTP rejects the call, to avoid a DB record pointing to an unregistered installation.

<!-- This is an auto-generated comment by CodeRabbit -->
created_at: 2025-05-13T22:34:58Z
author_association: CONTRIBUTOR
diff_hunk: @@ -1,16 +1,14 @@
 import type { Request, Response } from "express";
 import { z } from "zod";
 import { createNotificationClient } from "@/notifications/client";
+import { prisma } from "@/utils/prisma";
 
-// Schema for registration
 const registrationSchema = z.object({
-  installationId: z.string(),
-  deliveryMechanism: z.object({
-    deliveryMechanismType: z.object({
-      case: z.enum(["apnsDeviceToken", "firebaseDeviceToken", "customToken"]),
-      value: z.string(),
-    }),
-  }),
+  deviceId: z.string(),
+  identityId: z.string(),
+  xmtpInstallationId: z.string(),
+  expoToken: z.string(),
+  pushToken: z.string(),
 });
path: src/api/v1/notifications/handlers/register-installation.ts
user: login: coderabbitai[bot]
body: _🛠️ Refactor suggestion_

**Make tokens optional & add cross-field validation**

`expoToken` and `pushToken` are both required, yet only `expoToken` is forwarded to XMTP.  
Some platforms (e.g. APNs) may have only `pushToken`, others only `expoToken`.

```diff
-  expoToken: z.string(),
-  pushToken: z.string(),
+  expoToken: z.string().optional(),
+  pushToken: z.string().optional(),
```

Then add `refine` to ensure **at least one** is present.  



<!-- suggestion_start -->

<details>
<summary>📝 Committable suggestion</summary>

> ‼️ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

```suggestion
const registrationSchema = z.object({
  deviceId: z.string(),
  identityId: z.string(),
  xmtpInstallationId: z.string(),
-  expoToken: z.string(),
-  pushToken: z.string(),
+  expoToken: z.string().optional(),
+  pushToken: z.string().optional(),
});
```

</details>

<!-- suggestion_end -->

<!-- This is an auto-generated comment by CodeRabbit -->
created_at: 2025-05-13T22:34:58Z
author_association: CONTRIBUTOR
diff_hunk: @@ -16,14 +17,62 @@ export async function unregisterInstallation(
   res: Response,
 ) {
   try {
-    const validatedData = unregisterRequestParamsSchema.parse(req.params);
+    const authenticatedXmtpId = req.app.locals.xmtpId;
 
+    const authenticatedUser = await prisma.deviceIdentity.findFirstOrThrow({
+      where: { xmtpId: authenticatedXmtpId },
+    });
+
path: src/api/v1/notifications/handlers/unregister-installation.ts
user: login: coderabbitai[bot]
body: _⚠️ Potential issue_

**Authenticated “user” lookup appears to fetch a DeviceIdentity, leading to faulty ownership checks**  

`prisma.deviceIdentity.findFirstOrThrow` returns a **DeviceIdentity** record whose primary key (`id`) is not the same as the owning **User**’s `id`.  
Later (lines 43-51) you compare `identityOnDevice.device.userId` with `authenticatedUser.id`, so a match will fail even for the rightful owner and trigger a false‐positive 403.

```diff
-const authenticatedUser = await prisma.deviceIdentity.findFirstOrThrow({
-  where: { xmtpId: authenticatedXmtpId },
-});
+const authenticatedUser = await prisma.user.findFirstOrThrow({
+  where: { xmtpId: authenticatedXmtpId },
+});
```

(Adjust the query to whatever table actually stores the owning user.)  
Without this fix, legitimate users cannot unregister their own installation.  



<!-- suggestion_start -->

<details>
<summary>📝 Committable suggestion</summary>

> ‼️ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

```suggestion
    const authenticatedXmtpId = req.app.locals.xmtpId;

    const authenticatedUser = await prisma.user.findFirstOrThrow({
      where: { xmtpId: authenticatedXmtpId },
    });
```

</details>

<!-- suggestion_end -->

<!-- This is an auto-generated comment by CodeRabbit -->
created_at: 2025-05-13T22:34:59Z
author_association: CONTRIBUTOR
diff_hunk: @@ -16,14 +17,62 @@
   res: Response,
 ) {
   try {
-    const validatedData = unregisterRequestParamsSchema.parse(req.params);
+    const authenticatedXmtpId = req.app.locals.xmtpId;
 
+    const authenticatedUser = await prisma.deviceIdentity.findFirstOrThrow({
+      where: { xmtpId: authenticatedXmtpId },
+    });
+
+    const { xmtpInstallationId } = unregisterRequestParamsSchema.parse(
+      req.params,
+    );
+
+    const identityOnDevice = await prisma.identitiesOnDevice.findUnique({
+      where: {
+        xmtpInstallationId: xmtpInstallationId,
+      },
+      include: {
+        device: { select: { userId: true } },
+      },
+    });
+
+    if (!identityOnDevice) {
+      return res.status(404).json({ error: "Installation not found" });
+    }
+
+    // Verify that the installation belongs to the authenticated user
+    if (identityOnDevice.device.userId !== authenticatedUser.id) {
+      req.log.warn(
+        `User ${authenticatedUser.id} attempt to unregister unowned installation ${xmtpInstallationId}`,
+      );
+      return res
+        .status(403)
+        .json({ error: "Forbidden: Installation access denied" });
+    }
path: src/api/v1/notifications/handlers/unregister-installation.ts
user: login: coderabbitai[bot]
body: _🛠️ Refactor suggestion_

**Authorization guard is relying on mismatched identifiers**  

Even after correcting the query above, consider comparing the *same* identifier types:

```diff
-if (identityOnDevice.device.userId !== authenticatedUser.id) {
+if (identityOnDevice.device.userId !== authenticatedUser.userId) {
```

(or simply select `userId` directly when fetching the user).  
Failing to align identifier domains results in spurious “Forbidden” responses.  



<!-- suggestion_start -->

<details>
<summary>📝 Committable suggestion</summary>

> ‼️ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

```suggestion
    // Verify that the installation belongs to the authenticated user
    if (identityOnDevice.device.userId !== authenticatedUser.userId) {
      req.log.warn(
        `User ${authenticatedUser.id} attempt to unregister unowned installation ${xmtpInstallationId}`,
      );
      return res
        .status(403)
        .json({ error: "Forbidden: Installation access denied" });
    }
```

</details>

<!-- suggestion_end -->

<!-- This is an auto-generated comment by CodeRabbit -->
created_at: 2025-05-13T22:34:59Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,11 @@
+/*
+  Warnings:
+
+  - A unique constraint covering the columns `[xmtpInstallationId]` on the table `IdentitiesOnDevice` will be added. If there are existing duplicate values, this will fail.
+
+*/
+-- AlterTable
+ALTER TABLE "IdentitiesOnDevice" ADD COLUMN     "xmtpInstallationId" TEXT;
path: prisma/migrations/20250513202135_add_xmtp_installation_id_on_identities_on_devices/migration.sql
user: login: neekolas
body: We are sure there are no duplicate values here?
created_at: 2025-05-13T22:54:19Z
author_association: COLLABORATOR
diff_hunk: @@ -0,0 +1,11 @@
+/*
+  Warnings:
+
+  - A unique constraint covering the columns `[xmtpInstallationId]` on the table `IdentitiesOnDevice` will be added. If there are existing duplicate values, this will fail.
+
+*/
+-- AlterTable
+ALTER TABLE "IdentitiesOnDevice" ADD COLUMN     "xmtpInstallationId" TEXT;
path: prisma/migrations/20250513202135_add_xmtp_installation_id_on_identities_on_devices/migration.sql
user: login: thierryskoda
body: @neekolas Like that there could be 2 `xmtpInstallationId` with the same values for 2 different identity? Or identity on another device?

I think those are unique no? But you are in a better place to tell me
created_at: 2025-05-13T23:01:02Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,11 @@
+/*
+  Warnings:
+
+  - A unique constraint covering the columns `[xmtpInstallationId]` on the table `IdentitiesOnDevice` will be added. If there are existing duplicate values, this will fail.
+
+*/
+-- AlterTable
+ALTER TABLE "IdentitiesOnDevice" ADD COLUMN     "xmtpInstallationId" TEXT;
path: prisma/migrations/20250513202135_add_xmtp_installation_id_on_identities_on_devices/migration.sql
user: login: neekolas
body: Can you ever be registered twice without wiping the local DB. 

There’s one installation ID per local convos database
created_at: 2025-05-13T23:03:58Z
author_association: COLLABORATOR
diff_hunk: @@ -0,0 +1,11 @@
+/*
+  Warnings:
+
+  - A unique constraint covering the columns `[xmtpInstallationId]` on the table `IdentitiesOnDevice` will be added. If there are existing duplicate values, this will fail.
+
+*/
+-- AlterTable
+ALTER TABLE "IdentitiesOnDevice" ADD COLUMN     "xmtpInstallationId" TEXT;
path: prisma/migrations/20250513202135_add_xmtp_installation_id_on_identities_on_devices/migration.sql
user: login: thierryskoda
body: Maybe my brain is dead but can you give an example of a scenario of what you are concern about?

The `xmtpInstallationId` is unique in the DB and it's linked to a unique `Device` and `DeviceIdentity` (which is what olds the inboxId
created_at: 2025-05-14T02:26:38Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,11 @@
+/*
+  Warnings:
+
+  - A unique constraint covering the columns `[xmtpInstallationId]` on the table `IdentitiesOnDevice` will be added. If there are existing duplicate values, this will fail.
+
+*/
+-- AlterTable
+ALTER TABLE "IdentitiesOnDevice" ADD COLUMN     "xmtpInstallationId" TEXT;
path: prisma/migrations/20250513202135_add_xmtp_installation_id_on_identities_on_devices/migration.sql
user: login: neekolas
body: If DeviceIdentity is as unique as the inboxID it should be fine. 

I don’t have enough context on when the device identities get created to have strong opinions
created_at: 2025-05-14T02:38:03Z
author_association: COLLABORATOR
diff_hunk: @@ -11,95 +13,231 @@ import express from "express";
 import { uint8ArrayToHex } from "uint8array-extras";
 import { notificationsRouter } from "@/api/v1/notifications/notifications.router";
 import { jsonMiddleware } from "@/middleware/json";
+import { pinoMiddleware } from "@/middleware/pino";
 import { createNotificationClient } from "@/notifications/client";
 import type { RegisterInstallationResponse } from "@/notifications/gen/notifications/v1/service_pb";
+import { prisma } from "@/utils/prisma";
 
 const app = express();
 app.use(jsonMiddleware);
+app.use(pinoMiddleware);
+
+const AUTH_XMTP_ID = "test-auth-xmtp-id";
+app.use((req, res, next) => {
+  req.app.locals.xmtpId = AUTH_XMTP_ID;
+  next();
+});
+
 app.use("/notifications", notificationsRouter);
 
 let server: Server;
-
-const testInstallationId = "test-installation-id";
 const notificationClient = createNotificationClient();
 
+const genericTestInstallationId1 = "test-install-id-1-for-subs";
+const genericTestInstallationId2 = "test-install-id-2-for-meta-subs";
+const genericTestInstallationId3 = "test-install-id-3-for-unsub";
+const genericTestInstallationId4 = "test-install-id-4-for-unsub-validate";
+
 beforeAll(() => {
   server = app.listen(3008);
 });
 
-afterAll(() => {
+afterAll(async () => {
   server.close();
+  await prisma.$disconnect();
+  try {
+    await notificationClient.deleteInstallation({
+      installationId: genericTestInstallationId1,
+    });
+    await notificationClient.deleteInstallation({
+      installationId: genericTestInstallationId2,
+    });
+    await notificationClient.deleteInstallation({
+      installationId: genericTestInstallationId3,
+    });
+    await notificationClient.deleteInstallation({
+      installationId: genericTestInstallationId4,
+    });
+  } catch (error) {
+    /* ignore */
+  }
path: tests/notifications.test.ts
user: login: coderabbitai[bot]
body: _⚠️ Potential issue_

**Fix ESLint failure – remove unused `error` binding**

The pipeline is failing because the `error` identifier is never used. The cleanest fix is to omit the catch binding (supported since ES2021):

```diff
-} catch (error) {
-  /* ignore */
-}
+} catch {
+  /* ignore */
+}
```
Do the same for the identical block in the `afterEach` hook to silence both lint errors.



<!-- suggestion_start -->

<details>
<summary>📝 Committable suggestion</summary>

> ‼️ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

```suggestion
  } catch {
    /* ignore */
  }
```

</details>

<!-- suggestion_end -->

<details>
<summary>🧰 Tools</summary>

<details>
<summary>🪛 ESLint</summary>

[error] 61-61: 'error' is defined but never used.

(@typescript-eslint/no-unused-vars)

</details>
<details>
<summary>🪛 GitHub Actions: Validate code quality</summary>

[error] 61-61: ESLint: 'error' is defined but never used. (@typescript-eslint/no-unused-vars)

</details>

</details>

<!-- This is an auto-generated comment by CodeRabbit -->
created_at: 2025-05-14T03:05:05Z
author_association: CONTRIBUTOR
diff_hunk: @@ -182,11 +182,30 @@ identitiesRouter.post(
         return;
       }
 
+      // Check if an identity with this user ID and XMTP ID is already linked to this device
path: src/api/v1/identities.ts
user: login: macroscopeapp[bot]
body: The code in `identities.ts` references `device.user.id` without confirming that `device` is not null. If `prisma.device.findUnique` returns null, this would lead to a runtime error when attempting to access `user` on a null value. Consider adding a check to ensure that `device` exists before accessing its properties or else document the rationale for expecting that `device` is always non-null.

```diff
+if (!device) {
+  throw new Error('Device not found');
+}

```
> 🚀 Reply to ask Macroscope to explain or update this suggestion.

> 👍 Helpful? React to give us feedback.
created_at: 2025-05-14T03:16:44Z
author_association: NONE
diff_hunk: @@ -0,0 +1,11 @@
+/*
+  Warnings:
+
+  - A unique constraint covering the columns `[xmtpInstallationId]` on the table `IdentitiesOnDevice` will be added. If there are existing duplicate values, this will fail.
+
+*/
+-- AlterTable
+ALTER TABLE "IdentitiesOnDevice" ADD COLUMN     "xmtpInstallationId" TEXT;
path: prisma/migrations/20250513202135_add_xmtp_installation_id_on_identities_on_devices/migration.sql
user: login: nplasterer
body: Thierry you would want to call dropClient any time you want to drop the cached version of the installationId from the bridge. Otherwise it will stay cached forever and will never create a new client from the same installationId. Which I think is what you want. But I do know Noe was using dropClient because we added that feature for him.
created_at: 2025-05-14T14:15:05Z
author_association: NONE

PR comments:
user: login: coderabbitai[bot]
created_at: 2025-05-13T22:28:41Z
body: <!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- This is an auto-generated comment: rate limited by coderabbit.ai -->

> [!WARNING]
> ## Rate limit exceeded
> 
> @thierryskoda has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait **5 minutes and 9 seconds** before requesting another review.
> 
> <details>
> <summary>⌛ How to resolve this issue?</summary>
> 
> After the wait time has elapsed, a review can be triggered using the `@coderabbitai review` command as a PR comment. Alternatively, push new commits to this PR.
> 
> We recommend that you space out your commits to avoid hitting the rate limit.
> 
> </details>
> 
> 
> <details>
> <summary>🚦 How do rate limits work?</summary>
> 
> CodeRabbit enforces hourly rate limits for each developer per organization.
> 
> Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
> 
> Please see our [FAQ](https://docs.coderabbit.ai/faq) for further information.
> 
> </details>
> 
> <details>
> <summary>📥 Commits</summary>
> 
> Reviewing files that changed from the base of the PR and between f6928e6aec115fd2bcc801780949f644b8c084a4 and 3c664680e3110b1b5699321383f522b6c6b58e5f.
> 
> </details>
> 
> <details>
> <summary>📒 Files selected for processing (2)</summary>
> 
> * `src/api/v1/identities.ts` (1 hunks)
> * `tests/notifications.test.ts` (4 hunks)
> 
> </details>

<!-- end of auto-generated comment: rate limited by coderabbit.ai -->
<!-- walkthrough_start -->

## Walkthrough

This change introduces a new unique, optional field `xmtpInstallationId` to the `IdentitiesOnDevice` model in the database schema. Notification registration, unregistration, and handling logic are refactored to use this new field for identifying installations, with enhanced validation, authorization, and cleanup routines for device and installation records.

## Changes

| File(s)                                                                                             | Change Summary                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
|---------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| prisma/migrations/.../migration.sql<br>prisma/schema.prisma                                       | Added an optional, unique `xmtpInstallationId` field to the `IdentitiesOnDevice` model and updated the database schema and migration scripts accordingly.                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| src/api/v1/notifications/handlers/handle-xmtp-notification.ts                                     | Refactored the notification handler to use `xmtpInstallationId` for device/installation lookup, improved error handling, and added a cleanup routine for failed installations. Push notification sending logic was updated to sequentially handle errors and trigger cleanups as needed.                                                                                                                                                                                                                                                                                                        |
| src/api/v1/notifications/handlers/register-installation.ts                                        | Changed the registration request body schema to include `xmtpInstallationId`, added ownership validation for devices and identities, and updated the registration logic to upsert the new field. Improved error handling and logging.                                                                                                                                                                                                                                                                                                                                                         |
| src/api/v1/notifications/handlers/unregister-installation.ts                                      | Modified the unregistration handler to use `xmtpInstallationId`, added user authorization checks, and updated the logic to nullify the installation ID before deleting the installation from the notification server. Enhanced error handling and response structure.                                                                                                                                                                                                                                                                                                                         |
| src/api/v1/notifications/notifications.router.ts                                                  | Changed the route parameter name in the DELETE endpoint for unregistering an installation from `:installationId` to `:xmtpInstallationId`.                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| tests/notifications.test.ts                                                                       | Expanded and restructured tests for notification registration, unregistration, subscription, and unsubscription. Added authentication middleware, improved validation and authorization tests, and enhanced setup/teardown logic to maintain database and external state consistency.                                                                                                                                                                                                                                                                                                     |

## Sequence Diagram(s)

```mermaid
sequenceDiagram
    participant Client
    participant API
    participant DB
    participant XMTPServer

    Client->>API: Register installation (deviceId, identityId, xmtpInstallationId, expoToken, pushToken)
    API->>DB: Validate ownership, upsert xmtpInstallationId for IdentitiesOnDevice
    API->>XMTPServer: Register notification installation
    API-->>Client: Registration response

    Client->>API: Unregister installation (xmtpInstallationId)
    API->>DB: Lookup IdentitiesOnDevice by xmtpInstallationId
    API->>DB: Nullify xmtpInstallationId
    API->>XMTPServer: Delete installation
    API-->>Client: Unregistration response

    XMTPServer->>API: Notification delivery webhook (xmtpInstallationId)
    API->>DB: Lookup IdentitiesOnDevice by xmtpInstallationId
    alt Not found or push failed
        API->>DB: Cleanup xmtpInstallationId and tokens
        API->>XMTPServer: Delete installation
    end
    API-->>XMTPServer: Webhook response
```

## Possibly related PRs

- ephemeraHQ/convos-backend#63: Refactors `handleXmtpNotification` to use `xmtpInstallationId` and adds cleanup logic; both PRs modify the same handler but focus on different aspects.

## Suggested reviewers

- neekolas

## Poem

> In the warren where code bunnies dwell,  
> A new field hops in, unique as a bell.  
> Installations now tracked with care,  
> Cleanup and checks everywhere!  
> Devices and tokens, all neat and right—  
> The schema’s refreshed, and the future is bright.  
> 🐇✨

<!-- walkthrough_end -->
<!-- internal state start -->


<!-- DwQgtGAEAqAWCWBnSTIEMB26CuAXA9mAOYCmGJATmriQCaQDG+Ats2bgFyQAOFk+AIwBWJBrngA3EsgEBPRvlqU0AgfFwA6NPEgQAfACgjoCEYDEZyAAUASpETZWaCrKNxU3bABsvkCiQBHbGlcABpIcVwvOkgAIgAzeAAPLgAhElwaPk9EWEgMfHFEhmp4fCx/IiQs+A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P9hBtAwLnwPiByDIBScaZAAPwFBoVkAgBgNMmQG0BEB6yWgL/00FQB4gz/SARYJIDP9QBhAlZGoNoDTJbBDAIAX/yAGQDBykAjwYOUHICAABT/AAYOTQCG1QkBgXfojH36aRD+x/fELJAOSRJ9AQAA -->

<!-- internal state end -->
<!-- finishing_touch_checkbox_start -->

<details open="true">
<summary>✨ Finishing Touches</summary>

- [ ] <!-- {"checkboxId": "7962f53c-55bc-4827-bfbf-6a18da830691"} --> 📝 Generate Docstrings

</details>

<!-- finishing_touch_checkbox_end -->
<!-- tips_start -->

---

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

<details>
<summary>❤️ Share</summary>

- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai)
- [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai)
- [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai)
- [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

</details>

<details>
<summary>🪧 Tips</summary>

### Chat

There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=ephemeraHQ/convos-backend&utm_content=67):

- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
  - `I pushed a fix in commit <commit_id>, please review it.`
  - `Explain this complex logic.`
  - `Open a follow-up GitHub issue for this discussion.`
- Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples:
  - `@coderabbitai explain this code block.`
  -	`@coderabbitai modularize this function.`
- PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
  - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.`
  - `@coderabbitai read src/utils.ts and explain its main purpose.`
  - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.`
  - `@coderabbitai help me debug CodeRabbit configuration file.`

### Support

Need help? Create a ticket on our [support page](https://www.coderabbit.ai/contact-us/support) for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

### CodeRabbit Commands (Invoked using PR comments)

- `@coderabbitai pause` to pause the reviews on a PR.
- `@coderabbitai resume` to resume the paused reviews.
- `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
- `@coderabbitai full review` to do a full review from scratch and review all the files again.
- `@coderabbitai summary` to regenerate the summary of the PR.
- `@coderabbitai generate docstrings` to [generate docstrings](https://docs.coderabbit.ai/finishing-touches/docstrings) for this PR.
- `@coderabbitai generate sequence diagram` to generate a sequence diagram of the changes in this PR.
- `@coderabbitai resolve` resolve all the CodeRabbit review comments.
- `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository.
- `@coderabbitai help` to get help.

### Other keywords and placeholders

- Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed.
- Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description.
- Add `@coderabbitai` anywhere in the PR title to generate the title automatically.

### CodeRabbit Configuration File (`.coderabbit.yaml`)

- You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository.
- Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json`

### Documentation and Community

- Visit our [Documentation](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit.
- Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback.
- Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.

</details>

<!-- tips_end -->
author_association: CONTRIBUTOR
user: login: macroscopeapp[bot]
created_at: 2025-05-13T22:45:04Z
body: ### Refactor XMTP push notification system to use installation IDs for device registration and notification delivery
* Adds `xmtpInstallationId` column to `IdentitiesOnDevice` table through new migration in [migration.sql](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-aba0dc12ac32fc9a011ad06d8f1c17cec0664b22ab8e52b651d0c4c719f2f233) and updates [schema.prisma](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-5b443964f4f3a611682db8f7e02177b0a8c632b2039e2bd5e4dd7347815c565c)
* Rewrites notification handling in [handle-xmtp-notification.ts](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-5e68d80d2d4bcf38d79f23de529a3d6dd023eb809a0ffb452350874ec493e29e) to use installation IDs with improved error handling and cleanup of failed installations
* Implements new authentication-based registration flow in [register-installation.ts](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-e832655c5ccce7cf8a4f8bb66174d34d4b8cb2b3df82b73532ce96f27b2b5dcd) using database transactions
* Updates unregistration process in [unregister-installation.ts](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-a17b268f05d5b036f7fe7086d3910b0ecca003982a5b2c8ed2dbe6e016a18c5f) to verify ownership and cleanup database records

#### 📍Where to Start
Start with the registration flow in [register-installation.ts](https://github.com/ephemeraHQ/convos-backend/pull/67/files#diff-e832655c5ccce7cf8a4f8bb66174d34d4b8cb2b3df82b73532ce96f27b2b5dcd) as it establishes the core data model and authentication patterns used throughout the other handlers.



#### Changes since #67 opened

- Parallelized database queries in push notification registration handlers [f933f9b]
- Optimized user authentication queries [f933f9b]
- Renamed device identity variables [f933f9b]
- Updated error logging references [f933f9b]
- Modified HTTP response handling pattern across notification endpoints [e27ccb1]
- Fixed user ID reference in unregister installation logging [e27ccb1]
- Restructured notifications test suite with authentication and database integration [7c95763]
- Modified error handling in notifications registration endpoint [7c95763]
- Renamed parameter in notifications unregister endpoint [7c95763]
- Removed unused error parameters from catch blocks in notification test lifecycle methods [f6928e6]
- Added duplicate prevention for identity-device associations in `/identities/device/:deviceId` endpoint [2d6c956]
- Removed test installation cleanup from `notifications` test suite [2d6c956]
- Added cleanup step in notification test setup [8309c45]
- Fixed error handling flow in `POST /identities/device/:deviceId` endpoint [3c66468]
----

_[Macroscope](https://app.macroscope.com) summarized 3c66468._
author_association: NONE
user: login: neekolas
created_at: 2025-05-14T02:24:45Z
body: I think the type errors are because you are returning from the route handler, when it doesn't expect anything to be returned. 
author_association: COLLABORATOR
user: login: thierryskoda
created_at: 2025-05-14T02:27:17Z
body: > I think the type errors are because you are returning from the route handler, when it doesn't expect anything to be returned.

Yes! Just pushed a fix... Thought I pushed before but didn't it PUSH
author_association: CONTRIBUTOR