1d927a8b33
* WIP close.com app * Removing leaked dev key (now invalid) * Misspelled env variable * Making progress still WIP * Progress + tests * Final touches * More unit tests * Finished up tests * Merge main * Removing unneeded stuff + submodules * Removing static props, fields fix * Removing unneeded stuff p2 * Commenting * Refactoring Close.com Calendar Service + initial structure * Progress con CloseComService * Standarizing APIs * Zodifying * Expanding sync services * Sendgrid Sync Service * using own request for sendgrid + debug logs * Making get last booking work for console * Helpscout dynamic app API * Standarizing calls + adding call from booking creation * Strategy change for last booking * Strategy change for last booking on help scout api * Fixing failing build * Implementing user deletion * Fix linting + slight cleaning * Undoing eslint disable * Removing more unsupported eslint properties * Closecom as non-standard sync service * Finishing closecom lead operations * Fixing lint * Guarding app from sync services * Reverting submodules * Applying PR feedback * Reverting API to be plain handler * Cleaning notes Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: zomars <zomars@me.com>
96 lines
2.5 KiB
TypeScript
96 lines
2.5 KiB
TypeScript
import { IdentityProvider } from "@prisma/client";
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { closeComUpsertTeamUser } from "@calcom/lib/sync/SyncServiceManager";
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import { hashPassword } from "@lib/auth";
|
|
import slugify from "@lib/slugify";
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
if (req.method !== "POST") {
|
|
return;
|
|
}
|
|
|
|
const data = req.body;
|
|
const { email, password } = data;
|
|
const username = slugify(data.username);
|
|
const userEmail = email.toLowerCase();
|
|
|
|
if (!username) {
|
|
res.status(422).json({ message: "Invalid username" });
|
|
return;
|
|
}
|
|
|
|
if (!userEmail || !userEmail.includes("@")) {
|
|
res.status(422).json({ message: "Invalid email" });
|
|
return;
|
|
}
|
|
|
|
if (!password || password.trim().length < 7) {
|
|
res.status(422).json({ message: "Invalid input - password should be at least 7 characters long." });
|
|
return;
|
|
}
|
|
|
|
// There is actually an existingUser if username matches
|
|
// OR if email matches and both username and password are set
|
|
const existingUser = await prisma.user.findFirst({
|
|
where: {
|
|
OR: [
|
|
{ username },
|
|
{
|
|
AND: [{ email: userEmail }, { password: { not: null } }, { username: { not: null } }],
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
if (existingUser) {
|
|
const message: string =
|
|
existingUser.email !== userEmail ? "Username already taken" : "Email address is already registered";
|
|
|
|
return res.status(409).json({ message });
|
|
}
|
|
|
|
const hashedPassword = await hashPassword(password);
|
|
|
|
const user = await prisma.user.upsert({
|
|
where: { email: userEmail },
|
|
update: {
|
|
username,
|
|
password: hashedPassword,
|
|
emailVerified: new Date(Date.now()),
|
|
identityProvider: IdentityProvider.CAL,
|
|
},
|
|
create: {
|
|
username,
|
|
email: userEmail,
|
|
password: hashedPassword,
|
|
identityProvider: IdentityProvider.CAL,
|
|
},
|
|
});
|
|
|
|
// If user has been invitedTo a team, we accept the membership
|
|
if (user.invitedTo) {
|
|
const team = await prisma.team.findFirst({
|
|
where: { id: user.invitedTo },
|
|
});
|
|
|
|
if (team) {
|
|
const membership = await prisma.membership.update({
|
|
where: {
|
|
userId_teamId: { userId: user.id, teamId: user.invitedTo },
|
|
},
|
|
data: {
|
|
accepted: true,
|
|
},
|
|
});
|
|
|
|
// Sync Services: Close.com
|
|
closeComUpsertTeamUser(team, user, membership.role);
|
|
}
|
|
}
|
|
|
|
res.status(201).json({ message: "Created user" });
|
|
}
|