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

Commit

Permalink
feat(user): now users can unsubscribe from notification emails
Browse files Browse the repository at this point in the history
close #655, close #654
  • Loading branch information
Buyantogtokh authored and batamar committed Dec 1, 2019
1 parent bb97937 commit c9896ad
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 22 deletions.
6 changes: 0 additions & 6 deletions src/data/resolvers/mutations/engageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,3 @@ export const sendViaMessenger = async (
});
}
};

/*
* Handle engage unsubscribe request
*/
export const handleEngageUnSubscribe = (query: { cid: string }) =>
Customers.updateOne({ _id: query.cid }, { $set: { doNotDisturb: 'Yes' } });
1 change: 1 addition & 0 deletions src/data/schema/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const types = `
getNotificationByEmail: Boolean
groupIds: [String]
brandIds: [String]
doNotDisturb: String
brands: [Brand]
isOwner: Boolean
Expand Down
64 changes: 53 additions & 11 deletions src/data/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,20 +320,23 @@ export const createTransporter = ({ ses }) => {
* Send email
*/
export const sendEmail = async ({
toEmails,
toEmails = [],
fromEmail,
title,
template = {},
modifier,
}: {
toEmails?: string[];
fromEmail?: string;
title?: string;
template?: { name?: string; data?: any; isCustom?: boolean };
modifier?: (data: any, email: string) => void;
}) => {
const NODE_ENV = getEnv({ name: 'NODE_ENV' });
const DEFAULT_EMAIL_SERVICE = getEnv({ name: 'DEFAULT_EMAIL_SERVICE', defaultValue: '' }) || 'SES';
const COMPANY_EMAIL_FROM = getEnv({ name: 'COMPANY_EMAIL_FROM' });
const AWS_SES_CONFIG_SET = getEnv({ name: 'AWS_SES_CONFIG_SET', defaultValue: '' });
const DOMAIN = getEnv({ name: 'DOMAIN' });

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

const { isCustom, data, name } = template;

// generate email content by given template
let html = await applyTemplate(data, name || '');
// for unsubscribe url
data.domain = DOMAIN;

if (!isCustom) {
html = await applyTemplate({ content: html }, 'base');
}
for (const toEmail of toEmails) {
if (modifier) {
modifier(data, toEmail);
}

// generate email content by given template
let html = await applyTemplate(data, name || '');

if (!isCustom) {
html = await applyTemplate({ content: html }, 'base');
}

return (toEmails || []).map(toEmail => {
const mailOptions = {
from: fromEmail || COMPANY_EMAIL_FROM,
to: toEmail,
Expand All @@ -373,7 +383,7 @@ export const sendEmail = async ({
debugEmail(error);
debugEmail(info);
});
});
}
};

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

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

// collect recipient emails
const toEmails: string[] = [];
Expand Down Expand Up @@ -439,12 +449,21 @@ export const sendNotification = async (doc: ISendNotification) => {
throw e;
}
}
}
} // end receiverIds loop

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

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

// for controlling email template data filling
const modifier = (data: any, email: string) => {
const user = recipients.find(item => item.email === email);

if (user) {
data.uid = user._id;
}
};

await sendEmail({
toEmails,
title: 'Notification',
Expand All @@ -456,6 +475,7 @@ export const sendNotification = async (doc: ISendNotification) => {
userName: getUserDetail(createdUser),
},
},
modifier,
});

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

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

const month = (new Date().getMonth() + 1) % 12;
if (currentMonth === 11) {
today.setFullYear(today.getFullYear() + 1);
}

const month = (currentMonth + 1) % 12;
const start = today.setMonth(month, 1);
const end = today.setMonth(month + 1, 0);

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

return { $and: result };
};

/*
* Handle engage unsubscribe request
*/
export const handleUnsubscription = async (query: { cid: string; uid: string }) => {
const { cid, uid } = query;

if (cid) {
await Customers.updateOne({ _id: cid }, { $set: { doNotDisturb: 'Yes' } });
}

if (uid) {
await Users.updateOne({ _id: uid }, { $set: { doNotDisturb: 'Yes' } });
}

return true;
};
1 change: 1 addition & 0 deletions src/db/models/definitions/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,5 @@ export const userSchema = new Schema({
brandIds: field({ type: [String] }),
groupIds: field({ type: [String] }),
deviceTokens: field({ type: [String], default: [] }),
doNotDisturb: field({ type: String, optional: true, default: 'No', label: 'Do not disturb' }),
});
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ import { filterXSS } from 'xss';
import apolloServer from './apolloClient';
import { companiesExport, customersExport } from './data/modules/coc/exporter';
import insightExports from './data/modules/insights/insightExports';
import { handleEngageUnSubscribe } from './data/resolvers/mutations/engageUtils';
import { checkFile, getEnv, readFileRequest, registerOnboardHistory, uploadFile } from './data/utils';
import {
checkFile,
getEnv,
handleUnsubscription,
readFileRequest,
registerOnboardHistory,
uploadFile,
} from './data/utils';
import { connect } from './db/connection';
import { debugExternalApi, debugInit } from './debuggers';
import './messageBroker';
Expand Down Expand Up @@ -241,8 +247,8 @@ app.post('/import-file', async (req: any, res, next) => {
});

// engage unsubscribe
app.get('/unsubscribe', async (req, res) => {
const unsubscribed = await handleEngageUnSubscribe(req.query);
app.get('/unsubscribe', async (req: any, res) => {
const unsubscribed = await handleUnsubscription(req.query);

if (unsubscribed) {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
Expand Down
3 changes: 2 additions & 1 deletion src/private/emailTemplates/notification.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
role="presentation"
cellpadding="0"
cellspacing="0"
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;"
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;"
align="center"
border="0"
>
Expand Down Expand Up @@ -80,6 +80,7 @@
</tr>
</tbody>
</table>
<div><a href="{{ domain }}/unsubscribe?uid={{uid}}" alt="Click here to unsubscribe">Click here to unsubscribe</a></div>
</div>
</td>
</tr>
Expand Down

0 comments on commit c9896ad

Please sign in to comment.