use default installation handler to install app

This commit is contained in:
aar2dee2 2023-08-31 15:40:23 +05:30
parent 786ca13e2e
commit a236ecad02

View File

@ -1,39 +1,16 @@
import type { NextApiRequest, NextApiResponse } from "next"; import { createDefaultInstallation } from "@calcom/app-store/_utils/installation";
import type { AppDeclarativeHandler } from "@calcom/types/AppHandler";
import prisma from "@calcom/prisma"; import appConfig from "../config.json";
export default async function handler(req: NextApiRequest, res: NextApiResponse) { const handler: AppDeclarativeHandler = {
if (!req.session?.user?.id) { appType: appConfig.type,
return res.status(401).json({ message: "You must be logged in to do this" }); variant: appConfig.variant,
} slug: appConfig.slug,
const appType = "make_automation"; supportsMultipleInstalls: false,
try { handlerType: "add",
const alreadyInstalled = await prisma.credential.findFirst({ createCredential: ({ appType, user, slug, teamId }) =>
where: { createDefaultInstallation({ appType, userId: user.id, slug, key: {}, teamId }),
type: appType, };
userId: req.session.user.id,
},
});
if (alreadyInstalled) {
throw new Error("Already installed");
}
const installation = await prisma.credential.create({
data: {
type: appType,
key: {},
userId: req.session.user.id,
appId: "make",
},
});
if (!installation) {
throw new Error("Unable to create user credential for make");
}
} catch (error: unknown) {
if (error instanceof Error) {
return res.status(500).json({ message: error.message });
}
return res.status(500);
}
return res.status(200).json({ url: "/apps/make/setup" }); export default handler;
}