-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a38544
commit 03f0fa7
Showing
10 changed files
with
123 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import { json } from "body-parser"; | ||
import express from "express"; | ||
|
||
import contactRoute from "./routes/contactRequest"; | ||
import { port } from "./config"; | ||
|
||
// Initialize Express App | ||
import { errorHandler } from "./errors/handler"; | ||
const app = express(); | ||
|
||
// Provide json body-parser middleware | ||
app.use(json()); | ||
|
||
// Tell app to listen on our port environment variable | ||
app.use("/api", contactRoute); | ||
app.use(errorHandler); | ||
app.listen(port, () => { | ||
console.log(`> Listening on port ${port}`); | ||
}); |
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,48 @@ | ||
import { Request, Response } from "express"; | ||
import nodemailer from "nodemailer"; | ||
|
||
export type ContactRequest = { | ||
fullName: string; | ||
email: string; | ||
phoneNumber: string; | ||
message?: string; | ||
}; | ||
|
||
export const handleContactRequest = (req: Request, res: Response): void => { | ||
const { | ||
fullName, | ||
email, | ||
phoneNumber, | ||
message = "No Message Provided", | ||
} = req.body as ContactRequest; | ||
|
||
console.log("Contact request received:", { fullName, email, phoneNumber, message }); | ||
|
||
const sendContactEmail = async (subject: string, message: string) => { | ||
const EMAIL_SUBJECT = `Contact Request from ${subject}`; | ||
const EMAIL_BODY = `Name: ${fullName}\nEmail: ${email}\nPhone: ${phoneNumber}\nMessage: ${message}`; | ||
const transporter = nodemailer.createTransport({ | ||
service: "Gmail", | ||
host: "smtp.gmail.com", | ||
secure: true, | ||
auth: { | ||
user: process.env.EMAIL, | ||
pass: process.env.EMAIL_PASSWORD, | ||
}, | ||
}); | ||
const mailOptions = { | ||
from: process.env.EMAIL, | ||
to: process.env.EMAIL, | ||
subject: EMAIL_SUBJECT, | ||
text: EMAIL_BODY, | ||
}; | ||
try { | ||
await transporter.sendMail(mailOptions); | ||
console.log("Contact email sent successfully"); | ||
} catch (error) { | ||
console.error("Error sending email:", error); | ||
} | ||
}; | ||
sendContactEmail(fullName, message); | ||
Check failure on line 46 in backend/src/controllers/contactRequest.ts GitHub Actions / Backend lint and style check
|
||
res.status(200).json({ message: "Contact request submitted successfully." }); | ||
}; |
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,9 @@ | ||
import { CustomError } from "./errors"; | ||
const INVALID_FORM = "Name, email, and phone are required fields."; | ||
const INVALID_PHONE = "Invalid phone number format."; | ||
const INVALID_EMAIL = "Invalid email format."; | ||
export class ContactError extends CustomError { | ||
static INVALID_FORM = new ContactError(0, 400, INVALID_FORM); | ||
static INVALID_PHONE = new ContactError(1, 400, INVALID_PHONE); | ||
static INVALID_EMAIL = new ContactError(2, 400, INVALID_EMAIL); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./errors"; | ||
export * from "./internal"; | ||
export * from "./contact"; |
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { CustomError } from "./errors"; | ||
|
||
const NO_APP_PORT = "Could not find app port env variable"; | ||
|
||
const NO_EMAIL = "Could not find email"; | ||
const NO_EMAIL_PASSWORD = "Could not find email password"; | ||
export class InternalError extends CustomError { | ||
static NO_APP_PORT = new InternalError(0, 500, NO_APP_PORT); | ||
static NO_EMAIL = new InternalError(1, 500, NO_EMAIL); | ||
static NO_EMAIL_PASSWORD = new InternalError(2, 500, NO_EMAIL_PASSWORD); | ||
} |
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,8 @@ | ||
import express from "express"; | ||
import { validateContactRequest } from "../validators/contactRequest"; | ||
import { handleContactRequest } from "../controllers/contactRequest"; | ||
const router = express.Router(); | ||
|
||
router.post("/contact", validateContactRequest, handleContactRequest); | ||
|
||
export default 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import express, { Request, Response, NextFunction } from "express"; | ||
Check warning on line 1 in backend/src/validators/contactRequest.ts GitHub Actions / Backend lint and style check
Check failure on line 1 in backend/src/validators/contactRequest.ts GitHub Actions / Backend lint and style check
|
||
import { ContactRequest } from "../controllers/contactRequest"; | ||
import { ContactError } from "../errors/contact"; | ||
export const validateContactRequest = (req: Request, res: Response, next: NextFunction) => { | ||
const { fullName, email, phoneNumber } = req.body as ContactRequest; | ||
|
||
if (!fullName || !email || !phoneNumber) { | ||
throw ContactError.INVALID_FORM; | ||
} | ||
|
||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
if (!emailRegex.test(email)) { | ||
throw ContactError.INVALID_EMAIL; | ||
} | ||
|
||
const phoneRegex = /^\d{10,15}$/; | ||
if (!phoneRegex.test(phoneNumber)) { | ||
throw ContactError.INVALID_PHONE; | ||
} | ||
|
||
next(); | ||
}; |