Skip to content

Commit

Permalink
chore: remove fake failure notif
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI committed Jul 24, 2024
1 parent aa38dc7 commit ca192ee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions api/src/controllers/reminder.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ const reminderCronJob = async (req, res) => {
if (!!notif) continue;

sendPushNotification({
userId: reminder.user.id,
matomoId: reminder.user.matomo_id,
pushNotifToken: reminder.user.push_notif_token,
channelId: "unique_reminder",
Expand Down Expand Up @@ -220,6 +221,7 @@ const reminderCronJob = async (req, res) => {
if (!!notif) continue;

sendPushNotification({
userId: reminder.user.id,
matomoId: reminder.user.matomo_id,
pushNotifToken: reminder.user.push_notif_token,
channelId: "unique_reminder",
Expand Down
17 changes: 16 additions & 1 deletion api/src/services/push-notifications.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const PushNotifications = require("node-pushnotifications");
const { capture } = require("../third-parties/sentry");
const prisma = require("../prisma");
const {
PUSH_NOTIFICATION_FIREBASE_SERVICE_ACCOUNT,
PUSH_NOTIFICATION_GCM_ID,
Expand Down Expand Up @@ -50,7 +51,7 @@ const NotificationService = new PushNotifications({
},
});

const sendPushNotification = async ({ matomoId, pushNotifToken, title, body, link, channelId, type }) => {
const sendPushNotification = async ({ userId, matomoId, pushNotifToken, title, body, link, channelId, type }) => {
const data = {
title,
body,
Expand All @@ -77,6 +78,20 @@ const sendPushNotification = async ({ matomoId, pushNotifToken, title, body, lin
userId: matomoId,
});
} else if (results[0]?.failure) {
const error = results[0].message?.[0]?.errorMsg;
if (error === "Requested entity was not found") {
// https://stackoverflow.com/a/56218146/5225096
await prisma.user.upsert({
where: { matomo_id: matomoId },
update: {
push_notif_token: null,
},
});
return { ok: false, results };
} else if (error === "apn write timeout") {
// just ignore, even Google know nothing about it
return { ok: false, results };
}
capture(`push notification sent failure: ${results[0].message?.[0]?.errorMsg}`, {
extra: { results, data, pushNotifToken },
user: {
Expand Down
1 change: 1 addition & 0 deletions api/src/utils/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ const notificationsCronJob = async () => {
for (const notif of notifs) {
if (!notif?.user?.push_notif_token) continue;
sendPushNotification({
userId: notif.user.id,
matomoId: notif.user.matomo_id,
pushNotifToken: notif.user.push_notif_token,
channelId: "unique_reminder",
Expand Down

0 comments on commit ca192ee

Please sign in to comment.