diff --git a/src/lib/notifications/emailHandler.ts b/src/lib/notifications/emailHandler.ts index b05ce35126786325d2d2acb2531a6ac838b36e3d..e5e6fc7ce07c7643e2e77a12171ba2df9c2cead8 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 780dda94901fe3f7c0ef4130babf3093bd4e6cd0..3ee3f4c986366e0abbe2905e0106e2f194d186bc 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 a7f1eb858a5bf9ab4492e03e41d67d9b459c3c15..415221ee4d88c8fabf13cdea4a5c0516e1052b9a 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 7f7429860028c84bb18e192320102812f2cdfeb7..9a5704adb50e338230db8ac6004f6850c622b973 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; + } }); };