From 8386891db2f6cd1f5952773a2cda2927704fc003 Mon Sep 17 00:00:00 2001 From: Ishola Ayomide <101745279+Ay4codes@users.noreply.github.com> Date: Thu, 21 Sep 2023 13:27:51 +0100 Subject: [PATCH] Gift Lunch Controller --- controllers/emailConfirmationController.js | 94 +++++++++++----------- controllers/giftLunchController.js | 42 +++++----- routes/lunchRoutes.js | 2 +- utils/response.js | 14 ++-- 4 files changed, 73 insertions(+), 79 deletions(-) diff --git a/controllers/emailConfirmationController.js b/controllers/emailConfirmationController.js index 663758ab..ddead37c 100644 --- a/controllers/emailConfirmationController.js +++ b/controllers/emailConfirmationController.js @@ -1,15 +1,15 @@ // import bcrypt from "bcryptjs" -const { transport } = require("../config/nodemailerConfig.js"); -const dotenv = require("dotenv"); +const dotenv = require('dotenv'); +const { transport } = require('../config/nodemailerConfig.js'); dotenv.config(); const teamMail = process.env.TEAM_MAIL; const issueOtp = async (userId, email) => { const otp = `${Math.floor(1000 + Math.random() * 9000)}`; - const saltRounds = 12 //This should be in environment variable + const saltRounds = 12; //This should be in environment variable -// const hashedOTP = await bcrypt.hash(otp, saltRounds); + // const hashedOTP = await bcrypt.hash(otp, saltRounds); //Save hased otp with userId and email for confirmation purposes //Hased OTP should be saved to db for confirmation later,then deleted upon successful authentication @@ -20,8 +20,8 @@ const issueOtp = async (userId, email) => { }; }; -const otpMessage = (otp, timeLeft)=> { - let template = ` +const otpMessage = (otp, timeLeft) => { + const template = `

Welcome to XXXX 2.0

@@ -31,17 +31,16 @@ const otpMessage = (otp, timeLeft)=> {
`; - return template; -} - + return template; +}; -// Function to send email with otp code +// Function to send email with otp code const sendEmail = async (email, message) => { const mailOptions = { - from:'team.lightning.hng@gmail.com', //This should be in environement variable + from: 'team.lightning.hng@gmail.com', //This should be in environement variable subject: 'Verify your email', - to:email, - html: message + to: email, + html: message, }; return new Promise((resolve, reject) => { @@ -52,45 +51,42 @@ const sendEmail = async (email, message) => { }); }; - - -const sendUserOtp = async (userId,email) => { - try { - if (!userId || !email) { - return ({ - status: false, - message: `User details cannot be empty`, - }); - }; - - //generate a new otp - const otp = await issueOtp(userId, email); - const message = otpMessage(otp.userOtp, otp.timeLeft); - - //send mail with otp details - await sendEmail(email,message); - - return ({ - status: true, - message: "otp sent successfully", - data: null - }); - } catch (error) { - console.log(error); - return ({ +const sendUserOtp = async (userId, email) => { + try { + if (!userId || !email) { + return { status: false, - message: `internal server error`, - }); + message: `User details cannot be empty`, + }; } - }; - module.exports ={ - issueOtp, - otpMessage, - sendEmail, - sendUserOtp + //generate a new otp + const otp = await issueOtp(userId, email); + const message = otpMessage(otp.userOtp, otp.timeLeft); + + //send mail with otp details + await sendEmail(email, message); + + return { + status: true, + message: 'otp sent successfully', + data: null, + }; + } catch (error) { + console.log(error); + return { + status: false, + message: `internal server error`, + }; } +}; +module.exports = { + issueOtp, + otpMessage, + sendEmail, + sendUserOtp, +}; - //call this function to test controller - // sendUserOtp('1','test@example.com') \ No newline at end of file +//call this function to test controller +// sendUserOtp('1','test@example.com') diff --git a/controllers/giftLunchController.js b/controllers/giftLunchController.js index c7f64b28..5152a1d8 100644 --- a/controllers/giftLunchController.js +++ b/controllers/giftLunchController.js @@ -4,37 +4,37 @@ const User = require('../models/user.model'); const response = require('../utils/response'); const giftLunch = async (req, res) => { - try { - const userId = req.user.id; + try { + const userId = req.user.id; - const {receiver_id, quantity, note } = req.body + const { receiverId, quantity, note } = req.body; - if (!receiver_id || !quantity || !note) return res.status(400).json(response(false, 'Missing required fields', null)); + if (!receiverId || !quantity || !note) return res.status(400).json(response(false, 'Missing required fields', null)); - const user = await User.findOne({ where: { id: userId } }); + const user = await User.findOne({ where: { id: userId } }); - if (!user) return res.status(404).json(response(false, 'User does not exist', null)); + if (!user) return res.status(404).json(response(false, 'User does not exist', null)); - const lunch = {receiver_id, quantity, note, redeemed: false} + const lunch = { receiverId, quantity, note, redeemed: false }; - // Create Launch - const newLunch = await Lunch.create(lunch) + // Create Launch + const newLunch = await Lunch.create(lunch); - const sender = await User.findOne({ where: { id: userId } }); + const sender = await User.findOne({ where: { id: userId } }); - const receiver = await User.findOne({ where: { id: receiver_id } }); + const receiver = await User.findOne({ where: { id: receiverId } }); - //Update the sender's balance - await sender.update({balance: sender.balance - quantity}); + //Update the sender's balance + await sender.update({ balance: sender.balance - quantity }); - //Update the receiver's balance - await receiver.update({balance: receiver.balance + quantity}); + //Update the receiver's balance + await receiver.update({ balance: receiver.balance + quantity }); - return res.status(201).json(response(true, 'Lunch gifted successfully', {lunch: newLunch})); + return res.status(201).json(response(true, 'Lunch gifted successfully', { lunch: newLunch })); - } catch (error) { - res.status(500).json(response(false, 'Internal Server Error', null)); - } -} + } catch (error) { + return res.status(500).json(response(false, 'Internal Server Error', null)); + } +}; -module.exports = {giftLunch} \ No newline at end of file +module.exports = { giftLunch }; diff --git a/routes/lunchRoutes.js b/routes/lunchRoutes.js index 69337c73..8749c450 100644 --- a/routes/lunchRoutes.js +++ b/routes/lunchRoutes.js @@ -8,6 +8,6 @@ const { giftLunch } = require('../controllers/giftLunchController'); router.get('/user/:userId/lunch/all', lunchControllers.getAllLunch); // Gift Launch -router.post('/gift_lunch', giftLunch) +router.post('/gift_lunch', giftLunch); module.exports = router; diff --git a/utils/response.js b/utils/response.js index 1aca66a3..4c501eb8 100644 --- a/utils/response.js +++ b/utils/response.js @@ -1,9 +1,7 @@ -const response = (success, message, data) => { - return { - success: success, - message: message, - data: data - } -} +const response = (success, message, data) => ({ + success: success, + message: message, + data: data, +}); -module.exports = (response) \ No newline at end of file +module.exports = response;