From 25333e20173e446f6b726dcc4ab4ea38ad8fc682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20N=C3=BDvlt?= Date: Fri, 1 Mar 2024 13:46:32 +0100 Subject: [PATCH] Remove endpoint --- src/pages/send-emails.ts | 111 --------------------------------------- 1 file changed, 111 deletions(-) delete mode 100644 src/pages/send-emails.ts diff --git a/src/pages/send-emails.ts b/src/pages/send-emails.ts deleted file mode 100644 index db05c36..0000000 --- a/src/pages/send-emails.ts +++ /dev/null @@ -1,111 +0,0 @@ -import type { APIContext } from "astro"; -import { db } from "../db"; -import { getEntry, getCollection } from "astro:content"; -import { mailClient } from "../mail"; -import { setTimeout } from "node:timers/promises"; - -export const prerender = false; - -const FROM = `vig@re-set.cz`; -// const FROM = `send@spinaveprachy.cz`; - -const variantIds: Record = { - "1": "oil-gas-policy", - "2": "coal-policy", - "3": "neptun-deep", -}; - -const variants = await getCollection("variants"); - -function getSubject(lang: string, variantNr: string) { - const variant = variants.find((v) => v.slug === `${lang}/${variantIds[variantNr]}`); - if (!variant) { - throw new Error("No variant"); - } - return `VIG – ${variant.data.title}`; -} - -async function processLetters() { - const letters = await db - .selectFrom("letters") - .where("confirmed", "=", 1) - .where("language", "=", "hu") - .where("id", ">", 177) - .select(["id", "firstName", "lastName", "language", "variant", "branch", "email", "message"]) - .execute(); - - console.log(letters); - - for (const letter of letters) { - const branch = await getEntry("branches", letter.branch as string); - const subject = getSubject( - letter.branch === "vig-holding" ? "en" : letter.language, - letter.variant - ); - console.log("Subject", subject); - - if (!branch) { - console.error("No branch", letter.branch); - continue; - } - - for (const recipientEmail of branch.data.emails) { - console.log("sending", letter.id, "to", recipientEmail); - const message = await db - .insertInto("messages") - .values({ - letterId: letter.id, - from: letter.email, - to: recipientEmail, - status: "pending", - }) - .returning(["id"]) - .executeTakeFirst(); - - if (!message) { - console.error("Error creating message"); - continue; - } - - const name = `${letter.firstName} ${letter.lastName}`; - const messageText = - letter.language === "hu" - ? letter.message.replace("Tisztelt Hölgyem,", "Tisztelt Hölgyem/Uram,") - : letter.message; - - try { - const values = { - subject, - from: `${name} <${FROM}>`, - to: [recipientEmail], - replyTo: `${name} <${letter.email}>`, - plainBody: messageText, - }; - const result = await mailClient.send(values); - console.log(result); - - await db - .updateTable("messages") - .set({ status: "success" }) - .where("id", "=", message.id) - .execute(); - } catch (error) { - console.log(error); - await db - .updateTable("messages") - .set({ status: "error" }) - .where("id", "=", message.id) - .execute(); - } - - await setTimeout(2000); - } - } -} - -export async function POST(ctx: APIContext) { - processLetters().catch((error) => { - console.log(error); - }); - return new Response(null, { status: 200 }); -}