// src/app/services/user.service.ts // // PURPOSE: // - Handle User Record CRUD operations // // RULES: // - Follow Prisma best practices for database operations // - Validate input data before processing // import type { UserItem } from '@prisma/client'; import prisma from '../lib/prisma'; type CreateUser = { email: string; // name?: string; // password: string; // role?: Role; // isEmailVerified?: boolean; // admin?: boolean; }; type UpdateUser = { email?: string; // name?: string; // password?: string; // role?: Role; // isEmailVerified?: boolean; isAdmin?: boolean; }; async function listUsers(): Promise { return prisma.userItem.findMany(); } async function getUserItem(userId: string): Promise { return prisma.userItem.findFirst({ where: { id: userId } }); } async function updateUser(userId: string, updateForm: UpdateUser): Promise { return prisma.userItem.update({ where: { id: userId }, data: updateForm, }); } // check if userId is a admin // check if userId is a admin async function isAdmin(userId: string): Promise { const user = await getUserItem(userId); return user?.isAdmin === true; } async function changeToAdmin(userIdToPromote: string, userIdOfApplicant: string) { // check the applicant is admin or not const userApplicant = await getUserItem(userIdOfApplicant); let promoteResult = {}; if (userApplicant && userApplicant.isAdmin) { // applicant is an admin promoteResult = await updateUser(userIdToPromote, { isAdmin: true }); } else { promoteResult = { status: 'failed', message: 'applicant is not a admin' }; } return promoteResult; } async function changeToUser(userIdToPromote: string, userIdOfApplicant: string) { // check the applicant is admin or not const userApplicant = await getUserItem(userIdOfApplicant); let promoteResult = {}; if (userApplicant && userApplicant.isAdmin) { // applicant is an admin promoteResult = await updateUser(userIdToPromote, { isAdmin: false }); } else { promoteResult = { status: 'failed', message: 'applicant is not a admin' }; } return promoteResult; } async function getUserById1(id: string): Promise { return prisma.userItem.findFirst({ where: { id } }); } async function getUserByEmail(email: string): Promise { // return prisma.userItem.findUnique({ // where: { email }, // include: { // Token: true, // }, // }); } async function createNewUser(createForm: CreateUser): Promise { // return prisma.userItem.create({ // data: { // email: createForm.email, // name: createForm.name, // password: createForm.password, // role: createForm.role || 'USER', // isEmailVerified: createForm.isEmailVerified || false, // }, // }); } async function deleteUser(userId: number): Promise { // return prisma.userItem.delete({ where: { id: userId } }); } export { // getUser, isAdmin, listUsers, updateUser, deleteUser, getUserById, changeToUser, createNewUser, changeToAdmin, getUserByEmail, type CreateUser, type UpdateUser, };