2022-03-25 19:17:37 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
2022-03-26 04:28:53 +00:00
|
|
|
import { ApiKey } from "@prisma/client";
|
2022-03-26 00:40:43 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
|
|
|
import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";
|
|
|
|
|
|
|
|
type ResponseData = {
|
|
|
|
data?: ApiKey;
|
|
|
|
message?: string;
|
|
|
|
error?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
|
|
|
const { body, method } = req;
|
2022-03-26 21:29:30 +00:00
|
|
|
const safe = schemaApiKey.safeParse(body);
|
|
|
|
if (method === "POST" && safe.success) {
|
2022-03-26 04:28:53 +00:00
|
|
|
const apiKey = await prisma.apiKey
|
2022-03-25 22:26:22 +00:00
|
|
|
.create({
|
|
|
|
data: {
|
|
|
|
...safe.data, user: { connect: { id: 1 } }
|
|
|
|
}
|
|
|
|
})
|
2022-03-26 04:28:53 +00:00
|
|
|
if (apiKey) {
|
|
|
|
res.status(201).json({ data: apiKey });
|
2022-03-26 21:29:30 +00:00
|
|
|
} else {
|
|
|
|
res.status(404).json({message: "API Key not created"});
|
|
|
|
}
|
2022-03-25 18:37:51 +00:00
|
|
|
} else {
|
|
|
|
// Reject any other HTTP method than POST
|
|
|
|
res.status(405).json({ error: "Only POST Method allowed" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withValidApiKey(createApiKey);
|