Skip to content

Commit

Permalink
Merge branch 'main' into 27-lage-algoritme-for-å-matche-personer-og-k…
Browse files Browse the repository at this point in the history
…omite-med-intervjutider
  • Loading branch information
jorgengaldal authored Jul 21, 2024
2 parents efb2dc0 + 78a2f47 commit d980180
Show file tree
Hide file tree
Showing 8 changed files with 3,872 additions and 5,447 deletions.
5 changes: 4 additions & 1 deletion .env.local.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ NEXTAUTH_SECRET=#use [openssl rand -hex 32] to generate a 32 bytes value

AUTH0_CLIENT_ID=#client id
AUTH0_CLIENT_SECRET=#client secret
AUTH0_ISSUER=#issuer base url, example: example.us.auth0.com
AUTH0_ISSUER=#issuer base url, example: example.us.auth0.com

AWS_SECRET_ACCESS_KEY=#aws secret access key
AWS_ACCESS_KEY_ID=#aws access key id
14 changes: 14 additions & 0 deletions lib/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ export type applicantType = {
periodId: string | ObjectId;
};

// applicantType modified to fit email content
export type emailDataType = {
name: string;
emails: string[];
phone: string;
grade: string;
about: string;
firstChoice: string;
secondChoice: string;
thirdChoice: string;
bankom: "Ja" | "Nei" | "Kanskje";
optionalCommittees: string;
};

export type periodType = {
_id: ObjectId;
name: string;
Expand Down
8,972 changes: 3,632 additions & 5,340 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"format:write": "prettier --write ."
},
"dependencies": {
"@aws-sdk/client-ses": "^3.614.0",
"@fullcalendar/core": "^6.1.11",
"@fullcalendar/interaction": "^6.1.11",
"@fullcalendar/react": "^6.1.11",
Expand Down
70 changes: 69 additions & 1 deletion pages/api/applicants/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { NextApiRequest, NextApiResponse } from "next";
import { createApplicant, getApplicants } from "../../../lib/mongo/applicants";
import { authOptions } from "../auth/[...nextauth]";
import { getServerSession } from "next-auth";
import { getPeriodById } from "../../../lib/mongo/periods";
import { getServerSession } from "next-auth";
import { emailDataType } from "../../../lib/types/types";
import { isApplicantType } from "../../../lib/utils/validators";
import { isAdmin, hasSession, checkOwId } from "../../../lib/utils/apiChecks";
import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses";
import capitalizeFirstLetter from "../../../utils/capitalizeFirstLetter";
import sendEmail from "../../../utils/sendEmail";
import { changeDisplayName } from "../../../lib/utils/toString";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const session = await getServerSession(req, res, authOptions);
Expand Down Expand Up @@ -49,6 +54,69 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {

const { applicant, error } = await createApplicant(requestBody);
if (error) throw new Error(error);

const sesClient = new SESClient({ region: "eu-north-1" });

if (applicant != null) {
let optionalCommitteesString = "";
if (applicant.optionalCommittees.length > 0) {
optionalCommitteesString = applicant.optionalCommittees
?.map(changeDisplayName)
.join(", ");
} else {
optionalCommitteesString = "Ingen";
}

const emailData: emailDataType = {
name: applicant.name,
emails: [applicant.email],
phone: applicant.phone,
grade: applicant.grade,
about: applicant.about.replace(/\n/g, "<br>"),
firstChoice: "Tom",
secondChoice: "Tom",
thirdChoice: "Tom",
bankom:
applicant.bankom == "yes"
? "Ja"
: applicant.bankom == "no"
? "Nei"
: "Kanskje",
optionalCommittees: optionalCommitteesString,
};

//Type guard
if (!Array.isArray(applicant.preferences)) {
emailData.firstChoice =
applicant.preferences.first == "onlineil"
? "Online IL"
: capitalizeFirstLetter(applicant.preferences.first);
emailData.secondChoice =
applicant.preferences.second == "onlineil"
? "Online IL"
: capitalizeFirstLetter(applicant.preferences.second);
emailData.thirdChoice =
applicant.preferences.third == "onlineil"
? "Online IL"
: capitalizeFirstLetter(applicant.preferences.third);
}

try {
await sendEmail({
sesClient: sesClient,
fromEmail: "[email protected]",
toEmails: emailData.emails,
subject: "Vi har mottatt din søknad!",
htmlContent: `Dette er en bekreftelse på at vi har mottatt din søknad. Du vil motta en ny e-post med intervjutider etter søkeperioden er over. Her er en oppsummering av din søknad:<br><br><strong>E-post:</strong> ${emailData.emails[0]}<br><br><strong>Fullt navn:</strong> ${emailData.name}<br><br><strong>Telefonnummer:</strong> ${emailData.phone}<br><br><strong>Trinn:</strong> ${emailData.grade}<br><br><strong>Førstevalg:</strong> ${emailData.firstChoice}<br><br><strong>Andrevalg:</strong> ${emailData.secondChoice}<br><br><strong>Tredjevalg:</strong> ${emailData.thirdChoice}<br><br><strong>Ønsker du å være økonomiansvarlig:</strong> ${emailData.bankom}<br><br><strong> Andre valg:</strong> ${emailData.optionalCommittees}<br><br><strong>Kort om deg selv:</strong><br>${emailData.about}`,
});

console.log("Email sent to: ", emailData.emails);
} catch (error) {
console.error("Error sending email: ", error);
throw error;
}
}

return res.status(201).json({ applicant });
}
} catch (error) {
Expand Down
Loading

0 comments on commit d980180

Please sign in to comment.