@webda/core
Version:
Expose API with Lambda
72 lines • 2.11 kB
JavaScript
import { Service, ServiceParameters } from "./service.js";
/**
* Parameters for multi notification service
*/
export class MultiNotificationParameters extends ServiceParameters {
/**
* @override
*/
constructor(params) {
super(params);
this.senders ?? (this.senders = []);
}
}
/**
* Allow an aggregation of Notification to send via multiple media
* like SMS and Email
*
* @WebdaModda
*/
export default class MultiNotificationService extends Service {
/**
* @override
*/
loadParameters(params) {
return new MultiNotificationParameters(params);
}
/**
* @override
*/
resolve() {
super.resolve();
this.senders = this.parameters.senders.map(s => {
let service = this.getService(s);
if (!service) {
throw new Error(`Unknown service '${s}'`);
}
return service;
});
return this;
}
/**
* @override
*/
async sendNotification(user, notification, replacements) {
const selectedSenders = (await Promise.all(this.senders.map(async (s) => {
if ((await s.hasNotification(notification)) && (await s.handleNotificationFor(user))) {
return s;
}
}))).filter(s => s !== undefined);
if (!selectedSenders.length) {
return;
}
if (!this.parameters.multiple) {
return selectedSenders.shift().sendNotification(user, notification, replacements);
}
await Promise.all(selectedSenders.map(s => s.sendNotification(user, notification, replacements)));
}
/**
* @override
*/
async handleNotificationFor(user) {
return (await Promise.all(this.senders.map(s => s.handleNotificationFor(user)))).some(s => s);
}
/**
* @override
*/
async hasNotification(notification) {
return (await Promise.all(this.senders.map(s => s.hasNotification(notification)))).some(s => s);
}
}
export { MultiNotificationService };
//# sourceMappingURL=notificationservice.js.map