Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Commit

Permalink
Merge pull request #262 from tellform/fixResponseCountOnFormList
Browse files Browse the repository at this point in the history
Fix incorrect responses count for form list view
  • Loading branch information
whitef0x0 committed Nov 14, 2017
2 parents 499f2c9 + ed6d917 commit 49141e6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
52 changes: 42 additions & 10 deletions app/controllers/forms.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ exports.getVisitorData = function(req, res) {
});
};


/**
* Create a new form
*/
Expand Down Expand Up @@ -428,26 +427,59 @@ exports.list = function(req, res) {

Form.find(searchObj)
.sort('-created')
.select('title language admin submissions isLive')
.populate('admin.username', 'admin._id')
.select('title language isLive')
.lean()
.exec(function(err, forms) {
if (err) {
res.status(400).send({
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
}

var form_ids = forms.map(function(form){
return form._id;
});

//Get number of submissions for each form
FormSubmission.aggregate([
{
$match: {
form: {
$in: form_ids
}
}
},
{
$group: {
_id: '$form',
responses: { $sum: 1 }
}
},
], function(err, results){
if (err) {
console.error(err);
return res.status(500).send({
message: errorHandler.getErrorMessage(err)
});
}

const result_ids = results.map(function(result){ return result._id.id });
var currIndex = -1;

for(var i=0; i<forms.length; i++){
forms[i] = helpers.removeSensitiveModelData('private_form', forms[i]);

forms[i].numberOfResponses = 0;
if(forms[i].submissions){
forms[i].numberOfResponses = forms[i].submissions.length;
delete forms[i].submissions;
currIndex = result_ids.indexOf(forms[i]._id.id)

if(currIndex > -1){
forms[i].submissionNum = results[currIndex].responses;
} else {
forms[i].submissionNum = 0;
}
}

res.json(forms);
}
});
});
};

Expand Down
2 changes: 1 addition & 1 deletion app/models/form.server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ var FormSchema = new Schema({
},

design: {
colors:{
colors: {
backgroundColor: {
type: String,
match: [/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ <h4 class="list-group-item-heading" data-ng-bind="form.title"></h4>
</a>
<div class="col-xs-12 responses-row">
<small class="list-group-item-text">
<span> {{ form.numberOfResponses }} {{ 'RESPONSES' | translate }} </span>
<span> {{ form.submissionNum }} {{ 'RESPONSES' | translate }} </span>
</small>
<br>
<br>
Expand Down

0 comments on commit 49141e6

Please sign in to comment.