From 9150577d41d19cde64ddccafcc3ac02cac0ba9cb Mon Sep 17 00:00:00 2001 From: Aashish Dhakal <85501584+dhakalaashish@users.noreply.github.com> Date: Mon, 4 Mar 2024 17:28:34 +0545 Subject: [PATCH 1/3] Avoid device duplication in createAlertMethod When user installs FireAlert app from the same phone after deleting the app, createAlertMethod does not detect that it is the same device, so creates the alertMethod again. The deviceName, however, usually remains unchanged. This commit leverages the constancy of deviceName to prevent duplicate device alertMethods. --- apps/server/src/server/api/routers/alertMethod.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/server/src/server/api/routers/alertMethod.ts b/apps/server/src/server/api/routers/alertMethod.ts index e8f9d3ae..fcf7eac1 100644 --- a/apps/server/src/server/api/routers/alertMethod.ts +++ b/apps/server/src/server/api/routers/alertMethod.ts @@ -175,9 +175,16 @@ export const alertMethodRouter = createTRPCRouter({ if (isDeviceVerified) { // Check if the destination (PlayerID) already exists in the table + // Retrieve alert methods that match the destination or (userId and deviceName) const existingAlertMethods = await ctx.prisma.alertMethod.findMany({ where: { - destination: input.destination + OR: [ + {destination: input.destination}, + {AND: [ + {userId: userId}, + {deviceName: input.deviceName} + ]} + ] } }); From 9275da16987b1eb37acbe3e33c77aaeb0ac7d7ef Mon Sep 17 00:00:00 2001 From: Aashish Dhakal <85501584+dhakalaashish@users.noreply.github.com> Date: Thu, 4 Apr 2024 16:44:17 +0545 Subject: [PATCH 2/3] Prevents duplicate devices alertMethod Corrects the previous commit --- .../src/server/api/routers/alertMethod.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/apps/server/src/server/api/routers/alertMethod.ts b/apps/server/src/server/api/routers/alertMethod.ts index fcf7eac1..dfb0c585 100644 --- a/apps/server/src/server/api/routers/alertMethod.ts +++ b/apps/server/src/server/api/routers/alertMethod.ts @@ -179,14 +179,22 @@ export const alertMethodRouter = createTRPCRouter({ const existingAlertMethods = await ctx.prisma.alertMethod.findMany({ where: { OR: [ - {destination: input.destination}, - {AND: [ - {userId: userId}, - {deviceName: input.deviceName} - ]} + // Checks for duplicates by deviceId for all devices (returns duplicate ios devices) + { deviceId: input.deviceId }, + // Checks for devices with the same name but different deviceId for the same user + // We need NOT on deviceId to prevent selecting ios devices (ios deviceNames are not unique) + // (returns duplicate android devices) + { + AND: [ + { userId: userId }, + { deviceName: input.deviceName }, + { deviceId: { not: input.deviceId } } + ] + } ] } }); + // If it does exist and is associated with a different userId, delete it for (const existingAlertMethod of existingAlertMethods) { From bde27e2a612422707eb0cc6f1c6d01d925cc12be Mon Sep 17 00:00:00 2001 From: Aashish Dhakal <85501584+dhakalaashish@users.noreply.github.com> Date: Thu, 4 Apr 2024 17:15:58 +0545 Subject: [PATCH 3/3] Update alertMethod.ts --- apps/server/src/server/api/routers/alertMethod.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/server/src/server/api/routers/alertMethod.ts b/apps/server/src/server/api/routers/alertMethod.ts index dfb0c585..b83749f0 100644 --- a/apps/server/src/server/api/routers/alertMethod.ts +++ b/apps/server/src/server/api/routers/alertMethod.ts @@ -179,6 +179,7 @@ export const alertMethodRouter = createTRPCRouter({ const existingAlertMethods = await ctx.prisma.alertMethod.findMany({ where: { OR: [ + {destination: input.destination}, // Checks for duplicates by deviceId for all devices (returns duplicate ios devices) { deviceId: input.deviceId }, // Checks for devices with the same name but different deviceId for the same user