import { prisma } from "@/lib/db/prisma"; import argon2 from "argon2"; /* * GetUsers * Returns all users * * @returns {"success": boolean, "users": [{"id":number, "username": string, "role": string},...]} */ export async function getUsers() { try{ const users = await prisma.user.findMany({ select: { id: true, username: true, role: true, } }); if (!users) { return { success: false, error: "User not found" }; } return { success: true, users }; } catch(error) { console.error("Error in getUsers", error); return { success: false, error: "Error while loading Users" }; } } /* * GetUserById * Returns a user by his ID * @param id - ID indicatin the user * @returns {"success": boolean, "user": {"id":number, "username": string, "role": string}} */ export async function getUserById(id: number) { try{ const user = await prisma.user.findUnique({ where: { id }, select: { id: true, username: true, role: true, } }); if (!user) { return { success: false, error: "User not found" }; } return { success: true, user }; } catch(error) { console.error("Error in getUserById", error); return { success: false, error: "Error while loading User" }; } } /* * GetUserByUsername * Returns a user by his username * @param username - Username of the user * @returns {"success": boolean, "user": {"id":number, "username": string, "role": string}} */ export async function getUserByUsername(username: string) { try{ const user = await prisma.user.findUnique({ where: { username }, select: { id: true, username: true, role: true, } }); if (!user) { return { success: false, error: "User not found" }; } return { success: true, user }; } catch(error) { console.error("Error in getUserByUsername", error); return { success: false, error: "Error while loading User" }; } } /* * CreateUser * Creates an user and returns this user * @param data - object with username, password and optional role * @returns {"success": boolean, "user": {"id":number, "username": string, "role": string}} */ export async function CreateUser(data: { username: string; password: string; role?: string }) { try{ const hash = await argon2.hash(data.password); const user = await prisma.user.create({ data: { username: data.username, password: hash, role: data.role ? data.role : undefined // Default 'user' from Database }, select: { id: true, username: true, role: true, } }); if (!user) { return { success: false, error: "Could not create user" }; } return { success: true, user }; } catch(error) { console.error("Error in updateUser", error); // P2002 = Unique Constraint Violation if (error.code === "P2002") { return { success: false, error: "Username aready exists",}; } return { success: false, error: "Error while creating User" }; } } /* * UpdateUser * Updates an existing user and returns the updated user * @param id - ID indicatin the user * @param data - object with optionally username, password and role * @returns {"success": boolean, "user": {"id": number, "username": string, "role": string}} */ export async function updateUser( id: number, data: { username?: string; password?: string; role?: string } ) { try { // Prepare update fields dynamically const updateData: { username?: string; password?: string; role?: string } = {}; if (data.username) { updateData.username = data.username; } if (data.password) { updateData.password = await argon2.hash(data.password); } if (data.role) { updateData.role = data.role; } // If no fields to update were provided if (Object.keys(updateData).length === 0) { return { success: false, error: "No fields provided to update" }; } const updatedUser = await prisma.user.update({ where: { id }, data: updateData, select: { id: true, username: true, role: true, } }); return { success: true, user: updatedUser }; } catch (error) { console.error("Error in updateUser", error); // P2025 = Record to update not found if (error.code === "P2025") { return { success: false, error: "User not found" }; } // P2002 = Unique Constraint Violation (username) if (error.code === "P2002") { return { success: false, error: "Username already exists" }; } return { success: false, error: "Error while updating User" }; } } /* * DeleteUser * Deletes an existing user and returns the deleted user * @param id - ID indicatin the user * @returns {"success": boolean, "user": {"id": number, "username": string, "role": string}} */ export async function deleteUser(id: number) { try { const user = await prisma.user.delete({ where: { id }, select: { id: true, username: true, role: true, } }); return { success: true, user }; } catch (error) { console.error("Error in deleteUser", error); // P2025 = Record to update not found if (error.code === "P2025") { return { success: false, error: "User not found" }; } return { success: false, error: "Error while deleting User" }; } }