cal.pub0.org/pages/api/users/index.ts

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-03-30 12:17:55 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
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-24 00:10:32 +00:00
import { schemaUserReadPublic } from "@lib/validations/user";
/**
* @swagger
2022-04-26 19:56:59 +00:00
* /users:
* get:
* summary: Find all users (admin only), returns your user if regular user.
* tags:
* - users
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No users were found
*/
async function allUsers(req: NextApiRequest, res: NextApiResponse<UsersResponse>) {
2022-04-22 01:36:33 +00:00
const userId = req.userId;
const data = await prisma.user.findMany({
where: {
id: userId,
},
});
2022-04-24 00:10:32 +00:00
const users = data.map((user) => schemaUserReadPublic.parse(user));
if (users) res.status(200).json({ users });
2022-03-30 12:17:55 +00:00
else
(error: Error) =>
res.status(404).json({
2022-03-30 12:17:55 +00:00
message: "No Users were found",
error,
});
}
// 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);