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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"drizzle-zod": "^0.5.1",
"express": "^5.1.0",
"express-zod-safe": "^1.3.3",
"nodemailer": "^9.0.5",
"pg": "^8.14.1",
"swagger-jsdoc": "^6.2.8",
"validator": "^13.15.0",
Expand All @@ -42,6 +43,7 @@
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/node": "^22.14.0",
"@types/nodemailer": "^8.0.1",
"@types/pg": "^8.11.11",
"@types/supertest": "^6.0.3",
"@types/swagger-jsdoc": "^6.0.4",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions src/error/error-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const HTTP_CLIENT_ERROR_MESSAGES = [
"Failed to execute the database command",
"Error parsing database response",
"Database error",
"Nice try!",
] as const;
const HTTP_SERVER_ERROR_MESSAGES = [
"Internal server error occurred",
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
assistantApplicationRouter,
teamApplicationRouter,
} from "@/src/routers/applications";
import { contactRouter } from "@/src/routers/contact";
import { expensesRouter } from "@/src/routers/expenses";
import { sponsorsRouter } from "@/src/routers/sponsors";
import { teamsRouter } from "@/src/routers/teams";
Expand Down Expand Up @@ -45,6 +46,8 @@ api.use("/assistantapplications", assistantApplicationRouter);

api.use("/teams", teamsRouter);

api.use("/contact", contactRouter);

// Error handling
api.use(
jsonParsingErrorHandler,
Expand Down
89 changes: 89 additions & 0 deletions src/routers/contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { clientError } from "@/src/error/http-errors";
import { sendEmail } from "@/src/services/mail-service";
import { Router, json } from "express";
import { z } from "zod";

export const contactRouter = Router();
contactRouter.use(json());

/**
* @openapi
* /contact:
* post:
* tags: [contact]
* summary: Send a contact email
* description: Send an email to contact, using the configured mail service.
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - receivingEmail
* - replyTo
* - about
* properties:
* receivingEmail:
* type: string
* format: email
* description: Recipient email address
* replyTo:
* type: string
* format: email
* nullable: true
* description: Optional reply-to address
* about:
* type: string
* description: Email subject
* text:
* type: string
* nullable: true
* description: Plain text email body
* responses:
* 200:
* description: Email sent successfully
* content:
* application/json:
* schema:
* type: string
* example: Successfully sent the email!
* 400:
* description: Invalid request format
*/
contactRouter.post("/", async (req, res, next) => {
const contactSchema = z.object({
receivingEmail: z.string().email(),
replyTo: z.string().email(),
about: z.string().min(1),
text: z.string().optional(),
});

const result = contactSchema.safeParse(req.body);

if (!result.success) {
return next(clientError(400, "Invalid request format", result.error));
}

const { receivingEmail, replyTo, about, text } = result.data;

const validEmailAddresses = [

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Kan ikke ha magic konstanter, vurder å legg det til i en global variabel, konfig fil eller databasen

"hovedstyret@vektorprogrammet.no",
"styret.ntnu@vektorprogrammet.no",
"uib@vektorprogrammet.no",
"nmbu@vektorprogrammet.no",
];

if (!validEmailAddresses.includes(receivingEmail)) {
return next(clientError(418, "Nice try!"));
}

try {
await sendEmail(receivingEmail, replyTo, about, text);

res.json("Successfully sent the email!");
} catch (error) {
console.error("CONTACT EMAIL ERROR:", error);
next(error);
}
});
32 changes: 32 additions & 0 deletions src/services/mail-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import nodemailer from "nodemailer";

const transporter = nodemailer.createTransport({
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
secure: true,
// biome-ignore lint/style/useNamingConvention: Nodemailer requires the `requireTLS` option name.
requireTLS: true,
service: "gmail",
auth: {
type: "OAuth2",
user: process.env.GOOGLE_FROM_EMAIL,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

vurder å bruke allerede eksisterende environment variabel parsere her

clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
refreshToken: process.env.GOOGLE_REFRESH_TOKEN,
},
});

export async function sendEmail(
to: string,
replyTo: string,
subject: string,
text?: string,
html?: string,
) {
await transporter.sendMail({
from: `"Vektorprogrammet" <${process.env.GOOGLE_FROM_EMAIL}>`,
to,
replyTo,
subject,
text,
html,
});
}
Loading