typescript-express-starter
Version:
Quick and Easy TypeScript Express Starter
55 lines (42 loc) • 1.96 kB
text/typescript
import { hash } from 'bcrypt';
import { Service } from 'typedi';
import { HttpException } from '@exceptions/httpException';
import { User } from '@interfaces/users.interface';
import { UserModel } from '@models/users.model';
()
export class UserService {
public async findAllUser(): Promise<User[]> {
const users: User[] = await UserModel.find();
return users;
}
public async findUserById(userId: string): Promise<User> {
const findUser: User = await UserModel.findOne({ _id: userId });
if (!findUser) throw new HttpException(409, "User doesn't exist");
return findUser;
}
public async createUser(userData: User): Promise<User> {
const findUser: User = await UserModel.findOne({ email: userData.email });
if (findUser) throw new HttpException(409, `This email ${userData.email} already exists`);
const hashedPassword = await hash(userData.password, 10);
const createUserData: User = await UserModel.create({ ...userData, password: hashedPassword });
return createUserData;
}
public async updateUser(userId: string, userData: User): Promise<User> {
if (userData.email) {
const findUser: User = await UserModel.findOne({ email: userData.email });
if (findUser && findUser._id != userId) throw new HttpException(409, `This email ${userData.email} already exists`);
}
if (userData.password) {
const hashedPassword = await hash(userData.password, 10);
userData = { ...userData, password: hashedPassword };
}
const updateUserById: User = await UserModel.findByIdAndUpdate(userId, { userData });
if (!updateUserById) throw new HttpException(409, "User doesn't exist");
return updateUserById;
}
public async deleteUser(userId: string): Promise<User> {
const deleteUserById: User = await UserModel.findByIdAndDelete(userId);
if (!deleteUserById) throw new HttpException(409, "User doesn't exist");
return deleteUserById;
}
}