2022-06-14 20:35:15 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
|
|
|
|
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
|
|
import { defaultResponder } from "@calcom/lib/server";
|
|
|
|
|
2022-06-15 22:18:40 +00:00
|
|
|
import { schemaQueryUserId } from "@lib/validations/shared/queryUserId";
|
2022-06-14 20:35:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /users/{id}:
|
|
|
|
* delete:
|
|
|
|
* summary: Remove an existing user
|
|
|
|
* operationId: removeUserById
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* example: 1
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: 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.
|
|
|
|
*/
|
|
|
|
export async function deleteHandler(req: NextApiRequest) {
|
2022-06-23 22:53:15 +00:00
|
|
|
const { prisma, isAdmin } = req;
|
2022-06-15 22:18:40 +00:00
|
|
|
const query = schemaQueryUserId.parse(req.query);
|
2022-06-14 20:35:15 +00:00
|
|
|
// Here we only check for ownership of the user if the user is not admin, otherwise we let ADMIN's edit any user
|
2022-06-15 22:18:40 +00:00
|
|
|
if (!isAdmin && query.userId !== req.userId)
|
|
|
|
throw new HttpError({ statusCode: 401, message: "Unauthorized" });
|
2022-06-14 20:51:02 +00:00
|
|
|
|
2022-06-15 23:48:29 +00:00
|
|
|
const user = await prisma.user.findUnique({ where: { id: query.userId } });
|
2022-06-14 20:51:02 +00:00
|
|
|
if (!user) throw new HttpError({ statusCode: 404, message: "User not found" });
|
|
|
|
|
|
|
|
await prisma.user.delete({ where: { id: user.id } });
|
|
|
|
return { message: `User with id: ${user.id} deleted successfully` };
|
2022-06-14 20:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultResponder(deleteHandler);
|