Skip to content
Snippets Groups Projects
Commit 2563f531 authored by Aaron Dötsch's avatar Aaron Dötsch
Browse files

Add notification handler return type

parent 29f76a30
No related branches found
No related tags found
No related merge requests found
......@@ -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
};
......@@ -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;
}
};
}));
......
......@@ -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[]>;
......@@ -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;
}
});
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment