From dfca13e2d80e45d11a0c18a7eea2ef35a18654d9 Mon Sep 17 00:00:00 2001 From: rhahao <26148770+rhahao@users.noreply.github.com> Date: Tue, 19 Sep 2023 20:02:13 +0300 Subject: [PATCH] feat(api): send welcome message after creating congregation account --- src/controllers/congregation-controller.js | 27 ++++++++++++++++++++++ src/locales/en/main.json | 4 +++- src/utils/sendEmail.js | 17 ++++++++++++++ src/views/welcomeCPE.handlebars | 13 +++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/views/welcomeCPE.handlebars diff --git a/src/controllers/congregation-controller.js b/src/controllers/congregation-controller.js index f372acc2..bc36a92f 100644 --- a/src/controllers/congregation-controller.js +++ b/src/controllers/congregation-controller.js @@ -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 { @@ -1429,6 +1430,30 @@ 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 }); @@ -1436,6 +1461,8 @@ export const createCongregation = async (req, res, next) => { 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); diff --git a/src/locales/en/main.json b/src/locales/en/main.json index 4073b9f6..ee0f88ac 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -1,5 +1,7 @@ { "emailFooter": "
Thank you\n
The Scheduling Workbox System Team
Hello
\nWe 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:
\n\nIf you did not request this link, you can safely ignore this email.
\nIf 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:
\n{{ linkPasswordLess }}
" + "passwordLessTemplate": "Hello
\nWe 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:
\n\nIf you did not request this link, you can safely ignore this email.
\nIf 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:
\n{{ linkPasswordLess }}
", + "welcomeCPESubject": "Welcome to CPE", + "welcomeCPETemplate": "Hello {{ name }},
\nYou recently created an sws2apps account to be used with the Congregation Program for Everyone (CPE) application. Your account is now ready to use with the following congregation: {{ congregation }}.
\nTo begin with, we are suggesting you to take a tour at our documentation website to learn more about all the features of CPE application.
\nIt is good to have you on board, and we warmly welcome you. 🎉🎉🎉
" } diff --git a/src/utils/sendEmail.js b/src/utils/sendEmail.js index d2648773..d0c64626 100644 --- a/src/utils/sendEmail.js +++ b/src/utils/sendEmail.js @@ -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'); +}; diff --git a/src/views/welcomeCPE.handlebars b/src/views/welcomeCPE.handlebars new file mode 100644 index 00000000..9feb22c5 --- /dev/null +++ b/src/views/welcomeCPE.handlebars @@ -0,0 +1,13 @@ + + + + + + +