Skip to content

Commit

Permalink
feat: implemented user invite with sendgrid
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyton505 committed Nov 3, 2024
1 parent e51654b commit e9d1bf0
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 17 deletions.
1 change: 0 additions & 1 deletion api/.env

This file was deleted.

Binary file added api/assets/pww-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 84 additions & 9 deletions api/package-lock.json

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

3 changes: 2 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@sendgrid/mail": "^8.1.4",
"axios": "^1.7.7",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
Expand All @@ -28,7 +29,7 @@
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/node": "^22.5.4",
"@types/node": "^22.8.1",
"@types/uuid": "^10.0.0",
"body-parser": "^1.20.3",
"prettier": "^3.3.3",
Expand Down
39 changes: 35 additions & 4 deletions api/src/routes/user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import express from "express"
import express from "express";
import sgMail from "@sendgrid/mail";

const router = express.Router()
const axios = require("axios").default
const router = express.Router();
const axios = require("axios").default;

router.use(express.json());

router.post("/test", async (req: any, res: any) => {
console.log("Received group data:")
Expand All @@ -10,4 +13,32 @@ router.post("/test", async (req: any, res: any) => {
return res.status(200).json(name)
})

export default router
router.post("/send-email", async (req: any, res: any) => {
try {
const SENDGRID_API_KEY = process.env.SEND_GRID_API_KEY || "";
const SEND_GRID_TEST_EMAIL = process.env.SEND_GRID_TEST_EMAIL || "";

if (SENDGRID_API_KEY === "" || SEND_GRID_TEST_EMAIL === "") {
throw new Error("SendGrid API key or test email is missing");
}

const { email, name } = req.body;

sgMail.setApiKey(SENDGRID_API_KEY);

await sgMail.send({
to: email,
from: SEND_GRID_TEST_EMAIL,
templateId: "d-7e26b82cf8624bafa4077b6ed73b52bf",
dynamicTemplateData: {
name: name,
},
});

return res.status(200).json({ message: "Email successfully sent" });
} catch (err) {
return res.status(400).json({ message: "Email sending failed" });
}
});

export default router;
4 changes: 2 additions & 2 deletions api/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from "express"
import express from "express";
import bodyParser from "body-parser"
import connectDB from "./config/db"
import connectDB from "./config/db";

import * as routes from "./routes/index"

Expand Down

0 comments on commit e9d1bf0

Please sign in to comment.