-
Notifications
You must be signed in to change notification settings - Fork 2
Contest winner through email #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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' }, | ||
| })); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export interface EmailApiData { | ||
| error?: { message: string }; | ||
| status?: string; | ||
| } |
| 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); | ||
| const receiver = await User.findById(receiverID); | ||
| if (!user || !receiver){ | ||
| return res.status(500).json({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| }) | ||
| } | ||
| }) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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? :)