"feat: add EventItem, EventReview models with seed data and mock files, update User and Event schemas"
This commit is contained in:
122
03_source/cms_backend/src/app/services/userItem.service.ts
Normal file
122
03_source/cms_backend/src/app/services/userItem.service.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
// 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<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 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,
|
||||
isAdmin,
|
||||
listUsers,
|
||||
updateUser,
|
||||
deleteUser,
|
||||
changeToUser,
|
||||
createNewUser,
|
||||
changeToAdmin,
|
||||
getUserByEmail,
|
||||
type CreateUser,
|
||||
type UpdateUser,
|
||||
};
|
Reference in New Issue
Block a user