Files
rudi 6be5cfb974 Added status to return
Status Rückmeldungen den 'returns' hinzugefügt.
Schanbro/PsalmTafel#30
2026-07-31 11:47:03 +02:00

209 lines
6.3 KiB
TypeScript

import { prisma } from "@/lib/db/prisma";
import argon2 from "argon2";
/**
* GetUsers
* Returns all users
*
* @returns {"success": boolean, "status":number, "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,
},
orderBy: [
{ id: "asc" },
],
});
return { success: true, status: 200, users };
} catch(error) {
console.error("Error in getUsers", error);
return { success: false, status: 500, error: "Error while loading Users" };
}
}
/**
* GetUserById
* Returns a user by his ID
* @param id - ID indicatin the user
* @returns {"success": boolean, "status":number, "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, status: 404, error: "User not found" };
}
return { success: true, status: 200, user };
} catch(error) {
console.error("Error in getUserById", error);
return { success: false, status: 500, error: "Error while loading User" };
}
}
/**
* GetUserByUsername
* Returns a user by his username
* @param username - Username of the user
* @returns {"success": boolean, "status":number, "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, status: 404, error: "User not found" };
}
return { success: true, status: 200, user };
} catch(error) {
console.error("Error in getUserByUsername", error);
return { success: false, status: 500, 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, "status":number, "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, status: 500, error: "Could not create user" };
}
return { success: true, status: 201, user };
} catch(error) {
console.error("Error in createUser", error);
// P2002 = Unique Constraint Violation
if (error.code === "P2002") {
return { success: false, status: 409, error: "Username aready exists",};
}
return { success: false, status: 500, 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, "status":number, "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, status: 400, error: "No fields provided to update" };
}
const user = await prisma.user.update({
where: { id },
data: updateData,
select: {
id: true,
username: true,
role: true,
}
});
return { success: true, status: 200, user };
} catch (error) {
console.error("Error in updateUser", error);
// P2025 = Record to update not found
if (error.code === "P2025") {
return { success: false, status: 404, error: "User not found" };
}
// P2002 = Unique Constraint Violation (username)
if (error.code === "P2002") {
return { success: false, status: 409, error: "Username already exists" };
}
return { success: false, status: 500, error: "Error while updating User" };
}
}
/**
* DeleteUser
* Deletes an existing user and returns the deleted user
* @param id - ID indicatin the user
* @returns {"success": boolean, "status":number, "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, status: 200, user };
} catch (error) {
console.error("Error in deleteUser", error);
// P2025 = Record to update not found
if (error.code === "P2025") {
return { success: false, status: 404, error: "User not found" };
}
return { success: false, status: 500, error: "Error while deleting User" };
}
}