Skip to content

Commit

Permalink
Dev (#191)
Browse files Browse the repository at this point in the history
* feat: [add sortObject default and dynamic]

---------

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 5, 2023
1 parent 81f105e commit b60326b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
24 changes: 18 additions & 6 deletions src/apis/v1/documents/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ export const getDocuments = async (urlParams: URLParams) => {
const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit;
const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip;
const order = urlParams.order || 'DESC';
const sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const count = DocumentModel.countDocuments({ is_approved: true });

const results = DocumentModel.find({ is_approved: true })
.skip(pageSize * currentPage)
.limit(pageSize)
.sort({ created_at: order === 'DESC' ? -1 : 1 })
.sort(sortObj)
.populate('author', '-is_deleted -is_blocked -roles -created_at -updated_at -__v')
.populate('subject', '-is_deleted -created_at -updated_at -__v');

Expand Down Expand Up @@ -78,6 +80,12 @@ export const createDocumentByAdmin = async (input: CreateDocumentRequestForAdmin

const result = await DocumentModel.create(document);

if (input.is_approved === true) {
const user = await UserModel.findOne({ _id: author });
const newRank = checkDedicationScoreCompatibility(user?.dedication_score + 1);
await UserModel.findByIdAndUpdate({ _id: author }, { $inc: { dedication_score: 1 }, rank: newRank });
}

logger.info(`Create new document by admin successfully`);
return result;
} catch (error) {
Expand Down Expand Up @@ -154,13 +162,15 @@ export const getDocumentsByAdmin = async (filter: DocumentFilter, urlParams: URL
const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit;
const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip;
const order = urlParams.order || 'DESC';
const sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const count = DocumentModel.countDocuments({ ...filter });

const results = DocumentModel.find({ ...filter })
.skip(pageSize * currentPage)
.limit(pageSize)
.sort({ created_at: order === 'DESC' ? -1 : 1 })
.sort(sortObj)
.populate('author', '-is_deleted -is_blocked -roles -created_at -updated_at -__v')
.populate('subject', '-is_deleted -created_at -updated_at -__v');

Expand Down Expand Up @@ -188,12 +198,14 @@ export const getDocumentsBySubjectId = async (subjectId: string, urlParams: URLP
const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit;
const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip;
const order = urlParams.order || 'DESC';
const sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const count = DocumentModel.countDocuments({ is_approved: true, subject: subjectId });
const results = DocumentModel.find({ is_approved: true, subject: subjectId })
.skip(pageSize * currentPage)
.limit(pageSize)
.sort({ created_at: order === 'DESC' ? -1 : 1 })
.sort(sortObj)
.populate('author', '-is_blocked -roles -created_at -updated_at -__v')
.populate('subject', '-is_deleted -created_at -updated_at -__v');

Expand Down Expand Up @@ -224,17 +236,17 @@ export const getDocumentsBySubjectId = async (subjectId: string, urlParams: URLP
export const getDocumentsByOwner = 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 sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const count = DocumentModel.countDocuments({ author: authorId });

const results = DocumentModel.find({ author: authorId })
.skip(pageSize * currentPage)
.limit(pageSize)
.sort({ created_at: order === 'DESC' ? -1 : 1 })
.sort(sortObj)
.populate('author', '-is_blocked -roles -created_at -updated_at -__v')
.populate('subject', '-is_deleted -created_at -updated_at -__v');

Expand Down
14 changes: 9 additions & 5 deletions src/apis/v1/exam/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const getExams = async (urlParams: URLParams) => {
const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit;
const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip;
const order = urlParams.order || 'DESC';
const sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const count = ExamModel.countDocuments();
const data = ExamModel.aggregate([
Expand Down Expand Up @@ -60,7 +62,7 @@ export const getExams = async (urlParams: URLParams) => {
},
},
{
$sort: { created_at: order === 'DESC' ? -1 : 1 },
$sort: sortObj,
},
{
$skip: Number(pageSize * currentPage),
Expand Down Expand Up @@ -195,6 +197,8 @@ export const getExamsBySubjectId = async (subjectId: string, urlParams: URLParam
const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit;
const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip;
const order = urlParams.order || 'DESC';
const sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const _id = new ObjectId(subjectId);
const count = ExamModel.countDocuments({ subject: subjectId });
Expand Down Expand Up @@ -243,7 +247,7 @@ export const getExamsBySubjectId = async (subjectId: string, urlParams: URLParam
},
},
{
$sort: { created_at: order === 'DESC' ? -1 : 1 },
$sort: sortObj,
},
{
$skip: Number(pageSize * currentPage),
Expand Down Expand Up @@ -277,10 +281,10 @@ export const getExamsBySubjectId = async (subjectId: string, urlParams: URLParam
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 sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const _id = new ObjectId(authorId);
const count = ExamModel.countDocuments({ author: authorId });
Expand Down Expand Up @@ -329,7 +333,7 @@ export const getExamsByOwner = async (authorId: string, urlParams: URLParams) =>
},
},
{
$sort: { created_at: order === 'DESC' ? -1 : 1 },
$sort: sortObj,
},
{
$skip: Number(pageSize * currentPage),
Expand Down
4 changes: 3 additions & 1 deletion src/apis/v1/questions/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ export const getQuestions = async function (urlParams: URLParams) {
const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit;
const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip;
const order = urlParams.order || 'DESC';
const sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const count = QuestionModel.countDocuments();
const question = QuestionModel.find()
.skip(pageSize * currentPage)
.limit(pageSize)
.sort({ created_at: order === 'DESC' ? -1 : 1 });
.sort(sortObj);
logger.info(`Get questions successfully`);

const resolveAll = await Promise.all([count, question]);
Expand Down
5 changes: 3 additions & 2 deletions src/apis/v1/subject/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ export const getSubjects = async (input: QuerySubjectDto, urlParams: URLParams)
try {
const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit;
const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip;

const order = urlParams.order || 'DESC';
const sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const count = SubjectModel.countDocuments(input);
const data = SubjectModel.find(input)
.skip(pageSize * currentPage)
.limit(pageSize)
.sort({ created_at: order === 'DESC' ? -1 : 1 });
.sort(sortObj);

const resolveAll = await Promise.all([count, data]);

Expand Down
5 changes: 3 additions & 2 deletions src/apis/v1/user/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ export const getUsers = async function (urlParams: URLParams) {
try {
const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit;
const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip;

const order = urlParams.order || 'DESC';
const sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const count = UserModel.countDocuments();
const users = UserModel.find()
.skip(pageSize * currentPage)
.limit(pageSize)
.sort({ created_at: order === 'DESC' ? -1 : 1 });
.sort(sortObj);

const resolveAll = await Promise.all([count, users]);

Expand Down
11 changes: 7 additions & 4 deletions src/apis/v1/userExam/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ export const getAllUserExamsByAdmin = async (urlParams: URLParams) => {
try {
const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit;
const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip;

const order = urlParams.order || 'DESC';
const sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const count = UserExamModel.countDocuments();
const result = UserExamModel.aggregate([
Expand Down Expand Up @@ -133,7 +134,7 @@ export const getAllUserExamsByAdmin = async (urlParams: URLParams) => {
},
},
{
$sort: { created_at: order === 'DESC' ? -1 : 1 },
$sort: sortObj,
},
{
$skip: Number(pageSize * currentPage),
Expand Down Expand Up @@ -164,8 +165,10 @@ export const getAllUserExamsByOwner = async (userId: string, filter: UserExamFil
try {
const pageSize = urlParams.pageSize || DEFAULT_PAGING.limit;
const currentPage = urlParams.currentPage || DEFAULT_PAGING.skip;

const order = urlParams.order || 'DESC';
const sort = urlParams.sort || 'created_at';
const sortObj: any = { [sort]: order === 'DESC' ? -1 : 1 };

const _id = new ObjectId(userId);

const count = UserExamModel.countDocuments({ author: userId, ...filter });
Expand Down Expand Up @@ -237,7 +240,7 @@ export const getAllUserExamsByOwner = async (userId: string, filter: UserExamFil
},
},
{
$sort: { created_at: order === 'DESC' ? -1 : 1 },
$sort: sortObj,
},
{
$skip: Number(pageSize * currentPage),
Expand Down

0 comments on commit b60326b

Please sign in to comment.