Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/#170 #172

Merged
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/apis/v1/documents/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
23 changes: 20 additions & 3 deletions src/apis/v1/documents/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
5 changes: 3 additions & 2 deletions src/apis/v1/exam/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
23 changes: 20 additions & 3 deletions src/apis/v1/exam/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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}`);
Expand Down