mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 20:59:16 +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 AppError from "../errors/AppError";
|
||||||
|
|
||||||
import CreateUserService from "../services/CreateUserService";
|
import CreateUserService from "../services/CreateUserService";
|
||||||
// import UpdateUserService from "../services/UpdateUserService";
|
import ListUsersService from "../services/ListUsersService";
|
||||||
// import ListUsersService from "../services/ListUsersService";
|
import UpdateUserService from "../services/UpdateUserService";
|
||||||
// import FindUserService from "../services/FindUserService";
|
import FindUserService from "../services/FindUserService";
|
||||||
|
import DeleteUserService from "../services/DeleteUserService";
|
||||||
|
|
||||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||||
// if (req.user.profile !== "admin") {
|
if (req.user.profile !== "admin") {
|
||||||
// throw new AppError("Only administrators can access this route.", 403); // should be handled better.
|
throw new AppError("Only administrators can access this route.", 403); // should be handled better.
|
||||||
// }
|
}
|
||||||
// const { searchParam, pageNumber } = req.query as any;
|
const { searchParam, pageNumber } = req.query as any;
|
||||||
|
|
||||||
// const { users, count, hasMore } = await ListUsersService({
|
const { users, count, hasMore } = await ListUsersService({
|
||||||
// searchParam,
|
searchParam,
|
||||||
// pageNumber
|
pageNumber
|
||||||
// });
|
});
|
||||||
|
|
||||||
// return res.json({ users, count, hasMore });
|
return res.json({ users, count, hasMore });
|
||||||
return res.json({ ok: "ok" });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
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);
|
return res.status(200).json(user);
|
||||||
};
|
};
|
||||||
|
|
||||||
// export const show = async (req: Request, res: Response): Promise<Response> => {
|
export const show = async (req: Request, res: Response): Promise<Response> => {
|
||||||
// const { userId } = req.params;
|
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 (
|
export const update = async (
|
||||||
// req: Request,
|
req: Request,
|
||||||
// res: Response
|
res: Response
|
||||||
// ): Promise<Response> => {
|
): Promise<Response> => {
|
||||||
// if (req.user.profile !== "admin") {
|
if (req.user.profile !== "admin") {
|
||||||
// throw new AppError("Only administrators can edit users.", 403);
|
throw new AppError("Only administrators can edit users.", 403);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// const { userId } = req.params;
|
const { userId } = req.params;
|
||||||
// const userData = req.body;
|
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,
|
Model,
|
||||||
DataType,
|
DataType,
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
BeforeUpdate
|
BeforeUpdate,
|
||||||
|
PrimaryKey
|
||||||
} from "sequelize-typescript";
|
} from "sequelize-typescript";
|
||||||
import { hash, compare } from "bcryptjs";
|
import { hash, compare } from "bcryptjs";
|
||||||
|
|
||||||
@Table
|
@Table
|
||||||
class User extends Model<User> {
|
class User extends Model<User> {
|
||||||
|
@PrimaryKey
|
||||||
|
@Column
|
||||||
|
id: number;
|
||||||
|
|
||||||
@Column
|
@Column
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
|
|||||||
@@ -5,16 +5,14 @@ import * as UserController from "../controllers/UserController";
|
|||||||
|
|
||||||
const userRoutes = Router();
|
const userRoutes = Router();
|
||||||
|
|
||||||
userRoutes.get("/users", (req, res) =>
|
userRoutes.get("/users", isAuth, UserController.index);
|
||||||
res.json({ meessage: "lets do some prettier shit here" })
|
|
||||||
);
|
|
||||||
|
|
||||||
userRoutes.post("/users", isAuth, UserController.store);
|
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;
|
export default userRoutes;
|
||||||
|
|||||||
@@ -57,12 +57,14 @@ const CreateUserService = async ({
|
|||||||
profile
|
profile
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
const serializedUser = {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
name: user.name,
|
name: user.name,
|
||||||
email: user.email,
|
email: user.email,
|
||||||
profile: user.profile
|
profile: user.profile
|
||||||
};
|
};
|
||||||
|
|
||||||
|
return serializedUser;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default CreateUserService;
|
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,5 +1,4 @@
|
|||||||
import { getRepository, Raw } from "typeorm";
|
import { Sequelize, Op } from "sequelize";
|
||||||
|
|
||||||
import User from "../models/User";
|
import User from "../models/User";
|
||||||
|
|
||||||
interface Request {
|
interface Request {
|
||||||
@@ -15,39 +14,37 @@ interface Response {
|
|||||||
|
|
||||||
const ListUsersService = async ({
|
const ListUsersService = async ({
|
||||||
searchParam = "",
|
searchParam = "",
|
||||||
pageNumber = 1,
|
pageNumber = 1
|
||||||
}: Request): Promise<Response> => {
|
}: Request): Promise<Response> => {
|
||||||
const usersRepository = getRepository(User);
|
const whereCondition = {
|
||||||
|
[Op.or]: [
|
||||||
const whereCondition = [
|
|
||||||
{
|
{
|
||||||
name: Raw(
|
name: Sequelize.where(
|
||||||
alias => `LOWER(${alias}) Like '%${searchParam.toLowerCase()}%'`
|
Sequelize.fn("LOWER", Sequelize.col("name")),
|
||||||
),
|
"LIKE",
|
||||||
|
`%${searchParam.toLowerCase()}%`
|
||||||
|
)
|
||||||
},
|
},
|
||||||
{
|
{ email: { [Op.like]: `%${searchParam.toLowerCase()}%` } }
|
||||||
email: Raw(
|
]
|
||||||
alias => `LOWER(${alias}) Like '%${searchParam.toLowerCase()}%'`
|
};
|
||||||
),
|
const limit = 20;
|
||||||
},
|
const offset = limit * (pageNumber - 1);
|
||||||
];
|
|
||||||
const take = 20;
|
|
||||||
const skip = take * (pageNumber - 1);
|
|
||||||
|
|
||||||
const [users, count] = await usersRepository.findAndCount({
|
const { count, rows: users } = await User.findAndCountAll({
|
||||||
where: whereCondition,
|
where: whereCondition,
|
||||||
select: ["name", "id", "email", "profile"],
|
attributes: ["name", "id", "email", "profile"],
|
||||||
skip,
|
limit,
|
||||||
take,
|
offset,
|
||||||
order: { createdAt: "DESC" },
|
order: [["createdAt", "DESC"]]
|
||||||
});
|
});
|
||||||
|
|
||||||
const hasMore = count > skip + users.length;
|
const hasMore = count > limit + users.length;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
users,
|
users,
|
||||||
count,
|
count,
|
||||||
hasMore,
|
hasMore
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { getRepository } from "typeorm";
|
|
||||||
import * as Yup from "yup";
|
import * as Yup from "yup";
|
||||||
|
|
||||||
import AppError from "../errors/AppError";
|
import AppError from "../errors/AppError";
|
||||||
@@ -16,16 +15,21 @@ interface Request {
|
|||||||
userId: string;
|
userId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Response {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
profile: string;
|
||||||
|
}
|
||||||
|
|
||||||
const UpdateUserService = async ({
|
const UpdateUserService = async ({
|
||||||
userData,
|
userData,
|
||||||
userId,
|
userId
|
||||||
}: Request): Promise<User | undefined> => {
|
}: Request): Promise<Response | undefined> => {
|
||||||
const usersRepository = getRepository(User);
|
|
||||||
|
|
||||||
const schema = Yup.object().shape({
|
const schema = Yup.object().shape({
|
||||||
name: Yup.string().min(2),
|
name: Yup.string().min(2),
|
||||||
email: Yup.string().email(),
|
email: Yup.string().email(),
|
||||||
password: Yup.string(),
|
password: Yup.string()
|
||||||
});
|
});
|
||||||
|
|
||||||
const { email, password, name } = userData;
|
const { email, password, name } = userData;
|
||||||
@@ -36,26 +40,29 @@ const UpdateUserService = async ({
|
|||||||
throw new AppError(err.message);
|
throw new AppError(err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await usersRepository.findOne({
|
const user = await User.findOne({
|
||||||
where: { id: userId },
|
where: { id: userId },
|
||||||
select: ["name", "id", "email", "profile"],
|
attributes: ["name", "id", "email", "profile"]
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new AppError("No user found with this ID.", 404);
|
throw new AppError("No user found with this ID.", 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
const teste = await usersRepository.update(userId, {
|
await user.update({
|
||||||
email,
|
email,
|
||||||
passwordHash: password,
|
password,
|
||||||
name,
|
name
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(teste);
|
const serializedUser = {
|
||||||
|
id: user.id,
|
||||||
|
name: user.name,
|
||||||
|
email: user.email,
|
||||||
|
profile: user.profile
|
||||||
|
};
|
||||||
|
|
||||||
delete user.passwordHash;
|
return serializedUser;
|
||||||
|
|
||||||
return user;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default UpdateUserService;
|
export default UpdateUserService;
|
||||||
|
|||||||
Reference in New Issue
Block a user