Files
var-monorepo/apps/hub/app/(app)/admin/user/action.ts
PxlLoewe dabcad2525 dev
2025-06-23 19:33:00 -07:00

108 lines
2.2 KiB
TypeScript

"use server";
import { prisma, Prisma } from "@repo/db";
import bcrypt from "bcryptjs";
import { sendMailByTemplate } from "../../../../helper/mail";
export const editUser = async (id: string, data: Prisma.UserUpdateInput) => {
return await prisma.user.update({
where: {
id: id,
},
data,
});
};
export const resetPassword = async (id: string) => {
const array = new Uint8Array(8);
crypto.getRandomValues(array);
const password = Array.from(array, (byte) => ("0" + (byte % 36).toString(36)).slice(-1)).join("");
const hashedPassword = await bcrypt.hash(password, 12);
const user = await prisma.user.update({
where: {
id: id,
},
data: {
password: hashedPassword,
},
});
await sendMailByTemplate(user.email, "password-change", {
user: user,
password: password,
});
return { password };
};
export const deleteDispoHistory = async (id: number) => {
return await prisma.connectedDispatcher.delete({
where: {
id: id,
},
});
};
export const deletePilotHistory = async (id: number) => {
return await prisma.connectedAircraft.delete({
where: {
id: id,
},
});
};
export const CheckEmailCode = async (code: string) => {
const users = await prisma.user.findMany({
where: {
emailVerificationToken: code,
},
select: {
id: true,
emailVerificationToken: true,
emailVerificationExpiresAt: true,
},
});
const user = users[0];
if (!user || !user.emailVerificationExpiresAt) {
return { error: "Code ist ungültig" };
}
if (user.emailVerificationExpiresAt < new Date()) {
return { error: "Code ist nicht mehr gültig" };
}
await prisma.user.update({
where: {
id: user.id,
},
data: {
emailVerified: true,
emailVerificationToken: null,
emailVerificationExpiresAt: null,
},
});
return {
message: "Email bestätigt!",
};
};
export const sendVerificationLink = async (userId: string) => {
const code = Math.floor(10000 + Math.random() * 90000).toString();
const user = await prisma.user.update({
where: {
id: userId,
},
data: {
emailVerificationToken: code,
emailVerificationExpiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
},
});
await sendMailByTemplate(user.email, "email-verification", {
user: user,
code,
});
};