diff --git a/models/issue.js b/models/issue.js index 1e7fcab..ff12486 100644 --- a/models/issue.js +++ b/models/issue.js @@ -3,6 +3,18 @@ var mongoose = require('mongoose') , ObjectId = Schema.ObjectId , categories = Object.keys(require('../fixtures/categories')); +/* + * Length in miliseconds for + * Issue Stages times... + */ +var dayTime = 24*60*60*1000 + , daysOfDiscussion = 7 + , discussionTime = dayTime * daysOfDiscussion + , daysOpenForIdeas = 7 + , openForIdeasTime = dayTime * daysOpenForIdeas + , daysOfVoting = 7 + , votingTime = dayTime * daysOfVoting; + var IssueVoteOptionSchema = require('./issueVoteOption').schema; /* @@ -24,10 +36,22 @@ var IssueSchema = new Schema({ updatedAt: {type: Date, default: Date.now} }, - // comments? - createdAt: { type: Date, default: Date.now }, - updatedAt: Date + updatedAt: Date, + + discussionEndDate: { type: Date, default: function() { + return Date.now() + discussionTime; + }}, + + openForIdeasEndDate: { type: Date, default: function() { + return Date.now() + openForIdeasTime + discussionTime; + }}, + + votingEndDate: { type: Date, default: function() { + return Date.now() + votingTime + openForIdeasTime + discussionTime; + }}, + + }); IssueSchema.path('title').validate(function(value) { diff --git a/routes/issues.js b/routes/issues.js index 3d67532..5ab5ecd 100644 --- a/routes/issues.js +++ b/routes/issues.js @@ -30,20 +30,27 @@ module.exports = function(app, utils) { Issue .findById(req.params.id) .populate('author') + .populate('census') .populate('vote.choices.idea') .populate('vote.choices.author') .populate('vote.choices.sponsor') + .populate('vote.voters') .exec(function(err, issue) { if(err) console.log(err); if(!issue) return res.redirect('/'); issue.loadComments(function(err, comments) { if(req.user) { Delegation - .findOne({truster: req.user.id, category: issue.category}) + .findOne({ truster: req.user.id, category: issue.category }) // .select('trustees') .populate('trustees') .exec(function(err, delegation) { - res.render('issue', {page: 'idea', issue: issue, author: issue.author, comments: comments, delegation: delegation}); + Delegation + .find({ trustees: req.user.id, category: issue.category }) + .populate('truster') + .exec(function(err, delegations) { + res.render('issue', { page: 'idea', issue: issue, author: issue.author, comments: comments, delegation: delegation, delegations: delegations }); + }); }); } else { res.render('issue', {page: 'idea', issue: issue, author: issue.author, comments: comments, delegation: {}}); diff --git a/strategy.js b/strategy.js index b24435e..cfacec7 100644 --- a/strategy.js +++ b/strategy.js @@ -19,7 +19,9 @@ module.exports = function(app) { }); passport.deserializeUser(function(citizenId, done) { - Citizen.findById(citizenId, function(err, citizen) { + Citizen + .findById(citizenId) + .exec(function(err, citizen) { done(null, citizen); }); }); diff --git a/views/element/delegation_form.jade b/views/element/delegation_form.jade index 0306b47..a1f10a3 100644 --- a/views/element/delegation_form.jade +++ b/views/element/delegation_form.jade @@ -10,15 +10,14 @@ form().form-delegation - if(delegation.trustees && delegation.trustees.length) { - - var len = delegation.trustees.length - 1; - - var trustee = delegation.trustees[len]; + - var trustee = delegation.trustees[delegation.trustees.length - 1]; h5 | Usted esta delegando su voto en span.fullname #{trustee.fullName} | (@ span.username #{trustee.username} | ) - img.profile-delegation(src="#{trustee.imageUrl}") + img.profile-delegation(src="#{trustee.imageUrlRSmall}") p a.delegate-button(href="#") Prefiero delegar mi voto en otra persona diff --git a/views/issue-citizen-info.jade b/views/issue-citizen-info.jade new file mode 100644 index 0000000..0b9c86e --- /dev/null +++ b/views/issue-citizen-info.jade @@ -0,0 +1,27 @@ +//- @param issue +//- @param citizen +//- @param delegation +//- @param delegations + +h3.block-title + i.icon + span Delegaciones +.box-content + - if(typeof(delegation) !== 'undefined' && delegation) { + - var trustee = delegation.trustees[0]; + h5 Mi delegado en + a(href="#")= categories[issue.category].name + + //- Citizen delegation in category + a(href="/profiles/#{trustee.id}", rel="tooltip", title="#{trustee.fullName}", data-placement="bottom", class="tt") + img(src="#{trustee.imageUrlMini}") + + - } + + - if(typeof(delegations) !== 'undefined' && delegations.length) { + h5 Ciudadanos delegando en mi + - delegations.forEach(function(d) { + a(href="/profiles/#{trustee.id}", rel="tooltip", title="#{trustee.fullName}", data-placement="bottom", class="tt") + img(src="#{trustee.imageUrlMini}") + - }) + - } diff --git a/views/issue-info.jade b/views/issue-info.jade new file mode 100644 index 0000000..83c8631 --- /dev/null +++ b/views/issue-info.jade @@ -0,0 +1,14 @@ +//- @param issue +//- @param citizen + +h3.block-title + i.icon + span Información +.box-content + h6 Ciudadanos activos en esta problemática + - if(issue.census.length) { + - issue.census.forEach(function(activeCitizen) { + a(href="/profiles/#{activeCitizen.id}", rel="tooltip", title="#{activeCitizen.fullName}", data-placement="bottom", class="tt") + img(src="#{activeCitizen.imageUrlMini}") + - }) + -} diff --git a/views/issue.jade b/views/issue.jade index 1a17d2b..217dcfa 100644 --- a/views/issue.jade +++ b/views/issue.jade @@ -18,23 +18,29 @@ block content a(href="")= categories[element.category].name h2= element.title .issue-content-description!= md(element.essay) - - // - #issue-full.modal(role="dialog", style="display:none;") - .modal-header - a.close(type="button", data-dismiss="modal", aria-hidden="true") × - h2= element.title - .modal-body - p.issue-content-description!= md(element.essay) - p - a(data-toggle="modal", role="button", href="#issue-full").full-read Leer Completo - + + .block-info.span9.box.clearfix + include issue-info + + - if(typeof(citizen) != 'undefined') { + .block-citizen-info.span9.box.clearfix + include issue-citizen-info + - } + .block-ideas.box.clearfix - - if(typeof(citizen) != 'undefined' && element.vote.voters.indexOf(citizen.id) >=0 ) { - - var voted = true; + - if(typeof(citizen) != 'undefined') { + - var voted = false; + - element.vote.voters.some(function(v) { + - if(v.id == citizen.id) { + - voted = true; + - }; + - return voted; + - }) - }; - - if(!voted) { + + - if( (Date.now() > element.discussionEndDate) && (Date.now() < element.votingEndDate) ) { .span9 + //- Change delegation - if(typeof(citizen) != 'undefined') { a.delegate(data-toggle="modal", role="button", href="#delegation") | Prefiero delegar mi voto a otra persona @@ -56,25 +62,67 @@ block content a.total-comments(data-toggle="modal", role="button", href="#choice-full-#{choice.id}") i | 23 - - if(!voted) { + - if(!voted && (Date.now() > element.openForIdeasEndDate) ) { a.vote(href="#") Votar - }; - }); - .span3 - a.create-idea.vote-option(data-toggle="modal", role="button", href='#compose-initiative', data-backdrop="false", data-keyboard="false", rel="tooltip", title="Redactar una iniciativa", data-placement="bottom") - i + - span Crear Propuesta - - } else { + + - if( Date.now() < element.openForIdeasEndDate ) { + .span3 + a.create-idea.vote-option(data-toggle="modal", role="button", href='#compose-initiative', data-backdrop="false", data-keyboard="false", rel="tooltip", title="Redactar una iniciativa", data-placement="bottom") + i + + span Crear Propuesta + - }; + + - } + + //- Preliminar results + - if( voted && (Date.now() < element.votingEndDate) ) { .span9.box.block-results h3.block-title i.icon span Resultados preliminares - - var totalVoters = element.vote.voters.length; - - var voters = totalVoters == 1 ? "Votó $n ciudadano" : "Votaron $n ciudadanos" - h4 - | #{voters.replace('$n',totalVoters)} .box-content.clearfix + h4 + | Hay + span.number= element.vote.voters.length + | votos contados + - element.vote.voters.forEach(function(voter) { + a(href="/profiles/#{voter.id}", rel="tooltip", title="#{voter.fullName}", data-placement="bottom", class="tt") + img(src="#{voter.imageUrlMini}") + - }); + + h4 + span.number= element.vote.voters.length + | votos por delegados + - element.vote.voters.forEach(function(voter) { + a(href="/profiles/#{voter.id}", rel="tooltip", title="#{voter.fullName}", data-placement="bottom", class="tt") + img(src="#{voter.imageUrlMini}") + - }) + + h4 + | y + span.number= element.vote.voters.length + | votos unipersonales + - element.vote.voters.forEach(function(voter) { + a(href="/profiles/#{voter.id}", rel="tooltip", title="#{voter.fullName}", data-placement="bottom", class="tt") + img(src="#{voter.imageUrlMini}") + - }) + - } + + //- Final results + - if(Date.now() > element.votingEndDate) { + .span9.box.block-results + h3.block-title + i.icon + span Resultados final + - var totalVoters = element.vote.voters.length; + .box-content.clearfix + h4 + span.number= element.vote.voters.length + | votos contados + - element.vote.choices.forEach(function(choice) { .partial(style="width:100%;float:left;") - var partial = choice.result / (totalVoters || 1) * 100; @@ -83,7 +131,7 @@ block content a(data-toggle="modal", role="button", href="#choice-full-#{choice.id}") | Leer... .bar(style="width:#{partial.toFixed(0)}%;") - span + span.tt(rel="tooltip", data-placement="top", title="#{choice.result} votos") | #{partial.toFixed(0)} % - }); - };