Skip to content

Commit 7063334

Browse files
committed
email notification server configuration
1 parent d2987e3 commit 7063334

File tree

9 files changed

+132
-37
lines changed

9 files changed

+132
-37
lines changed

packages/lib/delivery/src/Delivery.ts

+6
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ export interface DeliveryServiceProperties {
55
//Number of bytes an envelop object should not exceed
66
sizeLimit: number;
77
notificationChannel: NotificationChannel[];
8+
// properties for email service
9+
smtpHost: string;
10+
smtpPort: number;
11+
smtpEmail: string;
12+
smtpUsername: string;
13+
smtpPassword: string;
814
}

packages/lib/delivery/src/Messages.test.ts

+20-12
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ import { getConversationId, getMessages, incomingMessage } from './Messages';
99
import { Session } from './Session';
1010
import { SpamFilterRules } from './spam-filter/SpamFilterRules';
1111

12-
import { MAIL_HTML, MAIL_SUBJECT } from './notifications/channels/Email';
13-
import { NotificationChannelType } from './notifications/types';
12+
import {
13+
NEW_MSG_EMAIL_TEMPLATE,
14+
NEW_MSG_EMAIL_SUBJECT,
15+
} from './notifications/templates/newMessage';
16+
import {
17+
NotificationChannelType,
18+
NotificationType,
19+
} from './notifications/types';
1420

1521
const SENDER_NAME = 'alice.eth';
1622
const RECEIVER_NAME = 'bob.eth';
@@ -451,7 +457,10 @@ describe('Messages', () => {
451457
return Promise.resolve([
452458
{
453459
type: NotificationChannelType.EMAIL,
454-
config: { recipientAddress: '[email protected]' },
460+
config: {
461+
recipientAddress: '[email protected]',
462+
notificationType: NotificationType.NEW_MESSAGE,
463+
},
455464
},
456465
]);
457466
};
@@ -479,14 +488,11 @@ describe('Messages', () => {
479488
{
480489
type: NotificationChannelType.EMAIL,
481490
config: {
482-
host: 'exmaple.host',
491+
483492
port: 1234,
484-
secure: true,
485-
auth: {
486-
user: 'foo',
487-
pass: 'bar',
488-
},
489-
senderAddress: '[email protected]',
493+
username: '[email protected]',
494+
password: 'bar',
495+
emailID: '[email protected]',
490496
},
491497
},
492498
],
@@ -503,8 +509,10 @@ describe('Messages', () => {
503509

504510
expect(sendMailMock).toHaveBeenCalledWith({
505511
506-
html: MAIL_HTML(testData.delvieryInformationBUnecrypted),
507-
subject: MAIL_SUBJECT,
512+
html: NEW_MSG_EMAIL_TEMPLATE(
513+
testData.delvieryInformationBUnecrypted,
514+
),
515+
subject: NEW_MSG_EMAIL_SUBJECT,
508516
509517
});
510518
//Check if the message was submitted to the socket

packages/lib/delivery/src/Messages.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { NotificationBroker } from './notifications';
2424
import {
2525
GetNotificationChannels,
2626
NotificationChannel,
27+
NotificationType,
2728
} from './notifications/types';
2829
import { checkToken, Session } from './Session';
2930
import { isSpam } from './spam-filter';
@@ -209,7 +210,10 @@ export async function incomingMessage(
209210
});
210211
//If not we're notifing the user that there is a new message waiting for them
211212
} else {
212-
const { sendNotification } = NotificationBroker(dsNotificationChannels);
213+
const { sendNotification } = NotificationBroker(
214+
dsNotificationChannels,
215+
NotificationType.NEW_MESSAGE,
216+
);
213217
await sendNotification(
214218
deliveryInformation,
215219
getUsersNotificationChannels,

packages/lib/delivery/src/notifications/broker/NotificationBroker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
NotificationChannel,
77
NotificationChannelType,
88
INotificationChannel,
9+
NotificationType,
910
} from '../types';
1011

1112
/**
@@ -55,10 +56,12 @@ export const _setupNotficationBroker = (
5556
*/
5657
export const NotificationBroker = (
5758
notificationChannel: NotificationChannel[],
59+
notificationType: NotificationType,
5860
): INotificationBroker => {
5961
const channels = notificationChannel.map((channel) => {
6062
switch (channel.type) {
6163
case NotificationChannelType.EMAIL:
64+
channel.config.notificationType = notificationType;
6265
return {
6366
type: NotificationChannelType.EMAIL,
6467
send: Email(channel.config).send,

packages/lib/delivery/src/notifications/channels/Email.ts

+35-24
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,57 @@ import { DeliveryInformation } from '@dm3-org/dm3-lib-messaging';
22
import { logError } from '@dm3-org/dm3-lib-shared';
33
import nodemailer from 'nodemailer';
44
import SMTPTransport from 'nodemailer/lib/smtp-transport';
5+
import { NotificationType } from '../types';
6+
import { fetchEmailSubjectAndTemplate } from '../utils';
57

6-
// Define types for email server and user configuration
7-
export type EmailNotificationServerConfig = SMTPTransport.Options & {
8-
senderAddress: string;
8+
// email server configuration
9+
export type EmailNotificationServerConfig = {
10+
host: string;
11+
port: number;
12+
username: string;
13+
password: string;
14+
emailID: string;
915
};
1016

17+
// email notification configuration
1118
export type EmailNotificationUserConfig = {
1219
recipientAddress: string;
20+
notificationType: NotificationType;
1321
};
1422

15-
// Constants for email subject and HTML template
16-
export const MAIL_SUBJECT = 'New DM3 Message';
17-
export const MAIL_HTML = (
18-
deliveryInformation: DeliveryInformation,
19-
) => `<html lang="en">
20-
<body>
21-
<p>You have received a new DM3 message from ${deliveryInformation.from}.
22-
<br/>
23-
Open <a href="app.dm3.network">DM3</a> to read it</p>
24-
<script src="index.js"></script>
25-
</body>
26-
</html>`;
27-
28-
// Define the Email function
23+
// method to send email
2924
export function Email(config: EmailNotificationServerConfig) {
3025
const send = async (
3126
mailConfig: EmailNotificationUserConfig,
3227
deliveryInformation: DeliveryInformation,
3328
) => {
34-
nodemailer.createTestAccount();
35-
const transport = nodemailer.createTransport(new SMTPTransport(config));
36-
3729
try {
38-
// Send the email using nodemailer
30+
// create transport with email credentials
31+
const transport: nodemailer.Transporter<SMTPTransport.SentMessageInfo> =
32+
nodemailer.createTransport({
33+
host: config.host,
34+
port: config.port,
35+
auth: {
36+
user: config.username,
37+
pass: config.password,
38+
},
39+
});
40+
41+
// fetch the specific subject & template of email
42+
const { subject, template } = fetchEmailSubjectAndTemplate(
43+
mailConfig.notificationType,
44+
deliveryInformation,
45+
);
46+
47+
// send the email using nodemailer
3948
await transport.sendMail({
40-
from: config.senderAddress,
49+
from: config.emailID,
4150
to: mailConfig.recipientAddress,
42-
subject: MAIL_SUBJECT,
43-
html: MAIL_HTML(deliveryInformation),
51+
subject: subject,
52+
html: template,
4453
});
54+
55+
// close the connection
4556
transport.close();
4657
} catch (err) {
4758
logError('Send mail failed ' + err);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { DeliveryInformation } from '@dm3-org/dm3-lib-messaging';
2+
3+
export const NEW_MSG_EMAIL_TEMPLATE = (
4+
deliveryInformation: DeliveryInformation,
5+
) =>
6+
`<html lang="en">
7+
<body>
8+
<p>You received a new message from ${deliveryInformation.from}.
9+
</body>
10+
</html>`;
11+
12+
export const NEW_MSG_EMAIL_SUBJECT = 'DM3 New Message';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const OTP_EMAIL_TEMPLATE = (otp: string) =>
2+
`<html lang="en">
3+
<body>
4+
<p>Your OTP to verify email ID is : ${otp}. The OTP will expire in 10 minutes.
5+
</body>
6+
</html>`;
7+
8+
export const OTP_EMAIL_SUBJECT = 'Email Verification';

packages/lib/delivery/src/notifications/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ export enum NotificationChannelType {
55
EMAIL = 'EMAIL',
66
}
77

8+
export enum NotificationType {
9+
NEW_MESSAGE = 'NEW_MESSAGE',
10+
OTP = 'OTP',
11+
}
12+
813
//The properties of a notification channel.
914
// Those properties are stored in the DB to let the user specify their notificatin channels
1015
export interface NotificationChannel {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { DeliveryInformation } from '@dm3-org/dm3-lib-messaging';
2+
import { NotificationType } from './types';
3+
import {
4+
NEW_MSG_EMAIL_SUBJECT,
5+
NEW_MSG_EMAIL_TEMPLATE,
6+
} from './templates/newMessage';
7+
import { OTP_EMAIL_SUBJECT, OTP_EMAIL_TEMPLATE } from './templates/otp';
8+
9+
// TODO: generates 5 digit OTP
10+
export const generateOtp = () => {
11+
return '12345';
12+
};
13+
14+
// to fetch subject & template of email based on notification type
15+
export const fetchEmailSubjectAndTemplate = (
16+
notificationType: NotificationType,
17+
deliveryInformation: DeliveryInformation,
18+
): {
19+
subject: string;
20+
template: string;
21+
} => {
22+
switch (notificationType) {
23+
case NotificationType.NEW_MESSAGE:
24+
return {
25+
subject: NEW_MSG_EMAIL_SUBJECT,
26+
template: NEW_MSG_EMAIL_TEMPLATE(deliveryInformation),
27+
};
28+
case NotificationType.OTP:
29+
return {
30+
subject: OTP_EMAIL_SUBJECT,
31+
template: OTP_EMAIL_TEMPLATE(generateOtp()),
32+
};
33+
default:
34+
throw new Error(
35+
`Notification type ${notificationType} is not supported`,
36+
);
37+
}
38+
};

0 commit comments

Comments
 (0)