♻️ 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
+15
View File
@@ -0,0 +1,15 @@
import jwt from "jsonwebtoken";
const generateToken = (payload, secret, expiresIn) => {
const token = jwt.sign(payload, secret, {
algorithm: "HS256",
expiresIn: expiresIn,
});
return token;
};
const verifyToken = (token, secret) => {
return jwt.verify(token, secret);
};
export const jwtHelpers = {
generateToken,
verifyToken,
};
+13
View File
@@ -0,0 +1,13 @@
export class AppError extends Error {
statusCode;
constructor(message, statusCode, stack = '') {
super(message);
this.statusCode = statusCode;
if (stack) {
this.stack = stack;
}
else {
Error.captureStackTrace(this, this.constructor);
}
}
}
+11
View File
@@ -0,0 +1,11 @@
const catchAsync = (fn) => {
return async (req, res, next) => {
try {
await fn(req, res, next);
}
catch (error) {
next(error);
}
};
};
export default catchAsync;
+23
View File
@@ -0,0 +1,23 @@
import { v2 as cloudinary } from 'cloudinary';
import fs from 'fs';
import { configs } from '../configs/index.js';
// Configuration
cloudinary.config({
cloud_name: configs.cloudinary.cloud_name,
api_key: configs.cloudinary.cloud_api_key,
api_secret: configs.cloudinary.cloud_api_secret,
});
const uploadCloud = async (file) => {
return new Promise((resolve, reject) => {
cloudinary.uploader.upload(file.path, (error, result) => {
fs.unlinkSync(file.path);
if (error) {
reject(error);
}
else {
resolve(result);
}
});
});
};
export default uploadCloud;
+113
View File
@@ -0,0 +1,113 @@
import nodemailer from 'nodemailer';
import { configs } from '../configs/index.js';
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: configs.email.app_email,
pass: configs.email.app_password,
},
});
// ✅ Email Sender Function
const sendMail = async (payload) => {
const info = await transporter.sendMail({
from: 'info@digitalcreditai.com',
to: payload.to,
subject: payload.subject,
text: payload.textBody,
html: `
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Welcome Email</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Fallback styles for unsupported clients (some email clients ignore <style> tags) */
@media only screen and (max-width: 600px) {
.container {
padding: 20px !important;
}
.btn {
padding: 12px 18px !important;
font-size: 16px !important;
}
}
</style>
</head>
<body style="margin: 0; padding: 0; font-family: Arial, sans-serif">
<div
style="
max-width: 600px;
margin: 40px auto;
background-color: #f4f4f4;
padding: 40px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
"
class="container"
>
<div style="font-size: 16px; color: #555555; line-height: 1.6">
<p style="margin-bottom: 30px">
Hi <strong>${payload?.name || ""}</strong>,
</p>
${payload?.htmlBody}
<div style="margin-top: 60px; text-align: center">
<img
style="width: 50px; height: 50px; border-radius: 50%"
src="https://i.ibb.co.com/RkFJjPWg/quick-launch-1.png"
alt="Quick Launch"
/>
<p style="font-size: 12px">The Support Team</p>
<h3>Quick Launch</h3>
</div>
</div>
<p
style="
font-size: 14px;
color: #999999;
margin-top: 20px;
margin-bottom: 10px;
text-align: center;
"
>
This is an automated message — please do not reply to this email.
<br />
If you need assistance, feel free to contact our support team.
<br /><br />
Thank you for choosing us!
</p>
<hr />
<div
style="
text-align: center;
font-size: 12px;
color: #999999;
margin-top: 20px;
"
>
&copy; 2026 to {{year}} Quick Launch. All rights reserved.
</div>
</div>
</body>
</html>
`,
});
return info;
};
export default sendMail;
+9
View File
@@ -0,0 +1,9 @@
const manageResponse = (res, payload) => {
res.status(payload.statusCode).json({
success: payload.success,
message: payload.message,
data: payload.data || undefined || null,
meta: payload.meta || undefined || null
});
};
export default manageResponse;
+23
View File
@@ -0,0 +1,23 @@
export const otpGenerator = () => {
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lower = "abcdefghijklmnopqrstuvwxyz";
const numbers = "0123456789";
const symbols = "@#$%&*!?";
const allChars = upper + lower + numbers + symbols;
let otp = "";
// Ensure at least one character from each set
otp += upper[Math.floor(Math.random() * upper.length)];
otp += lower[Math.floor(Math.random() * lower.length)];
otp += numbers[Math.floor(Math.random() * numbers.length)];
otp += symbols[Math.floor(Math.random() * symbols.length)];
// Fill the rest randomly
for (let i = otp.length; i < 6; i++) {
otp += allChars[Math.floor(Math.random() * allChars.length)];
}
// Shuffle to remove predictable order
otp = otp
.split("")
.sort(() => Math.random() - 0.5)
.join("");
return otp;
};
+15
View File
@@ -0,0 +1,15 @@
const paginationHelper = (options) => {
const page = Number(options?.page) || 1;
const limit = Number(options?.limit) || 10;
const skip = (page - 1) * limit;
const sortBy = options?.sortBy || "createdAt";
const sortOrder = options?.sortOrder || "desc";
return {
page,
limit,
skip,
sortBy,
sortOrder
};
};
export default paginationHelper;