diff --git a/src/apis/v1/documents/controller.ts b/src/apis/v1/documents/controller.ts index 91ab060..323f3c2 100644 --- a/src/apis/v1/documents/controller.ts +++ b/src/apis/v1/documents/controller.ts @@ -52,7 +52,8 @@ export const getDocumentsBySubjectId = async (req: RequestWithUser, res: Respons }; export const getDocumentsByOwner = async (req: RequestWithUser, res: Response) => { + const urlParams: URLParams = req.searchParams; const author: string = req?.user?._id; - const result = await service.getDocumentsByOwner(author); - res.send(fmt.formatResponse(result, Date.now() - req.startTime, 'OK')); + const { result, meta } = await service.getDocumentsByOwner(author, urlParams); + res.send(fmt.formatResponse(result, Date.now() - req.startTime, 'OK', meta.total, meta.currentPage, meta.pageSize)); }; diff --git a/src/apis/v1/documents/service.ts b/src/apis/v1/documents/service.ts index 4a620b3..96d34fb 100644 --- a/src/apis/v1/documents/service.ts +++ b/src/apis/v1/documents/service.ts @@ -207,16 +207,33 @@ export const getDocumentsBySubjectId = async (subjectId: string) => { } }; -export const getDocumentsByOwner = async (authorId: string) => { +export const getDocumentsByOwner = async (authorId: string, urlParams: URLParams) => { try { - const results = await DocumentModel.find({ author: authorId }) + const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit; + + const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip; + + const order = urlParams.order || 'DESC'; + + const count = DocumentModel.countDocuments({ author: authorId }); + + const results = DocumentModel.find({ author: authorId }) + .skip(pageSize * currentPage) + .limit(pageSize) + .sort({ created_at: order === 'DESC' ? -1 : 1 }) .populate('author', '-is_blocked -roles -created_at -updated_at -__v') .populate('subject', '-is_deleted -created_at -updated_at -__v'); + const resolveAll = await Promise.all([count, results]); return { - documents: results.map((document) => { + result: resolveAll[1].map((document: DocumentType) => { return { ...document.toObject(), author: hideUserInfoIfRequired(document?.author) }; }), + meta: { + total: resolveAll[0], + currentPage, + pageSize, + }, }; } catch (error) { logger.error(`Error while get documents by Owner: ${error}`); diff --git a/src/apis/v1/exam/controller.ts b/src/apis/v1/exam/controller.ts index 463b78d..98a5ec8 100644 --- a/src/apis/v1/exam/controller.ts +++ b/src/apis/v1/exam/controller.ts @@ -62,8 +62,9 @@ export const getDraftExam = async (req: RequestWithUser, res: Response) => { }; export const getExamsByOwner = async (req: RequestWithUser, res: Response) => { + const urlParams: URLParams = req.searchParams; const author = req?.user?._id; - const result = await service.getExamsByOwner(String(author)); + const { result, meta } = await service.getExamsByOwner(String(author), urlParams); - res.send(fmt.formatResponse(result, Date.now() - req.startTime, 'OK')); + res.send(fmt.formatResponse(result, Date.now() - req.startTime, 'OK', meta.total, meta.currentPage, meta.pageSize)); }; diff --git a/src/apis/v1/exam/service.ts b/src/apis/v1/exam/service.ts index 7586301..19828b5 100644 --- a/src/apis/v1/exam/service.ts +++ b/src/apis/v1/exam/service.ts @@ -271,8 +271,14 @@ export const getExamsBySubjectId = async (subjectId: string, urlParams: URLParam throw new HttpException(400, ErrorCodes.BAD_REQUEST.MESSAGE, ErrorCodes.BAD_REQUEST.CODE); } }; -export const getExamsByOwner = async (authorId: string) => { +export const getExamsByOwner = async (authorId: string, urlParams: URLParams) => { try { + const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit; + + const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip; + + const order = urlParams.order || 'DESC'; + const _id = new ObjectId(authorId); const count = ExamModel.countDocuments({ author: authorId }); const data = ExamModel.aggregate([ @@ -320,14 +326,25 @@ export const getExamsByOwner = async (authorId: string) => { }, }, { - $sort: { created_at: -1 }, + $sort: { created_at: order === 'DESC' ? -1 : 1 }, + }, + { + $skip: Number(pageSize * currentPage), + }, + { + $limit: Number(pageSize), }, ]); const resolveAll = await Promise.all([count, data]); return { - exams: resolveAll[1].map((exam: Exam) => { + result: resolveAll[1].map((exam: any) => { return { ...exam, author: hideUserInfoIfRequired(exam?.author) }; }), + meta: { + total: resolveAll[0], + currentPage, + pageSize, + }, }; } catch (error) { logger.error(`Error while get exam by Owner: ${error}`);