Skip to content
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

Send email notification when educator account is created #149

Merged
merged 3 commits into from
Oct 11, 2024
Merged
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
31 changes: 31 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@types/express": "^4.17.13",
"@types/express-session": "^1.17.4",
"@types/jsonwebtoken": "^8.5.8",
"@types/nodemailer": "^6.4.16",
"@types/supertest": "^6.0.2",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^7.1.1",
Expand All @@ -33,6 +34,7 @@
"jsonwebtoken": "^8.5.1",
"mysql2": "^2.3.3",
"nanoid": "^3.3.2",
"nodemailer": "^6.9.15",
"path": "^0.12.7",
"sequelize": "^6.37.1",
"sha3": "^2.1.4",
Expand Down
55 changes: 30 additions & 25 deletions src/email.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
// import nodemailer from "nodemailer";
import nodemailer, { SendMailOptions } from "nodemailer";
import dotenv from "dotenv";

// const transporter = nodemailer.createTransport({
// host: "smtp.gmail.com",
// port: 587,
// secure: false,
// auth: {
// user: "[email protected]",
// pass: "C@rifio00"
// }
// });
dotenv.config();

// export async function sendEmail(to: string, from: string, subject: string, body: string) {
// const options = {
// from: from,
// to: to,
// subject: subject,
// text: body
// };
// transporter.sendMail(options, (error, info) => {
// if (error) {
// console.log(error);
// } else {
// console.log("Email sent: " + info.response);
// }
// });
// }
const transporter = nodemailer.createTransport({
service: process.env.EMAIL_SERVICE,
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD,
},
});

export interface SendEmailOptions {
to: string;
subject: string;
text: string;
}

export async function sendEmail(options: SendEmailOptions) {
const sendOptions: SendMailOptions = {
...options,
from: process.env.EMAIL_FROM,
};
transporter.sendMail(sendOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
}
17 changes: 16 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import * as Either from "effect/Either";
import { setupApp } from "./app";
import { getAPIKey } from "./authorization";
import { Sequelize } from "sequelize";
import { sendEmail } from "./email";

// TODO: Clean up these type definitions

Expand Down Expand Up @@ -144,10 +145,24 @@ export function createApp(db: Sequelize): Express {
result = SignUpResult.BadRequest;
}
const statusCode = SignUpResult.statusCode(result);
const success = SignUpResult.success(result);

if (success) {
sendEmail({
to: "[email protected]",
subject: "Educator account created",
text: `
Educator account created at ${Date()}:
Name: ${data.first_name} ${data.last_name}
Email: ${data.email}
`,
})
.catch(error => console.log(error));
}
res.status(statusCode).json({
educator_info: data,
status: result,
success: SignUpResult.success(result)
success,
});
});

Expand Down
Loading