Skip to content

Commit 137d01a

Browse files
authored
Merge pull request #8 from WillACosta/feature/improve-ai-flow
[AUTOMATED-PR] - main <- feature/improve-ai-flow
2 parents 85f4f31 + dbbabe4 commit 137d01a

File tree

10 files changed

+78
-82
lines changed

10 files changed

+78
-82
lines changed

src/common/functions/safe-api-call.function.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@ export async function safeApiCall<T>(
88
): Promise<AppResponse<T>> {
99
try {
1010
const data = await handler()
11-
return res.status(200).json({
12-
success: true,
13-
data,
14-
})
11+
return res.send({ data })
1512
} catch (err: any) {
13+
if (err instanceof Error && err.message)
14+
return res.status(500).json({
15+
error: {
16+
message: err.message,
17+
},
18+
})
19+
1620
return res.status(500).json({
17-
success: false,
18-
error: err,
21+
error: {
22+
message: 'Unexpected error!',
23+
},
1924
})
2025
}
2126
}

src/common/middlewares/body-validation.middleware.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ export const changeUserRoleSchema = z.object({
2222
})
2323

2424
export const translateTextSchema = z.object({
25-
text: z.string().min(1, 'text field is required'),
25+
text: z.string().min(1, 'Text field is required'),
2626
language: z.string().min(1, 'Language field is required'),
2727
})
2828

29+
export const searchInDocumentsSchema = z.object({
30+
query: z.string().min(1, 'Query field is required'),
31+
})
32+
2933
export const validateRequestBody = (schema: z.ZodSchema<any>) => {
3034
return (req: Request, res: AppResponse, next: NextFunction) => {
3135
try {

src/common/types/network.types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export interface AppRequest<
1414
}
1515

1616
export type AppResponse<T = any> = Response<{
17-
success: boolean
1817
data?: T
19-
error?: { message: string | string[]; code?: string }
18+
error?: { message: string | string[] }
2019
}>

src/modules/auth/application/routes/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ const router = express.Router()
4343
* schema:
4444
* type: object
4545
* properties:
46-
* success:
47-
* type: boolean
48-
* example: true
4946
* data:
5047
* type: object
5148
* properties:
@@ -75,9 +72,6 @@ const router = express.Router()
7572
* schema:
7673
* type: object
7774
* properties:
78-
* success:
79-
* type: boolean
80-
* example: false
8175
* error:
8276
* type: object
8377
* properties:

src/modules/core/utils/upload.config.ts

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Request, Response } from 'express'
22

3+
import { AppResponse } from '@/common/types'
34
import crypto from 'crypto'
45
import multer, { MulterError } from 'multer'
56
import path from 'path'
@@ -20,11 +21,15 @@ const fileFilter = (
2021
file: Express.Multer.File,
2122
cb: multer.FileFilterCallback,
2223
) => {
23-
const allowedTypes = ['image/jpeg', 'image/png', 'application/pdf']
24+
const allowedTypes = ['application/pdf']
2425
if (allowedTypes.includes(file.mimetype)) {
2526
cb(null, true)
2627
} else {
27-
cb(new Error('Invalid file type. Only JPEG, PNG, and PDF are allowed.'))
28+
cb(
29+
new Error('Invalid file type. Only PDF files are allowed.', {
30+
cause: 'FILE_NOT_ALLOWED',
31+
}),
32+
)
2833
}
2934
}
3035

@@ -40,21 +45,50 @@ export const uploadMultipleDocs = multer({
4045
limits: { fileSize: 5 * 1024 * 1024 },
4146
}).array('documents', 10)
4247

43-
export function handleMulterErrorMessages(err: any, res: Response): Response {
48+
export function handleMulterErrorMessages(
49+
err: any,
50+
res: AppResponse,
51+
): Response {
4452
if (err instanceof MulterError) {
4553
switch (err.code) {
4654
case 'LIMIT_FILE_SIZE':
47-
return res.status(400).json({ error: 'File size exceeds the limit!' })
55+
return res.status(400).json({
56+
error: {
57+
message: 'File size exceeds the limit!',
58+
},
59+
})
4860
case 'LIMIT_FILE_COUNT':
49-
return res.status(400).json({ error: 'Too many files uploaded!' })
61+
return res.status(400).json({
62+
error: {
63+
message: 'Too many files uploaded!',
64+
},
65+
})
5066
case 'LIMIT_UNEXPECTED_FILE':
51-
return res.status(400).json({ error: 'Unexpected file uploaded!' })
67+
return res.status(400).json({
68+
error: {
69+
message: 'Unexpected file uploaded!',
70+
},
71+
})
5272
default:
53-
return res
54-
.status(400)
55-
.json({ error: 'Unexpected error was occurred while uploading!' })
73+
return res.status(400).json({
74+
error: {
75+
message: 'Unexpected error was occurred while uploading!',
76+
},
77+
})
5678
}
5779
}
5880

59-
return res.status(500).json({ error: 'An unknown error occurred!' })
81+
if (err instanceof Error && err.cause == 'FILE_NOT_ALLOWED') {
82+
return res.status(400).json({
83+
error: {
84+
message: err.message,
85+
},
86+
})
87+
}
88+
89+
return res.status(500).json({
90+
error: {
91+
message: 'An unknown error occurred!',
92+
},
93+
})
6094
}
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { AppRequest, AppResponse } from '@/common/types';
1+
import { safeApiCall } from '@/common/functions'
2+
import { AppRequest, AppResponse } from '@/common/types'
23
import { searchInDocumentsUseCase, translateUseCase } from '@/di'
34
import { ChatMemory } from '@/modules/core'
45

@@ -8,30 +9,27 @@ export class GenAIController {
89
res: AppResponse,
910
) {
1011
const { text, language } = req.body
11-
const result = await translateUseCase.invoke({ text, language })
12-
return res.send({ success: true, data: result })
12+
return safeApiCall(() => translateUseCase.invoke({ text, language }), res)
1313
}
1414

1515
async searchInDocuments(
1616
req: AppRequest<any, any, { query: string }>,
1717
res: AppResponse,
1818
) {
1919
const { query } = req.body
20-
const { result } = await searchInDocumentsUseCase.invoke({
21-
query: query,
22-
userId: req.user!.id,
23-
})
2420

25-
return res.send({ success: true, data: result })
21+
return safeApiCall(
22+
() =>
23+
searchInDocumentsUseCase.invoke({
24+
query: query,
25+
userId: req.user!.id,
26+
}),
27+
res,
28+
)
2629
}
2730

2831
async getChatHistory(req: AppRequest, res: AppResponse) {
2932
const chatMemory = new ChatMemory(req.user!.id)
30-
const history = await chatMemory.retrieveMemoryHistory()
31-
32-
return res.send({
33-
success: true,
34-
data: history,
35-
})
33+
return await safeApiCall(() => chatMemory.retrieveMemoryHistory(), res)
3634
}
3735
}

src/modules/genai/application/routes/index.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express from 'express'
22

33
import {
44
isAuthenticated,
5+
searchInDocumentsSchema,
56
translateTextSchema,
67
validateRequestBody,
78
} from '@/common/middlewares'
@@ -37,9 +38,6 @@ const router = express.Router()
3738
* schema:
3839
* type: object
3940
* properties:
40-
* success:
41-
* type: boolean
42-
* example: true
4341
* data:
4442
* type: string
4543
* example: "Hello!"
@@ -50,9 +48,6 @@ const router = express.Router()
5048
* schema:
5149
* type: object
5250
* properties:
53-
* success:
54-
* type: boolean
55-
* example: false
5651
* error:
5752
* type: array
5853
* items:
@@ -97,9 +92,6 @@ router.post(
9792
* schema:
9893
* type: object
9994
* properties:
100-
* success:
101-
* type: boolean
102-
* example: true
10395
* data:
10496
* type: string
10597
* example: The project aims to develop and validate a web platform (software) for automatically correcting assessments created in Microsoft Word.
@@ -114,6 +106,7 @@ router.post(
114106
router.post(
115107
'/search-in-documents',
116108
isAuthenticated,
109+
validateRequestBody(searchInDocumentsSchema),
117110
genAIController.searchInDocuments,
118111
)
119112

@@ -132,9 +125,6 @@ router.post(
132125
* schema:
133126
* type: object
134127
* properties:
135-
* success:
136-
* type: boolean
137-
* example: true
138128
* data:
139129
* type: array
140130
* items:

src/modules/resources/application/controllers/resources.controller.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { safeApiCall } from '@/common/functions'
12
import { AppRequest, AppResponse } from '@/common/types'
23
import { storeDocumentsUseCase } from '@/di'
34

@@ -17,7 +18,6 @@ export class ResourcesController {
1718

1819
if (!req.files || (req.files as Express.Multer.File[]).length === 0) {
1920
return res.status(400).json({
20-
success: false,
2121
error: { message: 'Please provide at least one document!' },
2222
})
2323
}
@@ -33,10 +33,9 @@ export class ResourcesController {
3333
} as UploadedDocument),
3434
)
3535

36-
await storeDocumentsUseCase.invoke({ docs })
36+
await safeApiCall(() => storeDocumentsUseCase.invoke({ docs }), res)
3737

38-
return res.status(201).json({
39-
success: true,
38+
return res.status(200).json({
4039
data: { docs },
4140
})
4241
})

src/modules/resources/application/routes/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ const router = express.Router()
3232
* schema:
3333
* type: object
3434
* properties:
35-
* success:
36-
* type: boolean
37-
* example: true
3835
* data:
3936
* type: array
4037
* items:
@@ -59,9 +56,6 @@ const router = express.Router()
5956
* schema:
6057
* type: object
6158
* properties:
62-
* success:
63-
* type: boolean
64-
* example: false
6559
* error:
6660
* type: object
6761
* properties:

src/modules/users/application/routes/index.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ const router = express.Router()
2626
* schema:
2727
* type: object
2828
* properties:
29-
* success:
30-
* type: boolean
31-
* example: true
3229
* data:
3330
* type: object
3431
* properties:
@@ -95,9 +92,6 @@ router.get('/', isAuthenticated, usersController.getUser)
9592
* schema:
9693
* type: object
9794
* properties:
98-
* success:
99-
* type: boolean
100-
* example: true
10195
* data:
10296
* type: object
10397
* properties:
@@ -121,9 +115,6 @@ router.get('/', isAuthenticated, usersController.getUser)
121115
* schema:
122116
* type: object
123117
* properties:
124-
* success:
125-
* type: boolean
126-
* example: false
127118
* error:
128119
* type: object
129120
* properties:
@@ -176,9 +167,6 @@ router.patch(
176167
* schema:
177168
* type: object
178169
* properties:
179-
* success:
180-
* type: boolean
181-
* example: true
182170
* data:
183171
* type: array
184172
* items:
@@ -207,9 +195,6 @@ router.patch(
207195
* schema:
208196
* type: object
209197
* properties:
210-
* success:
211-
* type: boolean
212-
* example: false
213198
* error:
214199
* type: object
215200
* properties:
@@ -243,9 +228,6 @@ router.patch(
243228
* schema:
244229
* type: object
245230
* properties:
246-
* success:
247-
* type: boolean
248-
* example: true
249231
* data:
250232
* type: array
251233
* items:
@@ -305,9 +287,6 @@ router.get(
305287
* schema:
306288
* type: object
307289
* properties:
308-
* success:
309-
* type: boolean
310-
* example: true
311290
* data:
312291
* type: array
313292
* items:

0 commit comments

Comments
 (0)