40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
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" });
|
|
}
|
|
|
|
const appType = "metamask_web3";
|
|
try {
|
|
const alreadyInstalled = await prisma.credential.findFirst({
|
|
where: {
|
|
type: appType,
|
|
userId: req.session.user.id,
|
|
},
|
|
});
|
|
if (alreadyInstalled) {
|
|
throw new Error("Already installed");
|
|
}
|
|
const installation = await prisma.credential.create({
|
|
data: {
|
|
type: appType,
|
|
key: { isWeb3Active: true },
|
|
userId: req.session.user.id,
|
|
appId: "metamask",
|
|
},
|
|
});
|
|
if (!installation) {
|
|
throw new Error("Unable to create user credential for metamask");
|
|
}
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return res.status(500).json({ message: error.message });
|
|
}
|
|
return res.status(500);
|
|
}
|
|
return res.redirect("/apps/installed");
|
|
}
|