56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
"use server";
|
|
import { prisma, Prisma } from "@repo/db";
|
|
import bcrypt from "bcryptjs";
|
|
import OLD_USER from "../../api/auth/[...nextauth]/var.User.json";
|
|
import { OldUser } from "../../../types/oldUser";
|
|
|
|
export const register = async ({ password, ...user }: Omit<Prisma.UserCreateInput, "publicId">) => {
|
|
const hashedPassword = await bcrypt.hash(password, 12);
|
|
const lastUserPublicId = await prisma.user.findFirst({
|
|
select: {
|
|
publicId: true,
|
|
},
|
|
orderBy: {
|
|
publicId: "desc",
|
|
},
|
|
});
|
|
let varPublicId = "VAR0000";
|
|
if (lastUserPublicId) {
|
|
const lastUserInt = parseInt(lastUserPublicId.publicId.replace("VAR", ""));
|
|
varPublicId = `VAR${(lastUserInt + 1).toString().padStart(4, "0")}`;
|
|
}
|
|
|
|
const existingUser = await prisma.user.findFirst({
|
|
where: {
|
|
email: user.email,
|
|
},
|
|
select: {
|
|
id: true,
|
|
},
|
|
});
|
|
|
|
const existingOldUser = (OLD_USER as OldUser[]).find((u) => u.email.toLocaleLowerCase() === user.email.toLocaleLowerCase());
|
|
|
|
if (existingUser) {
|
|
return {
|
|
error: "Ein Nutzer mit dieser E-Mail-Adresse existiert bereits.",
|
|
};
|
|
}
|
|
|
|
if (existingOldUser) {
|
|
return {
|
|
error:
|
|
"Diese Email existriert bereits in der alten Version von VAR. Bitte melde dich an oder ändere dein Passwort.",
|
|
};
|
|
}
|
|
|
|
const newUser = prisma.user.create({
|
|
data: {
|
|
...user,
|
|
publicId: varPublicId,
|
|
password: hashedPassword,
|
|
},
|
|
});
|
|
return newUser;
|
|
};
|