Skip to content

Commit

Permalink
feat(api): send welcome message after creating congregation account
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Sep 20, 2023
1 parent b907495 commit dfca13e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/controllers/congregation-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { users } from '../classes/Users.js';
import { congregations } from '../classes/Congregations.js';
import { allowedRoles, createCongregationAllowedRoles } from '../constant/constant.js';
import { LANGUAGE_LIST } from '../locales/langList.js';
import { sendWelcomeCPE } from '../utils/sendEmail.js';

export const getLastCongregationBackup = async (req, res, next) => {
try {
Expand Down Expand Up @@ -1429,13 +1430,39 @@ export const createCongregation = async (req, res, next) => {
return;
}

// is congregation authentic
const language = req.headers.applanguage || 'e';
const url = process.env.APP_CONGREGATION_API + new URLSearchParams({ country: country_code, language, name: cong_number });

const response = await fetch(url);
if (response.status !== 200) {
res.locals.type = 'warn';
res.locals.message = 'an error occured while verifying the congregation data';
res.status(response.status).json({ message: 'REQUEST_NOT_VALIDATED' });

return;
}

const congsList = await response.json();
const tmpCong = congsList[0];

if (tmpCong.congName !== cong_name || tmpCong.congNumber !== cong_number) {
res.locals.type = 'warn';
res.locals.message = 'this request does not match any valid congregation';
res.status(400).json({ message: 'BAD_REQUEST' });

return;
}

// create congregation
const newCong = await congregations.create({ country_code, cong_name, cong_number });

// add user to congregation
const tmpUser = users.findUserByAuthUid(uid);
const user = await newCong.addUser(tmpUser.id, ['admin', app_requestor], fullname);

sendWelcomeCPE(user.user_uid, fullname, `${cong_name} (${cong_number})`, language);

res.locals.type = 'info';
res.locals.message = 'congregation created successfully';
res.status(200).json(user);
Expand Down
4 changes: 3 additions & 1 deletion src/locales/en/main.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"emailFooter": "<p>Thank you\n<br>The Scheduling Workbox System Team</p>",
"passwordLessSubject": "Passwordless Link to Sign in to CPE app",
"passwordLessTemplate": "<p>Hello</p>\n<p>We received a request to sign in to Congregation Program for Everyone using this email address. If you want to sign in with your {{ email }} account, please use the link below:</p>\n<p><a href='{{ linkPasswordLess }}'>Sign in</a></p>\n<p>If you did not request this link, you can safely ignore this email.</p>\n<p>If you are having trouble opening the link above, please use the following link. Make sure the link is not cropped when you are opening it:</p>\n<p>{{ linkPasswordLess }}</p>"
"passwordLessTemplate": "<p>Hello</p>\n<p>We received a request to sign in to Congregation Program for Everyone using this email address. If you want to sign in with your {{ email }} account, please use the link below:</p>\n<p><a href='{{ linkPasswordLess }}'>Sign in</a></p>\n<p>If you did not request this link, you can safely ignore this email.</p>\n<p>If you are having trouble opening the link above, please use the following link. Make sure the link is not cropped when you are opening it:</p>\n<p>{{ linkPasswordLess }}</p>",
"welcomeCPESubject": "Welcome to CPE",
"welcomeCPETemplate": "<p>Hello {{ name }},</p>\n<p>You recently created an sws2apps account to be used with the <strong>Congregation Program for Everyone (CPE)</strong> application. Your account is now ready to use with the following congregation: <strong>{{ congregation }}.</strong></p>\n<p>To begin with, we are suggesting you to take a tour at our <a href='https://sws2apps.com/docs/category/congregation-program-for-everyone'>documentation website</a> to learn more about all the features of CPE application.</p>\n<p>It is good to have you on board, and we warmly welcome you. 🎉🎉🎉</p>"
}
17 changes: 17 additions & 0 deletions src/utils/sendEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,20 @@ export const sendPasswordlessLinkSignIn = async (recipient, link, templateLang)

sendEmail(options, 'Passwordless link sent to user');
};

export const sendWelcomeCPE = async (recipient, name, congregation, templateLang) => {
const t = i18n(templateLang.toLowerCase());

const options = {
from: gmailConfig.sender,
to: recipient,
subject: t('welcomeCPESubject'),
template: 'welcomeCPE',
context: {
welcomeCPETemplate: t('welcomeCPETemplate', { name, congregation }),
emailFooter: t('emailFooter'),
},
};

sendEmail(options, 'Welcome message sent to user');
};
13 changes: 13 additions & 0 deletions src/views/welcomeCPE.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to CPE</title>
</head>
<body>
{{{ welcomeCPETemplate }}}
{{{ emailFooter }}}
</body>
</html>

0 comments on commit dfca13e

Please sign in to comment.