Skip to content

Commit

Permalink
Fix issues with quest locks
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpolman committed Aug 7, 2024
1 parent ebbd766 commit afc1566
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
1 change: 0 additions & 1 deletion apps/api/src/app/controllers/pools/get.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()];

Expand Down
29 changes: 19 additions & 10 deletions apps/api/src/app/controllers/pools/quests/list.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -26,28 +27,36 @@ 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 } },
{ $unionWith: { coll: QuestWeb3.collection.name } },
{ $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,
Expand Down

0 comments on commit afc1566

Please sign in to comment.