Moved API files to correct dir

This commit is contained in:
zomars committed 2023-05-25 11:29:10 -07:00
1 parent ea32a698a3
commit 47488f5fff
176 files changed

No files matched your search

@@ -0,0 +1,51 @@
import type { NextApiRequest } from "next";
import { HttpError } from "@calcom/lib/http-error";
import { defaultResponder } from "@calcom/lib/server";
import { schemaQueryUserId } from "~/lib/validations/shared/queryUserId";
/**
* @swagger
* /users/{userId}:
* delete:
* summary: Remove an existing user
* operationId: removeUserById
* parameters:
* - in: path
* name: userId
* example: 1
* schema:
* type: integer
* required: true
* description: ID of the user to delete
* - in: query
* name: apiKey
* schema:
* type: string
* required: true
* description: Your API key
* 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) {
const { prisma, isAdmin } = req;
const query = schemaQueryUserId.parse(req.query);
// Here we only check for ownership of the user if the user is not admin, otherwise we let ADMIN's edit any user
if (!isAdmin && query.userId !== req.userId) throw new HttpError({ statusCode: 403, message: "Forbidden" });
const user = await prisma.user.findUnique({ where: { id: query.userId } });
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` };
}
export default defaultResponder(deleteHandler);
+50
View File
@@ -0,0 +1,50 @@
import type { NextApiRequest } from "next";
import { HttpError } from "@calcom/lib/http-error";
import { defaultResponder } from "@calcom/lib/server";
import { schemaQueryUserId } from "~/lib/validations/shared/queryUserId";
import { schemaUserReadPublic } from "~/lib/validations/user";
/**
* @swagger
* /users/{userId}:
* get:
* summary: Find a user, returns your user if regular user.
* operationId: getUserById
* parameters:
* - in: path
* name: userId
* example: 4
* schema:
* type: integer
* required: true
* description: ID of the user to get
* - in: query
* name: apiKey
* schema:
* type: string
* required: true
* description: Your API key
* tags:
* - users
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: User was not found
*/
export async function getHandler(req: NextApiRequest) {
const { prisma, isAdmin } = req;
const query = schemaQueryUserId.parse(req.query);
// Here we only check for ownership of the user if the user is not admin, otherwise we let ADMIN's edit any user
if (!isAdmin && query.userId !== req.userId) throw new HttpError({ statusCode: 403, message: "Forbidden" });
const data = await prisma.user.findUnique({ where: { id: query.userId } });
const user = schemaUserReadPublic.parse(data);
return { user };
}
export default defaultResponder(getHandler);
+121
View File
@@ -0,0 +1,121 @@
import type { NextApiRequest } from "next";
import { HttpError } from "@calcom/lib/http-error";
import { defaultResponder } from "@calcom/lib/server";
import { schemaQueryUserId } from "~/lib/validations/shared/queryUserId";
import { schemaUserEditBodyParams, schemaUserReadPublic } from "~/lib/validations/user";
/**
* @swagger
* /users/{userId}:
* patch:
* summary: Edit an existing user
* operationId: editUserById
* parameters:
* - in: path
* name: userId
* example: 4
* schema:
* type: integer
* required: true
* description: ID of the user to edit
* - in: query
* name: apiKey
* required: true
* schema:
* type: string
* description: Your API key
* requestBody:
* description: Edit an existing attendee related to one of your bookings
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* email:
* type: string
* format: email
* description: Email that belongs to the user being edited
* username:
* type: string
* description: Username for the user being edited
* brandColor:
* description: The user's brand color
* type: string
* darkBrandColor:
* description: The user's brand color for dark mode
* type: string
* weekStart:
* description: Start of the week. Acceptable values are one of [SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]
* type: string
* timeZone:
* description: The user's time zone
* type: string
* theme:
* description: Default theme for the user. Acceptable values are one of [DARK, LIGHT]
* type: string
* timeFormat:
* description: The user's time format. Acceptable values are one of [TWELVE, TWENTY_FOUR]
* type: string
* locale:
* description: The user's locale. Acceptable values are one of [EN, FR, IT, RU, ES, DE, PT, RO, NL, PT_BR, ES_419, KO, JA, PL, AR, IW, ZH_CH, ZH_TW, CS, SR, SV, VI]
* type: string
* examples:
* user:
* summary: An example of USER
* value:
* email: [email protected]
* username: johndoe
* weekStart: MONDAY
* brandColor: #555555
* darkBrandColor: #111111
* timeZone: EUROPE/PARIS
* theme: LIGHT
* timeFormat: TWELVE
* locale: FR
* tags:
* - users
* responses:
* 200:
* description: OK, user edited successfuly
* 400:
* description: Bad request. User body is invalid.
* 401:
* description: Authorization information is missing or invalid.
* 403:
* description: Insufficient permissions to access resource.
*/
export async function patchHandler(req: NextApiRequest) {
const { prisma, isAdmin } = req;
const query = schemaQueryUserId.parse(req.query);
// Here we only check for ownership of the user if the user is not admin, otherwise we let ADMIN's edit any user
if (!isAdmin && query.userId !== req.userId) throw new HttpError({ statusCode: 403, message: "Forbidden" });
const body = await schemaUserEditBodyParams.parseAsync(req.body);
// disable role changes unless admin.
if (!isAdmin && body.role) {
body.role = undefined;
}
const userSchedules = await prisma.schedule.findMany({
where: { userId: query.userId },
});
const userSchedulesIds = userSchedules.map((schedule) => schedule.id);
// @note: here we make sure user can only make as default his own scheudles
if (body.defaultScheduleId && !userSchedulesIds.includes(Number(body.defaultScheduleId))) {
throw new HttpError({
statusCode: 400,
message: "Bad request: Invalid default schedule id",
});
}
const data = await prisma.user.update({
where: { id: query.userId },
data: body,
});
const user = schemaUserReadPublic.parse(data);
return { user };
}
export default defaultResponder(patchHandler);
@@ -0,0 +1,9 @@
import { defaultHandler } from "@calcom/lib/server";
import { withMiddleware } from "~/lib/helpers/withMiddleware";
export default withMiddleware()(
defaultHandler({
GET: import("~/pages/api/availability/_get"),
})
);
@@ -0,0 +1,11 @@
import { defaultHandler } from "@calcom/lib/server";
import { withMiddleware } from "~/lib/helpers/withMiddleware";
export default withMiddleware()(
defaultHandler({
GET: import("./_get"),
PATCH: import("./_patch"),
DELETE: import("./_delete"),
})
);
+50
View File
@@ -0,0 +1,50 @@
import type { Prisma } from "@prisma/client";
import type { NextApiRequest } from "next";
import { defaultResponder } from "@calcom/lib/server";
import { withMiddleware } from "~/lib/helpers/withMiddleware";
import { schemaUsersReadPublic } from "~/lib/validations/user";
/**
* @swagger
* /users:
* get:
* operationId: listUsers
* summary: Find all users.
* parameters:
* - in: query
* name: apiKey
* required: true
* schema:
* type: string
* description: Your API key
* tags:
* - users
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No users were found
*/
export async function getHandler(req: NextApiRequest) {
const {
userId,
prisma,
isAdmin,
pagination: { take, skip },
} = req;
const where: Prisma.UserWhereInput = {};
// If user is not ADMIN, return only his data.
if (!isAdmin) where.id = userId;
const [total, data] = await prisma.$transaction([
prisma.user.count(),
prisma.user.findMany({ where, take, skip }),
]);
const users = schemaUsersReadPublic.parse(data);
return { users, total };
}
export default withMiddleware("pagination")(defaultResponder(getHandler));
+93
View File
@@ -0,0 +1,93 @@
import type { NextApiRequest } from "next";
import { HttpError } from "@calcom/lib/http-error";
import { defaultResponder } from "@calcom/lib/server";
import { schemaUserCreateBodyParams } from "~/lib/validations/user";
/**
* @swagger
* /users:
* post:
* operationId: addUser
* summary: Creates a new user
* parameters:
* - in: query
* name: apiKey
* required: true
* schema:
* type: string
* description: Your API key
* requestBody:
* description: Create a new user
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - email
* - username
* properties:
* email:
* type: string
* format: email
* description: Email that belongs to the user being edited
* username:
* type: string
* description: Username for the user being created
* brandColor:
* description: The new user's brand color
* type: string
* darkBrandColor:
* description: The new user's brand color for dark mode
* type: string
* weekStart:
* description: Start of the week. Acceptable values are one of [SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]
* type: string
* timeZone:
* description: The new user's time zone. Eg- 'EUROPE/PARIS'
* type: string
* theme:
* description: Default theme for the new user. Acceptable values are one of [DARK, LIGHT]
* type: string
* timeFormat:
* description: The new user's time format. Acceptable values are one of [TWELVE, TWENTY_FOUR]
* type: string
* locale:
* description: The new user's locale. Acceptable values are one of [EN, FR, IT, RU, ES, DE, PT, RO, NL, PT_BR, ES_419, KO, JA, PL, AR, IW, ZH_CH, ZH_TW, CS, SR, SV, VI]
* type: string
* examples:
* user:
* summary: An example of USER
* value:
* email: '[email protected]'
* username: 'johndoe'
* weekStart: 'MONDAY'
* brandColor: '#555555'
* darkBrandColor: '#111111'
* timeZone: 'EUROPE/PARIS'
* theme: 'LIGHT'
* timeFormat: 'TWELVE'
* locale: 'FR'
* tags:
* - users
* responses:
* 201:
* description: OK, user created
* 400:
* description: Bad request. user body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function postHandler(req: NextApiRequest) {
const { prisma, isAdmin } = req;
// If user is not ADMIN, return unauthorized.
if (!isAdmin) throw new HttpError({ statusCode: 401, message: "You are not authorized" });
const data = await schemaUserCreateBodyParams.parseAsync(req.body);
const user = await prisma.user.create({ data });
req.statusCode = 201;
return { user };
}
export default defaultResponder(postHandler);
+10
View File
@@ -0,0 +1,10 @@
import { defaultHandler } from "@calcom/lib/server";
import { withMiddleware } from "~/lib/helpers/withMiddleware";
export default withMiddleware()(
defaultHandler({
GET: import("./_get"),
POST: import("./_post"),
})
);