Skip to content

Commit

Permalink
Dev (#194)
Browse files Browse the repository at this point in the history
* feat: [#192] (#193)

---------

Co-authored-by: dungnguyenn1103 <91179133+DungNguyen2003@users.noreply.github.com>
Co-authored-by: Loc Xuan Dao <102164071+locxuandao@users.noreply.github.com>
Co-authored-by: Snyk bot <snyk-bot@snyk.io>
  • Loading branch information
4 people authored May 7, 2023
1 parent b60326b commit e248344
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/apis/v1/documents/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -206,20 +206,21 @@ 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 {
result: {
documents: resultAll[1].map((document: any) => {
return { ...document.toObject(), author: hideUserInfoIfRequired(document?.author) };
}),
subject: resultAll[2],
subject: subject,
},
meta: {
total: resultAll[0],
Expand Down
11 changes: 6 additions & 5 deletions src/apis/v1/exam/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
},
},
{
Expand All @@ -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: {
Expand Down

0 comments on commit e248344

Please sign in to comment.