first commit
This commit is contained in:
14
calcom/packages/app-store/sendgrid/api/_getAdd.ts
Normal file
14
calcom/packages/app-store/sendgrid/api/_getAdd.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { NextApiRequest } from "next";
|
||||
|
||||
import { defaultResponder } from "@calcom/lib/server";
|
||||
|
||||
import checkSession from "../../_utils/auth";
|
||||
import { checkInstalled } from "../../_utils/installation";
|
||||
|
||||
export async function getHandler(req: NextApiRequest) {
|
||||
const session = checkSession(req);
|
||||
await checkInstalled("sendgrid", session.user?.id);
|
||||
return { url: "/apps/sendgrid/setup" };
|
||||
}
|
||||
|
||||
export default defaultResponder(getHandler);
|
||||
45
calcom/packages/app-store/sendgrid/api/_postAdd.ts
Normal file
45
calcom/packages/app-store/sendgrid/api/_postAdd.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { NextApiRequest } from "next";
|
||||
|
||||
import { symmetricEncrypt } from "@calcom/lib/crypto";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { defaultResponder } from "@calcom/lib/server";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import checkSession from "../../_utils/auth";
|
||||
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
||||
|
||||
export async function getHandler(req: NextApiRequest) {
|
||||
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" }) };
|
||||
}
|
||||
|
||||
export default defaultResponder(getHandler);
|
||||
6
calcom/packages/app-store/sendgrid/api/add.ts
Normal file
6
calcom/packages/app-store/sendgrid/api/add.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { defaultHandler } from "@calcom/lib/server";
|
||||
|
||||
export default defaultHandler({
|
||||
GET: import("./_getAdd"),
|
||||
POST: import("./_postAdd"),
|
||||
});
|
||||
31
calcom/packages/app-store/sendgrid/api/check.ts
Normal file
31
calcom/packages/app-store/sendgrid/api/check.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { NextApiRequest } from "next";
|
||||
|
||||
import Sendgrid from "@calcom/lib/Sendgrid";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
||||
|
||||
import checkSession from "../../_utils/auth";
|
||||
|
||||
export async function getHandler(req: NextApiRequest) {
|
||||
const { api_key } = req.body;
|
||||
if (!api_key) throw new HttpError({ statusCode: 400, message: "No Api Key provoided to check" });
|
||||
|
||||
checkSession(req);
|
||||
|
||||
const sendgrid: Sendgrid = new Sendgrid(api_key);
|
||||
|
||||
try {
|
||||
const usernameInfo = await sendgrid.username();
|
||||
if (usernameInfo.username) {
|
||||
return {};
|
||||
} else {
|
||||
throw new HttpError({ statusCode: 404 });
|
||||
}
|
||||
} catch (e) {
|
||||
throw new HttpError({ statusCode: 500, message: e as string });
|
||||
}
|
||||
}
|
||||
|
||||
export default defaultHandler({
|
||||
POST: Promise.resolve({ default: defaultResponder(getHandler) }),
|
||||
});
|
||||
2
calcom/packages/app-store/sendgrid/api/index.ts
Normal file
2
calcom/packages/app-store/sendgrid/api/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { default as add } from "./add";
|
||||
export { default as check } from "./check";
|
||||
Reference in New Issue
Block a user