update
This commit is contained in:
@@ -76,7 +76,7 @@ class DepartmentControllerClass extends Controller {
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
data: {},
|
||||
message: "",
|
||||
message: "ويرايش شد",
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
@@ -86,6 +86,7 @@ class DepartmentControllerClass extends Controller {
|
||||
try {
|
||||
const id = req?.params?.id;
|
||||
|
||||
console.log(id)
|
||||
await this.#service.delete(id);
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
|
||||
@@ -7,7 +7,7 @@ departmentRouter.get("/all", DepartmentController.getAll);
|
||||
departmentRouter.get("/list", DepartmentController.getAllList);
|
||||
departmentRouter.get("/get/:id", DepartmentController.getById);
|
||||
departmentRouter.post("/create", DepartmentController.create);
|
||||
departmentRouter.delete("/remove/:id", DepartmentController.delete);
|
||||
departmentRouter.delete("/delete/:id", DepartmentController.delete);
|
||||
departmentRouter.put("/update/:id", DepartmentController.update);
|
||||
|
||||
export default departmentRouter;
|
||||
|
||||
@@ -71,6 +71,7 @@ class DepartmentServiceClass extends Controller {
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
throw new createHttpError.InternalServerError(
|
||||
GlobalMessages.errors.server,
|
||||
);
|
||||
@@ -129,14 +130,28 @@ class DepartmentServiceClass extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
await department.update({
|
||||
slug,
|
||||
displayName,
|
||||
});
|
||||
const updateData: Partial<any> = {};
|
||||
|
||||
if (slug !== undefined) updateData.slug = slug;
|
||||
if (displayName !== undefined) updateData.displayName = displayName;
|
||||
|
||||
await department.update(updateData);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw new createHttpError.Conflict(DepartmentMessages.error.update);
|
||||
} catch (error: any) {
|
||||
if (error instanceof createHttpError.HttpError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (error instanceof UniqueConstraintError) {
|
||||
throw new createHttpError.Conflict(
|
||||
DepartmentMessages.error.create.unique,
|
||||
);
|
||||
}
|
||||
|
||||
throw new createHttpError.InternalServerError(
|
||||
DepartmentMessages.error.update,
|
||||
);
|
||||
}
|
||||
}
|
||||
async delete(id: string) {
|
||||
|
||||
@@ -9,9 +9,21 @@ class UserControllerClass extends Controller {
|
||||
super();
|
||||
this.#service = UserService;
|
||||
}
|
||||
async create(req: any, res: ServerResponse, next: NextFunction) {
|
||||
try {
|
||||
await this.#service.create(req.body || {});
|
||||
res.status(201).json({
|
||||
status: 201,
|
||||
data: {},
|
||||
message: "ساخته شد",
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
async getAll(req: any, res: ServerResponse, next: NextFunction) {
|
||||
try {
|
||||
const data = await this.#service.getAll();
|
||||
const data = await this.#service.getAll(req);
|
||||
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
@@ -23,6 +35,62 @@ class UserControllerClass extends Controller {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
async getList(req: any, res: ServerResponse, next: NextFunction) {
|
||||
try {
|
||||
const data = await this.#service.getList();
|
||||
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
data,
|
||||
message: "Ok",
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
async update(req: any, res: ServerResponse, next: NextFunction) {
|
||||
try {
|
||||
const id = req?.params?.id;
|
||||
await this.#service.update(id, req.body);
|
||||
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
data: {},
|
||||
message: "ويرايش شد",
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
async delete(req: any, res: ServerResponse, next: NextFunction) {
|
||||
try {
|
||||
const id = req?.params?.id;
|
||||
await this.#service.delete(id);
|
||||
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
data: {},
|
||||
message: "حذف شد",
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
async getById(req: any, res: ServerResponse, next: NextFunction) {
|
||||
try {
|
||||
const id = req?.params?.id;
|
||||
const data = await this.#service.getById(id);
|
||||
res.status(201).json({
|
||||
status: 201,
|
||||
data,
|
||||
message: "ساخته شد",
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const UserController = new UserControllerClass();
|
||||
|
||||
@@ -4,5 +4,10 @@ import UserController from "../controller/user.controller";
|
||||
const userRouter = router.Router();
|
||||
|
||||
userRouter.get("/all", UserController.getAll);
|
||||
userRouter.get("/list", UserController.getList);
|
||||
userRouter.get("/get/:id", UserController.getById);
|
||||
userRouter.post("/create", UserController.create);
|
||||
userRouter.put("/update/:id", UserController.update);
|
||||
userRouter.delete("/delete/:id", UserController.delete);
|
||||
|
||||
export default userRouter;
|
||||
|
||||
@@ -1,11 +1,167 @@
|
||||
import createHttpError from "http-errors";
|
||||
import { GlobalMessages } from "../../../core/messages";
|
||||
import User from "../../../models/User";
|
||||
import { Op } from "sequelize";
|
||||
|
||||
interface UserDataBody {
|
||||
fullname: string;
|
||||
nationalCode: string;
|
||||
mobile: string;
|
||||
roleId: string;
|
||||
}
|
||||
|
||||
class UserServiceClass {
|
||||
async getAll() {
|
||||
async getById(id: string) {
|
||||
try {
|
||||
const data = await User.findAll({ raw: true });
|
||||
const user = await User.findByPk(id, {
|
||||
include: ["role", "assignedTickets"],
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new createHttpError.NotFound("كاربر يافت نشد");
|
||||
}
|
||||
|
||||
return user;
|
||||
} catch (error) {
|
||||
throw new createHttpError.InternalServerError(
|
||||
GlobalMessages.errors.server,
|
||||
);
|
||||
}
|
||||
}
|
||||
async create(data: UserDataBody) {
|
||||
try {
|
||||
const { fullname, nationalCode, mobile, roleId } = data;
|
||||
|
||||
await User.create({
|
||||
fullname,
|
||||
nationalCode,
|
||||
mobile,
|
||||
roleId,
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw new createHttpError.InternalServerError(
|
||||
GlobalMessages.errors.server,
|
||||
);
|
||||
}
|
||||
}
|
||||
async update(id: string, data: UserDataBody) {
|
||||
try {
|
||||
const user = await User.findByPk(id);
|
||||
|
||||
if (!user) {
|
||||
throw new createHttpError.NotFound("كاربر يافت نشد");
|
||||
}
|
||||
|
||||
await user.update(data);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw new createHttpError.InternalServerError(
|
||||
GlobalMessages.errors.server,
|
||||
);
|
||||
}
|
||||
}
|
||||
async delete(id: string) {
|
||||
try {
|
||||
const user = await User.findByPk(id);
|
||||
|
||||
if (!user) {
|
||||
throw new createHttpError.NotFound("كاربر يافت نشد");
|
||||
}
|
||||
|
||||
await user.destroy();
|
||||
} catch (error) {
|
||||
throw new createHttpError.InternalServerError(
|
||||
GlobalMessages.errors.server,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getAll(req:any) {
|
||||
try {
|
||||
const {
|
||||
page = 1,
|
||||
limit = 10,
|
||||
fullname,
|
||||
mobile,
|
||||
nationalCode,
|
||||
roleId,
|
||||
startDate,
|
||||
endDate,
|
||||
} = req.query;
|
||||
|
||||
const pageNumber = parseInt(page);
|
||||
const limitNumber = parseInt(limit);
|
||||
|
||||
const offset = (pageNumber - 1) * limitNumber;
|
||||
|
||||
const whereClause: any = {};
|
||||
|
||||
if (fullname) {
|
||||
whereClause.fullname = {
|
||||
[Op.like]: `%${fullname}%`,
|
||||
};
|
||||
}
|
||||
|
||||
if (mobile) {
|
||||
whereClause.mobile = {
|
||||
[Op.like]: `%${mobile}%`,
|
||||
};
|
||||
}
|
||||
|
||||
if (nationalCode) {
|
||||
whereClause.nationalCode = {
|
||||
[Op.like]: `%${nationalCode}%`,
|
||||
};
|
||||
}
|
||||
|
||||
if (roleId) {
|
||||
whereClause.roleId = roleId;
|
||||
}
|
||||
|
||||
if (startDate || endDate) {
|
||||
whereClause.createdAt = {};
|
||||
|
||||
if (startDate) {
|
||||
whereClause.createdAt[Op.gte] = new Date(startDate);
|
||||
}
|
||||
|
||||
if (endDate) {
|
||||
const end = new Date(endDate);
|
||||
end.setHours(23, 59, 59, 999);
|
||||
whereClause.createdAt[Op.lte] = end;
|
||||
}
|
||||
}
|
||||
|
||||
const { count, rows } = await User.findAndCountAll({
|
||||
where: whereClause,
|
||||
order: [["createdAt", "DESC"]],
|
||||
limit: limitNumber,
|
||||
offset: offset,
|
||||
include: [
|
||||
{
|
||||
association: "role",
|
||||
},
|
||||
],
|
||||
raw: true,
|
||||
nest: true,
|
||||
});
|
||||
|
||||
return {
|
||||
data: rows,
|
||||
totalItems: count,
|
||||
totalPages: Math.ceil(count / limitNumber),
|
||||
currentPage: pageNumber,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new createHttpError.InternalServerError(
|
||||
GlobalMessages.errors.server,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getList() {
|
||||
try {
|
||||
const data = await User.findAll({ raw: true, include: ["role"] });
|
||||
return data;
|
||||
} catch (error) {
|
||||
throw new createHttpError.InternalServerError(
|
||||
|
||||
Reference in New Issue
Block a user