Skip to content

Commit cd106e9

Browse files
authored
Merge pull request #32 from demokratie-live/sprint#7/whats-hot
Sprint#7/whats hot
2 parents 90e5f6d + 08f6928 commit cd106e9

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/graphql/resolvers/Activity.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
export default {
44
Query: {
55
activityIndex: async (parent, { procedureId }, { ProcedureModel, ActivityModel, user }) => {
6-
if (!user) {
7-
throw new Error('No auth');
8-
}
96
const procedure = await ProcedureModel.findOne({ procedureId });
107
const activityIndex = await ActivityModel.find({ procedure }).count();
118
const active = await ActivityModel.findOne({

src/graphql/resolvers/Procedure.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,48 @@ export default {
6060
.skip(offset)
6161
.limit(pageSize);
6262
}
63+
if (type === 'HOT') {
64+
const oneWeekAgo = new Date();
65+
period = { $gte: 19 };
66+
sort = {};
67+
const schemaProps = Object.keys(ProcedureModel.schema.obj).reduce(
68+
(obj, prop) => ({ ...obj, [prop]: { $first: `$${prop}` } }),
69+
{},
70+
);
71+
const hotProcedures = await ProcedureModel.aggregate([
72+
{
73+
$match: {
74+
period,
75+
$or: [
76+
{ voteDate: { $gt: oneWeekAgo.setDate(oneWeekAgo.getDate() - 7) } },
77+
{ voteDate: null },
78+
],
79+
},
80+
},
81+
{
82+
$lookup: {
83+
from: 'activities',
84+
localField: '_id',
85+
foreignField: 'procedure',
86+
as: 'activityIndex',
87+
},
88+
},
89+
{ $unwind: '$activityIndex' },
90+
{
91+
$group: {
92+
_id: '$_id',
93+
...schemaProps,
94+
activities: { $sum: 1 },
95+
},
96+
},
97+
{ $sort: { activities: -1 } },
98+
99+
{ $skip: offset },
100+
{ $limit: pageSize },
101+
]);
102+
103+
return hotProcedures;
104+
}
63105

64106
const activeVotings = await ProcedureModel.find({
65107
voteDate: { $exists: false },

0 commit comments

Comments
 (0)