Skip to content

Commit 4f8e7ea

Browse files
committed
Return question categories sorted alphabetically
1 parent ba46b14 commit 4f8e7ea

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

backend/question-service/src/controllers/questionController.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response } from "express";
22
import Question, { IQuestion } from "../models/Question.ts";
3-
import { checkIsExistingQuestion } from "../utils/utils.ts";
3+
import { checkIsExistingQuestion, sortAlphabetically } from "../utils/utils.ts";
44
import {
55
DUPLICATE_QUESTION_RESPONSE_MESSAGE,
66
QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE,
@@ -203,7 +203,12 @@ export const readQuestionsList = async (
203203
res.status(200).json({
204204
message: QN_RETRIEVED_MESSAGE,
205205
questionCount: filteredQuestionCount,
206-
questions: filteredQuestions.map(formatQuestionResponse),
206+
questions: filteredQuestions
207+
.map(formatQuestionResponse)
208+
.map((question) => ({
209+
...question,
210+
categories: sortAlphabetically(question.categories),
211+
})),
207212
});
208213
} catch (error) {
209214
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
@@ -241,7 +246,7 @@ export const readCategories = async (
241246

242247
res.status(200).json({
243248
message: CATEGORIES_RETRIEVED_MESSAGE,
244-
categories: uniqueCats,
249+
categories: sortAlphabetically(uniqueCats),
245250
});
246251
} catch (error) {
247252
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });

backend/question-service/src/utils/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ export const uploadFileToFirebase = async (
4848
blobStream.end(file.buffer);
4949
});
5050
};
51+
52+
export const sortAlphabetically = (arr: string[]) => {
53+
return [...arr].sort((a, b) => a.localeCompare(b, undefined, { sensitivity: 'base' }));
54+
};

backend/question-service/swagger.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ paths:
327327
tags:
328328
- questions
329329
summary: Returns question categories
330-
description: Returns list of unique question categories
330+
description: Returns list of unique question categories sorted alphabetically
331331
responses:
332332
200:
333333
description: Successful Response

0 commit comments

Comments
 (0)