2022-07-14 12:40:53 +00:00
|
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import type { Session } from "next-auth";
|
2022-03-23 22:00:30 +00:00
|
|
|
|
2022-09-15 19:53:09 +00:00
|
|
|
import getInstalledAppPath from "@calcom/app-store/_utils/getInstalledAppPath";
|
2022-06-07 06:13:32 +00:00
|
|
|
import { deriveAppDictKeyFromType } from "@calcom/lib/deriveAppDictKeyFromType";
|
2022-07-14 12:40:53 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import type { AppDeclarativeHandler, AppHandler } from "@calcom/types/AppHandler";
|
2022-06-07 06:13:32 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import { getSession } from "@lib/auth";
|
|
|
|
import { HttpError } from "@lib/core/http/error";
|
|
|
|
|
2022-07-14 12:40:53 +00:00
|
|
|
const defaultIntegrationAddHandler = async ({
|
|
|
|
slug,
|
|
|
|
supportsMultipleInstalls,
|
|
|
|
appType,
|
|
|
|
user,
|
|
|
|
createCredential,
|
|
|
|
}: {
|
|
|
|
slug: string;
|
|
|
|
supportsMultipleInstalls: boolean;
|
|
|
|
appType: string;
|
|
|
|
user?: Session["user"];
|
|
|
|
createCredential: AppDeclarativeHandler["createCredential"];
|
|
|
|
}) => {
|
|
|
|
if (!user?.id) {
|
|
|
|
throw new HttpError({ statusCode: 401, message: "You must be logged in to do this" });
|
|
|
|
}
|
|
|
|
if (!supportsMultipleInstalls) {
|
|
|
|
const alreadyInstalled = await prisma.credential.findFirst({
|
|
|
|
where: {
|
|
|
|
appId: slug,
|
|
|
|
userId: user.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (alreadyInstalled) {
|
|
|
|
throw new Error("App is already installed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await createCredential({ user: user, appType, slug });
|
|
|
|
};
|
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
// Check that user is authenticated
|
|
|
|
req.session = await getSession({ req });
|
|
|
|
|
|
|
|
const { args } = req.query;
|
|
|
|
|
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
return res.status(404).json({ message: `API route not found` });
|
|
|
|
}
|
|
|
|
|
2022-06-07 06:13:32 +00:00
|
|
|
const [appName, apiEndpoint] = args;
|
2022-03-23 22:00:30 +00:00
|
|
|
try {
|
2022-05-02 20:39:35 +00:00
|
|
|
/* Absolute path didn't work */
|
2022-06-07 06:13:32 +00:00
|
|
|
const handlerMap = (await import("@calcom/app-store/apps.server.generated")).apiHandlers;
|
2022-04-04 20:26:14 +00:00
|
|
|
|
2022-06-07 06:13:32 +00:00
|
|
|
const handlerKey = deriveAppDictKeyFromType(appName, handlerMap);
|
|
|
|
const handlers = await handlerMap[handlerKey as keyof typeof handlerMap];
|
2022-07-14 12:40:53 +00:00
|
|
|
const handler = handlers[apiEndpoint as keyof typeof handlers] as AppHandler;
|
|
|
|
let redirectUrl = "/apps/installed";
|
|
|
|
if (typeof handler === "undefined")
|
2022-03-23 22:00:30 +00:00
|
|
|
throw new HttpError({ statusCode: 404, message: `API handler not found` });
|
|
|
|
|
2022-07-14 12:40:53 +00:00
|
|
|
if (typeof handler === "function") {
|
|
|
|
await handler(req, res);
|
|
|
|
} else {
|
|
|
|
await defaultIntegrationAddHandler({ user: req.session?.user, ...handler });
|
2022-11-13 15:51:31 +00:00
|
|
|
redirectUrl = handler.redirect?.url || getInstalledAppPath(handler);
|
|
|
|
res.json({ url: redirectUrl, newTab: handler.redirect?.newTab });
|
2022-07-14 12:40:53 +00:00
|
|
|
}
|
2022-04-06 12:37:06 +00:00
|
|
|
return res.status(200);
|
2022-03-23 22:00:30 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2022-07-14 12:40:53 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
if (error instanceof HttpError) {
|
|
|
|
return res.status(error.statusCode).json({ message: error.message });
|
|
|
|
}
|
2022-07-14 12:40:53 +00:00
|
|
|
if (error instanceof Error) {
|
|
|
|
return res.status(400).json({ message: error.message });
|
|
|
|
}
|
2022-03-23 22:00:30 +00:00
|
|
|
return res.status(404).json({ message: `API handler not found` });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default handler;
|