68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import { loadWebConfig } from "@toolctrl/config";
|
|
import { getAppConfiguration, updateAppConfiguration } from "~/utils/db/appConfiguration.db.server";
|
|
|
|
export const MAILJET_PROVIDER_ID = "mailjet" as const;
|
|
|
|
type MailjetProviderId = typeof MAILJET_PROVIDER_ID;
|
|
type MailjetSource = "environment" | "missing";
|
|
|
|
export type MailjetProviderStatus = {
|
|
id: MailjetProviderId;
|
|
name: "Mailjet";
|
|
configured: boolean;
|
|
source: {
|
|
apiKey: MailjetSource;
|
|
secretKey: MailjetSource;
|
|
fromEmail: MailjetSource;
|
|
};
|
|
missing: string[];
|
|
};
|
|
|
|
export type UpdateMailjetSettingsResult =
|
|
| { success: true }
|
|
| { success: false; error: string };
|
|
|
|
export async function getMailjetProviderStatus({ request }: { request: Request }): Promise<MailjetProviderStatus> {
|
|
await getAppConfiguration({ request });
|
|
|
|
const mailjet = loadWebConfig().config.mailjet;
|
|
|
|
const source = {
|
|
apiKey: mailjet.apiKey ? "environment" : "missing",
|
|
secretKey: mailjet.secretKey ? "environment" : "missing",
|
|
fromEmail: mailjet.fromEmail ? "environment" : "missing",
|
|
} satisfies MailjetProviderStatus["source"];
|
|
|
|
const missing = [
|
|
source.apiKey === "missing" ? "MAILJET_API_KEY" : null,
|
|
source.secretKey === "missing" ? "MAILJET_SECRET_KEY" : null,
|
|
source.fromEmail === "missing" ? "MAILJET_FROM_EMAIL" : null,
|
|
].filter((item): item is string => Boolean(item));
|
|
|
|
return {
|
|
id: MAILJET_PROVIDER_ID,
|
|
name: "Mailjet",
|
|
configured: missing.length === 0,
|
|
source,
|
|
missing,
|
|
};
|
|
}
|
|
|
|
export async function updateMailjetSettings(form: FormData): Promise<UpdateMailjetSettingsResult> {
|
|
const provider = form.get("emailProvider")?.toString();
|
|
if (provider !== MAILJET_PROVIDER_ID) {
|
|
return { success: false, error: "Only Mailjet is supported for transactional email." };
|
|
}
|
|
|
|
if (form.get("apiKey")?.toString().trim()) {
|
|
return { success: false, error: "Mailjet API keys are configured through environment variables only." };
|
|
}
|
|
|
|
await updateAppConfiguration({
|
|
emailProvider: MAILJET_PROVIDER_ID,
|
|
emailSupportEmail: form.get("emailSupportEmail")?.toString(),
|
|
});
|
|
|
|
return { success: true };
|
|
}
|