diff --git a/apps/web/pages/api/user/[id].ts b/apps/web/pages/api/user/[id].ts index 252f9279b2345f..420ecbe28ea5fc 100644 --- a/apps/web/pages/api/user/[id].ts +++ b/apps/web/pages/api/user/[id].ts @@ -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 }); @@ -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,