2022-07-06 19:31:07 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { getSession } from "next-auth/react";
|
|
|
|
|
|
|
|
import { defaultHandler } from "@calcom/lib/server";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { checkUsername } from "@calcom/lib/server/checkUsername";
|
2022-07-06 19:31:07 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import { userMetadata as zodUserMetadata } from "@calcom/prisma/zod-utils";
|
|
|
|
|
|
|
|
export async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
const { intentUsername } = req.body;
|
|
|
|
// Check that user is authenticated
|
|
|
|
try {
|
|
|
|
const session = await getSession({ req });
|
|
|
|
const userId = session?.user?.id;
|
2022-08-31 19:44:47 +00:00
|
|
|
const user = await prisma.user.findFirstOrThrow({
|
2022-07-06 19:31:07 +00:00
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
metadata: true,
|
|
|
|
},
|
|
|
|
where: { id: userId },
|
|
|
|
});
|
|
|
|
const checkPremiumUsernameResult = await checkUsername(intentUsername);
|
|
|
|
|
|
|
|
if (userId && user) {
|
|
|
|
const userMetadata = zodUserMetadata.parse(user.metadata);
|
|
|
|
|
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
metadata: {
|
|
|
|
...userMetadata,
|
|
|
|
intentUsername,
|
|
|
|
isIntentPremium: checkPremiumUsernameResult.premium,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
res.status(501).send({ message: "intent-username.save.error" });
|
|
|
|
}
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultHandler({
|
|
|
|
GET: Promise.resolve({ default: getHandler }),
|
|
|
|
});
|