diff --git a/controllers/attachment.js b/controllers/attachment.js index af52f3c5..1badbc98 100644 --- a/controllers/attachment.js +++ b/controllers/attachment.js @@ -3,15 +3,17 @@ const attachmentService = require('~/services/attachment') const getMatchOptions = require('~/utils/getMatchOptions') const getSortOptions = require('~/utils/getSortOptions') const getRegex = require('~/utils/getRegex') +const getCategoriesOptions = require('~/utils/getCategoriesOption') const getAttachments = async (req, res) => { const { id: author } = req.user const { fileName, sort, skip, limit, categories } = req.query + const categoriesOptions = getCategoriesOptions(categories) const match = getMatchOptions({ author, fileName: getRegex(fileName), - ...(categories?.length && { category: categories }) + category: categoriesOptions }) const sortOptions = getSortOptions(sort) diff --git a/controllers/lesson.js b/controllers/lesson.js index 52c39c2c..bba2c106 100644 --- a/controllers/lesson.js +++ b/controllers/lesson.js @@ -1,4 +1,5 @@ const lessonService = require('~/services/lesson') +const getCategoriesOptions = require('~/utils/getCategoriesOption') const getMatchOptions = require('~/utils/getMatchOptions') const getRegex = require('~/utils/getRegex') const getSortOptions = require('~/utils/getSortOptions') @@ -23,8 +24,9 @@ const getLessonById = async (req, res) => { const getLessons = async (req, res) => { const { id: author } = req.user const { title, sort, skip, limit, categories } = req.query + const categoriesOptions = getCategoriesOptions(categories) - const match = getMatchOptions({ author, title: getRegex(title), ...(categories?.length && { category: categories }) }) + const match = getMatchOptions({ author, title: getRegex(title), category: categoriesOptions }) const sortOptions = getSortOptions(sort) const lessons = await lessonService.getLessons(match, sortOptions, parseInt(skip), parseInt(limit)) diff --git a/controllers/question.js b/controllers/question.js index adde2811..32cafb35 100644 --- a/controllers/question.js +++ b/controllers/question.js @@ -1,16 +1,19 @@ const questionService = require('~/services/question') +const getCategoriesOptions = require('~/utils/getCategoriesOption') +const getMatchOptions = require('~/utils/getMatchOptions') const getRegex = require('~/utils/getRegex') const getSortOptions = require('~/utils/getSortOptions') const getQuestions = async (req, res) => { const { id: author } = req.user const { title, sort, skip, limit, categories } = req.query + const categoriesOptions = getCategoriesOptions(categories) - const match = { + const match = getMatchOptions({ author, title: getRegex(title), - ...(categories?.length && { category: categories }) - } + category: categoriesOptions + }) const sortOptions = getSortOptions(sort) const questions = await questionService.getQuestions(match, sortOptions, parseInt(skip), parseInt(limit)) diff --git a/controllers/quiz.js b/controllers/quiz.js index 39777d24..938d6303 100644 --- a/controllers/quiz.js +++ b/controllers/quiz.js @@ -1,12 +1,15 @@ const quizService = require('~/services/quiz') +const getCategoriesOptions = require('~/utils/getCategoriesOption') +const getMatchOptions = require('~/utils/getMatchOptions') const getRegex = require('~/utils/getRegex') const getSortOptions = require('~/utils/getSortOptions') const getQuizzes = async (req, res) => { const { id: author } = req.user const { title, sort, skip, limit, categories } = req.query + const categoriesOptions = getCategoriesOptions(categories) - const match = { author, title: getRegex(title), ...(categories?.length && { category: categories }) } + const match = getMatchOptions({ author, title: getRegex(title), category: categoriesOptions }) const sortOptions = getSortOptions(sort) const quizzes = await quizService.getQuiz(match, sortOptions, parseInt(skip), parseInt(limit)) diff --git a/utils/getCategoriesOption.js b/utils/getCategoriesOption.js new file mode 100644 index 00000000..23edc82f --- /dev/null +++ b/utils/getCategoriesOption.js @@ -0,0 +1,8 @@ +const getCategoriesOptions = (categories) => { + if (categories) { + return categories.map((item) => (item === 'null' ? null : item)) + } else { + return + } +} +module.exports = getCategoriesOptions