2022-04-02 01:51:33 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { ResourceResponse } from "@lib/types";
|
2022-04-11 13:10:16 +00:00
|
|
|
import { schemaResourceBodyParams, schemaResourcePublic, withValidResource } from "@lib/validations/resource";
|
2022-04-02 01:51:33 +00:00
|
|
|
import {
|
|
|
|
schemaQueryIdParseInt,
|
|
|
|
withValidQueryIdTransformParseInt,
|
|
|
|
} from "@lib/validations/shared/queryIdTransformParseInt";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-11 13:10:16 +00:00
|
|
|
* /v1/resources/{id}/edit:
|
2022-04-02 01:51:33 +00:00
|
|
|
* patch:
|
|
|
|
* summary: Edit an existing resource
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the resource to edit
|
2022-04-28 23:38:40 +00:00
|
|
|
|
2022-04-02 01:51:33 +00:00
|
|
|
* tags:
|
|
|
|
* - resources
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, resource edited successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Resource body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-05-16 21:06:29 +00:00
|
|
|
export async function editResource({ query, body }: NextApiRequest, res: NextApiResponse<ResourceResponse>) {
|
2022-04-30 18:53:19 +00:00
|
|
|
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
|
|
|
const safeBody = schemaResourceBodyParams.safeParse(body);
|
2022-04-02 01:51:33 +00:00
|
|
|
|
|
|
|
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
|
|
|
|
const resource = await prisma.resource.update({
|
|
|
|
where: { id: safeQuery.data.id },
|
|
|
|
data: safeBody.data,
|
|
|
|
});
|
|
|
|
const data = schemaResourcePublic.parse(resource);
|
|
|
|
|
|
|
|
if (data) res.status(200).json({ data });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-11 13:10:16 +00:00
|
|
|
export default withMiddleware("HTTP_PATCH")(
|
|
|
|
withValidQueryIdTransformParseInt(withValidResource(editResource))
|
|
|
|
);
|