generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2214 from Real-Dev-Squad/develop
Dev to Main Sync
- Loading branch information
Showing
24 changed files
with
709 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; | ||
export const phoneNumberRegex = /^[+]{1}(?:[0-9\-\\(\\)\\/.]\s?){6,15}[0-9]{1}$/; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { CustomRequest, CustomResponse } from "../types/global"; | ||
const { addOrUpdate } = require("../models/users"); | ||
const { INTERNAL_SERVER_ERROR } = require("../constants/errorMessages"); | ||
const nodemailer = require("nodemailer"); | ||
const config = require("config"); | ||
const emailServiceConfig = config.get("emailServiceConfig"); | ||
|
||
export const subscribe = async (req: CustomRequest, res: CustomResponse) => { | ||
const { email } = req.body; | ||
const phoneNumber = req.body.phoneNumber || null; | ||
const userId = req.userData.id; | ||
const data = { email, isSubscribed: true, phoneNumber }; | ||
const userAlreadySubscribed = req.userData.isSubscribed; | ||
try { | ||
if (userAlreadySubscribed) { | ||
return res.boom.badRequest("User already subscribed"); | ||
} | ||
await addOrUpdate(data, userId); | ||
return res.status(201).json("User subscribed successfully"); | ||
} catch (error) { | ||
logger.error(`Error occurred while subscribing: ${error.message}`); | ||
return res.boom.badImplementation(INTERNAL_SERVER_ERROR); | ||
} | ||
}; | ||
|
||
export const unsubscribe = async (req: CustomRequest, res: CustomResponse) => { | ||
const userId = req.userData.id; | ||
const userAlreadySubscribed = req.userData.isSubscribed; | ||
try { | ||
if (!userAlreadySubscribed) { | ||
return res.boom.badRequest("User is already unsubscribed"); | ||
} | ||
await addOrUpdate( | ||
{ | ||
isSubscribed: false, | ||
}, | ||
userId | ||
); | ||
return res.status(200).json("User unsubscribed successfully"); | ||
} catch (error) { | ||
logger.error(`Error occurred while unsubscribing: ${error.message}`); | ||
return res.boom.badImplementation(INTERNAL_SERVER_ERROR); | ||
} | ||
}; | ||
|
||
// TODO: currently we are sending test email to a user only (i.e., Tejas sir as decided) | ||
// later we need to make service which send email to all subscribed user | ||
export const sendEmail = async (req: CustomRequest, res: CustomResponse) => { | ||
try { | ||
const transporter = nodemailer.createTransport({ | ||
host: emailServiceConfig.host, | ||
port: emailServiceConfig.port, | ||
secure: false, | ||
|
||
auth: { | ||
user: emailServiceConfig.email, | ||
pass: emailServiceConfig.password, | ||
}, | ||
}); | ||
|
||
const info = await transporter.sendMail({ | ||
from: `"Real Dev Squad" <${emailServiceConfig.email}>`, | ||
to: "[email protected]", | ||
subject: "Hello local, Testing in progress.", | ||
text: "working for notification feature", | ||
html: "<b>Hello world!</b>", | ||
}); | ||
|
||
return res.send({ message: "Email sent successfully", info }); | ||
} catch (error) { | ||
logger.error("Error occurred while sending email:", error.message); | ||
return res.boom.badImplementation(INTERNAL_SERVER_ERROR); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const authenticateProfile = (authenticate) => { | ||
return async (req, res, next) => { | ||
if (req.query.profile === "true") { | ||
return await authenticate(req, res, next); | ||
} | ||
return next(); | ||
}; | ||
}; | ||
|
||
module.exports = authenticateProfile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { NextFunction } from "express"; | ||
import { CustomRequest, CustomResponse } from "../types/global"; | ||
|
||
export const devFlagMiddleware = (req: CustomRequest, res: CustomResponse, next: NextFunction) => { | ||
try { | ||
const dev = req.query.dev === "true"; | ||
if (!dev) { | ||
return res.boom.notFound("Route not found"); | ||
} | ||
next(); | ||
} catch (err) { | ||
logger.error("Error occurred in devFlagMiddleware:", err.message); | ||
next(err); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { NextFunction } from "express"; | ||
import { CustomRequest, CustomResponse } from "../../types/global"; | ||
import { emailRegex, phoneNumberRegex } from "../../constants/subscription-validator"; | ||
import Joi from 'joi'; | ||
|
||
export const validateSubscribe = (req: CustomRequest, res: CustomResponse, next: NextFunction) => { | ||
|
||
if(req.body.email){ | ||
req.body.email = req.body.email.trim(); | ||
} | ||
if (req.body.phoneNumber) { | ||
req.body.phoneNumber = req.body.phoneNumber.trim(); | ||
} | ||
const subscribeSchema = Joi.object({ | ||
phoneNumber: Joi.string().allow('').optional().regex(phoneNumberRegex), | ||
email: Joi.string().required().regex(emailRegex) | ||
}); | ||
const { error } = subscribeSchema.validate(req.body); | ||
if (error) { | ||
return res.status(400).json({ error: error.details[0].message }); | ||
} | ||
next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import express from "express"; | ||
import authenticate from "../middlewares/authenticate"; | ||
import { subscribe, unsubscribe, sendEmail } from "../controllers/subscription"; | ||
import { validateSubscribe } from "../middlewares/validators/subscription"; | ||
const authorizeRoles = require("../middlewares/authorizeRoles"); | ||
const router = express.Router(); | ||
const { SUPERUSER } = require("../constants/roles"); | ||
|
||
router.post("/", authenticate, validateSubscribe, subscribe); | ||
router.patch("/", authenticate, unsubscribe); | ||
router.get("/notify", authenticate, authorizeRoles([SUPERUSER]), sendEmail); | ||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const subscribedMessage = "User subscribed successfully"; | ||
export const unSubscribedMessage = "User unsubscribed successfully"; | ||
export const subscriptionData = { | ||
phoneNumber: "+911234567890", | ||
email: "[email protected]", | ||
}; | ||
|
Oops, something went wrong.