Skip to content

Commit

Permalink
feat(Services): implement PrototypeApiService
Browse files Browse the repository at this point in the history
This commit implements PrototypeApiService to remove boilerplate code
from our services and improve code documentation. To demonstrate how the
PrototypeApiService works, the FiscalService has been created to inherit
directly from it.  The CashPayments service also is updated to inherit
from the PrototypeApiService.

This also increments mocha's versioning since the recent bug fix.

Finally, a bug with the bhCurrencySelect has been fixed.

Closes #214.
  • Loading branch information
jniles committed May 27, 2016
1 parent edfc591 commit 6f46be2
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 109 deletions.
6 changes: 2 additions & 4 deletions client/src/js/components/bhCurrencySelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function bhCurrencySelect($scope, Currencies) {
$scope.$watchCollection(function () {
return $ctrl.disableIds;
}, function (array) {
array = array || [];
if (!array) { return; }

// loop through the currencies, disabling the currencies with ids in the
// disabledIds array.
Expand All @@ -97,8 +97,6 @@ function bhCurrencySelect($scope, Currencies) {

// if the two array lengths are equal, it means every currency is disabled
$ctrl.allDisabled = ($ctrl.currencies.length === array.length);
if ($ctrl.allDisabled) {
$ctrl.form.$setValidity('currencies', false);
}
$ctrl.form.$setValidity('currencies', $ctrl.allDisabled);
});
}
149 changes: 45 additions & 104 deletions client/src/js/services/CashService.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,30 @@
/**
* CashService
*
* This service interacts with the server-side /cash API.
*
* @module services/CashService
*/

angular.module('bhima.services')
.service('CashService', CashService);

CashService.$inject = [ '$http', 'util', 'ExchangeRateService', 'uuid', 'SessionService' ];
CashService.$inject = [
'PrototypeApiService', 'ExchangeRateService', 'SessionService'
];

/**
* A service to interact with the server-side /cash API.
* @class CashService
*
* @constructor CashService
* @description
* A service to interact with the server-side /cash API.
*/
function CashService($http, util, Exchange, uuid, sessionService ) {
function CashService(PrototypeApiService, Exchange, Session) {
var service = this;
var baseUrl = '/cash/';

service.read = read;
// inherit prototype API methods
angular.extend(service, PrototypeApiService);

// bind the base url
service.url = '/cash/';

// custom methods
service.create = create;
service.update = update;
service.delete = remove;
service.reference = reference;
service.getTransferRecord = getTransferRecord;

/**
* Fetchs cash payments from the server. If an uuid is specified, will read a
* single JSON out of the service, otherwise, fetches every cash payment in the
* database.
*
* @method read
* @param {string} uuid (optional) - a cash payment UUID
* @param {object} options - parameters to be passed as HTTP query strings
* @returns {object|array} payments One or more cash payments.
*/
function read(uuid, options) {
var target = baseUrl.concat(uuid || '');

return $http.get(target, options)
.then(util.unwrapHttpResponse);
}

/**
* Cash Payments can be made to multiple invoices. This function loops
* though the invoices in selected order, allocating the global amount to each
Expand Down Expand Up @@ -83,9 +64,11 @@ function CashService($http, util, Exchange, uuid, sessionService ) {
}

/**
* @method create
*
* @description
* Creates a cash payment from a JSON passed from a form.
*
* @method create
* @param {object} data A JSON object containing the cash payment record defn
* @returns {object} payment A promise resolved with the database uuid.
*/
Expand All @@ -109,101 +92,59 @@ function CashService($http, util, Exchange, uuid, sessionService ) {
// remove data.invoices property before submission to the server
delete data.invoices;

return $http.post(baseUrl, { payment : data })
.then(util.unwrapHttpResponse);
}

/**
* Fetchs cash payments from the server. If an id is specified, will read a single
* JSON out of the service, otherwise, fetches every cash payment in the database.
*
* @method update
* @param {string} uuid A cash payment UUID
* @returns {object} payments A promise containing the entire cash payment record
*/
function update(uuid, data) {
var target = baseUrl.concat(uuid);
return $http.put(target, data)
.then(util.unwrapHttpResponse);
}

/**
* Deletes cash payments from the database based on the id passed in.
*
* @method delete
* @param {string} uuid A cash payment UUID
* @returns {promise} promise - a resolved or rejected empty promise
*/
function remove(uuid) {
var target = baseUrl.concat(uuid);

// Technically, we are not returning any body, so unwrappHttpResponse does
// not do anything. However, to keep uniformity with the API, I've included
// it.
return $http.delete(target)
.then(util.unwrapHttpResponse);
// call the prototype create method with the formatted data
return PrototypeApiService.create.call(service, { payment : data });
}

/**
* Searches for a cash payment by its reference.
*
* @method reference
* @param {string} reference
* @returns {promise} promise - a resolved or rejected promise with the
* @param {String} reference
* @returns {Promise} promise - a resolved or rejected promise with the
* result sent from the server.
*/
function reference(ref) {
var target = baseUrl + 'references/' + ref;

return $http.get(target)
.then(util.unwrapHttpResponse);
var target = service.url.concat('references/', ref);
return this.$http.get(target)
.then(this.util.unwrapHttpResponse);
}

/**
* This methode is responsible to create a voucher object and it back
**/
function getTransferRecord (cashAccountCurrency, amount, currency_id){
* This method is responsible to create a voucher object and it back
*/
function getTransferRecord(cashAccountCurrency, amount, currencyId) {
/**
* The date field is set at the server side
* @todo the date in timestamp type in the database
*/
var voucher = {
uuid : uuid(),
project_id : sessionService.project.id,
currency_id : currency_id,
project_id : Session.project.id,
currency_id : currencyId,
amount : amount,
description : generateTransferDescription(),
user_id : sessionService.user.id,
items : []
};

var cashVoucherLine = {
uuid : uuid (),
account_id : cashAccountCurrency.account_id,
debit : 0,
credit : amount,
voucher_uuid : voucher.uuid
user_id : Session.user.id,

// two lines (debit and credit) to be recorded in the database
items : [{
account_id : cashAccountCurrency.account_id,
debit : 0,
credit : amount,
}, {
account_id : cashAccountCurrency.transfer_account_id,
debit : amount,
credit : 0,
}]
};

var transferVoucherLine = {
uuid : uuid (),
account_id : cashAccountCurrency.transfer_account_id,
debit : amount,
credit : 0,
voucher_uuid : voucher.uuid
};

voucher.items.push(cashVoucherLine);
voucher.items.push(transferVoucherLine);

return { voucher : voucher };
}

/**
* This methode is responsible to generate a description for the transfer operation
* @private
**/
* This method is responsible to generate a description for the transfer operation.
* @private
*/
function generateTransferDescription (){
return ['Transfer voucher', new Date().toISOString().slice(0, 10), sessionService.user.id].join('/');
return 'Transfer Voucher/'.concat(new Date().toISOString().slice(0, 10), '/', Session.user.id);
}
}
22 changes: 22 additions & 0 deletions client/src/js/services/FiscalYearService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
angular.module('bhima.services')
.service('FiscalService', FiscalService);

FiscalService.$inject = ['PrototypeApiService'];

/**
* Fiscal Service
*
* This service is responsible for loading the Fiscal Years and Periods, as well
* as providing metadata like period totals, opening balances and such.
*/
function FiscalService(PrototypeApiService) {
var service = this;

// inherit from the PrototypeApiService
angular.extend(service, PrototypeApiService);

// the service URL
service.url = '/fiscal/';

return service;
}
14 changes: 14 additions & 0 deletions client/src/js/services/PeriodService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
angular.module('bhima.services')
.service('PeriodService', PeriodService);

PeriodService.$inject = ['PrototypeApiService'];

function PeriodService(PrototypeApiService) {
var service = this;

// inherit methods from the
angular.extend(service, PrototypeApiService);


return service;
}
Loading

0 comments on commit 6f46be2

Please sign in to comment.