cal.pub0.org/pages/api/user/profile.ts

42 lines
999 B
TypeScript
Raw Normal View History

2021-07-11 19:35:56 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@lib/prisma";
import { getSession } from "@lib/auth";
import { pick } from "lodash";
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
if (!session) {
2021-07-11 19:35:56 +00:00
res.status(401).json({ message: "Not authenticated" });
return;
}
try {
await prisma.user.update({
where: {
id: session.user.id,
},
data: {
...pick(req.body, [
"username",
"name",
"avatar",
"timeZone",
"weekStart",
"hideBranding",
"theme",
"completedOnboarding",
]),
bio: req.body.description,
2021-07-11 19:35:56 +00:00
},
2021-04-07 15:03:02 +00:00
});
} catch (e) {
if (e.code === "P2002") {
2021-07-11 19:35:56 +00:00
return res.status(409).json({ message: "Username already taken" });
}
throw e;
}
2021-07-11 19:35:56 +00:00
return res.status(200).json({ message: "Profile updated successfully" });
}