2022-04-21 18:17:56 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2023-08-02 03:54:28 +00:00
|
|
|
import { throwIfNotHaveAdminAccessToTeam } from "@calcom/app-store/_utils/throwIfNotHaveAdminAccessToTeam";
|
2022-04-21 18:17:56 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
2022-09-15 19:53:09 +00:00
|
|
|
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
|
|
|
|
2022-04-21 18:17:56 +00:00
|
|
|
/**
|
|
|
|
* This is an example endpoint for an app, these will run under `/api/integrations/[...args]`
|
|
|
|
* @param req
|
|
|
|
* @param res
|
|
|
|
*/
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
if (!req.session?.user?.id) {
|
|
|
|
return res.status(401).json({ message: "You must be logged in to do this" });
|
|
|
|
}
|
2023-08-02 03:54:28 +00:00
|
|
|
const { teamId } = req.query;
|
|
|
|
|
|
|
|
await throwIfNotHaveAdminAccessToTeam({ teamId: Number(teamId) ?? null, userId: req.session.user.id });
|
|
|
|
|
|
|
|
const installForObject = teamId ? { teamId: Number(teamId) } : { userId: req.session.user.id };
|
2022-04-21 18:17:56 +00:00
|
|
|
const appType = "jitsi_video";
|
|
|
|
try {
|
|
|
|
const alreadyInstalled = await prisma.credential.findFirst({
|
|
|
|
where: {
|
|
|
|
type: appType,
|
2023-08-02 03:54:28 +00:00
|
|
|
...installForObject,
|
2022-04-21 18:17:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
if (alreadyInstalled) {
|
|
|
|
throw new Error("Already installed");
|
|
|
|
}
|
|
|
|
const installation = await prisma.credential.create({
|
|
|
|
data: {
|
|
|
|
type: appType,
|
|
|
|
key: {},
|
2023-08-02 03:54:28 +00:00
|
|
|
...installForObject,
|
2022-05-02 20:39:35 +00:00
|
|
|
appId: "jitsi",
|
2022-04-21 18:17:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!installation) {
|
|
|
|
throw new Error("Unable to create user credential for jitsivideo");
|
|
|
|
}
|
|
|
|
} catch (error: unknown) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
return res.status(500).json({ message: error.message });
|
|
|
|
}
|
|
|
|
return res.status(500);
|
|
|
|
}
|
2022-09-15 19:53:09 +00:00
|
|
|
return res.status(200).json({ url: getInstalledAppPath({ variant: "conferencing", slug: "jitsi" }) });
|
2022-04-21 18:17:56 +00:00
|
|
|
}
|