From 2563f5319817ef8a8df1bf59d24e865d40ede33f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aaron=20D=C3=B6tsch?= <aaron@fsmpi.rwth-aachen.de> Date: Fri, 4 Aug 2023 03:54:23 +0200 Subject: [PATCH] Add notification handler return type --- src/lib/notifications/emailHandler.ts | 2 +- src/lib/notifications/handler.ts | 23 ++++++++++++++++++++--- src/lib/notifications/types.ts | 8 ++++++-- src/lib/notifications/webhookHandler.ts | 9 ++++++++- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/lib/notifications/emailHandler.ts b/src/lib/notifications/emailHandler.ts index b05ce35..e5e6fc7 100644 --- a/src/lib/notifications/emailHandler.ts +++ b/src/lib/notifications/emailHandler.ts @@ -4,5 +4,5 @@ import type { HandlerReturnType, NotificationData, NotificationType, PossibleNot export async function handleEmail<T extends PossibleNotificationType>(recipient: string, type: NotificationType<T>, data: NotificationData<T>): Promise<HandlerReturnType> { const emailTitle = getMessageTitle(type); const emailBody = formatMessage(type, data); - return Promise.resolve(); // TODO actually send email + return Promise.resolve({status: "success"}); // TODO actually send email }; diff --git a/src/lib/notifications/handler.ts b/src/lib/notifications/handler.ts index 780dda9..3ee3f4c 100644 --- a/src/lib/notifications/handler.ts +++ b/src/lib/notifications/handler.ts @@ -17,14 +17,31 @@ export async function sendNotification<T extends PossibleNotificationType>(user: return Promise.all(notificationChannels.map(channel => { switch(channel.channelType){ case ChannelType.EMAIL.key: { - return ChannelType.EMAIL.handler(channel.recipient, type, data); + return ChannelType.EMAIL.handler(channel.recipient, type, data) + .catch(err => { + return { + status: "error", + channel, + error: err + } as HandlerReturnType; + }); } case ChannelType.WEBHOOK.key: { - return ChannelType.WEBHOOK.handler(channel.recipient, type, data); + return ChannelType.WEBHOOK.handler(channel.recipient, type, data) + .catch(err => { + return { + status: "error", + channel, + error: err + } as HandlerReturnType; + }); } default: { console.error(`Unknown channel type`, channel.channelType); - return Promise.resolve(); + return { + status: "ignored", + channel + } as HandlerReturnType; } }; })); diff --git a/src/lib/notifications/types.ts b/src/lib/notifications/types.ts index a7f1eb8..415221e 100644 --- a/src/lib/notifications/types.ts +++ b/src/lib/notifications/types.ts @@ -21,7 +21,10 @@ export type NotificationData<T extends PossibleNotificationType> = T extends "receive-transfer" ? ReceiveTransferNotificationData : never; -export type HandlerReturnType = any; +export type HandlerReturnSuccess = {status: "success"}; +export type HandlerReturnError = {status: "error", channel: NotificationChannel, error: any}; +export type HandlerReturnIgnored = {status: "ignored", channel: NotificationChannel}; +export type HandlerReturnType = HandlerReturnSuccess | HandlerReturnError | HandlerReturnIgnored; export type NotificationHandler<T extends PossibleNotificationType> = (recipient: string, type: NotificationType<T>, data: NotificationData<T>) => Promise<HandlerReturnType>; export type ChannelType<T extends PossibleNotificationType> = {key: number, handler: NotificationHandler<T>}; @@ -32,4 +35,5 @@ export type NotificationChannel = { notificationTypes: number, recipient: string }; -export type GenericNotificationHandler<T extends PossibleNotificationType> = (user: {id:number,notificationChannels:NotificationChannel[]|undefined}|number, type: NotificationType<T>, data: NotificationData<T>) => Promise<any>; + +export type GenericNotificationHandler<T extends PossibleNotificationType> = (user: {id:number,notificationChannels:NotificationChannel[]|undefined}|number, type: NotificationType<T>, data: NotificationData<T>) => Promise<HandlerReturnType[]>; diff --git a/src/lib/notifications/webhookHandler.ts b/src/lib/notifications/webhookHandler.ts index 7f74298..9a5704a 100644 --- a/src/lib/notifications/webhookHandler.ts +++ b/src/lib/notifications/webhookHandler.ts @@ -6,10 +6,17 @@ export async function handleWebhook<T extends PossibleNotificationType>(recipien const headers = { "Content-Type": "application/json" }; - console.log({recipient, type, data, body}); return fetch(recipient, { method: "POST", body, headers + }).then(res => { + if(res.status >= 200 && res.status < 300){ + return { + status: "success" + }; + }else{ + throw res; + } }); }; -- GitLab