2022-03-25 22:26:22 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-25 22:26:22 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { UserResponse } from "@lib/types";
|
|
|
|
import {
|
|
|
|
schemaQueryIdParseInt,
|
|
|
|
withValidQueryIdTransformParseInt,
|
|
|
|
} from "@lib/validations/shared/queryIdTransformParseInt";
|
|
|
|
import { schemaUserPublic } from "@lib/validations/user";
|
2022-03-25 22:26:22 +00:00
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
2022-03-31 20:14:37 +00:00
|
|
|
* /api/users/{id}:
|
2022-03-30 14:56:24 +00:00
|
|
|
* get:
|
2022-03-31 20:14:37 +00:00
|
|
|
* summary: Get a user by ID
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: Numeric ID of the user to get
|
|
|
|
* tags:
|
|
|
|
* - users
|
2022-03-30 14:56:24 +00:00
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: User was not found
|
|
|
|
*/
|
2022-03-30 12:17:55 +00:00
|
|
|
export async function userById(req: NextApiRequest, res: NextApiResponse<UserResponse>) {
|
2022-03-29 01:59:57 +00:00
|
|
|
const safe = await schemaQueryIdParseInt.safeParse(req.query);
|
2022-03-30 12:17:55 +00:00
|
|
|
if (!safe.success) throw new Error("Invalid request query");
|
2022-03-25 22:26:22 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
const user = await prisma.user.findUnique({ where: { id: safe.data.id } });
|
|
|
|
const data = schemaUserPublic.parse(user);
|
2022-03-25 22:26:22 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
if (user) res.status(200).json({ data });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: "User was not found",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
2022-03-25 22:26:22 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(userById));
|