2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2022-03-25 22:26:22 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
2022-03-29 01:23:22 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-03-30 12:17:55 +00:00
|
|
|
import { UsersResponse } from "@lib/types";
|
2022-04-08 18:54:48 +00:00
|
|
|
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
|
2022-03-30 12:17:55 +00:00
|
|
|
import { schemaUserPublic } from "@lib/validations/user";
|
2022-03-25 22:26:22 +00:00
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-08 23:29:26 +00:00
|
|
|
* /v1/users:
|
2022-03-30 14:56:24 +00:00
|
|
|
* get:
|
2022-04-07 19:55:43 +00:00
|
|
|
* summary: Get all users (admin only), returns your user if regular user.
|
2022-03-31 20:14:37 +00:00
|
|
|
* tags:
|
|
|
|
* - users
|
2022-03-30 14:56:24 +00:00
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: No users were found
|
|
|
|
*/
|
2022-04-07 19:55:43 +00:00
|
|
|
async function allUsers(req: NextApiRequest, res: NextApiResponse<UsersResponse>) {
|
|
|
|
const userId = getCalcomUserId(res);
|
2022-04-11 10:03:15 +00:00
|
|
|
const data = await prisma.user.findMany({
|
2022-04-07 19:55:43 +00:00
|
|
|
where: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
});
|
2022-04-11 10:03:15 +00:00
|
|
|
const users = data.map((user) => schemaUserPublic.parse(user));
|
|
|
|
if (users) res.status(200).json({ users });
|
2022-03-30 12:17:55 +00:00
|
|
|
else
|
|
|
|
(error: Error) =>
|
2022-03-30 14:56:24 +00:00
|
|
|
res.status(404).json({
|
2022-03-30 12:17:55 +00:00
|
|
|
message: "No Users were found",
|
|
|
|
error,
|
|
|
|
});
|
2022-03-25 22:26:22 +00:00
|
|
|
}
|
2022-04-07 19:55:43 +00:00
|
|
|
// No POST endpoint for users for now as a regular user you're expected to signup.
|
2022-03-29 01:23:22 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
export default withMiddleware("HTTP_GET")(allUsers);
|