Skip to content

Commit d6268c1

Browse files
committed
optimize increaseActivityIndex
1 parent 02420a9 commit d6268c1

File tree

4 files changed

+13
-33
lines changed

4 files changed

+13
-33
lines changed

src/graphql/resolvers/Activity.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default {
99
const procedure = await ProcedureModel.findOne({ procedureId });
1010
const activityIndex = await ActivityModel.find({ procedure }).count();
1111
return {
12-
index: activityIndex,
12+
activityIndex,
1313
};
1414
},
1515
},
@@ -31,8 +31,7 @@ export default {
3131
await ActivityModel.create({ user, procedure });
3232
}
3333
const activityIndex = await ActivityModel.find({ procedure }).count();
34-
console.log(activityIndex);
35-
return { ...procedure.toObject(), activityIndex };
34+
return { activityIndex };
3635
},
3736
},
3837
};

src/graphql/resolvers/Procedure.js

Lines changed: 7 additions & 26 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, ActivityModel }) => {
4+
procedures: async (parent, { type, offset, pageSize }, { ProcedureModel }) => {
55
let currentStates = [];
66
switch (type) {
77
case 'PREPARATION':
@@ -58,12 +58,7 @@ export default {
5858
return ProcedureModel.find({ currentStatus: { $in: currentStates }, period })
5959
.sort(sort)
6060
.skip(offset)
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-
}));
61+
.limit(pageSize);
6762
}
6863

6964
const activeVotings = await ProcedureModel.find({
@@ -83,28 +78,14 @@ export default {
8378
.sort(sort)
8479
.skip(offset - activeVotings.length > 0 ? offset - activeVotings.length : 0)
8580
.limit(pageSize - activeVotings.length)
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-
}));
81+
.then(finishedVotings => [...activeVotings, ...finishedVotings]);
9282
},
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 }) =>
83+
procedure: async (parent, { id }, { ProcedureModel }) =>
84+
ProcedureModel.findOne({ procedureId: id }),
85+
searchProcedures: (parent, { term }, { ProcedureModel }) =>
9986
ProcedureModel.find(
10087
{ $text: { $search: term }, period: 19 },
10188
{ 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-
})),
89+
).sort({ score: { $meta: 'textScore' } }),
10990
},
11091
};

src/graphql/schemas/Activity.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
export default `
22
33
type ActivityIndex {
4-
index: Int
4+
activityIndex: Int
55
}
66
77
type Query {
8-
activityIndex(procedureId: Int!): ActivityIndex
8+
activityIndex(procedureId: String!): ActivityIndex
99
}
1010
1111
type Mutation {
12-
increaseActivity(procedureId: Int!): Procedure
12+
increaseActivity(procedureId: String!): ActivityIndex
1313
}
1414
`;

src/graphql/schemas/Procedure.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ enum ProcedureType {
88
type Procedure {
99
_id: ID!
1010
title: String!
11-
procedureId: String
11+
procedureId: String!
1212
type: String
1313
period: Int
1414
currentStatus: String

0 commit comments

Comments
 (0)