feat: add user remove route

This commit is contained in:
canove
2020-09-14 19:24:38 -03:00
parent 7f33e33078
commit b3cf16bbd3
7 changed files with 155 additions and 115 deletions

View File

@@ -57,12 +57,14 @@ const CreateUserService = async ({
profile
});
return {
const serializedUser = {
id: user.id,
name: user.name,
email: user.email,
profile: user.profile
};
return serializedUser;
};
export default CreateUserService;

View File

@@ -0,0 +1,16 @@
import User from "../models/User";
import AppError from "../errors/AppError";
const DeleteUserService = async (id: string): Promise<void> => {
const user = await User.findOne({
where: { id }
});
if (!user) {
throw new AppError("No user found with this ID.", 404);
}
await user.destroy();
};
export default DeleteUserService;

View File

@@ -1,54 +1,51 @@
import { getRepository, Raw } from "typeorm";
import { Sequelize, Op } from "sequelize";
import User from "../models/User";
interface Request {
searchParam?: string;
pageNumber?: number;
searchParam?: string;
pageNumber?: number;
}
interface Response {
users: User[];
count: number;
hasMore: boolean;
users: User[];
count: number;
hasMore: boolean;
}
const ListUsersService = async ({
searchParam = "",
pageNumber = 1,
searchParam = "",
pageNumber = 1
}: Request): Promise<Response> => {
const usersRepository = getRepository(User);
const whereCondition = {
[Op.or]: [
{
name: Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("name")),
"LIKE",
`%${searchParam.toLowerCase()}%`
)
},
{ email: { [Op.like]: `%${searchParam.toLowerCase()}%` } }
]
};
const limit = 20;
const offset = limit * (pageNumber - 1);
const whereCondition = [
{
name: Raw(
alias => `LOWER(${alias}) Like '%${searchParam.toLowerCase()}%'`
),
},
{
email: Raw(
alias => `LOWER(${alias}) Like '%${searchParam.toLowerCase()}%'`
),
},
];
const take = 20;
const skip = take * (pageNumber - 1);
const { count, rows: users } = await User.findAndCountAll({
where: whereCondition,
attributes: ["name", "id", "email", "profile"],
limit,
offset,
order: [["createdAt", "DESC"]]
});
const [users, count] = await usersRepository.findAndCount({
where: whereCondition,
select: ["name", "id", "email", "profile"],
skip,
take,
order: { createdAt: "DESC" },
});
const hasMore = count > limit + users.length;
const hasMore = count > skip + users.length;
return {
users,
count,
hasMore,
};
return {
users,
count,
hasMore
};
};
export default ListUsersService;

View File

@@ -1,61 +1,68 @@
import { getRepository } from "typeorm";
import * as Yup from "yup";
import AppError from "../errors/AppError";
import User from "../models/User";
interface UserData {
email?: string;
password?: string;
name?: string;
profile?: string;
email?: string;
password?: string;
name?: string;
profile?: string;
}
interface Request {
userData: UserData;
userId: string;
userData: UserData;
userId: string;
}
interface Response {
id: number;
name: string;
email: string;
profile: string;
}
const UpdateUserService = async ({
userData,
userId,
}: Request): Promise<User | undefined> => {
const usersRepository = getRepository(User);
userData,
userId
}: Request): Promise<Response | undefined> => {
const schema = Yup.object().shape({
name: Yup.string().min(2),
email: Yup.string().email(),
password: Yup.string()
});
const schema = Yup.object().shape({
name: Yup.string().min(2),
email: Yup.string().email(),
password: Yup.string(),
});
const { email, password, name } = userData;
const { email, password, name } = userData;
try {
await schema.validate({ email, password, name });
} catch (err) {
throw new AppError(err.message);
}
try {
await schema.validate({ email, password, name });
} catch (err) {
throw new AppError(err.message);
}
const user = await User.findOne({
where: { id: userId },
attributes: ["name", "id", "email", "profile"]
});
const user = await usersRepository.findOne({
where: { id: userId },
select: ["name", "id", "email", "profile"],
});
if (!user) {
throw new AppError("No user found with this ID.", 404);
}
if (!user) {
throw new AppError("No user found with this ID.", 404);
}
await user.update({
email,
password,
name
});
const teste = await usersRepository.update(userId, {
email,
passwordHash: password,
name,
});
const serializedUser = {
id: user.id,
name: user.name,
email: user.email,
profile: user.profile
};
console.log(teste);
delete user.passwordHash;
return user;
return serializedUser;
};
export default UpdateUserService;