♻️ refactor(account, order, plan, profile, support, email): restructure application modules and enhance error handling

Updated Docker configuration, refactored middleware for improved error handling, and restructured account, order, plan, profile, and support modules, including their routes, services, and validations. Enhanced email processing queues and utilities for token generation, pagination, and response management to streamline the application architecture and enhance maintainability.
This commit is contained in:
abumahid
2026-04-21 03:12:39 +06:00
parent c881efea0f
commit 0f7af70b90
94 changed files with 2593 additions and 127 deletions
+23
View File
@@ -0,0 +1,23 @@
import { configs } from "../configs/index.js";
import { AppError } from "../utils/app_error.js";
import { jwtHelpers } from "../utils/JWT.js";
const auth = (...roles) => {
return async (req, res, next) => {
try {
const token = req.headers.authorization || req.cookies.access_token;
if (!token) {
throw new AppError("You are not authorize!!", 401);
}
const verifiedUser = jwtHelpers.verifyToken(token, configs.jwt.access_token);
if (!roles.length || !roles.includes(verifiedUser.role)) {
throw new AppError("You are not authorize!!", 401);
}
req.user = verifiedUser;
next();
}
catch (err) {
next(err);
}
};
};
export default auth;
+47
View File
@@ -0,0 +1,47 @@
import { ZodError } from "zod";
import { configs } from "../configs/index.js";
import handleZodError from "../errors/zodError.js";
import { AppError } from "../utils/app_error.js";
const globalErrorHandler = (err, req, res, next) => {
let statusCode = 500;
let message = "Something went wrong!";
let errorSources = [
{
path: "",
message: "Something went wrong",
},
];
if (err instanceof ZodError) {
const simplifiedError = handleZodError(err);
statusCode = simplifiedError?.statusCode;
message = simplifiedError?.message;
errorSources = simplifiedError?.errorSources;
}
else if (err instanceof AppError) {
statusCode = err?.statusCode;
message = err.message;
errorSources = [
{
path: "",
message: err?.message,
},
];
}
else if (err instanceof Error) {
message = err.message;
errorSources = [
{
path: "",
message: err?.message,
},
];
}
res.status(statusCode).json({
success: false,
message,
errorSources,
err,
stack: configs.env === "development" ? err?.stack : null,
});
};
export default globalErrorHandler;
+8
View File
@@ -0,0 +1,8 @@
const notFound = (req, res, next) => {
res.status(404).json({
message: 'Sorry Route is not found!! 😴😴😴',
success: false,
error: '',
});
};
export default notFound;
+12
View File
@@ -0,0 +1,12 @@
const RequestValidator = (schema) => {
return async (req, res, next) => {
try {
req.body = await schema.parseAsync(req.body);
next();
}
catch (err) {
next(err);
}
};
};
export default RequestValidator;
+12
View File
@@ -0,0 +1,12 @@
import multer from "multer";
import path from "path";
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(process.cwd(), "uploads"));
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
const uploader = multer({ storage: storage });
export default uploader;