Remove endpoint
This commit is contained in:
parent
dd972ef560
commit
25333e2017
1 changed files with 0 additions and 111 deletions
|
@ -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<string, string> = {
|
||||
"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 });
|
||||
}
|
Reference in a new issue