Skip to content

Commit

Permalink
fix: push tokens duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudambro committed Jan 30, 2024
1 parent 2e91363 commit 7f0d4a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "deleted_at" TIMESTAMP(3),
ADD COLUMN "deleted_because" TEXT;
2 changes: 2 additions & 0 deletions api-node/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ model User {
push_notif_token String?
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
deleted_at DateTime?
deleted_because String?
favorite_indicator IndicatorsSlugEnum?
notifications_preference NotifationEnum[]
notifications Notification[]
Expand Down
26 changes: 26 additions & 0 deletions api-node/src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,32 @@ router.put(
},
});

// Cleaning of old users with the same push_notif_token
// https://docs.expo.dev/push-notifications/faq/#when-and-why-does-the-expopushtoken-change
// > The ExpoPushToken will remain the same across app upgrades.
// > On iOS, it will also remain the same even after uninstalling the app and reinstalling it.
// > On Android, this results in the push token changing.
const pushToken = updatedDbUser.push_notif_token;
const usersWithSamePushToken = await prisma.user.findMany({
where: {
id: { not: updatedDbUser.id },
push_notif_token: pushToken,
},
});

if (usersWithSamePushToken.length > 0) {
await prisma.user.updateMany({
where: {
id: { in: usersWithSamePushToken.map((u) => u.id) },

This comment has been minimized.

Copy link
@Charlesdoiron

Charlesdoiron Jan 30, 2024

Contributor

id: { in: usersWithSamePushToken.map((user) => user.id) },

},
data: {
push_notif_token: `DELETED_${pushToken}`,
deleted_at: new Date(),
deleted_because: `push_notif_token taken by a more recent user (id: ${updatedDbUser.id})`,
},
});
}

res.status(200).send({ ok: true, data: updatedDbUser });
},
),
Expand Down

0 comments on commit 7f0d4a9

Please sign in to comment.