Skip to content
This repository was archived by the owner on Nov 21, 2020. It is now read-only.

Commit c9896ad

Browse files
BuyantogtokhBattulga BatAmar
authored andcommitted
feat(user): now users can unsubscribe from notification emails
close #655, close #654
1 parent bb97937 commit c9896ad

File tree

6 files changed

+67
-22
lines changed

6 files changed

+67
-22
lines changed

src/data/resolvers/mutations/engageUtils.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,3 @@ export const sendViaMessenger = async (
209209
});
210210
}
211211
};
212-
213-
/*
214-
* Handle engage unsubscribe request
215-
*/
216-
export const handleEngageUnSubscribe = (query: { cid: string }) =>
217-
Customers.updateOne({ _id: query.cid }, { $set: { doNotDisturb: 'Yes' } });

src/data/schema/user.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export const types = `
5858
getNotificationByEmail: Boolean
5959
groupIds: [String]
6060
brandIds: [String]
61+
doNotDisturb: String
6162
6263
brands: [Brand]
6364
isOwner: Boolean

src/data/utils.ts

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -320,20 +320,23 @@ export const createTransporter = ({ ses }) => {
320320
* Send email
321321
*/
322322
export const sendEmail = async ({
323-
toEmails,
323+
toEmails = [],
324324
fromEmail,
325325
title,
326326
template = {},
327+
modifier,
327328
}: {
328329
toEmails?: string[];
329330
fromEmail?: string;
330331
title?: string;
331332
template?: { name?: string; data?: any; isCustom?: boolean };
333+
modifier?: (data: any, email: string) => void;
332334
}) => {
333335
const NODE_ENV = getEnv({ name: 'NODE_ENV' });
334336
const DEFAULT_EMAIL_SERVICE = getEnv({ name: 'DEFAULT_EMAIL_SERVICE', defaultValue: '' }) || 'SES';
335337
const COMPANY_EMAIL_FROM = getEnv({ name: 'COMPANY_EMAIL_FROM' });
336338
const AWS_SES_CONFIG_SET = getEnv({ name: 'AWS_SES_CONFIG_SET', defaultValue: '' });
339+
const DOMAIN = getEnv({ name: 'DOMAIN' });
337340

338341
// do not send email it is running in test mode
339342
if (NODE_ENV === 'test') {
@@ -351,14 +354,21 @@ export const sendEmail = async ({
351354

352355
const { isCustom, data, name } = template;
353356

354-
// generate email content by given template
355-
let html = await applyTemplate(data, name || '');
357+
// for unsubscribe url
358+
data.domain = DOMAIN;
356359

357-
if (!isCustom) {
358-
html = await applyTemplate({ content: html }, 'base');
359-
}
360+
for (const toEmail of toEmails) {
361+
if (modifier) {
362+
modifier(data, toEmail);
363+
}
364+
365+
// generate email content by given template
366+
let html = await applyTemplate(data, name || '');
367+
368+
if (!isCustom) {
369+
html = await applyTemplate({ content: html }, 'base');
370+
}
360371

361-
return (toEmails || []).map(toEmail => {
362372
const mailOptions = {
363373
from: fromEmail || COMPANY_EMAIL_FROM,
364374
to: toEmail,
@@ -373,7 +383,7 @@ export const sendEmail = async ({
373383
debugEmail(error);
374384
debugEmail(info);
375385
});
376-
});
386+
}
377387
};
378388

379389
/**
@@ -406,7 +416,7 @@ export const sendNotification = async (doc: ISendNotification) => {
406416
const receiverIds = [...new Set(receivers)];
407417

408418
// collecting emails
409-
const recipients = await Users.find({ _id: { $in: receiverIds } });
419+
const recipients = await Users.find({ _id: { $in: receiverIds }, isActive: true, doNotDisturb: { $ne: 'Yes' } });
410420

411421
// collect recipient emails
412422
const toEmails: string[] = [];
@@ -439,12 +449,21 @@ export const sendNotification = async (doc: ISendNotification) => {
439449
throw e;
440450
}
441451
}
442-
}
452+
} // end receiverIds loop
443453

444454
const MAIN_APP_DOMAIN = getEnv({ name: 'MAIN_APP_DOMAIN' });
445455

446456
link = `${MAIN_APP_DOMAIN}${link}`;
447457

458+
// for controlling email template data filling
459+
const modifier = (data: any, email: string) => {
460+
const user = recipients.find(item => item.email === email);
461+
462+
if (user) {
463+
data.uid = user._id;
464+
}
465+
};
466+
448467
await sendEmail({
449468
toEmails,
450469
title: 'Notification',
@@ -456,6 +475,7 @@ export const sendNotification = async (doc: ISendNotification) => {
456475
userName: getUserDetail(createdUser),
457476
},
458477
},
478+
modifier,
459479
});
460480

461481
return true;
@@ -806,8 +826,13 @@ export const getToday = (date: Date): Date => {
806826

807827
export const getNextMonth = (date: Date): { start: number; end: number } => {
808828
const today = getToday(date);
829+
const currentMonth = new Date().getMonth();
809830

810-
const month = (new Date().getMonth() + 1) % 12;
831+
if (currentMonth === 11) {
832+
today.setFullYear(today.getFullYear() + 1);
833+
}
834+
835+
const month = (currentMonth + 1) % 12;
811836
const start = today.setMonth(month, 1);
812837
const end = today.setMonth(month + 1, 0);
813838

@@ -856,3 +881,20 @@ export const regexSearchText = (searchValue: string) => {
856881

857882
return { $and: result };
858883
};
884+
885+
/*
886+
* Handle engage unsubscribe request
887+
*/
888+
export const handleUnsubscription = async (query: { cid: string; uid: string }) => {
889+
const { cid, uid } = query;
890+
891+
if (cid) {
892+
await Customers.updateOne({ _id: cid }, { $set: { doNotDisturb: 'Yes' } });
893+
}
894+
895+
if (uid) {
896+
await Users.updateOne({ _id: uid }, { $set: { doNotDisturb: 'Yes' } });
897+
}
898+
899+
return true;
900+
};

src/db/models/definitions/users.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,5 @@ export const userSchema = new Schema({
115115
brandIds: field({ type: [String] }),
116116
groupIds: field({ type: [String] }),
117117
deviceTokens: field({ type: [String], default: [] }),
118+
doNotDisturb: field({ type: String, optional: true, default: 'No', label: 'Do not disturb' }),
118119
});

src/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ import { filterXSS } from 'xss';
1313
import apolloServer from './apolloClient';
1414
import { companiesExport, customersExport } from './data/modules/coc/exporter';
1515
import insightExports from './data/modules/insights/insightExports';
16-
import { handleEngageUnSubscribe } from './data/resolvers/mutations/engageUtils';
17-
import { checkFile, getEnv, readFileRequest, registerOnboardHistory, uploadFile } from './data/utils';
16+
import {
17+
checkFile,
18+
getEnv,
19+
handleUnsubscription,
20+
readFileRequest,
21+
registerOnboardHistory,
22+
uploadFile,
23+
} from './data/utils';
1824
import { connect } from './db/connection';
1925
import { debugExternalApi, debugInit } from './debuggers';
2026
import './messageBroker';
@@ -241,8 +247,8 @@ app.post('/import-file', async (req: any, res, next) => {
241247
});
242248

243249
// engage unsubscribe
244-
app.get('/unsubscribe', async (req, res) => {
245-
const unsubscribed = await handleEngageUnSubscribe(req.query);
250+
app.get('/unsubscribe', async (req: any, res) => {
251+
const unsubscribed = await handleUnsubscription(req.query);
246252

247253
if (unsubscribed) {
248254
res.setHeader('Content-Type', 'text/html; charset=utf-8');

src/private/emailTemplates/notification.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
role="presentation"
33
cellpadding="0"
44
cellspacing="0"
5-
style=" max-width: 560px;font-size:0px;width:100%;background: #fff; padding-bottom: 30px;box-shadow: 0px 0px 4px 0px rgba(0,0,0,0.2);border-radius: 10px;"
5+
style="max-width: 560px;font-size:0px;width:100%;background: #fff; padding-bottom: 30px;box-shadow: 0px 0px 4px 0px rgba(0,0,0,0.2);border-radius: 10px;"
66
align="center"
77
border="0"
88
>
@@ -80,6 +80,7 @@
8080
</tr>
8181
</tbody>
8282
</table>
83+
<div><a href="{{ domain }}/unsubscribe?uid={{uid}}" alt="Click here to unsubscribe">Click here to unsubscribe</a></div>
8384
</div>
8485
</td>
8586
</tr>

0 commit comments

Comments
 (0)