Skip to content

Commit 5f09c2b

Browse files
committed
add activity
1 parent 40c82b5 commit 5f09c2b

File tree

7 files changed

+77
-8
lines changed

7 files changed

+77
-8
lines changed

src/graphql/resolvers/Activity.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-underscore-dangle */
2+
3+
export default {
4+
Query: {},
5+
6+
Mutation: {
7+
increaseActivity: async (parent, { procedureId }, { ProcedureModel, ActivityModel, user }) => {
8+
console.log('########### increaseActivity', user);
9+
if (!user) {
10+
throw new Error('No auth');
11+
}
12+
const procedure = await ProcedureModel.findOne({ procedureId });
13+
if (!procedure) {
14+
throw new Error('Procedure not found');
15+
}
16+
const activity = await ActivityModel.findOne({
17+
user,
18+
procedure,
19+
});
20+
if (!activity) {
21+
await ActivityModel.create({ user, procedure });
22+
}
23+
const activityIndex = await ActivityModel.find({ procedure }).count();
24+
return { ...procedure.toObject(), activityIndex };
25+
},
26+
},
27+
};

src/graphql/resolvers/Procedure.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint no-underscore-dangle: ["error", { "allow": ["_id"] }] */
22
export default {
33
Query: {
4-
procedures: async (parent, { type, offset, pageSize }, { ProcedureModel }) => {
4+
procedures: async (parent, { type, offset, pageSize }, { ProcedureModel, ActivityModel }) => {
55
let currentStates = [];
66
switch (type) {
77
case 'PREPARATION':
@@ -58,7 +58,12 @@ export default {
5858
return ProcedureModel.find({ currentStatus: { $in: currentStates }, period })
5959
.sort(sort)
6060
.skip(offset)
61-
.limit(pageSize);
61+
.limit(pageSize)
62+
.then(results =>
63+
results.map(async (procedure) => {
64+
const activityIndex = await ActivityModel.find({ procedure }).count();
65+
return { ...procedure.toObject(), activityIndex };
66+
}));
6267
}
6368

6469
const activeVotings = await ProcedureModel.find({
@@ -78,15 +83,28 @@ export default {
7883
.sort(sort)
7984
.skip(offset - activeVotings.length > 0 ? offset - activeVotings.length : 0)
8085
.limit(pageSize - activeVotings.length)
81-
.then(finishedVotings => [...activeVotings, ...finishedVotings]);
86+
.then(finishedVotings => [...activeVotings, ...finishedVotings])
87+
.then(results =>
88+
results.map(async (procedure) => {
89+
const activityIndex = await ActivityModel.find({ procedure }).count();
90+
return { ...procedure.toObject(), activityIndex };
91+
}));
8292
},
83-
procedure: async (parent, { id }, { ProcedureModel }) =>
84-
ProcedureModel.findOne({ procedureId: id }),
85-
86-
searchProcedures: (parent, { term }, { ProcedureModel }) =>
93+
procedure: async (parent, { id }, { ProcedureModel, ActivityModel }) =>
94+
ProcedureModel.findOne({ procedureId: id }).then(async (procedure) => {
95+
const activityIndex = await ActivityModel.find({ procedure }).count();
96+
return { ...procedure.toObject(), activityIndex };
97+
}),
98+
searchProcedures: (parent, { term }, { ProcedureModel, ActivityModel }) =>
8799
ProcedureModel.find(
88100
{ $text: { $search: term }, period: 19 },
89101
{ score: { $meta: 'textScore' } },
90-
).sort({ score: { $meta: 'textScore' } }),
102+
)
103+
.sort({ score: { $meta: 'textScore' } })
104+
.then(results =>
105+
results.map(async (procedure) => {
106+
const activityIndex = await ActivityModel.find({ procedure }).count();
107+
return { ...procedure.toObject(), activityIndex };
108+
})),
91109
},
92110
};

src/graphql/resolvers/User.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export default {
1717

1818
rsa.setPrivateString(process.env.SECRET_KEY);
1919
const deviceHash = rsa.decrypt(deviceHashEncrypted);
20+
if (!deviceHash) {
21+
throw new Error('invalid deviceHash');
22+
}
2023
const user = await UserModel.create({ deviceHash });
2124

2225
return { token: user.createToken() };

src/graphql/schemas/Activity.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default `
2+
3+
type Mutation {
4+
increaseActivity(procedureId: Int!): Procedure
5+
}
6+
`;

src/graphql/schemas/Procedure.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Procedure {
1717
voteDate: Date
1818
subjectGroups: [String]
1919
submissionDate: Date
20+
activityIndex: Int
2021
importantDocuments: [Document]
2122
}
2223

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import auth from './express/auth';
1919
// Models
2020
import ProcedureModel from './models/Procedure';
2121
import UserModel from './models/User';
22+
import ActivityModel from './models/Activity';
2223

2324
const app = express();
2425

@@ -57,6 +58,7 @@ app.use(constants.GRAPHQL_PATH, (req, res, next) => {
5758
// Models
5859
ProcedureModel,
5960
UserModel,
61+
ActivityModel,
6062
},
6163
tracing: true,
6264
cacheControl: true,

src/models/Activity.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint no-underscore-dangle: ["error", { "allow": ["_id"] }] */
2+
import mongoose, { Schema } from 'mongoose';
3+
4+
const ActivitySchema = new Schema(
5+
{
6+
user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
7+
procedure: { type: Schema.Types.ObjectId, ref: 'Procedure', required: true },
8+
},
9+
{ timestamps: false },
10+
);
11+
12+
export default mongoose.model('Activity', ActivitySchema);

0 commit comments

Comments
 (0)