prettier
This commit is contained in:
1 parent
39ae2aed37
commit
1de7bc4146
126 files changed
+1240
-992
No files matched your search
@@ -1,29 +1,27 @@
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import type { BaseResponse } from "@lib/types";
|
||||
import {
|
||||
schemaQueryIdParseInt,
|
||||
withValidQueryIdTransformParseInt,
|
||||
} from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function deleteUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
export async function deleteUser(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
|
||||
const safe = await schemaQueryIdParseInt.safeParse(req.query);
|
||||
if (safe.success) {
|
||||
const data = await prisma.user
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
// We only remove the user type from the database if there's an existing resource.
|
||||
if (data) res.status(200).json({ message: `User with id: ${safe.data.id} deleted successfully` });
|
||||
// This catches the error thrown by prisma.user.delete() if the resource is not found.
|
||||
else res.status(400).json({ message: `User with id: ${safe.data.id} was not able to be processed` });
|
||||
}
|
||||
if (!safe.success) throw new Error("Invalid request query", safe.error);
|
||||
|
||||
const data = await prisma.user.delete({ where: { id: safe.data.id } });
|
||||
|
||||
if (data) res.status(200).json({ message: `User with id: ${safe.data.id} deleted successfully` });
|
||||
else
|
||||
(error: Error) =>
|
||||
res.status(400).json({
|
||||
message: `User with id: ${safe.data.id} was not able to be processed`,
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
export default withMiddleware("deleteOnly", "addRequestId")(
|
||||
withValidQueryIdTransformParseInt(
|
||||
deleteUser
|
||||
)
|
||||
);
|
||||
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteUser));
|
||||
@@ -1,36 +1,33 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { User } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaUser, withValidUser } from "@lib/validations/user";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import type { UserResponse } from "@lib/types";
|
||||
import {
|
||||
schemaQueryIdParseInt,
|
||||
withValidQueryIdTransformParseInt,
|
||||
} from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaUserBodyParams, schemaUserPublic, withValidUser } from "@lib/validations/user";
|
||||
|
||||
type ResponseData = {
|
||||
data?: User;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
export async function editUser(req: NextApiRequest, res: NextApiResponse<UserResponse>) {
|
||||
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
|
||||
const safeBody = await schemaUserBodyParams.safeParse(req.body);
|
||||
|
||||
export async function editUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = await schemaUser.safeParse(body);
|
||||
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
|
||||
const user = await prisma.user.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: safeBody.data,
|
||||
});
|
||||
const data = schemaUserPublic.parse(user);
|
||||
|
||||
if (safeQuery.success && safeBody.success) {
|
||||
const data = await prisma.user.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: safeBody.data,
|
||||
})
|
||||
if (data) res.status(200).json({ data });
|
||||
else (error: unknown) => res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })
|
||||
}
|
||||
if (data) res.status(200).json({ data });
|
||||
else
|
||||
(error: Error) =>
|
||||
res.status(404).json({
|
||||
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
export default withMiddleware("patchOnly","addRequestId")(
|
||||
withValidQueryIdTransformParseInt(
|
||||
withValidUser(
|
||||
editUser)
|
||||
)
|
||||
);
|
||||
export default withMiddleware("HTTP_PATCH")(withValidQueryIdTransformParseInt(withValidUser(editUser)));
|
||||
@@ -1,30 +1,29 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { User } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import type { UserResponse } from "@lib/types";
|
||||
import {
|
||||
schemaQueryIdParseInt,
|
||||
withValidQueryIdTransformParseInt,
|
||||
} from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { schemaUserPublic } from "@lib/validations/user";
|
||||
|
||||
type ResponseData = {
|
||||
data?: User;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function userById(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
export async function userById(req: NextApiRequest, res: NextApiResponse<UserResponse>) {
|
||||
const safe = await schemaQueryIdParseInt.safeParse(req.query);
|
||||
if (safe.success) {
|
||||
const data = await prisma.user.findUnique({ where: { id: safe.data.id } });
|
||||
if (!safe.success) throw new Error("Invalid request query");
|
||||
|
||||
if (data) res.status(200).json({ data });
|
||||
else res.status(404).json({ message: "User was not found" });
|
||||
}
|
||||
const user = await prisma.user.findUnique({ where: { id: safe.data.id } });
|
||||
const data = schemaUserPublic.parse(user);
|
||||
|
||||
if (user) res.status(200).json({ data });
|
||||
else
|
||||
(error: Error) =>
|
||||
res.status(404).json({
|
||||
message: "User was not found",
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export default withMiddleware("addRequestId","getOnly")(
|
||||
withValidQueryIdTransformParseInt(
|
||||
userById
|
||||
)
|
||||
);
|
||||
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(userById));
|
||||
+15
-12
@@ -1,19 +1,22 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { User } from "@calcom/prisma/client";
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
type ResponseData = {
|
||||
data?: User[];
|
||||
error?: unknown;
|
||||
};
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
async function allUsers(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const data = await prisma.user.findMany();
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import { UsersResponse } from "@lib/types";
|
||||
import { schemaUserPublic } from "@lib/validations/user";
|
||||
|
||||
async function allUsers(_: NextApiRequest, res: NextApiResponse<UsersResponse>) {
|
||||
const users = await prisma.user.findMany();
|
||||
const data = users.map((user) => schemaUserPublic.parse(user));
|
||||
|
||||
if (data) res.status(200).json({ data });
|
||||
else res.status(400).json({ error: "No data found" });
|
||||
else
|
||||
(error: Error) =>
|
||||
res.status(400).json({
|
||||
message: "No Users were found",
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
export default withMiddleware("addRequestId","getOnly")(allUsers);
|
||||
export default withMiddleware("HTTP_GET")(allUsers);
|
||||
+16
-19
@@ -1,28 +1,25 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
import { User } from "@calcom/prisma/client";
|
||||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import { schemaUser, withValidUser } from "@lib/validations/user";
|
||||
import type { UserResponse } from "@lib/types";
|
||||
import { schemaUserBodyParams, schemaUserPublic, withValidUser } from "@lib/validations/user";
|
||||
|
||||
async function createUser(req: NextApiRequest, res: NextApiResponse<UserResponse>) {
|
||||
const safe = schemaUserBodyParams.safeParse(req.body);
|
||||
if (!safe.success) throw new Error("Invalid request body", safe.error);
|
||||
|
||||
type ResponseData = {
|
||||
data?: User;
|
||||
error?: object;
|
||||
};
|
||||
const user = await prisma.user.create({ data: safe.data });
|
||||
const data = schemaUserPublic.parse(user);
|
||||
|
||||
async function createUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const safe = schemaUser.safeParse(req.body);
|
||||
if (safe.success) {
|
||||
const data = await prisma.user
|
||||
.create({ data: safe.data })
|
||||
if (data) res.status(201).json({ data })
|
||||
else (error: unknown) => res.status(400).json({ error: { message: "Could not create user type", error: error } });
|
||||
}
|
||||
if (data) res.status(201).json({ data, message: "User created successfully" });
|
||||
else
|
||||
(error: Error) =>
|
||||
res.status(400).json({
|
||||
message: "Could not create new user",
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
export default withMiddleware("addRequestId","postOnly")(
|
||||
withValidUser(
|
||||
createUser
|
||||
)
|
||||
);
|
||||
export default withMiddleware("HTTP_POST")(withValidUser(createUser));
|
||||
Reference in new issue
Block a user