Skip to content

Commit 130a23f

Browse files
committed
Resolve code smells and remove bad code from backend
Signed-off-by: SeeuSim <[email protected]>
1 parent 652012c commit 130a23f

File tree

19 files changed

+155
-474
lines changed

19 files changed

+155
-474
lines changed

backend/question/.env.local

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
EXPRESS_ENV="local"
2+
PEERPREP_UI_HOST="http://localhost:5173"
3+
4+
EXPRESS_PORT=9002
25
EXPRESS_DB_HOST="localhost"
36
EXPRESS_DB_PORT=5433
47
POSTGRES_DB="question"
58
POSTGRES_USER="peerprep-qn-express"
69
POSTGRES_PASSWORD="Xk8qEcEI2sizjfEn/lF6mLqiyBECjIHY3q6sdXf9poQ="
7-
PGDATA="/data/qn-db"
10+
PGDATA="/data/qn-db"

backend/question/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,26 @@
2020
"license": "ISC",
2121
"description": "",
2222
"dependencies": {
23+
"cors": "^2.8.5",
2324
"drizzle-orm": "^0.33.0",
2425
"env-cmd": "^10.1.0",
2526
"express": "^4.21.0",
27+
"helmet": "^8.0.0",
2628
"http-status-codes": "^2.3.0",
2729
"pino": "^9.4.0",
2830
"pino-http": "^10.3.0",
2931
"postgres": "^3.4.4"
3032
},
3133
"devDependencies": {
34+
"@swc/core": "^1.7.26",
35+
"@swc/helpers": "^0.5.13",
3236
"@types/express": "^4.17.21",
3337
"@types/node": "^22.5.5",
3438
"drizzle-kit": "^0.24.2",
3539
"nodemon": "^3.1.4",
3640
"pino-pretty": "^11.2.2",
3741
"ts-node": "^10.9.2",
38-
"tsc-alias": "^1.8.10"
42+
"tsc-alias": "^1.8.10",
43+
"tsconfig-paths": "^4.2.0"
3944
}
4045
}

backend/question/src/controller/question-controller.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
IGetQuestionPayload,
1111
IGetRandomQuestionPayload,
1212
} from '@/services/get/types';
13+
import { StatusCodes } from 'http-status-codes';
1314

1415
export const getQuestions = async (req: Request, res: Response): Promise<Response> => {
1516
const payload: IGetQuestionsPayload = {
@@ -22,9 +23,16 @@ export const getQuestions = async (req: Request, res: Response): Promise<Respons
2223

2324
try {
2425
const result = await getQuestionsService(payload);
25-
return res.status(result.code).json(result);
26+
if (!result.data || result.code < 400) {
27+
return res.status(result.code).json({
28+
message: result.error?.message ?? 'An error occurred',
29+
});
30+
}
31+
return res.status(result.code).json(result.data);
2632
} catch (error) {
27-
return res.status(500).json({ success: false, message: 'An error occurred', error });
33+
return res
34+
.status(StatusCodes.INTERNAL_SERVER_ERROR)
35+
.json({ success: false, message: 'An error occurred', error });
2836
}
2937
};
3038

@@ -35,9 +43,16 @@ export const getQuestionDetails = async (req: Request, res: Response): Promise<R
3543

3644
try {
3745
const result = await getQuestionDetailsService(payload);
38-
return res.status(result.code).json(result);
46+
if (!result.data || result.code < 400) {
47+
return res.status(result.code).json({
48+
message: result.error?.message ?? 'An error occurred',
49+
});
50+
}
51+
return res.status(result.code).json(result.data);
3952
} catch (error) {
40-
return res.status(500).json({ success: false, message: 'An error occurred', error });
53+
return res
54+
.status(StatusCodes.INTERNAL_SERVER_ERROR)
55+
.json({ success: false, message: 'An error occurred', error });
4156
}
4257
};
4358

@@ -51,7 +66,9 @@ export const getRandomQuestion = async (req: Request, res: Response): Promise<Re
5166
const result = await getRandomQuestionService(payload);
5267
return res.status(result.code).json(result);
5368
} catch (error) {
54-
return res.status(500).json({ success: false, message: 'An error occurred', error });
69+
return res
70+
.status(StatusCodes.INTERNAL_SERVER_ERROR)
71+
.json({ success: false, message: 'An error occurred', error });
5572
}
5673
};
5774

@@ -61,13 +78,17 @@ export const searchQuestionsByTitle = async (req: Request, res: Response): Promi
6178
const limit = parseInt(req.query.limit as string) || 10;
6279

6380
if (!title) {
64-
return res.status(400).json({ success: false, message: 'Title is required' });
81+
return res
82+
.status(StatusCodes.UNPROCESSABLE_ENTITY)
83+
.json({ success: false, message: 'Title is required' });
6584
}
6685

6786
try {
6887
const result = await searchQuestionsByTitleService(title.toString(), page, limit);
6988
return res.status(result.code).json(result);
7089
} catch (error) {
71-
return res.status(500).json({ success: false, message: 'An error occurred', error });
90+
return res
91+
.status(StatusCodes.INTERNAL_SERVER_ERROR)
92+
.json({ success: false, message: 'An error occurred', error });
7293
}
7394
};

backend/question/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { logger } from '@/lib/utils';
22
import app, { dbHealthCheck } from '@/server';
33

4-
const port = process.env.PORT || 8001;
4+
const port = Number.parseInt(process.env.EXPRESS_PORT ?? '8001');
55

66
const listenMessage = `App listening on port: ${port}`;
77
app.listen(port, () => {

backend/question/src/routes/question-routes.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

backend/question/src/server.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import { exit } from 'process';
2-
import questionsRouter from './routes/question-routes';
2+
3+
import cors from 'cors';
34
import express, { json } from 'express';
45
import pino from 'pino-http';
56
import { sql } from 'drizzle-orm';
7+
import helmet from 'helmet';
68

9+
import questionsRouter from '@/routes/question';
710
import { config, db } from '@/lib/db';
811
import { logger } from '@/lib/utils';
912

1013
const app = express();
1114
app.use(pino());
1215
app.use(json());
16+
app.use(helmet());
17+
app.use(
18+
cors({
19+
origin: [process.env.PEERPREP_UI_HOST!],
20+
credentials: true,
21+
})
22+
);
23+
1324
app.use('/questions', questionsRouter);
1425
app.get('/', async (_req, res) => {
1526
res.json({

backend/question/src/services/get/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
IGetRandomQuestionPayload,
1212
IGetRandomQuestionResponse,
1313
} from './types';
14+
import { StatusCodes } from 'http-status-codes';
1415

1516
export const getQuestionsService = async (
1617
payload: IGetQuestionsPayload
@@ -47,7 +48,7 @@ export const getQuestionsService = async (
4748
]);
4849

4950
return {
50-
code: 200,
51+
code: StatusCodes.OK,
5152
data: {
5253
questions: results.map((q) => ({
5354
id: q.id,
@@ -73,7 +74,7 @@ export const getQuestionDetailsService = async (
7374

7475
if (result.length === 0) {
7576
return {
76-
code: 404,
77+
code: StatusCodes.NOT_FOUND,
7778
data: { question: null },
7879
error: {
7980
message: 'Question not found',
@@ -82,7 +83,7 @@ export const getQuestionDetailsService = async (
8283
}
8384

8485
return {
85-
code: 200,
86+
code: StatusCodes.OK,
8687
data: { question: result[0] },
8788
};
8889
};
@@ -117,7 +118,7 @@ export const getRandomQuestionService = async (
117118

118119
if (result.length === 0) {
119120
return {
120-
code: 404,
121+
code: StatusCodes.NOT_FOUND,
121122
data: { question: null },
122123
error: {
123124
message: 'No matching questions found',
@@ -126,7 +127,7 @@ export const getRandomQuestionService = async (
126127
}
127128

128129
return {
129-
code: 200,
130+
code: StatusCodes.OK,
130131
data: { question: result[0] },
131132
};
132133
};
@@ -156,7 +157,7 @@ export const searchQuestionsByTitleService = async (
156157

157158
// Return the results as per IGetQuestionsResponse format
158159
return {
159-
code: 200,
160+
code: StatusCodes.OK,
160161
data: {
161162
questions: results, // Directly returning the query results
162163
totalQuestions: results.length, // Count of questions returned

backend/user/drizzle/0000_steady_madame_masque.sql

Lines changed: 0 additions & 13 deletions
This file was deleted.

backend/user/drizzle/meta/0000_snapshot.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "d8e4806c-713c-4202-a3f8-4a8be2c8b711",
2+
"id": "d0b8800a-d4b6-49f8-b2cf-57f4a64fc64a",
33
"prevId": "00000000-0000-0000-0000-000000000000",
44
"version": "7",
55
"dialect": "postgresql",
@@ -10,9 +10,10 @@
1010
"columns": {
1111
"id": {
1212
"name": "id",
13-
"type": "serial",
13+
"type": "uuid",
1414
"primaryKey": true,
15-
"notNull": true
15+
"notNull": true,
16+
"default": "gen_random_uuid()"
1617
},
1718
"email": {
1819
"name": "email",

backend/user/drizzle/meta/_journal.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
{
66
"idx": 0,
77
"version": "7",
8-
"when": 1727590732890,
9-
"tag": "0000_steady_madame_masque",
8+
"when": 1727623675374,
9+
"tag": "0000_initial_schema",
1010
"breakpoints": true
1111
}
1212
]

0 commit comments

Comments
 (0)