diff --git a/src/accounts/account.config.js b/src/accounts/account.config.js new file mode 100644 index 0000000..e3d412b --- /dev/null +++ b/src/accounts/account.config.js @@ -0,0 +1,4 @@ +module.exports = function($resourceProvider) { + // Keep trailing slashes to avoid redirect by flask. + $resourceProvider.defaults.stripTrailingSlashes = false; +}; diff --git a/src/accounts/account.controller.js b/src/accounts/account.controller.js new file mode 100644 index 0000000..b7ddf96 --- /dev/null +++ b/src/accounts/account.controller.js @@ -0,0 +1,149 @@ +var accountFormTmpl = require('./account.form.tmpl.html'), + accountDeleteTmpl = require('./account.delete.tmpl.html'); + +module.exports = function(Account, Notification, $log, $q, $modal) { + var vm = this; + + /* + * Return the class for an account current value compared to authorized + * overdraft. + */ + vm.rowClass = function(account) { + // eslint-disable-next-line camelcase + if (!account || !account.authorized_overdraft || !account.current) { + return; + } + + // eslint-disable-next-line camelcase + if (account.current < account.authorized_overdraft) { + return 'danger'; + } else if (account.current < 0) { + return 'warning'; + } + }; + + /* + * Return the class for a value compared to account authorized overdraft. + */ + vm.valueClass = function(account, value) { + if (!account || !value) { + return; + } + + // eslint-disable-next-line camelcase + if (value < account.authorized_overdraft) { + return 'text-danger'; + } else if (value < 0) { + return 'text-warning'; + } + }; + + /* + * Add an empty account. + */ + vm.add = function() { + var account = new Account({ + // eslint-disable-next-line camelcase + authorized_overdraft: 0 + }); + + // Insert account at the begining of the array. + return vm.modify(account); + }; + + /* + * Save account. + */ + vm.save = function(account) { + return account.$save().then(function(data) { + Notification.success('Account #' + data.id + ' saved.'); + + vm.accounts = Account.query(); + + return data; + }, function(result){ + $log.error('Error while saving account', account, result); + + Notification.error( + 'Error while saving account: ' + result.message + ); + + return $q.reject(result); + }); + }; + + vm.confirmDelete = function(account) { + var title = "Delete account #" + account.id; + + $modal({ + templateUrl: accountDeleteTmpl, + controller: function($scope, title, account, $delete) { + $scope.title = title; + $scope.account = account; + $scope.$delete = function() { + $scope.$hide(); + $delete($scope.account); + }; + }, + locals: { + title: title, + account: account, + $delete: vm.delete + } + }); + }; + + /* + * Delete an account. + */ + vm.delete = function(account) { + var id = account.id; + + return account.$delete().then(function() { + Notification.success('account #' + id + ' deleted.'); + + vm.accounts = Account.query(); + + return account; + }, function(result) { + Notification.error( + 'An error occurred while trying to delete account #' + + id + ':
' + result + ); + }); + }; + + /* + * Open the popup to modify the account, save it on confirm. + */ + vm.modify = function(account) { + // FIXME Alexis Lahouze 2017-06-15 i18n + var title = "Account"; + + if (account.id) { + title = title + " #" + account.id; + } + + $modal({ + templateUrl: accountFormTmpl, + controller: function($scope, title, account, $save) { + $scope.title = title; + $scope.account = account; + $scope.account.authorized_overdraft *= -1; + $scope.$save = function() { + $scope.$hide(); + $scope.account.authorized_overdraft *= -1; + $save($scope.account); + }; + }, + locals: { + title: title, + account: account, + $save: vm.save + } + }); + }; + + // Load accounts. + vm.accounts = Account.query(); +}; diff --git a/src/accounts/account.factory.js b/src/accounts/account.factory.js new file mode 100644 index 0000000..e604e97 --- /dev/null +++ b/src/accounts/account.factory.js @@ -0,0 +1,26 @@ +module.exports = function($resource) { + var Account = $resource( + '/api/account/:id', { + id: '@id' + } + ); + + Account.prototype.getBalances = function() { + var Balances = $resource('/api/account/:id/balances', {id: this.id}); + + this.balances = Balances.get(); + }; + + Account.prototype.getBalance = function(begin, end) { + var Balance = $resource( + '/api/account/:id/balance', { + id: this.id, + begin: begin.format('YYYY-MM-DD'), + end: end.format('YYYY-MM-DD') + }); + + this.balance = Balance.get(); + }; + + return Account; +}; diff --git a/src/accounts/index.js b/src/accounts/index.js index f89971e..bee5a9f 100644 --- a/src/accounts/index.js +++ b/src/accounts/index.js @@ -26,8 +26,9 @@ var ngResource = require('angular-resource'), ngUiNotification = require('angular-ui-notification'), ngStrap = require('angular-strap'); -var accountFormTmpl = require('./account.form.tmpl.html'), - accountDeleteTmpl = require('./account.delete.tmpl.html'); +var AccountFactory = require('./account.factory.js'); +var AccountConfig = require('./account.config.js'); +var AccountController = require('./account.controller.js'); module.exports = angular.module('accountant.accounts', [ ngResource, @@ -37,183 +38,10 @@ module.exports = angular.module('accountant.accounts', [ ngStrap, ]) - .config(function($resourceProvider) { - // Keep trailing slashes to avoid redirect by flask.. - $resourceProvider.defaults.stripTrailingSlashes = false; - }) + .config(AccountConfig) - .factory('Account', function($resource) { - var Account = $resource( - '/api/account/:id', { - id: '@id' - } - ); + .factory('Account', AccountFactory) - Account.prototype.getBalances = function() { - var Balances = $resource('/api/account/:id/balances', {id: this.id}); - - this.balances = Balances.get(); - }; - - Account.prototype.getBalance = function(begin, end) { - var Balance = $resource( - '/api/account/:id/balance', { - id: this.id, - begin: begin.format('YYYY-MM-DD'), - end: end.format('YYYY-MM-DD') - }); - - this.balance = Balance.get(); - }; - - return Account; - }) - - .controller('AccountController', function(Account, Notification, $log, $q, $modal) { - var vm = this; - - /* - * Return the class for an account current value compared to authorized - * overdraft. - */ - vm.rowClass = function(account) { - // eslint-disable-next-line camelcase - if (!account || !account.authorized_overdraft || !account.current) { - return; - } - - // eslint-disable-next-line camelcase - if (account.current < account.authorized_overdraft) { - return 'danger'; - } else if (account.current < 0) { - return 'warning'; - } - }; - - /* - * Return the class for a value compared to account authorized overdraft. - */ - vm.valueClass = function(account, value) { - if (!account || !value) { - return; - } - - // eslint-disable-next-line camelcase - if (value < account.authorized_overdraft) { - return 'text-danger'; - } else if (value < 0) { - return 'text-warning'; - } - }; - - /* - * Add an empty account. - */ - vm.add = function() { - var account = new Account({ - // eslint-disable-next-line camelcase - authorized_overdraft: 0 - }); - - // Insert account at the begining of the array. - return vm.modify(account); - }; - - /* - * Save account. - */ - vm.save = function(account) { - return account.$save().then(function(data) { - Notification.success('Account #' + data.id + ' saved.'); - - vm.accounts = Account.query(); - - return data; - }, function(result){ - $log.error('Error while saving account', account, result); - - Notification.error( - 'Error while saving account: ' + result.message - ); - - return $q.reject(result); - }); - }; - - vm.confirmDelete = function(account) { - var title = "Delete account #" + account.id; - - $modal({ - templateUrl: accountDeleteTmpl, - controller: function($scope, title, account, $delete) { - $scope.title = title; - $scope.account = account; - $scope.$delete = function() { - $scope.$hide(); - $delete($scope.account); - }; - }, - locals: { - title: title, - account: account, - $delete: vm.delete - } - }); - }; - - /* - * Delete an account. - */ - vm.delete = function(account) { - var id = account.id; - - return account.$delete().then(function() { - Notification.success('account #' + id + ' deleted.'); - - vm.accounts = Account.query(); - - return account; - }, function(result) { - Notification.error( - 'An error occurred while trying to delete account #' + - id + ':
' + result - ); - }); - }; - - /* - * Open the popup to modify the account, save it on confirm. - */ - vm.modify = function(account) { - // FIXME Alexis Lahouze 2017-06-15 i18n - var title = "Account"; - - if (account.id) { - title = title + " #" + account.id; - } - - $modal({ - templateUrl: accountFormTmpl, - controller: function($scope, title, account, $save) { - $scope.title = title; - $scope.account = account; - $scope.account.authorized_overdraft *= -1; - $scope.$save = function() { - $scope.$hide(); - $scope.account.authorized_overdraft *= -1; - $save($scope.account); - }; - }, - locals: { - title: title, - account: account, - $save: vm.save - } - }); - }; - - // Load accounts. - vm.accounts = Account.query(); - }) + .controller('AccountController', AccountController) .name;