Skip to content

Commit

Permalink
Merge pull request #89 from Plant-for-the-Planet-org/hotfix/fix-notif…
Browse files Browse the repository at this point in the history
…ication-bug

Hotfix/fix notification bug
  • Loading branch information
dhakalaashish committed Oct 20, 2023
2 parents 1fd9b8e + 4a05c9b commit 68cd140
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
28 changes: 27 additions & 1 deletion apps/server/src/Services/Notifier/Notifier/DeviceNotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,35 @@ import {type NotificationParameters} from '../../../Interfaces/NotificationParam
import type Notifier from '../Notifier';
import {NOTIFICATION_METHOD} from '../methodConstants';
import {env} from '../../../env.mjs';
import {logger} from '../../../../src/server/logger';
import {logger} from '../../../server/logger';
import {prisma} from '../../../server/db';

class DeviceNotifier implements Notifier {
getSupportedMethods(): Array<string> {
return [NOTIFICATION_METHOD.DEVICE];
}

async deleteNotificationAndDevice(destination: string, notificationId: string): Promise<void> {
try {
// Delete the notification
await prisma.notification.delete({
where: {
id: notificationId,
},
});
// Unverify and disable the alertMethod
await prisma.alertMethod.deleteMany({
where: {
destination: destination,
method: NOTIFICATION_METHOD.DEVICE,
}
});
logger(`Notification with ID: ${notificationId} deleted and alertMethod for destination: ${destination} has been unverified and disabled.`, "info");
} catch (error) {
logger(`Database Error: Couldn't modify the alertMethod or delete the notification: ${error}`, "error");
}
}

// OneSignal can send both iOS and android notifications,
// "destination" from AlertMethod for method "device"
// is the OneSignal player ID of the device.
Expand Down Expand Up @@ -46,6 +68,10 @@ class DeviceNotifier implements Notifier {
`Failed to send device notification. Error: ${response.statusText} for ${parameters.id}`,
'error',
);
// If device not found
if(response.status === 404){
await this.deleteNotificationAndDevice(destination, parameters.id)
}
return false;
}

Expand Down
45 changes: 43 additions & 2 deletions apps/server/src/Services/Notifier/Notifier/WebhookNotifier.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {prisma} from '../../../server/db';
import {logger} from '../../../../src/server/logger';
import {type NotificationParameters} from '../../../Interfaces/NotificationParameters';
import type Notifier from '../Notifier';
Expand All @@ -8,6 +9,31 @@ class WebhookNotifier implements Notifier {
return [NOTIFICATION_METHOD.WEBHOOK];
}

async deleteNotificationDisableAndUnverifyWebhook(destination: string, notificationId: string): Promise<void> {
try {
// Delete the notification
await prisma.notification.delete({
where: {
id: notificationId,
},
});
// Unverify and disable the alertMethod
await prisma.alertMethod.updateMany({
where: {
destination: destination,
method: NOTIFICATION_METHOD.WEBHOOK,
},
data: {
isVerified: false,
isEnabled: false,
},
});
logger(`Notification with ID: ${notificationId} deleted and alertMethod for destination: ${destination} has been unverified and disabled.`, "info");
} catch (error) {
logger(`Database Error: Couldn't modify the alertMethod or delete the notification: ${error}`, "error");
}
}

async notify(
destination: string,
parameters: NotificationParameters,
Expand All @@ -34,12 +60,27 @@ class WebhookNotifier implements Notifier {

if (!response.ok) {
logger(
`Failed to send webhook notification. Error: ${response.statusText} for ${parameters.id}`,
`Failed to send webhook notification. Error: ${response.statusText} for ${parameters.id}.`,
'error',
);
// Specific status code handling
if (response.status === 404) {
// Webhook URL Not Found - Token not found
await this.deleteNotificationDisableAndUnverifyWebhook(destination, parameters.id);
} else if (response.status === 401){
// Unauthorized
await this.deleteNotificationDisableAndUnverifyWebhook(destination, parameters.id);
} else if (response.status === 403){
// Forbidden
await this.deleteNotificationDisableAndUnverifyWebhook(destination, parameters.id);
} else {
logger(
`Failed to send webhook notification. Something went wrong. Try again in next run.`,
'error',
);
}
return false;
}

return true;
}
}
Expand Down

1 comment on commit 68cd140

@vercel
Copy link

@vercel vercel bot commented on 68cd140 Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.