Skip to content
Closed
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions apps/web/pages/api/user/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { NextApiRequest, NextApiResponse } from "next";

import { getSession } from "@lib/auth";
import prisma from "@lib/prisma";
import { randomString } from "@lib/random";
import slugify from "@lib/slugify";

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const session = await getSession({ req });
Expand Down Expand Up @@ -33,6 +35,28 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

if (req.method === "PATCH") {
const user = await prisma.user.findUnique({
where: {
id: authenticatedUser.id,
},
select: {
username: true,
name: true,
},
});

if (!user?.username && user?.name && req.body.data?.completedOnboarding) {
const usernameSlug = (username: string) => slugify(username) + "-" + randomString(6).toLowerCase();
const firstUserName = usernameSlug(user.name);
await prisma.user.update({
where: {
id: authenticatedUser.id,
},
data: {
username: firstUserName,
},
});
}
const updatedUser = await prisma.user.update({
where: {
id: authenticatedUser.id,
Expand Down