157 lines
4.1 KiB
TypeScript
157 lines
4.1 KiB
TypeScript
import { User, Role, Prisma, Event } from '@prisma/client';
|
|
import httpStatus from 'http-status';
|
|
import prisma from '../client';
|
|
import ApiError from '../utils/ApiError';
|
|
import { encryptPassword } from '../utils/encryption';
|
|
|
|
/**
|
|
* Create a user
|
|
* @param {Object} userBody
|
|
* @returns {Promise<User>}
|
|
*/
|
|
// const createUser = async (
|
|
// email: string,
|
|
// password: string,
|
|
// name?: string,
|
|
// role: Role = Role.USER
|
|
// ): Promise<User> => {
|
|
// if (await getEventByEmail(email)) {
|
|
// throw new ApiError(httpStatus.BAD_REQUEST, 'Email already taken');
|
|
// }
|
|
// return prisma.user.create({
|
|
// data: {
|
|
// email,
|
|
// name,
|
|
// password: await encryptPassword(password),
|
|
// role
|
|
// }
|
|
// });
|
|
// };
|
|
|
|
/**
|
|
* Query for users
|
|
* @param {Object} filter - Prisma filter
|
|
* @param {Object} options - Query options
|
|
* @param {string} [options.sortBy] - Sort option in the format: sortField:(desc|asc)
|
|
* @param {number} [options.limit] - Maximum number of results per page (default = 10)
|
|
* @param {number} [options.page] - Current page (default = 1)
|
|
* @returns {Promise<QueryResult>}
|
|
*/
|
|
const queryEvents = async <Key extends keyof Event>(
|
|
filter: object,
|
|
options: {
|
|
limit?: number;
|
|
page?: number;
|
|
sortBy?: string;
|
|
sortType?: 'asc' | 'desc';
|
|
},
|
|
keys: Key[] = [
|
|
'id',
|
|
'email',
|
|
'name',
|
|
'password',
|
|
'role',
|
|
'isEmailVerified',
|
|
'createdAt',
|
|
'updatedAt'
|
|
] as Key[]
|
|
): Promise<Pick<Event, Key>[]> => {
|
|
// const page = options.page ?? 1;
|
|
// const limit = options.limit ?? 10;
|
|
// const sortBy = options.sortBy;
|
|
// const sortType = options.sortType ?? 'desc';
|
|
const events = await prisma.event.findMany();
|
|
return events as Pick<Event, Key>[];
|
|
};
|
|
|
|
/**
|
|
* Get event by id
|
|
* @param {ObjectId} id
|
|
* @param {Array<Key>} keys
|
|
* @returns {Promise<Pick<Event, Key> | null>}
|
|
*/
|
|
const getEventById = async <Key extends keyof Event>(
|
|
id: number,
|
|
keys: Key[] = ['id'] as Key[]
|
|
): Promise<Pick<Event, Key> | null> => {
|
|
return prisma.event.findUnique({
|
|
where: { id }
|
|
// select: keys.reduce((obj, k) => ({ ...obj, [k]: true }), {})
|
|
}) as Promise<Pick<Event, Key> | null>;
|
|
};
|
|
|
|
/**
|
|
* Get event by email
|
|
* @param {string} email
|
|
* @param {Array<Key>} keys
|
|
* @returns {Promise<Pick<User, Key> | null>}
|
|
*/
|
|
// const getEventByEmail = async <Key extends keyof User>(
|
|
// email: string,
|
|
// keys: Key[] = [
|
|
// 'id',
|
|
// 'email',
|
|
// 'name',
|
|
// 'password',
|
|
// 'role',
|
|
// 'isEmailVerified',
|
|
// 'createdAt',
|
|
// 'updatedAt'
|
|
// ] as Key[]
|
|
// ): Promise<Pick<User, Key> | null> => {
|
|
// return prisma.user.findUnique({
|
|
// where: { email },
|
|
// select: keys.reduce((obj, k) => ({ ...obj, [k]: true }), {})
|
|
// }) as Promise<Pick<User, Key> | null>;
|
|
// };
|
|
|
|
/**
|
|
* Update user by id
|
|
* @param {ObjectId} userId
|
|
* @param {Object} updateBody
|
|
* @returns {Promise<User>}
|
|
*/
|
|
// const updateUserById = async <Key extends keyof User>(
|
|
// userId: number,
|
|
// updateBody: Prisma.UserUpdateInput,
|
|
// keys: Key[] = ['id', 'email', 'name', 'role'] as Key[]
|
|
// ): Promise<Pick<User, Key> | null> => {
|
|
// const user = await getEventById(userId, ['id', 'email', 'name']);
|
|
// if (!user) {
|
|
// throw new ApiError(httpStatus.NOT_FOUND, 'User not found');
|
|
// }
|
|
// if (updateBody.email && (await getEventByEmail(updateBody.email as string))) {
|
|
// throw new ApiError(httpStatus.BAD_REQUEST, 'Email already taken');
|
|
// }
|
|
// const updatedUser = await prisma.user.update({
|
|
// where: { id: user.id },
|
|
// data: updateBody,
|
|
// select: keys.reduce((obj, k) => ({ ...obj, [k]: true }), {})
|
|
// });
|
|
// return updatedUser as Pick<User, Key> | null;
|
|
// };
|
|
|
|
/**
|
|
* Delete user by id
|
|
* @param {ObjectId} userId
|
|
* @returns {Promise<User>}
|
|
*/
|
|
// const deleteUserById = async (userId: number): Promise<User> => {
|
|
// const user = await getEventById(userId);
|
|
// if (!user) {
|
|
// throw new ApiError(httpStatus.NOT_FOUND, 'User not found');
|
|
// }
|
|
// await prisma.user.delete({ where: { id: user.id } });
|
|
// return user;
|
|
// };
|
|
|
|
export default {
|
|
// createUser,
|
|
queryEvents,
|
|
getEventById
|
|
// getEventById,
|
|
// getEventByEmail,
|
|
// updateUserById,
|
|
// deleteUserById
|
|
};
|