import { u as symmetricEncrypt, l as logger, p as prisma } from "./index-Cw1zbw54.js";
import { H as HttpError } from "./http-error-RJXCPZV2.js";
import { d as defaultResponder } from "./defaultResponder-s4VQhYfH.js";
import { c as checkSession } from "./auth-_2QcUxTs.js";
import { g as getInstalledAppPath } from "./getInstalledAppPath-6P3H-VDB.js";
async function getHandler(req) {
  const session = checkSession(req);
  const { api_key } = req.body;
  if (!api_key)
    throw new HttpError({ statusCode: 400, message: "No Api Key provided to check" });
  let encrypted;
  try {
    encrypted = symmetricEncrypt(JSON.stringify({ api_key }), process.env.CALENDSO_ENCRYPTION_KEY || "");
  } catch (reason) {
    logger.error("Could not add Sendgrid app", reason);
    throw new HttpError({ statusCode: 500, message: "Invalid length - CALENDSO_ENCRYPTION_KEY" });
  }
  const data = {
    type: "sendgrid_other_calendar",
    key: { encrypted },
    userId: session.user?.id,
    appId: "sendgrid"
  };
  try {
    await prisma.credential.create({
      data
    });
  } catch (reason) {
    logger.error("Could not add Sendgrid app", reason);
    throw new HttpError({ statusCode: 500, message: "Could not add Sendgrid app" });
  }
  return { url: getInstalledAppPath({ variant: "other", slug: "sendgrid" }) };
}
const _postAdd = defaultResponder(getHandler);
export {
  _postAdd as default,
  getHandler
};
