diff --git a/src/apis/v1/documents/service.ts b/src/apis/v1/documents/service.ts index c7cbbaa..5350010 100644 --- a/src/apis/v1/documents/service.ts +++ b/src/apis/v1/documents/service.ts @@ -9,7 +9,7 @@ import { UpdateDocumentByAdminDto, UpdateDocumentByOwnerDto, } from './dto/DocumentsDto'; -import { SubjectModel, UserModel } from 'models'; +import { UserModel } from 'models'; import URLParams from 'utils/rest/urlparams'; import { DEFAULT_PAGING, RANK_TYPE } from 'utils/constants'; import { checkDedicationScoreCompatibility, checkRankCompatibility, hideUserInfoIfRequired } from 'utils'; @@ -206,12 +206,13 @@ export const getDocumentsBySubjectId = async (subjectId: string, urlParams: URLP .skip(pageSize * currentPage) .limit(pageSize) .sort(sortObj) + .select('-is_deleted -deleted_at -is_approved -__v -content') .populate('author', '-is_blocked -roles -created_at -updated_at -__v') .populate('subject', '-is_deleted -created_at -updated_at -__v'); - const subject = SubjectModel.findOne({ _id: subjectId }); + const resultAll = await Promise.all([count, results]); - const resultAll = await Promise.all([count, results, subject]); + const subject = resultAll[1]?.length > 0 ? resultAll[1][0]?.subject : {}; logger.info(`Get all documents by subjectId successfully`); return { @@ -219,7 +220,7 @@ export const getDocumentsBySubjectId = async (subjectId: string, urlParams: URLP documents: resultAll[1].map((document: any) => { return { ...document.toObject(), author: hideUserInfoIfRequired(document?.author) }; }), - subject: resultAll[2], + subject: subject, }, meta: { total: resultAll[0], diff --git a/src/apis/v1/exam/service.ts b/src/apis/v1/exam/service.ts index ddf385b..50b889b 100644 --- a/src/apis/v1/exam/service.ts +++ b/src/apis/v1/exam/service.ts @@ -2,7 +2,7 @@ import { ObjectId } from 'mongodb'; import { PipelineStage } from 'mongoose'; import { ErrorCodes, HttpException } from 'exceptions'; -import { ExamModel, SubjectModel, UserModel } from 'models'; +import { ExamModel, UserModel } from 'models'; import { DEFAULT_PAGING, RANK_TYPE } from 'utils/constants'; import { logger } from 'utils/logger'; import URLParams from 'utils/rest/urlparams'; @@ -243,7 +243,7 @@ export const getExamsBySubjectId = async (subjectId: string, urlParams: URLParam 'author.created_at': 0, 'author.updated_at': 0, 'author.__v': 0, - 'questions.author': 0, + questions: 0, }, }, { @@ -256,15 +256,16 @@ export const getExamsBySubjectId = async (subjectId: string, urlParams: URLParam $limit: Number(pageSize), }, ]); - const subject = SubjectModel.findOne({ _id: subjectId }); - const resolveAll = await Promise.all([count, data, subject]); + + const resolveAll = await Promise.all([count, data]); + const subject = resolveAll[1]?.length > 0 ? resolveAll[1][0]?.subject : null; return { result: { exams: resolveAll[1].map((exam: Exam) => { return { ...exam, author: hideUserInfoIfRequired(exam?.author) }; }), - subject: resolveAll[2], + subject, }, meta: {