Skip to content

Commit aafc48c

Browse files
committed
refactor code and add notificationHandler
1 parent 101d093 commit aafc48c

File tree

5 files changed

+89
-43
lines changed

5 files changed

+89
-43
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
package-lock.json
3-
.DS_Store
3+
.DS_Store
4+
.idea

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ const exampleNotification = require('./notifications/exampleNotification');
109109

110110
app.get('/', (req, res) => {
111111
// send notificaiton
112-
res.notify({ email : '[email protected]' } , exampleNotification() )
112+
res.notify(exampleNotification()).to({ email : '[email protected]' })
113113
res.send('Hello World!')
114114
});
115115
```

example/server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ app.use(notification.register(notificationConfig))
2626

2727
app.get('/', (req, res) => {
2828
// send notificaiton
29-
res.notify({ email : '[email protected]' } , exampleNotification() )
29+
res.notify(exampleNotification()).to({ email : '[email protected]' })
3030
res.send('Hello World!')
3131
})
3232

3333
app.listen(port, () => {
3434
console.log(`Example app listening on port ${port}`)
35-
})
35+
})

src/index.js

+24-39
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const MailChannel = require('./channels/mailChannel');
2+
const NotificationHandler = require("./notificationHandler");
23

34

45
/**
@@ -7,45 +8,29 @@ const MailChannel = require('./channels/mailChannel');
78
* @returns {function} return middleware
89
*/
910

10-
module.exports = ({ config , channels }) => {
11-
12-
if(typeof config != 'object')
13-
throw new Error('you must set conifg as object')
14-
15-
/**
16-
* LIST OF CHANNELS THAT NOTIFICATION CAN SEND
17-
* And Merge Default Channels with Custom Channels
18-
*/
19-
channels = {
20-
mail : MailChannel,
21-
...channels
22-
}
23-
24-
/**
25-
* the user data for send notification
26-
* @param notifiable
27-
* @param notification
28-
*/
29-
const notifyHandler = async (notifiable, notification) => {
30-
// get channels that notification must send to them
31-
let channelsMustBeNotify = notification.via();
32-
33-
// notify to notifiable with different channels
34-
for (const channel of channelsMustBeNotify) {
35-
await channelToNotify(channel , { notifiable, notification });
11+
module.exports = ({config, channels}) => {
12+
13+
if (typeof config != 'object')
14+
throw new Error('you must set conifg as object')
15+
16+
/**
17+
* LIST OF CHANNELS THAT NOTIFICATION CAN SEND
18+
* And Merge Default Channels with Custom Channels
19+
*/
20+
channels = {
21+
mail: MailChannel,
22+
...channels
23+
}
24+
25+
/**
26+
* the user data for send notification
27+
* @param notification
28+
*/
29+
const notifyHandler = (notification) => (new NotificationHandler(config, notification)).setChannels(channels).handle()
30+
31+
return (req, res, next) => {// set notify handler
32+
res.notify = notifyHandler;
33+
next();
3634
}
37-
}
38-
39-
const channelToNotify = async (channel , { notifiable, notification }) => {
40-
// check channel exists in global channels list
41-
if(channels[channel])
42-
await (new channels[channel](config)).send(notifiable , notification);
43-
}
44-
45-
return (req , res , next) => {
46-
// set notify handler
47-
res.notify = notifyHandler;
48-
next();
49-
}
5035
}
5136

src/notificationHandler.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class NotificationHandler {
2+
/**
3+
* the notification class to send message with different channels
4+
*/
5+
notification;
6+
7+
8+
/**
9+
* channels list that the system support
10+
*/
11+
channels;
12+
13+
/**
14+
*
15+
* prepare list of channels and notifications must be sent
16+
*/
17+
listOfNotificationMustSendWithChannels = {};
18+
19+
20+
/**
21+
* list of configs to get from register middleware of package
22+
*/
23+
config;
24+
25+
constructor(config, notification) {
26+
this.config = config;
27+
this.notification = notification;
28+
}
29+
30+
31+
setChannels(channels) {
32+
this.channels = channels;
33+
return this;
34+
}
35+
36+
handle() {
37+
// get channels that notification must send to them
38+
let channelsMustBeNotify = this.notification.via();
39+
40+
// notify to notifiable with different channels
41+
for (const channel of channelsMustBeNotify) {
42+
this.listOfNotificationMustSendWithChannels[channel] = this.notification;
43+
}
44+
45+
return this;
46+
}
47+
48+
async to(notifiable) {
49+
for (const channel in this.listOfNotificationMustSendWithChannels) {
50+
let notification = this.listOfNotificationMustSendWithChannels[channel];
51+
// check channel exists in global channels list
52+
if (this.channels[channel]) {
53+
await (new this.channels[channel](this.config)).send(notifiable, notification);
54+
}
55+
}
56+
57+
}
58+
}
59+
60+
module.exports = NotificationHandler;

0 commit comments

Comments
 (0)