2022-04-03 22:49:05 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { UserResponse } from "@lib/types";
|
|
|
|
import {
|
|
|
|
schemaQueryIdParseInt,
|
|
|
|
withValidQueryIdTransformParseInt,
|
|
|
|
} from "@lib/validations/shared/queryIdTransformParseInt";
|
2022-04-26 20:23:33 +00:00
|
|
|
import { schemaUserEditBodyParams, schemaUserReadPublic } from "@lib/validations/user";
|
2022-04-03 22:49:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-26 19:56:59 +00:00
|
|
|
* /users/{id}:
|
2022-04-03 22:49:05 +00:00
|
|
|
* get:
|
2022-04-29 15:29:57 +00:00
|
|
|
* summary: Find a user, returns your user if regular user.
|
2022-04-03 22:49:05 +00:00
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: Numeric ID of the user to get
|
|
|
|
* tags:
|
|
|
|
* - users
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: User was not found
|
|
|
|
* patch:
|
|
|
|
* summary: Edit an existing user
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: Numeric ID of the user to edit
|
|
|
|
* tags:
|
|
|
|
* - users
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, user edited successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. User body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* delete:
|
|
|
|
* summary: Remove an existing user
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: Numeric ID of the user to delete
|
|
|
|
* tags:
|
|
|
|
* - users
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, user removed successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. User id is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-04-24 00:10:32 +00:00
|
|
|
export async function userById(req: NextApiRequest, res: NextApiResponse<any>) {
|
|
|
|
const { method, query, body, userId } = req;
|
2022-04-10 00:10:34 +00:00
|
|
|
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
2022-04-24 00:10:32 +00:00
|
|
|
console.log(body);
|
2022-04-03 22:49:05 +00:00
|
|
|
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
|
2022-04-23 00:40:39 +00:00
|
|
|
if (safeQuery.data.id !== userId) res.status(401).json({ message: "Unauthorized" });
|
2022-04-23 00:17:06 +00:00
|
|
|
else {
|
2022-04-07 19:55:43 +00:00
|
|
|
switch (method) {
|
|
|
|
case "GET":
|
|
|
|
await prisma.user
|
|
|
|
.findUnique({ where: { id: safeQuery.data.id } })
|
2022-04-24 00:10:32 +00:00
|
|
|
.then((data) => schemaUserReadPublic.parse(data))
|
2022-04-11 10:03:15 +00:00
|
|
|
.then((user) => res.status(200).json({ user }))
|
2022-04-07 19:55:43 +00:00
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({ message: `User with id: ${safeQuery.data.id} not found`, error })
|
|
|
|
);
|
|
|
|
break;
|
2022-04-03 22:49:05 +00:00
|
|
|
|
2022-04-07 19:55:43 +00:00
|
|
|
case "PATCH":
|
2022-04-24 00:10:32 +00:00
|
|
|
const safeBody = schemaUserEditBodyParams.safeParse(body);
|
|
|
|
if (!safeBody.success) {
|
|
|
|
res.status(400).json({ message: "Bad request", error: safeBody.error });
|
|
|
|
throw new Error("Invalid request body");
|
|
|
|
}
|
2022-04-24 21:56:25 +00:00
|
|
|
const userSchedules = await prisma.schedule.findMany({
|
|
|
|
where: { userId },
|
|
|
|
});
|
|
|
|
const userSchedulesIds = userSchedules.map((schedule) => schedule.id);
|
|
|
|
// @note: here we make sure user can only make as default his own scheudles
|
|
|
|
if (!userSchedulesIds.includes(Number(safeBody?.data?.defaultScheduleId))) {
|
|
|
|
res.status(400).json({
|
|
|
|
message: "Bad request",
|
|
|
|
error: "Invalid default schedule id",
|
|
|
|
});
|
|
|
|
throw new Error("Invalid request body value: defaultScheduleId");
|
|
|
|
}
|
2022-04-07 19:55:43 +00:00
|
|
|
await prisma.user
|
|
|
|
.update({
|
2022-04-24 00:10:32 +00:00
|
|
|
where: { id: userId },
|
2022-04-07 19:55:43 +00:00
|
|
|
data: safeBody.data,
|
|
|
|
})
|
2022-04-24 00:10:32 +00:00
|
|
|
.then((data) => schemaUserReadPublic.parse(data))
|
2022-04-11 10:03:15 +00:00
|
|
|
.then((user) => res.status(200).json({ user }))
|
2022-04-07 19:55:43 +00:00
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({ message: `User with id: ${safeQuery.data.id} not found`, error })
|
|
|
|
);
|
|
|
|
break;
|
2022-04-03 22:49:05 +00:00
|
|
|
|
2022-04-07 19:55:43 +00:00
|
|
|
case "DELETE":
|
|
|
|
await prisma.user
|
|
|
|
.delete({ where: { id: safeQuery.data.id } })
|
|
|
|
.then(() =>
|
|
|
|
res.status(200).json({ message: `User with id: ${safeQuery.data.id} deleted successfully` })
|
|
|
|
)
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({ message: `User with id: ${safeQuery.data.id} not found`, error })
|
|
|
|
);
|
|
|
|
break;
|
2022-04-03 22:49:05 +00:00
|
|
|
|
2022-04-07 19:55:43 +00:00
|
|
|
default:
|
|
|
|
res.status(405).json({ message: "Method not allowed" });
|
|
|
|
break;
|
|
|
|
}
|
2022-04-23 00:17:06 +00:00
|
|
|
}
|
2022-04-03 22:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(userById));
|