1
1
import { Request , Response } from "express" ;
2
2
import Question , { IQuestion } from "../models/Question.ts" ;
3
- import { checkIsExistingQuestion } from "../utils/utils.ts" ;
3
+ import { checkIsExistingQuestion , sortAlphabetically } from "../utils/utils.ts" ;
4
4
import {
5
5
DUPLICATE_QUESTION_MESSAGE ,
6
6
QN_DESC_EXCEED_CHAR_LIMIT_MESSAGE ,
@@ -13,6 +13,8 @@ import {
13
13
PAGE_LIMIT_REQUIRED_MESSAGE ,
14
14
PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE ,
15
15
CATEGORIES_RETRIEVED_MESSAGE ,
16
+ MONGO_OBJ_ID_FORMAT ,
17
+ MONGO_OBJ_ID_MALFORMED_MESSAGE ,
16
18
} from "../utils/constants.ts" ;
17
19
18
20
import { upload } from "../../config/multer" ;
@@ -78,6 +80,7 @@ export const createImageLink = async (
78
80
79
81
const uploadPromises = files . map ( ( file ) => uploadFileToFirebase ( file ) ) ;
80
82
const imageUrls = await Promise . all ( uploadPromises ) ;
83
+ console . log ( imageUrls ) ;
81
84
res
82
85
. status ( 200 )
83
86
. json ( { message : "Images uploaded successfully" , imageUrls } ) ;
@@ -95,7 +98,13 @@ export const updateQuestion = async (
95
98
const { id } = req . params ;
96
99
const { title, description } = req . body ;
97
100
101
+ if ( ! id . match ( MONGO_OBJ_ID_FORMAT ) ) {
102
+ res . status ( 400 ) . json ( { message : MONGO_OBJ_ID_MALFORMED_MESSAGE } ) ;
103
+ return ;
104
+ }
105
+
98
106
const currentQuestion = await Question . findById ( id ) ;
107
+
99
108
if ( ! currentQuestion ) {
100
109
res . status ( 404 ) . json ( { message : QN_NOT_FOUND_MESSAGE } ) ;
101
110
return ;
@@ -203,7 +212,12 @@ export const readQuestionsList = async (
203
212
res . status ( 200 ) . json ( {
204
213
message : QN_RETRIEVED_MESSAGE ,
205
214
questionCount : filteredQuestionCount ,
206
- questions : filteredQuestions . map ( formatQuestionResponse ) ,
215
+ questions : filteredQuestions
216
+ . map ( formatQuestionResponse )
217
+ . map ( ( question ) => ( {
218
+ ...question ,
219
+ categories : sortAlphabetically ( question . categories ) ,
220
+ } ) ) ,
207
221
} ) ;
208
222
} catch ( error ) {
209
223
res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
@@ -241,7 +255,7 @@ export const readCategories = async (
241
255
242
256
res . status ( 200 ) . json ( {
243
257
message : CATEGORIES_RETRIEVED_MESSAGE ,
244
- categories : uniqueCats ,
258
+ categories : sortAlphabetically ( uniqueCats ) ,
245
259
} ) ;
246
260
} catch ( error ) {
247
261
res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
0 commit comments