2022-02-11 22:20:10 +00:00
|
|
|
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
2021-09-22 19:52:38 +00:00
|
|
|
import { pick } from "lodash";
|
2021-07-11 19:35:56 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-03 20:51:21 +00:00
|
|
|
import { getSession } from "@lib/auth";
|
2021-09-22 19:52:38 +00:00
|
|
|
import prisma from "@lib/prisma";
|
2021-04-07 15:03:02 +00:00
|
|
|
|
2021-10-07 15:43:20 +00:00
|
|
|
import { resizeBase64Image } from "@server/lib/resizeBase64Image";
|
|
|
|
|
2021-04-07 15:03:02 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-07-11 19:35:56 +00:00
|
|
|
const session = await getSession({ req: req });
|
2021-04-07 15:03:02 +00:00
|
|
|
|
2021-06-09 12:26:00 +00:00
|
|
|
if (!session) {
|
2021-07-11 19:35:56 +00:00
|
|
|
res.status(401).json({ message: "Not authenticated" });
|
|
|
|
return;
|
2021-06-09 12:26:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 17:21:23 +00:00
|
|
|
try {
|
2021-09-30 20:37:29 +00:00
|
|
|
const avatar = req.body.avatar ? await resizeBase64Image(req.body.avatar) : undefined;
|
2021-09-08 17:21:23 +00:00
|
|
|
await prisma.user.update({
|
2021-06-09 12:26:00 +00:00
|
|
|
where: {
|
2021-09-08 17:21:23 +00:00
|
|
|
id: session.user.id,
|
|
|
|
},
|
|
|
|
data: {
|
2021-09-17 11:25:48 +00:00
|
|
|
...pick(req.body, [
|
|
|
|
"username",
|
|
|
|
"name",
|
|
|
|
"timeZone",
|
|
|
|
"weekStart",
|
|
|
|
"hideBranding",
|
|
|
|
"theme",
|
|
|
|
"completedOnboarding",
|
2021-09-23 08:49:17 +00:00
|
|
|
"locale",
|
2021-09-17 11:25:48 +00:00
|
|
|
]),
|
2021-09-30 20:37:29 +00:00
|
|
|
avatar,
|
2021-09-17 11:25:48 +00:00
|
|
|
bio: req.body.description,
|
2021-07-11 19:35:56 +00:00
|
|
|
},
|
2021-04-07 15:03:02 +00:00
|
|
|
});
|
2021-09-08 17:21:23 +00:00
|
|
|
} catch (e) {
|
2022-02-10 10:44:46 +00:00
|
|
|
if (e instanceof PrismaClientKnownRequestError) {
|
|
|
|
if (e.code === "P2002") {
|
|
|
|
return res.status(409).json({ message: "Username already taken" });
|
|
|
|
}
|
2021-06-09 12:26:00 +00:00
|
|
|
}
|
2021-09-08 17:21:23 +00:00
|
|
|
throw e;
|
2021-06-09 12:26:00 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 19:35:56 +00:00
|
|
|
return res.status(200).json({ message: "Profile updated successfully" });
|
2021-07-09 22:59:21 +00:00
|
|
|
}
|