"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("./user-v6mfWQ2d.cjs");
const httpError = require("./http-error-mkY8Ke7V.cjs");
const defaultResponder = require("./defaultResponder-QWPOXt61.cjs");
const auth = require("./auth--eKdiWSi.cjs");
const getInstalledAppPath = require("./getInstalledAppPath-3c7mqtrk.cjs");
async function getHandler(req) {
  const session = auth.checkSession(req);
  const { api_key } = req.body;
  if (!api_key)
    throw new httpError.HttpError({ statusCode: 400, message: "No Api Key provided to check" });
  let encrypted;
  try {
    encrypted = index.symmetricEncrypt(JSON.stringify({ api_key }), process.env.CALENDSO_ENCRYPTION_KEY || "");
  } catch (reason) {
    index.logger.error("Could not add Sendgrid app", reason);
    throw new httpError.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 index.prisma.credential.create({
      data
    });
  } catch (reason) {
    index.logger.error("Could not add Sendgrid app", reason);
    throw new httpError.HttpError({ statusCode: 500, message: "Could not add Sendgrid app" });
  }
  return { url: getInstalledAppPath.getInstalledAppPath({ variant: "other", slug: "sendgrid" }) };
}
const _postAdd = defaultResponder.defaultResponder(getHandler);
exports.default = _postAdd;
exports.getHandler = getHandler;
