diff --git a/apps/api/src/app/controllers/pools/get.controller.ts b/apps/api/src/app/controllers/pools/get.controller.ts index 7b28f1cca..86143f879 100644 --- a/apps/api/src/app/controllers/pools/get.controller.ts +++ b/apps/api/src/app/controllers/pools/get.controller.ts @@ -3,7 +3,6 @@ import { param } from 'express-validator'; import { Event, Participant, Widget, Wallet, Webhook } from '@thxnetwork/api/models'; import PoolService from '@thxnetwork/api/services/PoolService'; import BrandService from '@thxnetwork/api/services/BrandService'; -import QuestService from '@thxnetwork/api/services/QuestService'; const validation = [param('id').isMongoId()]; diff --git a/apps/api/src/app/controllers/pools/quests/list.controller.ts b/apps/api/src/app/controllers/pools/quests/list.controller.ts index 0bd892333..5a3156d1d 100644 --- a/apps/api/src/app/controllers/pools/quests/list.controller.ts +++ b/apps/api/src/app/controllers/pools/quests/list.controller.ts @@ -9,11 +9,12 @@ import { QuestDaily, QuestWebhook, } from '@thxnetwork/api/models'; +import { PipelineStage } from 'mongoose'; const validation = [ param('id').isMongoId(), - query('page').isInt(), - query('limit').isInt(), + query('page').optional().isInt(), + query('limit').optional().isInt(), query('isPublished') .optional() .isBoolean() @@ -26,8 +27,12 @@ const controller = async (req: Request, res: Response) => { const poolId = req.params.id; const page = Number(req.query.page); const limit = Number(req.query.limit); - const $match = { poolId, isPublished: req.query.isPublished }; - const pipeline = [ + const $match = { poolId }; + if (typeof req.query.isPublished !== 'undefined') { + $match['isPublished'] = req.query.isPublished; + } + // Build the aggregation pipeline + const pipeline: PipelineStage[] = [ { $unionWith: { coll: QuestInvite.collection.name } }, { $unionWith: { coll: QuestSocial.collection.name } }, { $unionWith: { coll: QuestCustom.collection.name } }, @@ -35,19 +40,23 @@ const controller = async (req: Request, res: Response) => { { $unionWith: { coll: QuestGitcoin.collection.name } }, { $unionWith: { coll: QuestWebhook.collection.name } }, { $match }, + { $sort: { createdAt: -1 } }, ]; + if (page) { + pipeline.push({ $skip: (page - 1) * limit }); + } + if (limit) { + pipeline.push({ $limit: limit }); + } + const results = await QuestDaily.aggregate(pipeline); + + // Count the total of quest documents const arr = await Promise.all( [QuestDaily, QuestInvite, QuestSocial, QuestCustom, QuestWeb3, QuestGitcoin, QuestWebhook].map( async (model) => await model.countDocuments($match), ), ); const total = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0); - const results = await QuestDaily.aggregate([ - ...pipeline, - { $sort: { index: 1 } }, - { $skip: (page - 1) * limit }, - { $limit: limit }, - ]); res.json({ total,