-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprovider.ts
More file actions
44 lines (37 loc) · 1.32 KB
/
provider.ts
File metadata and controls
44 lines (37 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import NotificationsProvider from 'hawk-worker-sender/src/provider';
import { Notification, EventsTemplateVariables } from 'hawk-worker-sender/types/template-variables';
import templates from './templates';
import { LoopTemplate } from '../types/template';
import LoopDeliverer from './deliverer';
/**
* This class provides a 'send' method that will renders and sends a notification
*/
export default class LoopProvider extends NotificationsProvider {
/**
* Class with the 'deliver' method for sending messages to the Loop
*/
private readonly deliverer: LoopDeliverer;
/**
* Constructor allows to separate dependencies that can't be tested,
* so in tests they will be mocked.
*/
constructor() {
super();
this.deliverer = new LoopDeliverer();
}
/**
* Send loop message to recipient
*
* @param to - recipient endpoint
* @param notification - notification with payload and type
*/
public async send(to: string, notification: Notification): Promise<void> {
let template: LoopTemplate;
switch (notification.type) {
case 'event': template = templates.EventTpl; break;
case 'several-events':template = templates.SeveralEventsTpl; break;
}
const message = template(notification.payload as EventsTemplateVariables);
await this.deliverer.deliver(to, message);
}
}