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

60 lines
1.7 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";
import { UserResponse, 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:
* operationId: listUsers
2022-04-30 17:46:04 +00:00
* summary: Find all users.
* tags:
* - users
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No users were found
*/
async function getAllorCreateUser(
{ userId, method, body }: NextApiRequest,
res: NextApiResponse<UsersResponse | UserResponse>
) {
if (method === "GET") {
const data = await prisma.user.findMany({
where: {
id: userId,
},
});
const users = data.map((user) => schemaUserReadPublic.parse(user));
if (users) res.status(200).json({ users });
else
(error: Error) =>
res.status(404).json({
message: "No Users were found",
error,
});
}
// else if (method === "POST") {
// const isAdmin = await prisma.user
// .findUnique({ where: { id: userId } })
// .then((user) => user?.role === "ADMIN");
// if (!isAdmin) res.status(401).json({ message: "You are not authorized" });
// else {
// const user = await prisma.user.create({
// data: schemaUserReadPublic.parse(body),
// });
// res.status(201).json({ user });
// }
// }
}
// No POST endpoint for users for now as a regular user you're expected to signup.
2022-03-29 01:23:22 +00:00
export default withMiddleware("HTTP_GET_OR_POST")(getAllorCreateUser);