2023-06-09 18:21:18 +10:00
|
|
|
import { hash } from 'bcrypt';
|
|
|
|
|
|
2023-12-14 15:22:54 +11:00
|
|
|
import { getStripeCustomerByUser } from '@documenso/ee/server-only/stripe/get-customer';
|
2023-06-09 18:21:18 +10:00
|
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
|
import { IdentityProvider } from '@documenso/prisma/client';
|
|
|
|
|
|
|
|
|
|
import { SALT_ROUNDS } from '../../constants/auth';
|
2023-12-14 15:22:54 +11:00
|
|
|
import { getFlag } from '../../universal/get-feature-flag';
|
2023-06-09 18:21:18 +10:00
|
|
|
|
|
|
|
|
export interface CreateUserOptions {
|
|
|
|
|
name: string;
|
|
|
|
|
email: string;
|
|
|
|
|
password: string;
|
2023-09-01 19:46:44 +10:00
|
|
|
signature?: string | null;
|
2023-06-09 18:21:18 +10:00
|
|
|
}
|
|
|
|
|
|
2023-09-01 19:46:44 +10:00
|
|
|
export const createUser = async ({ name, email, password, signature }: CreateUserOptions) => {
|
2023-12-14 15:22:54 +11:00
|
|
|
const isBillingEnabled = await getFlag('app_billing');
|
|
|
|
|
|
2023-06-09 18:21:18 +10:00
|
|
|
const hashedPassword = await hash(password, SALT_ROUNDS);
|
|
|
|
|
|
|
|
|
|
const userExists = await prisma.user.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
email: email.toLowerCase(),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (userExists) {
|
|
|
|
|
throw new Error('User already exists');
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 15:22:54 +11:00
|
|
|
let user = await prisma.user.create({
|
2023-06-09 18:21:18 +10:00
|
|
|
data: {
|
|
|
|
|
name,
|
|
|
|
|
email: email.toLowerCase(),
|
|
|
|
|
password: hashedPassword,
|
2023-09-01 19:46:44 +10:00
|
|
|
signature,
|
2023-06-09 18:21:18 +10:00
|
|
|
identityProvider: IdentityProvider.DOCUMENSO,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-12-14 15:22:54 +11:00
|
|
|
|
|
|
|
|
if (isBillingEnabled) {
|
|
|
|
|
try {
|
|
|
|
|
const stripeSession = await getStripeCustomerByUser(user);
|
|
|
|
|
user = stripeSession.user;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return user;
|
2023-06-09 18:21:18 +10:00
|
|
|
};
|