Feature: add account switch

This commit is contained in:
2015-10-09 12:59:38 +00:00
parent 13b0a57600
commit 69570d8702
8 changed files with 173 additions and 27 deletions

View File

@@ -37,7 +37,7 @@ module.exports = function(config) {
// preprocess matching files before serving them to the browser // preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: { preprocessors: {
'public/js/**/*.js': ['coverage'], 'public/js/**/!(global.controller).js': ['coverage'],
'public/**/*.controller.js': ['coverage'], 'public/**/*.controller.js': ['coverage'],
}, },

View File

@@ -12,7 +12,7 @@
if( !input ) { if( !input ) {
return ''; return '';
} }
var category = categories.filter(function(elt, idx) { var category = categories.filter(function(elt) {
return elt._id === input; return elt._id === input;
}); });
if( category.length > 0 ) { if( category.length > 0 ) {
@@ -28,7 +28,7 @@
return ''; return '';
} }
var category = categories.filter(function(elt, idx) { var category = categories.filter(function(elt) {
return elt._id === category_id; return elt._id === category_id;
})[0]; })[0];
@@ -36,7 +36,7 @@
return ''; return '';
} }
var res = category.sub_categories.filter( function(elt, idx) { var res = category.sub_categories.filter( function(elt) {
return elt._id === input; return elt._id === input;
}); });
if( res.length === 1 ) { if( res.length === 1 ) {
@@ -47,9 +47,9 @@
}; };
} }
AccountController.$inject = ['$scope', '$location', '$routeParams', 'FlashService', 'AccountService']; AccountController.$inject = ['$scope', '$rootScope', '$routeParams', 'FlashService', 'AccountsService', 'AccountService'];
function AccountController($scope, $location, $routeParams, FlashService, AccountService) { function AccountController($scope, $rootScope, $routeParams, FlashService, AccountsService, AccountService) {
var vm = this; var vm = this;
$scope.calendar = { $scope.calendar = {
@@ -62,13 +62,14 @@
$scope.calendar.opened[which] = true; $scope.calendar.opened[which] = true;
} }
}; };
vm.dataLoading = false; vm.dataLoading = false;
vm.accounts = undefined;
vm.account = undefined;
vm.entries = []; vm.entries = [];
vm.balance = undefined; vm.balance = undefined;
vm.categories = []; vm.categories = [];
vm.sub_categories = []; vm.sub_categories = [];
vm.account = undefined;
vm.create = create; vm.create = create;
vm.drop = drop; vm.drop = drop;
vm.edit = edit; vm.edit = edit;
@@ -77,6 +78,8 @@
vm.disabledSubCategories = false; vm.disabledSubCategories = false;
vm.edit_sub_categories = []; vm.edit_sub_categories = [];
$rootScope.current_account = $routeParams.account_id;
(function init() { (function init() {
vm.dataLoading = true; vm.dataLoading = true;
AccountService.details($routeParams.account_id) AccountService.details($routeParams.account_id)
@@ -99,6 +102,14 @@
FlashService.error(response.message); FlashService.error(response.message);
} }
}); });
AccountsService.list()
.then(function(response) {
if( response.success ) {
$rootScope.accounts = response.accounts
} else {
FlashService.error(response.message);
}
});
})(); })();
function create() { function create() {

View File

@@ -5,9 +5,9 @@
.module('cloudbudget') .module('cloudbudget')
.controller('AccountsController', AccountsController); .controller('AccountsController', AccountsController);
AccountsController.$inject = ['$scope', '$location', '$rootScope', 'FlashService', 'AccountsService']; AccountsController.$inject = ['$scope', '$location', 'FlashService', 'AccountsService'];
function AccountsController($scope, $location, $rootScope, FlashService, AccountsService) { function AccountsController($scope, $location, FlashService, AccountsService) {
var vm = this; var vm = this;
vm.dataLoading = false; vm.dataLoading = false;

View File

@@ -14,20 +14,29 @@
<link rel="stylesheet" href="/css/cloudbudget.css" type="text/css" /> <link rel="stylesheet" href="/css/cloudbudget.css" type="text/css" />
</head> </head>
<body> <body ng-controller="GlobalController as globalController">
<div class="container"> <nav class="navbar navbar-inverse">
<nav class="navbar navbar-inverse"> <div class="container-fluid">
<div class="navbar-header"> <div class="navbar-header">
<a class="navbar-brand" href="/">CloudBudget</a> <a class="navbar-brand" href="/">CloudBudget</a>
</div> </div>
<ul class="nav navbar-nav"> <form class="nav navbar-left navbar-form">
<li><a href="/login" ng-hide="{{globals.user}}">Login</a></li> <label for=""current_account>Account</label>
<li><a href="/logout" ng-show="{{globals.user}}">Logout</a></li> <select name="current_account" id="current_account" class="form-control input-sm" ng-model="globalController.current_account" ng-change="globalController.change_account($data)">
<option value="{{account._id}}" ng-repeat="account in accounts" >{{account.name}}</option>
</select>
</form>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="/login" ng-hide="{{globals.user}}">Login</a>
<a href="/logout" ng-show="{{globals.user}}">Logout</a>
</li>
</ul> </ul>
</nav> </div>
</div> </nav>
<div growl></div>
<div growl></div>
<div ng-view></div> <div ng-view></div>
<div class="credits text-center"> <div class="credits text-center">
@@ -55,6 +64,7 @@
<script type="text/javascript" src="/js/services/accounts.service.js"></script> <script type="text/javascript" src="/js/services/accounts.service.js"></script>
<script type="text/javascript" src="/js/services/account.service.js"></script> <script type="text/javascript" src="/js/services/account.service.js"></script>
<script type="text/javascript" src="/js/global.controller.js"></script>
<script type="text/javascript" src="/home/home.controller.js"></script> <script type="text/javascript" src="/home/home.controller.js"></script>
<script type="text/javascript" src="/login/login.controller.js"></script> <script type="text/javascript" src="/login/login.controller.js"></script>
<script type="text/javascript" src="/register/register.controller.js"></script> <script type="text/javascript" src="/register/register.controller.js"></script>

View File

@@ -0,0 +1,26 @@
(function() {
'use strict';
angular
.module('cloudbudget')
.controller('GlobalController', GlobalController);
GlobalController.$inject = ['$scope', '$rootScope', '$location'];
function GlobalController($scope, $rootScope, $location) {
var vm = this;
vm.change_account = change_account;
vm.current_account = undefined;
$scope.$watch(function() {
return $rootScope.current_account;
}, function() {
vm.current_account = $rootScope.current_account;
}, true);
function change_account() {
$location.path('/account/' + vm.current_account);
};
}
})();

View File

@@ -48,14 +48,26 @@ describe('AccountController', function() {
"type": "DEPOSIT", "type": "DEPOSIT",
"category": "560a84058812ad8d0ff200f0", "category": "560a84058812ad8d0ff200f0",
"sub_category": "560a84058812ad8d0ff200f3" "sub_category": "560a84058812ad8d0ff200f3"
}; },
DEFAULT_ACCOUNTS = [
{
"_id": "560a84058812ad8d0ff200ee",
"name": "foo",
"reference": "baz",
"user_id": "55b78934d2a706265ea28e9c"
}, {
"_id": "560a7ad08812ad8d0ff20068",
"name": "bar",
"user_id": "55b78934d2a706265ea28e9c"
}
];
beforeEach(module('cloudbudget')); beforeEach(module('cloudbudget'));
beforeEach(inject(function ( _$rootScope_, _$httpBackend_, $controller, _$location_, $routeParams, _$timeout_, _$filter_, _AccountService_, _FlashService_, _apiRoutes_) { beforeEach(inject(function ( _$rootScope_, _$httpBackend_, $controller, _$location_, _$timeout_, _$filter_, _AccountService_, _FlashService_, _apiRoutes_) {
$location = _$location_; $location = _$location_;
$httpBackend = $httpBackend; $httpBackend = _$httpBackend_;
$rootScope = _$rootScope_.$new(); $rootScope = _$rootScope_;
$scope = _$rootScope_.$new(); $scope = _$rootScope_.$new();
$scope.form = { $scope.form = {
$valid: true, $valid: true,
@@ -74,18 +86,21 @@ describe('AccountController', function() {
'$rootScope': $rootScope, '$rootScope': $rootScope,
'$routeParams': {account_id: DEFAULT_ACCOUNT._id}, '$routeParams': {account_id: DEFAULT_ACCOUNT._id},
FlashService: _FlashService_, FlashService: _FlashService_,
AccountService: _AccountService_, AccountService: _AccountService_
}); });
}; };
})); }));
describe('init()', function() { describe('init()', function() {
it('should init successfully', inject(function($httpBackend) { it('should init successfully', inject(function($httpBackend, $rootScope) {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id) $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
.respond(DEFAULT_ACCOUNT); .respond(DEFAULT_ACCOUNT);
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100}); .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
@@ -95,15 +110,19 @@ describe('AccountController', function() {
accountController.account._id.should.be.equal(DEFAULT_ACCOUNT._id); accountController.account._id.should.be.equal(DEFAULT_ACCOUNT._id);
accountController.entries.should.be.instanceof(Array).and.have.lengthOf(1); accountController.entries.should.be.instanceof(Array).and.have.lengthOf(1);
accountController.balance.should.be.equal(100); accountController.balance.should.be.equal(100);
$rootScope.accounts.should.be.instanceof(Array).and.have.lengthOf(2);
})); }));
it('should fail to init', inject(function($httpBackend) { it('should fail to init', inject(function($httpBackend, $rootScope) {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id) $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
.respond(400); .respond(400);
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond(400); .respond(400);
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(400);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
$timeout.flush(); $timeout.flush();
@@ -111,6 +130,7 @@ describe('AccountController', function() {
should.not.exist(accountController.account); should.not.exist(accountController.account);
accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0); accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
should.not.exist(accountController.balance); should.not.exist(accountController.balance);
should.not.exist($rootScope.accounts);
})); }));
}); });
@@ -122,6 +142,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[], balance: 0}); .respond({entry: null, entries:[], balance: 0});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
$timeout.flush(); $timeout.flush();
@@ -150,6 +173,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[], balance: 0}); .respond({entry: null, entries:[], balance: 0});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
@@ -179,6 +205,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100}); .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
@@ -203,6 +232,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[DEFAULT_ACCOUNT], balance: 100}); .respond({entry: null, entries:[DEFAULT_ACCOUNT], balance: 100});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
$timeout.flush(); $timeout.flush();
@@ -228,6 +260,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100}); .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
$timeout.flush(); $timeout.flush();
@@ -264,6 +299,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100}); .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
$timeout.flush(); $timeout.flush();
@@ -361,6 +399,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[], balance: 0}); .respond({entry: null, entries:[], balance: 0});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
$timeout.flush(); $timeout.flush();
@@ -378,6 +419,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[], balance: 0}); .respond({entry: null, entries:[], balance: 0});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
$timeout.flush(); $timeout.flush();
@@ -395,6 +439,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[], balance: 0}); .respond({entry: null, entries:[], balance: 0});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
$timeout.flush(); $timeout.flush();
@@ -414,6 +461,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[], balance: 0}); .respond({entry: null, entries:[], balance: 0});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
$timeout.flush(); $timeout.flush();
@@ -431,6 +481,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[], balance: 0}); .respond({entry: null, entries:[], balance: 0});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
$timeout.flush(); $timeout.flush();
@@ -448,6 +501,9 @@ describe('AccountController', function() {
$httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries') $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
.respond({entry: null, entries:[], balance: 0}); .respond({entry: null, entries:[], balance: 0});
$httpBackend.expect('GET', apiRoutes.accounts )
.respond(DEFAULT_ACCOUNTS);
var accountController = createController(); var accountController = createController();
$httpBackend.flush(); $httpBackend.flush();
$timeout.flush(); $timeout.flush();

View File

@@ -0,0 +1,43 @@
describe('GlobalController', function() {
var $location,
$rootScope,
$scope,
createController,
DEFAULT_ACCOUNTS = [
{
"_id": "560a84058812ad8d0ff200ee",
"name": "foo",
"reference": "baz",
"user_id": "55b78934d2a706265ea28e9c"
}, {
"_id": "560a7ad08812ad8d0ff20068",
"name": "bar",
"user_id": "55b78934d2a706265ea28e9c"
}
];
beforeEach(module('cloudbudget'));
beforeEach(inject(function ( _$rootScope_, $controller, _$location_) {
$location = _$location_;
$rootScope = _$rootScope_;
$scope = _$rootScope_.$new();
createController = function() {
return $controller('GlobalController', {
'$scope': $scope,
'$location': $location,
'$rootScope': $rootScope
});
};
}));
it('should init successfully', inject(function($location, $rootScope) {
var globalController = createController();
globalController.current_account = '560a84058812ad8d0ff200ee';
globalController.change_account();
$location.path().should.be.equal('/account/560a84058812ad8d0ff200ee')
}));
});

View File

@@ -14,7 +14,7 @@ describe('HomeController', function() {
var homeController = $controller('HomeController', { var homeController = $controller('HomeController', {
'$rootScope': rootScope '$rootScope': rootScope
}); });
homeController.firstname = 'George'; homeController.firstname = 'George';
homeController.lastname = 'Harrison'; homeController.lastname = 'Harrison';
homeController.getFullname().should.be.equal('George Harrison'); homeController.getFullname().should.be.equal('George Harrison');