first commit
This commit is contained in:
13
calcom/packages/app-store/closecom/api/_getAdd.ts
Normal file
13
calcom/packages/app-store/closecom/api/_getAdd.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import checkSession from "../../_utils/auth";
|
||||
import { checkInstalled } from "../../_utils/installation";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = checkSession(req);
|
||||
await checkInstalled("closecom", session.user?.id);
|
||||
|
||||
const returnTo = req.query.returnTo;
|
||||
|
||||
return res.status(200).json({ url: `/apps/closecom/setup${returnTo ? `?returnTo=${returnTo}` : ""}` });
|
||||
}
|
||||
45
calcom/packages/app-store/closecom/api/_postAdd.ts
Normal file
45
calcom/packages/app-store/closecom/api/_postAdd.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { NextApiRequest, NextApiResponse } 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";
|
||||
import appConfig from "../config.json";
|
||||
|
||||
export async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = checkSession(req);
|
||||
|
||||
const { api_key } = req.body;
|
||||
if (!api_key) throw new HttpError({ statusCode: 400, message: "No Api Key provoided to check" });
|
||||
|
||||
const encrypted = symmetricEncrypt(JSON.stringify({ api_key }), process.env.CALENDSO_ENCRYPTION_KEY || "");
|
||||
|
||||
const data = {
|
||||
type: appConfig.type,
|
||||
key: { encrypted },
|
||||
userId: session.user?.id,
|
||||
appId: appConfig.slug,
|
||||
};
|
||||
|
||||
try {
|
||||
await prisma.credential.create({
|
||||
data,
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
} catch (reason) {
|
||||
logger.error("Could not add Close.com app", reason);
|
||||
return res.status(500).json({ message: "Could not add Close.com app" });
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
url: req.query.returnTo ? req.query.returnTo : getInstalledAppPath({ variant: "crm", slug: "closecom" }),
|
||||
});
|
||||
}
|
||||
|
||||
export default defaultResponder(getHandler);
|
||||
29
calcom/packages/app-store/closecom/api/_postCheck.ts
Normal file
29
calcom/packages/app-store/closecom/api/_postCheck.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import CloseCom from "@calcom/lib/CloseCom";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { defaultResponder } from "@calcom/lib/server";
|
||||
|
||||
import checkSession from "../../_utils/auth";
|
||||
|
||||
export async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { api_key } = req.body;
|
||||
if (!api_key) throw new HttpError({ statusCode: 400, message: "No Api Key provoided to check" });
|
||||
|
||||
checkSession(req);
|
||||
|
||||
const closeCom: CloseCom = new CloseCom(api_key);
|
||||
|
||||
try {
|
||||
const userInfo = await closeCom.me();
|
||||
if (userInfo.first_name) {
|
||||
return res.status(200).end();
|
||||
} else {
|
||||
return res.status(404).end();
|
||||
}
|
||||
} catch (e) {
|
||||
return res.status(500).json({ message: e });
|
||||
}
|
||||
}
|
||||
|
||||
export default defaultResponder(getHandler);
|
||||
6
calcom/packages/app-store/closecom/api/add.ts
Normal file
6
calcom/packages/app-store/closecom/api/add.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { defaultHandler } from "@calcom/lib/server";
|
||||
|
||||
export default defaultHandler({
|
||||
GET: import("./_getAdd"),
|
||||
POST: import("./_postAdd"),
|
||||
});
|
||||
5
calcom/packages/app-store/closecom/api/check.ts
Normal file
5
calcom/packages/app-store/closecom/api/check.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { defaultHandler } from "@calcom/lib/server";
|
||||
|
||||
export default defaultHandler({
|
||||
POST: import("./_postCheck"),
|
||||
});
|
||||
2
calcom/packages/app-store/closecom/api/index.ts
Normal file
2
calcom/packages/app-store/closecom/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