Skip to content

Commit

Permalink
feat: notify owners by email on new completions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelovicentegc committed Feb 17, 2024
1 parent 1ff19fb commit 14e6615
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/api/data/completions/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CHIRON_FOREIGN_KEY, CHIRON_VENDOR_ID } from "@/lib/config";
import { getApiKey } from "@/lib/db/reads";
import { saveCompletion } from "@/lib/db/writes";
import { decrypt } from "@/lib/encryption";
import sendEmail from "@/lib/send-email";
import { headers } from "next/headers";
import { NextResponse } from "next/server";

Expand Down Expand Up @@ -63,6 +64,12 @@ export async function POST(req) {
try {
await saveCompletion(data);

await sendEmail({
to: process.env.ALLOWED_EMAILS,
subject: "New completion",
text: `A new completion has been created for ${vendorId}`,
});

return new NextResponse(JSON.stringify("Created"), {
status: 201,
});
Expand Down
21 changes: 21 additions & 0 deletions lib/send-email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import nodemailer from "nodemailer";

export default async function sendEmail({ to, subject, text }) {
const transporter = nodemailer.createTransport({
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
});

const info = await transporter.sendMail({
from: process.env.EMAIL_FROM,
to,
subject,
text,
});

console.log("Message sent: %s", info.messageId);
}

0 comments on commit 14e6615

Please sign in to comment.