Skip to content

Commit

Permalink
Merge pull request #159 from Plant-for-the-Planet-org/feature/prevent…
Browse files Browse the repository at this point in the history
…-duplicate-device-alertMethod

Avoid device duplication in createAlertMethod
  • Loading branch information
dhakalaashish authored Apr 4, 2024
2 parents 2f2a36c + bde27e2 commit f09a833
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion apps/server/src/server/api/routers/alertMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,27 @@ 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},
// 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) {
Expand Down

0 comments on commit f09a833

Please sign in to comment.