Skip to content

Commit a328115

Browse files
committed
refactoring the domains of the project into services and align with docker file
1 parent 08030a6 commit a328115

18 files changed

Lines changed: 100 additions & 111 deletions

eslint.config.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ export default defineConfig([
3434
},
3535
rules: {
3636
...tseslint.configs.recommended.rules,
37-
"@typescript-eslint/no-unused-vars": "error",
37+
"@typescript-eslint/no-unused-vars": [
38+
"error",
39+
{ "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" },
40+
],
3841
},
3942
},
4043
]);

src/controllers/auth.controller.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,11 @@ export const refreshToken = async (
115115
next: NextFunction,
116116
): Promise<void> => {
117117
try {
118-
const result = await authService.refreshToken(
119-
req.headers.authorization,
120-
);
118+
const result = await authService.refreshToken(req.headers.authorization);
121119
res.status(200).json(result);
122120
} catch (error) {
123121
if (error instanceof jwt.JsonWebTokenError) {
124-
res
125-
.status(401)
126-
.json({ message: "Invalid token, please login again" });
122+
res.status(401).json({ message: "Invalid token, please login again" });
127123
return;
128124
}
129125
if (error instanceof AppError) {

src/controllers/order.controller.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cloudinary.config({
1414
export const getOrderById = async (
1515
req: Request<{ orderId: string }>,
1616
res: Response,
17-
next: NextFunction,
17+
_next: NextFunction,
1818
): Promise<void> => {
1919
try {
2020
const orderData = await orderService.getOrderById(req.params.orderId);
@@ -33,7 +33,7 @@ export const getOrderById = async (
3333
export const getBuyerOrders = async (
3434
req: Request<{ buyerId: string }, unknown, unknown, { orderStatus?: string }>,
3535
res: Response,
36-
next: NextFunction,
36+
_next: NextFunction,
3737
): Promise<void> => {
3838
try {
3939
const buyerOrders = await orderService.getBuyerOrders(
@@ -56,7 +56,7 @@ export const getSellerOrders = async (
5656
{ orderStatus?: string; productName?: string }
5757
>,
5858
res: Response,
59-
next: NextFunction,
59+
_next: NextFunction,
6060
): Promise<void> => {
6161
try {
6262
const sellerOrders = await orderService.getSellerOrders(
@@ -73,13 +73,9 @@ export const getSellerOrders = async (
7373
};
7474

7575
export const createOrder = async (
76-
req: Request<
77-
{ productId: string },
78-
unknown,
79-
orderService.CreateOrderInput
80-
>,
76+
req: Request<{ productId: string }, unknown, orderService.CreateOrderInput>,
8177
res: Response,
82-
next: NextFunction,
78+
_next: NextFunction,
8379
): Promise<void> => {
8480
try {
8581
const buyerId =
@@ -103,13 +99,9 @@ export const createOrder = async (
10399
};
104100

105101
export const updateOrder = async (
106-
req: Request<
107-
{ orderId: string },
108-
unknown,
109-
orderService.UpdateOrderInput
110-
>,
102+
req: Request<{ orderId: string }, unknown, orderService.UpdateOrderInput>,
111103
res: Response,
112-
next: NextFunction,
104+
_next: NextFunction,
113105
): Promise<void> => {
114106
try {
115107
const result = await orderService.updateOrder(
@@ -131,7 +123,7 @@ export const updateOrder = async (
131123
export const getTransaction = async (
132124
req: Request<{ orderId: string }>,
133125
res: Response,
134-
next: NextFunction,
126+
_next: NextFunction,
135127
): Promise<void> => {
136128
try {
137129
const transaction = await orderService.getTransactionByOrderId(
@@ -153,11 +145,7 @@ export const getTransaction = async (
153145
};
154146

155147
export const updateDispatchDetails = async (
156-
req: Request<
157-
{ orderId: string },
158-
unknown,
159-
orderService.DispatchDetailsInput
160-
>,
148+
req: Request<{ orderId: string }, unknown, orderService.DispatchDetailsInput>,
161149
res: Response,
162150
next: NextFunction,
163151
): Promise<void> => {

src/controllers/product.controller.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cloudinary.config({
1414
export const allProducts = async (
1515
req: Request,
1616
res: Response,
17-
next: NextFunction,
17+
_next: NextFunction,
1818
): Promise<void> => {
1919
try {
2020
const products = await productService.findAllProducts();
@@ -34,7 +34,7 @@ export const allProducts = async (
3434
export const getProduct = async (
3535
req: Request<{ productId: string }>,
3636
res: Response,
37-
next: NextFunction,
37+
_next: NextFunction,
3838
): Promise<void> => {
3939
try {
4040
const foundProduct = await productService.findProductById(
@@ -55,7 +55,7 @@ export const getProduct = async (
5555
export const userProducts = async (
5656
req: Request<{ userId: string }>,
5757
res: Response,
58-
next: NextFunction,
58+
_next: NextFunction,
5959
): Promise<void> => {
6060
try {
6161
const userProducts = await productService.findProductsByUserId(
@@ -115,7 +115,7 @@ export const createProduct = async (
115115
export const updateProduct = async (
116116
req: Request<{ productId: string }>,
117117
res: Response,
118-
next: NextFunction,
118+
_next: NextFunction,
119119
): Promise<void> => {
120120
try {
121121
const body = { ...req.body };
@@ -144,7 +144,7 @@ export const updateProduct = async (
144144
export const removeProduct = async (
145145
req: Request<{ productId: string }>,
146146
res: Response,
147-
next: NextFunction,
147+
_next: NextFunction,
148148
): Promise<void> => {
149149
try {
150150
await productService.removeProduct(req.params.productId);

src/controllers/product.search.controller.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { AppError } from "../errors";
33
import * as productSearchService from "../services/product.search.service";
44

55
export const getAllProductSearch = async (
6-
req: Request<unknown, unknown, unknown, productSearchService.ProductSearchQuery>,
6+
req: Request<
7+
unknown,
8+
unknown,
9+
unknown,
10+
productSearchService.ProductSearchQuery
11+
>,
712
res: Response,
8-
next: NextFunction,
13+
_next: NextFunction,
914
): Promise<void> => {
1015
try {
1116
const query = req.query as productSearchService.ProductSearchQuery;

src/controllers/review.controller.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as reviewService from "../services/review.service";
66
export const orderReview = async (
77
req: Request<{ orderId: string }>,
88
res: Response,
9-
next: NextFunction,
9+
_next: NextFunction,
1010
): Promise<void> => {
1111
try {
1212
const orderReviewData = await reviewService.getReviewByOrderId(
@@ -33,7 +33,7 @@ export const orderReview = async (
3333
export const getReviewByProdId = async (
3434
req: Request<{ productId: string }, unknown, unknown, { rating?: string }>,
3535
res: Response,
36-
next: NextFunction,
36+
_next: NextFunction,
3737
): Promise<void> => {
3838
try {
3939
const reviews = await reviewService.getReviewsByProductId(
@@ -68,7 +68,7 @@ export const createReview = async (
6868
params: { productId: string; orderId: string };
6969
},
7070
res: Response,
71-
next: NextFunction,
71+
_next: NextFunction,
7272
): Promise<void> => {
7373
try {
7474
if (!req.userData || typeof req.userData === "string") {
@@ -95,7 +95,7 @@ export const createReview = async (
9595
export const updateReview = async (
9696
req: Request<{ reviewId: string }, unknown, reviewService.UpdateReviewInput>,
9797
res: Response,
98-
next: NextFunction,
98+
_next: NextFunction,
9999
): Promise<void> => {
100100
try {
101101
const result = await reviewService.updateReview(
@@ -117,16 +117,13 @@ export const updateReview = async (
117117
export const deleteOwnReview = async (
118118
req: AuthenticatedRequest & { params: { reviewId: string } },
119119
res: Response,
120-
next: NextFunction,
120+
_next: NextFunction,
121121
): Promise<void> => {
122122
try {
123123
if (!req.userData || typeof req.userData === "string") {
124124
throw new AppError("Invalid authentication token", 401);
125125
}
126-
await reviewService.deleteReview(
127-
req.params.reviewId,
128-
req.userData.UserId,
129-
);
126+
await reviewService.deleteReview(req.params.reviewId, req.userData.UserId);
130127
res.status(204).end();
131128
} catch (error) {
132129
if (error instanceof AppError) {

src/controllers/user.controller.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cloudinary.config({
1313
export const getAllUserData = async (
1414
req: Request,
1515
res: Response,
16-
next: NextFunction,
16+
_next: NextFunction,
1717
): Promise<void> => {
1818
try {
1919
const users = await userService.getAllUsers();
@@ -33,7 +33,7 @@ export const getAllUserData = async (
3333
export const getUserData = async (
3434
req: Request<{ userId: string }>,
3535
res: Response,
36-
next: NextFunction,
36+
_next: NextFunction,
3737
): Promise<void> => {
3838
try {
3939
const userData = await userService.getUserById(req.params.userId);
@@ -73,7 +73,7 @@ export const updateUser = async (
7373
export const deleteUser = async (
7474
req: Request<{ userId: string }>,
7575
res: Response,
76-
next: NextFunction,
76+
_next: NextFunction,
7777
): Promise<void> => {
7878
try {
7979
const result = await userService.deleteUser(req.params.userId);
@@ -90,13 +90,21 @@ export const deleteUser = async (
9090
};
9191

9292
export const updatePassword = async (
93-
req: Request<unknown, unknown, { password: string; userId: string; oldPassword?: string }>,
93+
req: Request<
94+
unknown,
95+
unknown,
96+
{ password: string; userId: string; oldPassword?: string }
97+
>,
9498
res: Response,
9599
next: NextFunction,
96100
): Promise<void> => {
97101
try {
98102
const { password, userId, oldPassword } = req.body;
99-
const result = await userService.updatePassword(userId, password, oldPassword);
103+
const result = await userService.updatePassword(
104+
userId,
105+
password,
106+
oldPassword,
107+
);
100108
res.status(200).json(result);
101109
} catch (error) {
102110
if (error instanceof AppError) {

src/env.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ if (
1919
CLOUDINARY_CLOUD_NAME &&
2020
CLOUDINARY_API_KEY &&
2121
CLOUDINARY_API_SECRET &&
22-
(!CLOUDINARY_URL || !String(CLOUDINARY_URL).toLowerCase().startsWith("cloudinary://"))
22+
(!CLOUDINARY_URL ||
23+
!String(CLOUDINARY_URL).toLowerCase().startsWith("cloudinary://"))
2324
) {
2425
process.env.CLOUDINARY_URL = `cloudinary://${CLOUDINARY_API_KEY}:${CLOUDINARY_API_SECRET}@${CLOUDINARY_CLOUD_NAME}`;
2526
}

src/errors/errorHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const errorHandler = (
1515
err: Error | AppError,
1616
_req: Request,
1717
res: Response,
18-
_next: NextFunction, // eslint-disable-line @typescript-eslint/no-unused-vars
18+
_next: NextFunction,
1919
): void => {
2020
console.log("Error handler called:", err);
2121

src/models/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ import runMigrations from "../utils/runMigrations";
99
const rawUrl = process.env.DATABASE_URL?.trim() || "";
1010
const connectionString =
1111
rawUrl &&
12-
rawUrl !== "undefined" &&
13-
(rawUrl.startsWith("postgres://") || rawUrl.startsWith("postgresql://"))
12+
rawUrl !== "undefined" &&
13+
(rawUrl.startsWith("postgres://") || rawUrl.startsWith("postgresql://"))
1414
? rawUrl
1515
: null;
1616
const useConnectionString = connectionString !== null;
1717

1818
if (!useConnectionString) {
1919
throw new Error(
2020
"Database not configured: set DATABASE_URL. " +
21-
"Locally: add it to .env. On Render/Docker: set DATABASE_URL in the service Environment (e.g. postgresql://user:pass@host:5432/dbname)."
21+
"Locally: add it to .env. On Render/Docker: set DATABASE_URL in the service Environment (e.g. postgresql://user:pass@host:5432/dbname).",
2222
);
2323
}
2424
if (rawUrl && rawUrl !== "undefined" && !useConnectionString) {
2525
throw new Error(
26-
`DATABASE_URL is set but invalid: must start with postgres:// or postgresql://. Got: ${rawUrl.slice(0, 30)}${rawUrl.length > 30 ? "..." : ""}`
26+
`DATABASE_URL is set but invalid: must start with postgres:// or postgresql://. Got: ${rawUrl.slice(0, 30)}${rawUrl.length > 30 ? "..." : ""}`,
2727
);
2828
}
2929

@@ -55,11 +55,11 @@ const sequelizeOptions = {
5555
dialectOptions:
5656
process.env.NOD_ENV === "production"
5757
? {
58-
ssl: {
59-
require: true,
60-
rejectUnauthorized: false, // Use this we use a service that uses a self-signed certificate
61-
},
62-
}
58+
ssl: {
59+
require: true,
60+
rejectUnauthorized: false, // Use this we use a service that uses a self-signed certificate
61+
},
62+
}
6363
: {},
6464
logging: console.log, // Set to false to disable SQL query logging
6565
models: models,

0 commit comments

Comments
 (0)