2022-03-25 19:17:37 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
2022-03-29 01:59:57 +00:00
|
|
|
import { ApiKey } from "@calcom/prisma/client";
|
2022-03-25 19:17:37 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
2022-03-25 19:17:37 +00:00
|
|
|
import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";
|
2022-03-29 01:59:57 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-03-26 21:29:30 +00:00
|
|
|
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
|
|
|
type ResponseData = {
|
2022-03-25 19:17:37 +00:00
|
|
|
data?: ApiKey;
|
2022-03-25 18:37:51 +00:00
|
|
|
message?: string;
|
|
|
|
error?: unknown;
|
|
|
|
};
|
|
|
|
|
2022-03-25 19:17:37 +00:00
|
|
|
export async function editApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
2022-03-25 18:37:51 +00:00
|
|
|
const { query, body, method } = req;
|
2022-03-26 00:40:43 +00:00
|
|
|
const safeQuery = await schemaQueryIdAsString.safeParse(query);
|
2022-03-25 19:17:37 +00:00
|
|
|
const safeBody = await schemaApiKey.safeParse(body);
|
2022-03-25 18:37:51 +00:00
|
|
|
|
2022-03-29 01:59:57 +00:00
|
|
|
if (safeQuery.success && safeBody.success) {
|
|
|
|
const data = await prisma.apiKey.update({
|
2022-03-25 18:37:51 +00:00
|
|
|
where: { id: safeQuery.data.id },
|
|
|
|
data: safeBody.data,
|
2022-03-29 01:59:57 +00:00
|
|
|
})
|
|
|
|
if (data) res.status(200).json({ data });
|
|
|
|
else (error: unknown) => res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })
|
|
|
|
}
|
2022-03-25 18:37:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 01:59:57 +00:00
|
|
|
export default withMiddleware("patchOnly","addRequestId")(
|
|
|
|
withValidQueryIdString(
|
|
|
|
withValidApiKey(
|
|
|
|
editApiKey)
|
|
|
|
)
|
|
|
|
);
|