mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-17 19:37:02 +00:00
feat: add user remove route
This commit is contained in:
@@ -4,23 +4,23 @@ import CheckSettingsHelper from "../helpers/CheckSettingsHelper";
|
||||
import AppError from "../errors/AppError";
|
||||
|
||||
import CreateUserService from "../services/CreateUserService";
|
||||
// import UpdateUserService from "../services/UpdateUserService";
|
||||
// import ListUsersService from "../services/ListUsersService";
|
||||
// import FindUserService from "../services/FindUserService";
|
||||
import ListUsersService from "../services/ListUsersService";
|
||||
import UpdateUserService from "../services/UpdateUserService";
|
||||
import FindUserService from "../services/FindUserService";
|
||||
import DeleteUserService from "../services/DeleteUserService";
|
||||
|
||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
// if (req.user.profile !== "admin") {
|
||||
// throw new AppError("Only administrators can access this route.", 403); // should be handled better.
|
||||
// }
|
||||
// const { searchParam, pageNumber } = req.query as any;
|
||||
if (req.user.profile !== "admin") {
|
||||
throw new AppError("Only administrators can access this route.", 403); // should be handled better.
|
||||
}
|
||||
const { searchParam, pageNumber } = req.query as any;
|
||||
|
||||
// const { users, count, hasMore } = await ListUsersService({
|
||||
// searchParam,
|
||||
// pageNumber
|
||||
// });
|
||||
const { users, count, hasMore } = await ListUsersService({
|
||||
searchParam,
|
||||
pageNumber
|
||||
});
|
||||
|
||||
// return res.json({ users, count, hasMore });
|
||||
return res.json({ ok: "ok" });
|
||||
return res.json({ users, count, hasMore });
|
||||
};
|
||||
|
||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
@@ -45,26 +45,41 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
return res.status(200).json(user);
|
||||
};
|
||||
|
||||
// export const show = async (req: Request, res: Response): Promise<Response> => {
|
||||
// const { userId } = req.params;
|
||||
export const show = async (req: Request, res: Response): Promise<Response> => {
|
||||
const { userId } = req.params;
|
||||
|
||||
// const user = await FindUserService(userId);
|
||||
const user = await FindUserService(userId);
|
||||
|
||||
// return res.status(200).json(user);
|
||||
// };
|
||||
return res.status(200).json(user);
|
||||
};
|
||||
|
||||
// export const update = async (
|
||||
// req: Request,
|
||||
// res: Response
|
||||
// ): Promise<Response> => {
|
||||
// if (req.user.profile !== "admin") {
|
||||
// throw new AppError("Only administrators can edit users.", 403);
|
||||
// }
|
||||
export const update = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<Response> => {
|
||||
if (req.user.profile !== "admin") {
|
||||
throw new AppError("Only administrators can edit users.", 403);
|
||||
}
|
||||
|
||||
// const { userId } = req.params;
|
||||
// const userData = req.body;
|
||||
const { userId } = req.params;
|
||||
const userData = req.body;
|
||||
|
||||
// const user = await UpdateUserService({ userData, userId });
|
||||
const user = await UpdateUserService({ userData, userId });
|
||||
|
||||
// return res.status(200).json(user);
|
||||
// };
|
||||
return res.status(200).json(user);
|
||||
};
|
||||
|
||||
export const remove = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<Response> => {
|
||||
const { userId } = req.params;
|
||||
|
||||
if (req.user.profile !== "admin") {
|
||||
throw new AppError("Only administrators can delete users.", 403);
|
||||
}
|
||||
|
||||
await DeleteUserService(userId);
|
||||
|
||||
return res.status(200).json({ message: "User deleted" });
|
||||
};
|
||||
|
||||
@@ -6,12 +6,17 @@ import {
|
||||
Model,
|
||||
DataType,
|
||||
BeforeCreate,
|
||||
BeforeUpdate
|
||||
BeforeUpdate,
|
||||
PrimaryKey
|
||||
} from "sequelize-typescript";
|
||||
import { hash, compare } from "bcryptjs";
|
||||
|
||||
@Table
|
||||
class User extends Model<User> {
|
||||
@PrimaryKey
|
||||
@Column
|
||||
id: number;
|
||||
|
||||
@Column
|
||||
name: string;
|
||||
|
||||
|
||||
@@ -5,16 +5,14 @@ import * as UserController from "../controllers/UserController";
|
||||
|
||||
const userRoutes = Router();
|
||||
|
||||
userRoutes.get("/users", (req, res) =>
|
||||
res.json({ meessage: "lets do some prettier shit here" })
|
||||
);
|
||||
userRoutes.get("/users", isAuth, UserController.index);
|
||||
|
||||
userRoutes.post("/users", isAuth, UserController.store);
|
||||
|
||||
// userRoutes.put("/users/:userId", isAuth, UserController.update);
|
||||
userRoutes.put("/users/:userId", isAuth, UserController.update);
|
||||
|
||||
// userRoutes.get("/users/:userId", isAuth, UserController.show);
|
||||
userRoutes.get("/users/:userId", isAuth, UserController.show);
|
||||
|
||||
// userRoutes.delete("/users/:userId", isAuth, UserController.delete);
|
||||
userRoutes.delete("/users/:userId", isAuth, UserController.remove);
|
||||
|
||||
export default userRoutes;
|
||||
|
||||
@@ -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;
|
||||
|
||||
16
backend/src/services/DeleteUserService.ts
Normal file
16
backend/src/services/DeleteUserService.ts
Normal 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;
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user