128 lines
3.3 KiB
TypeScript
128 lines
3.3 KiB
TypeScript
// 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 { User } 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<UserItem[]> {
|
|
// return prisma.userItem.findMany();
|
|
// }
|
|
|
|
// async function getUserItem(userId: string): Promise<UserItem | null> {
|
|
// return prisma.userItem.findFirst({ where: { id: userId } });
|
|
// }
|
|
|
|
// async function updateUser(userId: string, updateForm: UpdateUser): Promise<User> {
|
|
// 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<boolean> {
|
|
// 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 getUserById(id: string): Promise<User | null> {
|
|
return prisma.user.findFirst({ where: { id } });
|
|
}
|
|
|
|
// async function getUserByEmail(email: string): Promise<void> {
|
|
// // return prisma.userItem.findUnique({
|
|
// // where: { email },
|
|
// // include: {
|
|
// // Token: true,
|
|
// // },
|
|
// // });
|
|
// }
|
|
|
|
// async function createNewUser(createForm: CreateUser): Promise<void> {
|
|
// // 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<void> {
|
|
// // return prisma.userItem.delete({ where: { id: userId } });
|
|
// }
|
|
|
|
export {
|
|
// getUser,
|
|
getUserById,
|
|
// isAdmin,
|
|
// listUsers,
|
|
// updateUser,
|
|
// deleteUser,
|
|
// changeToUser,
|
|
// createNewUser,
|
|
// changeToAdmin,
|
|
// getUserByEmail,
|
|
// type CreateUser,
|
|
// type UpdateUser,
|
|
};
|