Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions client/src/helpers/APICalls/sendEmails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { FetchOptions } from '../../interface/FetchOptions';
import { EmailApiData } from '../../interface/Email';

interface Props {
receiverID: string;
contestID: string;
}

export async function sendWinnerEmail({ receiverID, contestID }: Props): Promise<EmailApiData> {
const fetchOptions: FetchOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ receiverID }),
credentials: 'include',
};
return await fetch(`/contest/${contestID}/winner`, fetchOptions)
.then((res) => res.json())
.catch(() => ({
error: { message: 'Unable to connect to server. Please try again' },
}));
}
1 change: 0 additions & 1 deletion client/src/helpers/APICalls/updateProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export async function updateProfile({ profileImage }: Props): Promise<ProfileApi
body: JSON.stringify({ profileImage, status }),
credentials: 'include',
};
console.log(`Posting ${profileImage}`);
return await fetch(`/users/profile`, fetchOptions)
.then((res) => res.json())
.catch(() => ({
Expand Down
4 changes: 4 additions & 0 deletions client/src/interface/Email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface EmailApiData {
error?: { message: string };
status?: string;
}
58 changes: 58 additions & 0 deletions server/controllers/sendgrid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const asyncHandler = require("express-async-handler");
const sgMail = require('@sendgrid/mail')

const User = require("../models/User");
const Contest = require("../models/Contest");

sgMail.setApiKey(process.env.SENDGRID_API_KEY)

exports.sendWinnerEmail = asyncHandler(async (req, res)=> {
const userID = req.user.id;
const contestID = req.params.id;
let success = false;
const { receiverID } = req.body;
try {
const user = await User.findById(userID);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for searching up a 'user' here? If he doesn't exist, how would he have logged in? :)

const receiver = await User.findById(receiverID);
if (!user || !receiver){
return res.status(500).json({
Copy link
Contributor

@rajivtitus rajivtitus Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this to a 'Not found' status code

status: "User not found"
})
}
const contest = await Contest.findById(contestID);
if (!contest){
return res.status(500).json({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

status: "Contest not found"
})
}
const msg = {
to: receiver.email,
from: {
name:'TeamVanillaDip',
email: '[email protected]',
},
subject: "Contest Winner",
text: `You have been selected a winner in the '${contest.title}'`,
}
await sgMail
.send(msg)
.then(() => {
console.log('Email sent')
success = true;
})
.catch((error) => {
console.error(error)
})
if (success) {
return res.status(200).json({
status: 'Email Sent Successfully'
})
}
return res.status(500).json({status: "An error Occured"})
} catch (error) {
console.error(error)
return res.status(500).json({status: "error",
error
})
}
})
2 changes: 1 addition & 1 deletion server/controllers/submission.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Submission = require("../models/Submission");
const Contest = require("../models/Contest");
const asyncHandler = require("express-async-handler");
const asyncHandler = require("express-async-handler")

exports.createSubmission = asyncHandler(async (req, res, next) => {
const userID = req.user.id;
Expand Down
44 changes: 44 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dev": "nodemon ./bin/www"
},
"dependencies": {
"@sendgrid/mail": "^7.4.5",
"aws-sdk": "^2.949.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
Expand Down
3 changes: 3 additions & 0 deletions server/routes/contest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ const {
getSubmissionByContestId
} = require("../controllers/contest");

const { sendWinnerEmail } = require("../controllers/sendgrid");

const { createSubmission } = require('../controllers/submission')

// CREATE
router.route("/create").post(protect, createContest);
router.route("/:id/submission").post(protect,createSubmission)
router.route("/:id/winner").post(protect, sendWinnerEmail)

// READ
router.route("/:id").get(getContestById);
Expand Down