import { serve } from "https://deno.land/std@0.192.0/http/server.ts";
serve(async (req) => {
// ✅ CORS preflight
if (req.method === "OPTIONS") {
return new Response("ok", {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "*",
},
});
}
try {
const { article, subscribers } = await req.json();
if (!article || !article.title) {
return new Response(JSON.stringify({ error: "Article data is required" }), {
status: 400,
headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" },
});
}
if (!subscribers || subscribers.length === 0) {
return new Response(JSON.stringify({ error: "No subscribers provided" }), {
status: 400,
headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" },
});
}
const RESEND_API_KEY = Deno.env.get("RESEND_API_KEY");
if (!RESEND_API_KEY) {
throw new Error("RESEND_API_KEY is not set");
}
const siteUrl = "https://broadcastbeat.com";
const articleUrl = article.slug
? `${siteUrl}/articles/${article.slug}`
: article.wp_slug
? `${siteUrl}/articles/${article.wp_slug}`
: siteUrl;
const categoryLabel = article.category
? `${article.category}`
: "";
const featuredImageBlock = article.featured_image
? ``
: "";
const excerptBlock = article.excerpt
? `
${article.excerpt}
` : ""; const authorBlock = article.author_name ? `By ${article.author_name}
` : ""; const htmlBody = `