From a8f97f0c4fe2bea23be66965c250b257ace04971 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Fri, 24 May 2013 22:49:33 +0200 Subject: fixed settlement in projects on dashboard. improves on #9 and closes #29 --- controllers/system.js | 32 ++++++++++++++++++++++++++------ views/dashboard.ejs | 23 +++-------------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/controllers/system.js b/controllers/system.js index c083835..c29e06f 100644 --- a/controllers/system.js +++ b/controllers/system.js @@ -99,19 +99,39 @@ exports.dashboard = function(req, res) { Access.loadUser(req.user._id, function(err, projects) { if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack }); var projectIDs = []; - projects.forEach(function(project) { projectIDs.push(project.project._id); }); + var pro = { project: [] }; + projects.forEach(function(project) { + projectIDs.push(project.project._id); + pro.project[project.project._id] = { + total: 0 // total for project + , user: 0 // what req-user has spent on project + , users: 0 // number of users on project + }; + }); Access.loadProjects(projectIDs, function(err, participants) { if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack }); + participants.forEach(function(p) { + pro.project[p.project].users++; + }); pPost.loadByProjects(projectIDs, function(err, posts) { if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack }); Access.loadProjects(projectIDs, function(err, participants) { if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack }); + + // FUN FUN FUN CALCULATIONS + + posts.forEach(function(p) { + if (String(p.user._id) === String(req.user._id)) pro.project[p.project._id].user += p.value; + pro.project[p.project._id].total += p.value; + }); + res.render('dashboard', { - title: 'Dashboard', - user: req.user, - projects: projects, - posts: posts, - participants: participants + title: 'Dashboard' + , user: req.user + , projects: projects + , posts: posts + , participants: participants + , pro: pro }); }); }); diff --git a/views/dashboard.ejs b/views/dashboard.ejs index 21aef0d..c759add 100644 --- a/views/dashboard.ejs +++ b/views/dashboard.ejs @@ -32,7 +32,8 @@
- Oppgjør: -200 kr + <% s = (pro.project[projects.project._id].user - (pro.project[projects.project._id].total / pro.project[projects.project._id].users)).toFixed(2); %> + Oppgjør: <%- s >= 0 ? '' + s : '' + s %> <%= projects.project.currency %>
@@ -55,25 +56,7 @@
-
-
-

Status total

-
-
-
- - - - - - -
300040001000
-
-
-

Her vises hvor mye andre skylder deg totalt, hvor mye du skylder andre totalt, og differansen mellom disse to tallene

-
-
-
+

Status total

-- cgit v1.2.3