Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminSsempala committed Feb 14, 2024
1 parent e4b1470 commit f438919
Show file tree
Hide file tree
Showing 9 changed files with 511 additions and 170 deletions.
14 changes: 9 additions & 5 deletions src/auth-service/config/global/email-templates.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const mongoose = require("mongoose");
const ObjectId = mongoose.Types.ObjectId;

const baseUrl = process.env.PLATFORM_STAGING_BASE_URL || process.env.PLATFORM_PRODUCTION_BASE_URL;

const emailTemplates = {
EMAIL_GREETINGS: function (name) {
return `<tr>
Expand All @@ -23,7 +25,8 @@ const emailTemplates = {
</table>
`;
},
EMAIL_FOOTER_TEMPLATE: function (email) {
EMAIL_FOOTER_TEMPLATE: function (email, product, type, paramString) {
const unSubsciptionUrl = `${baseUrl}/api/v2/users/unsubscribe/${product}/${type}?${paramString}`;

Check warning on line 29 in src/auth-service/config/global/email-templates.js

View check run for this annotation

Codecov / codecov/patch

src/auth-service/config/global/email-templates.js#L28-L29

Added lines #L28 - L29 were not covered by tests
return `
<table style="width: 100%; text-align: center; padding-top: 32px; padding-bottom: 32px;">
<tr>
Expand Down Expand Up @@ -56,8 +59,9 @@ const emailTemplates = {
<span
style="color: #667085; font-size: 14px; font-family: Inter; font-weight: 400; line-height: 20px; word-wrap: break-word;">.
If you'd rather not receive this kind of email, you can </span>
<span
<a href =${unSubsciptionUrl} target = "_blank" <span
style="color: #135DFF; font-size: 14px; font-family: Inter; font-weight: 400; line-height: 20px; word-wrap: break-word;">unsubscribe</span>
</a>
<span
style="color: #667085; font-size: 14px; font-family: Inter; font-weight: 400; line-height: 20px; word-wrap: break-word;">
or </span>
Expand All @@ -76,11 +80,11 @@ const emailTemplates = {
`;
},

EMAIL_BODY: function (email, content, name) {
const footerTemplate = this.EMAIL_FOOTER_TEMPLATE(email);
EMAIL_BODY: function (email, content, name, product, type, paramString) {
const footerTemplate = this.EMAIL_FOOTER_TEMPLATE(email, product, type, paramString);

Check warning on line 84 in src/auth-service/config/global/email-templates.js

View check run for this annotation

Codecov / codecov/patch

src/auth-service/config/global/email-templates.js#L83-L84

Added lines #L83 - L84 were not covered by tests
const headerTemplate = this.EMAIL_HEADER_TEMPLATE();
let greetings = this.EMAIL_GREETINGS(name);
if (!name) {
if (!name || name === "") {
greetings = ``;
}
return `<!DOCTYPE html>
Expand Down
16 changes: 11 additions & 5 deletions src/auth-service/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,17 @@ const UserSchema = new Schema(
category: {
type: String,
},
notifications: {
email: { type: Boolean, default: false },
push: { type: Boolean, default: false },
text: { type: Boolean, default: false },
phone: { type: Boolean, default: false },
mobile_notifications: {
email: { type: Boolean, default: true },
push: { type: Boolean, default: true },
text: { type: Boolean, default: true },
phone: { type: Boolean, default: true },
},
analytics_notifications: {
email: { type: Boolean, default: true },
push: { type: Boolean, default: true },
text: { type: Boolean, default: true },
phone: { type: Boolean, default: true },
},
profilePicture: {
type: String,
Expand Down
146 changes: 82 additions & 64 deletions src/auth-service/routes/v2/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -1041,8 +1041,8 @@ router.get(
);

/*********************************** user notifications **********************/
router.post(
"/subscribe/:type",
router.get(
"/subscribe/:product/:type",
oneOf([
[
query("tenant")
Expand All @@ -1054,40 +1054,49 @@ router.post(
.toLowerCase()
.isIn(["airqo"])
.withMessage("the tenant value is not among the expected ones"),
query("email")
.optional()
.notEmpty()
.withMessage("the email must not be empty if provided")
.bail()
.isEmail()
.withMessage("this is not a valid email address")
.trim(),
query("mongo_user_id")
.optional()
.notEmpty()
.withMessage("the mongo_user_id must not be empty if provided")
.bail()
.trim()
.isMongoId()
.withMessage("the mongo_user_id must be an object ID")
.bail()
.customSanitizer((value) => {
return ObjectId(value);
}),
query("firebase_user_id")
.optional()
.notEmpty()
.withMessage("the firebase_uid must not be empty if provided")
.bail()
.trim(),
],
]),
oneOf([
body("email")
.exists()
.withMessage(
"the user identifier is missing in request, consider using the email"
)
.bail()
.notEmpty()
.withMessage("the email must not be empty if provided")
.bail()
.isEmail()
.withMessage("this is not a valid email address")
.trim(),
body("user_id")
.exists()
.withMessage(
"the user identifier is missing in request, consider using the user_id"
)
.bail()
.notEmpty()
.withMessage("the user_id must not be empty if provided")
.bail()
.trim()
.isMongoId()
.withMessage("the user_id must be an object ID")
.bail()
.customSanitizer((value) => {
return ObjectId(value);
}),
]),
oneOf([
[
param("product")
.exists()
.withMessage("the product must be provided")
.bail()
.notEmpty()
.withMessage("the product should not be empty if provided")
.bail()
.trim()
.toLowerCase()
.isIn(["analytics", "mobile", "website"])
.withMessage(
"the product value is not among the expected ones: analytics, mobile and website"
),
param("type")
.exists()
.withMessage("the type must be provided")
Expand All @@ -1105,8 +1114,8 @@ router.post(
]),
createUserController.subscribeToNotifications
);
router.post(
"/unsubscribe/:type",
router.get(
"/unsubscribe/:product/:type",
oneOf([
[
query("tenant")
Expand All @@ -1118,40 +1127,49 @@ router.post(
.toLowerCase()
.isIn(["airqo"])
.withMessage("the tenant value is not among the expected ones"),
query("email")
.optional()
.notEmpty()
.withMessage("the email must not be empty if provided")
.bail()
.isEmail()
.withMessage("this is not a valid email address")
.trim(),
query("mongo_user_id")
.optional()
.notEmpty()
.withMessage("the mongo_user_id must not be empty if provided")
.bail()
.trim()
.isMongoId()
.withMessage("the mongo_user_id must be an object ID")
.bail()
.customSanitizer((value) => {
return ObjectId(value);
}),
query("firebase_user_id")
.optional()
.notEmpty()
.withMessage("the firebase_uid must not be empty if provided")
.bail()
.trim(),
],
]),
oneOf([
body("email")
.exists()
.withMessage(
"the user identifier is missing in request, consider using the email"
)
.bail()
.notEmpty()
.withMessage("the email must not be empty if provided")
.bail()
.isEmail()
.withMessage("this is not a valid email address")
.trim(),
body("user_id")
.exists()
.withMessage(
"the user identifier is missing in request, consider using the user_id"
)
.bail()
.notEmpty()
.withMessage("the user_id must not be empty if provided")
.bail()
.trim()
.isMongoId()
.withMessage("the user_id must be an object ID")
.bail()
.customSanitizer((value) => {
return ObjectId(value);
}),
]),
oneOf([
[
param("product")
.exists()
.withMessage("the product must be provided")
.bail()
.notEmpty()
.withMessage("the product should not be empty if provided")
.bail()
.trim()
.toLowerCase()
.isIn(["analytics", "mobile", "website"])
.withMessage(
"the product value is not among the expected ones: analytics, mobile and website"
),
param("type")
.exists()
.withMessage("the type must be provided")
Expand Down
2 changes: 2 additions & 0 deletions src/auth-service/utils/create-candidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ const createCandidate = {

if (responseFromCreateCandidate.success === true) {
const createdCandidate = await responseFromCreateCandidate.data;
const user_id = createdCandidate._id;
const responseFromSendEmail = await mailer.candidate(
{
firstName,
lastName,
email,
tenant,
user_id
},
next
);
Expand Down
5 changes: 5 additions & 0 deletions src/auth-service/utils/create-inquiry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const UserModel = require("@models/User");
const InquiryModel = require("@models/Inquiry");
const { logObject } = require("@utils/log");
const mailer = require("@utils/mailer");
const CreateUserUtil = require("@utils/create-user");
const httpStatus = require("http-status");
const constants = require("@config/constants");
const generatFilter = require("@utils/generate-filter");
Expand Down Expand Up @@ -32,6 +34,8 @@ const inquiry = {
);

if (responseFromCreateInquiry.success === true) {
const responseFromListUser = await CreateUserUtil.list(request, next);
const user_id = responseFromListUser.data[0]._id;
const createdInquiry = await responseFromCreateInquiry.data;
const responseFromSendEmail = await mailer.inquiry(
{
Expand All @@ -40,6 +44,7 @@ const inquiry = {
category,
message,
tenant,
user_id
},
next
);
Expand Down
2 changes: 2 additions & 0 deletions src/auth-service/utils/create-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const createAccessRequest = {
email: user.email,
tenant,
entity_title: group.grp_title,
user_id: user._id,
},
next
);
Expand Down Expand Up @@ -486,6 +487,7 @@ const createAccessRequest = {
email: user.email,
tenant,
entity_title: network.net_name,
user_id: user._id,
},
next
);
Expand Down
Loading

0 comments on commit f438919

Please sign in to comment.