2021-09-22 19:52:38 +00:00
|
|
|
import { pick } from "lodash";
|
2021-09-02 12:13:19 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-07-12 17:43:53 +00:00
|
|
|
import z from "zod";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
2021-09-03 20:51:21 +00:00
|
|
|
import { getSession } from "@lib/auth";
|
2021-09-02 12:13:19 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2022-01-07 20:23:37 +00:00
|
|
|
const session = await getSession({ req });
|
2021-09-02 12:13:19 +00:00
|
|
|
|
2022-01-07 20:23:37 +00:00
|
|
|
if (!session?.user.id) {
|
2021-09-02 12:13:19 +00:00
|
|
|
return res.status(401).json({ message: "Not authenticated" });
|
|
|
|
}
|
|
|
|
|
2022-07-12 17:43:53 +00:00
|
|
|
const querySchema = z.object({
|
|
|
|
id: z.string().transform((val) => parseInt(val)),
|
|
|
|
});
|
|
|
|
|
|
|
|
const parsedQuery = querySchema.safeParse(req.query);
|
|
|
|
const userId = parsedQuery.success ? parsedQuery.data.id : null;
|
|
|
|
|
|
|
|
if (!userId) {
|
|
|
|
return res.status(400).json({ message: "No user id provided" });
|
|
|
|
}
|
2021-09-02 12:13:19 +00:00
|
|
|
|
|
|
|
const authenticatedUser = await prisma.user.findFirst({
|
2022-01-07 20:23:37 +00:00
|
|
|
rejectOnNotFound: true,
|
2021-09-02 12:13:19 +00:00
|
|
|
where: {
|
2022-01-07 20:23:37 +00:00
|
|
|
id: session.user.id,
|
2021-09-02 12:13:19 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (userId !== authenticatedUser.id) {
|
|
|
|
return res.status(401).json({ message: "Unauthorized" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === "GET") {
|
|
|
|
return res.status(405).json({ message: "Method Not Allowed" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === "PATCH") {
|
|
|
|
const updatedUser = await prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: authenticatedUser.id,
|
|
|
|
},
|
|
|
|
data: {
|
2021-09-17 14:41:28 +00:00
|
|
|
...pick(req.body.data, [
|
2021-09-17 11:25:48 +00:00
|
|
|
"username",
|
|
|
|
"name",
|
|
|
|
"avatar",
|
|
|
|
"timeZone",
|
2022-07-26 18:02:44 +00:00
|
|
|
"timeFormat",
|
2021-09-17 11:25:48 +00:00
|
|
|
"weekStart",
|
|
|
|
"hideBranding",
|
|
|
|
"theme",
|
|
|
|
"completedOnboarding",
|
|
|
|
]),
|
2021-09-22 07:25:33 +00:00
|
|
|
bio: req.body.description ?? req.body.data?.bio,
|
2021-09-17 11:25:48 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
username: true,
|
|
|
|
name: true,
|
|
|
|
email: true,
|
|
|
|
emailVerified: true,
|
|
|
|
bio: true,
|
|
|
|
avatar: true,
|
|
|
|
timeZone: true,
|
2022-07-26 18:02:44 +00:00
|
|
|
timeFormat: true,
|
2021-09-17 11:25:48 +00:00
|
|
|
weekStart: true,
|
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
|
|
|
bufferTime: true,
|
|
|
|
hideBranding: true,
|
|
|
|
theme: true,
|
|
|
|
createdDate: true,
|
|
|
|
plan: true,
|
|
|
|
completedOnboarding: true,
|
2021-09-02 12:13:19 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
return res.status(200).json({ message: "User Updated", data: updatedUser });
|
|
|
|
}
|
|
|
|
}
|