diff --git a/app/api/data/completions/route.js b/app/api/data/completions/route.js index 25fc9eb..a98e231 100644 --- a/app/api/data/completions/route.js +++ b/app/api/data/completions/route.js @@ -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"; @@ -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, }); diff --git a/lib/send-email.js b/lib/send-email.js new file mode 100644 index 0000000..d0c42b6 --- /dev/null +++ b/lib/send-email.js @@ -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); +}