diff --git a/client/app/account/dashboard/dashboard.jade b/client/app/account/dashboard/dashboard.jade index aaae548..c368332 100644 --- a/client/app/account/dashboard/dashboard.jade +++ b/client/app/account/dashboard/dashboard.jade @@ -28,7 +28,7 @@ div(ng-include='"components/navbar/navbar.html"') span.pull-right.label.label-default {{contrib.value.contributions.length}} contributions div.panel-body span.badge.badge-success(ng-repeat="value in contrib.value.contributions track by ($index+contrib.value.slug)", style="margin-right:5px;") - span {{value}}    + span {{value | toBTC}}    i.fa.fa-btc .col-md-6 .row diff --git a/client/app/projects/EditProject/EditProject.controller.js b/client/app/projects/EditProject/EditProject.controller.js index 2889db1..f1a375f 100644 --- a/client/app/projects/EditProject/EditProject.controller.js +++ b/client/app/projects/EditProject/EditProject.controller.js @@ -8,13 +8,19 @@ angular.module('bitCrowdFundsApp') var currentUser = Auth.getCurrentUser(); $scope.currentProject = ProjectRes.get({name: $scope.nameProject}); + $scope.isAdmin = function () + { + if (currentUser.role === 'admin') + return true; + return false; + }; + //var $scope.asAccess = false; if (!(currentUser.role == 'admin') && !(currentUser.name == $scope.currentProject.name)) $location.path( "/projects" ); $scope.editProject = function () { - console.log($scope.currentProject); /*var res = ProjectRes.update($scope.nameProject, $scope.currentProject); if (!res) return $scope.message = "Fail to update"; @@ -29,7 +35,7 @@ angular.module('bitCrowdFundsApp') }, project: $scope.currentProject }; - $http.put('api/projects/'+$scope.nameProject, obj) + $http.put('api/projects/'+$scope.currentProject.slug, obj) .success(function(data, status, headers, config) { $scope.message = "Project updated"; @@ -38,5 +44,21 @@ angular.module('bitCrowdFundsApp') { $scope.message = "Fail to update"; }); - } + }; + + $scope.returnFunds = function () + { + if (currentUser.role === 'admin') + { + $http.put('api/projects/'+$scope.currentProject.slug+'/returnFunds', {}) + .success(function(data, status, headers, config) + { + $scope.message = 'Funds returned'; + }) + .error(function(data, status, headers, config) + { + $scope.message = 'Fail to return Funds'; + }); + } + }; }); diff --git a/client/app/projects/EditProject/editproject.jade b/client/app/projects/EditProject/editproject.jade index b6d00ee..2cd57ea 100644 --- a/client/app/projects/EditProject/editproject.jade +++ b/client/app/projects/EditProject/editproject.jade @@ -2,7 +2,8 @@ div(ng-include='"components/navbar/navbar.html"') .container .row.row-centered .col-sm-12 - h1 {{currentProject.name}} + h1 {{currentProject.name}}   + button.btn-danger(ng-click='returnFunds()', ng-show='isAdmin()') Return Funds br .col-sm-6.col-centered.well.col-sm-offset-3 form.form(name='editProjectForm', novalidate='') diff --git a/client/app/projects/project/project.controller.js b/client/app/projects/project/project.controller.js index 16b26da..00937b6 100644 --- a/client/app/projects/project/project.controller.js +++ b/client/app/projects/project/project.controller.js @@ -19,8 +19,10 @@ angular.module('bitCrowdFundsApp') $scope.contribute = function() { - var repl = $scope.contribAmount.replace(',', '.'); - var contribSatoshi = parseFloat(repl*100000000); + var repl = $scope.contribAmount.replace(',', '.'); + var contribSatoshi = parseFloat(repl*100000000); + console.log($scope.currentProject.slug); + $http.post('api/projects/'+$scope.currentProject.slug+'/contrib', {userId: $scope.currentUser._id, userName: $scope.currentUser.name, amount: contribSatoshi}) .success(function(data, status, headers, config) { diff --git a/package.json b/package.json index 75d18bb..0d13317 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.0.0", "main": "server/app.js", "dependencies": { + "async": "^0.9.0", "bitcoin-address": "^0.3.0", "bitcoinjs-lib": "^1.2.0", "body-parser": "~1.5.0", diff --git a/server/api/project/index.js b/server/api/project/index.js index 164fb54..5645261 100644 --- a/server/api/project/index.js +++ b/server/api/project/index.js @@ -11,9 +11,10 @@ router.get('/', controller.index); router.get('/my/projects', auth.isAuthenticated(), controller.myProjects); router.get('/:name', controller.show); router.post('/', auth.isAuthenticated(), controller.create); -router.post('/:name/contrib', controller.contribute); +router.post('/:name/contrib', auth.isAuthenticated, controller.contribute); router.put('/:id', auth.isAuthenticated(), controller.update); router.patch('/:id', auth.isAuthenticated(), controller.update); router.delete('/:id', auth.hasRole('admin'), controller.destroy); +router.put('/:name/returnFunds', auth.hasRole('admin'), controller.returnFunds); module.exports = router; diff --git a/server/api/project/project.controller.js b/server/api/project/project.controller.js index cfd57a5..a48434a 100644 --- a/server/api/project/project.controller.js +++ b/server/api/project/project.controller.js @@ -4,6 +4,7 @@ var _ = require('lodash'); var Project = require('./project.model'); var User = require('../user/user.model'); var bitcoin = require('bitcoin-address'); +var async = require('async'); //var UserC = require('../user/user.controller'); @@ -115,8 +116,10 @@ function handleError(res, err) { exports.contribute = function(req, res) { + console.log("FUCKIT"); var toContrib = Number(req.body.amount); var nameProj = req.params.name;//.replace("%20", " "); + console.log(nameProj); User.findById(req.body.userId, function (err, user) { if (err || !user) @@ -140,6 +143,7 @@ exports.contribute = function(req, res) project.contributors.push({contribId: user._id, amount: toContrib}); project.save(function (err) { + console.log("TAMERELACHIENNE"); console.log(err); if (err) return handleError(res, err); @@ -153,5 +157,45 @@ exports.contribute = function(req, res) return handleError(res, 'Amout > Balance'); } }); +}; -} +exports.returnFunds = function(req, res) +{ + //TODO : check if project date is passed or do nothing ? + + var name = req.params.name; + console.log('HERE'); + Project.findOne({slug: name}, function (err, project) + { + if (err || !project) + return handleError(res, err); + console.log(project); + async.eachSeries(project.contributors, function (element, callback) + { + console.log('here'); + console.log(element); + User.findById(element.contribId, function (err, user) + { + if (err || !user) + callback(err); + user.balance += element.amount; + user.save(callback(err)); + }); + }, function (err) + { + if (err) + return handleError(res, err); + project.contributorsOld = project.contributors.concat(project.contributors); + project.amountRaised = 0; + project.contributors = []; + //project.active = false; + project.save(function (err) + { + if (err) + return handleError(res, err); + res.json(200); + }); + //res.json(200); + }) + }); +};