Add non-destructive dev seed on startup
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { Resend } from "resend";
|
||||
|
||||
const resend = new Resend(process.env.RESEND_API_KEY);
|
||||
|
||||
type SendAppEmailInput = {
|
||||
to: string | string[];
|
||||
subject: string;
|
||||
react: ReactNode;
|
||||
from?: string;
|
||||
};
|
||||
|
||||
type SendAppEmailResult =
|
||||
| { success: true; id?: string }
|
||||
| { success: false; error: string };
|
||||
|
||||
export function getAppEmailConfigError() {
|
||||
if (!process.env.RESEND_API_KEY) {
|
||||
return "RESEND_API_KEY ist nicht konfiguriert.";
|
||||
}
|
||||
|
||||
if (!process.env.EMAIL_FROM) {
|
||||
return "EMAIL_FROM ist nicht konfiguriert.";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function sendAppEmail({
|
||||
to,
|
||||
subject,
|
||||
react,
|
||||
from = process.env.EMAIL_FROM,
|
||||
}: SendAppEmailInput): Promise<SendAppEmailResult> {
|
||||
if (!process.env.RESEND_API_KEY) {
|
||||
return {
|
||||
success: false,
|
||||
error: "RESEND_API_KEY ist nicht konfiguriert.",
|
||||
};
|
||||
}
|
||||
|
||||
if (!from) {
|
||||
return {
|
||||
success: false,
|
||||
error: "EMAIL_FROM ist nicht konfiguriert.",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const { data, error } = await resend.emails.send({
|
||||
from: from!,
|
||||
to,
|
||||
subject,
|
||||
react,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error("Resend konnte die E-Mail nicht versenden:", error);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message ?? "E-Mail-Versand fehlgeschlagen.",
|
||||
};
|
||||
}
|
||||
|
||||
return { success: true, id: data?.id };
|
||||
} catch (error) {
|
||||
console.error("Unerwarteter Fehler beim E-Mail-Versand:", error);
|
||||
return {
|
||||
success: false,
|
||||
error:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Unerwarteter Fehler beim E-Mail-Versand.",
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user