From 2f62192f716147259c904a0d92e4cd1ff728f264 Mon Sep 17 00:00:00 2001 From: Jonathan Niles Date: Wed, 1 Jan 2025 18:37:29 -0600 Subject: [PATCH] chore(accounts): remove tech debt Removes technical debt by removing duplicate code. --- .../modules/accounts/AccountStoreService.js | 6 ++- .../accounts/accountCategories.service.js | 11 ++--- .../modules/accounts/accountTypes.service.js | 18 +-------- client/src/modules/functions/functions.js | 40 +++++++++---------- .../src/modules/functions/functions.routes.js | 32 +++++++-------- .../functions/modals/function.modal.js | 2 +- server/controllers/admin/functions.js | 2 +- test/integration/functions.js | 6 ++- 8 files changed, 52 insertions(+), 65 deletions(-) diff --git a/client/src/modules/accounts/AccountStoreService.js b/client/src/modules/accounts/AccountStoreService.js index c8f41d0b89..75fd17a623 100644 --- a/client/src/modules/accounts/AccountStoreService.js +++ b/client/src/modules/accounts/AccountStoreService.js @@ -15,12 +15,14 @@ function AccountStoreService($q, Accounts, AccountTypes, Store) { const service = this; let initialLoad = true; let initTypeLoad = true; + service.accounts = accountStore; service.types = typeStore; + const accounts = new Store(); const accountTypes = new Store(); - const typeRequest = AccountTypes.getAccountType() + const typeRequest = AccountTypes.read() .then((result) => { accountTypes.setData(result); initTypeLoad = false; @@ -29,7 +31,7 @@ function AccountStoreService($q, Accounts, AccountTypes, Store) { function accountStore(importedAccounts) { if (importedAccounts || initialLoad) { - return Accounts.read(null, { detailed : 1 }, true) + return Accounts.read(null, { detailed: 1 }, true) .then((result) => { accounts.setData(result); initialLoad = false; diff --git a/client/src/modules/accounts/accountCategories.service.js b/client/src/modules/accounts/accountCategories.service.js index fc17f130bc..d8c926c747 100644 --- a/client/src/modules/accounts/accountCategories.service.js +++ b/client/src/modules/accounts/accountCategories.service.js @@ -1,9 +1,7 @@ angular.module('bhima.services') -.service('AccountCategoryService', AccountCategoryService); + .service('AccountCategoryService', AccountCategoryService); -AccountCategoryService.$inject = [ - 'PrototypeApiService', '$http', 'util', -]; +AccountCategoryService.$inject = ['PrototypeApiService']; /** * Account Category Service @@ -11,8 +9,5 @@ AccountCategoryService.$inject = [ * A service wrapper for the /account/categories HTTP endpoint. */ function AccountCategoryService(Api) { - var baseUrl = '/accounts/categories/'; - var service = new Api(baseUrl); - - return service; + return new Api('/accounts/categories/'); } diff --git a/client/src/modules/accounts/accountTypes.service.js b/client/src/modules/accounts/accountTypes.service.js index 62db602fac..bc4713f7ff 100644 --- a/client/src/modules/accounts/accountTypes.service.js +++ b/client/src/modules/accounts/accountTypes.service.js @@ -12,29 +12,15 @@ function AccountTypeService(Api, $http, util) { const baseUrl = '/accounts/types/'; const service = new Api(baseUrl); - service.getAccountType = getAccountType; service.getTypeText = getTypeText; - /** - * @helper - * This Method return a list of Account Type - * */ - function getAccountType(id, params) { - const url = baseUrl.concat(id || ''); - return $http.get(url, { params }) - .then(util.unwrapHttpResponse); - } - /** * @helper * This Method return a text an Account Type * */ function getTypeText(typeId, accountTypes) { - const accountText = accountTypes.filter((item) => { - return item.id === typeId; - }); - - return accountText[0].type; + const needle = accountTypes.find(item => item.id === typeId); + return needle.type; } return service; diff --git a/client/src/modules/functions/functions.js b/client/src/modules/functions/functions.js index 2a7fa9da42..a769334147 100644 --- a/client/src/modules/functions/functions.js +++ b/client/src/modules/functions/functions.js @@ -23,27 +23,27 @@ function FunctionManagementController(Functions, Modals, Notify, uiGridConstants // options for the UI grid vm.gridOptions = { - appScopeProvider: vm, - enableColumnMenus: false, - fastWatch: true, - flatEntityAccess: true, - enableSorting: true, - onRegisterApi: onRegisterApiFn, - columnDefs: [{ - field: 'fonction_txt', - displayName: 'FORM.LABELS.DESIGNATION', - headerCellFilter: 'translate', + appScopeProvider : vm, + enableColumnMenus : false, + fastWatch : true, + flatEntityAccess : true, + enableSorting : true, + onRegisterApi : onRegisterApiFn, + columnDefs : [{ + field : 'fonction_txt', + displayName : 'FORM.LABELS.DESIGNATION', + headerCellFilter : 'translate', }, { - field: 'numEmployees', - displayName: 'TABLE.AGGREGATES.NUM_EMPLOYEES', - headerCellFilter: 'translate', + field : 'numEmployees', + displayName : 'TABLE.AGGREGATES.NUM_EMPLOYEES', + headerCellFilter : 'translate', }, { - field: 'action', - width: 80, - displayName: '', - cellTemplate: '/modules/functions/templates/action.tmpl.html', - enableSorting: false, - enableFiltering: false, + field : 'action', + width : 80, + displayName : '', + cellTemplate : '/modules/functions/templates/action.tmpl.html', + enableSorting : false, + enableFiltering : false, }], }; @@ -59,7 +59,7 @@ function FunctionManagementController(Functions, Modals, Notify, uiGridConstants function loadFunctions() { vm.loading = true; - Functions.read(null, { detailed: 1 }) + Functions.read(null, { detailed : 1 }) .then((data) => { vm.gridOptions.data = data; }) diff --git a/client/src/modules/functions/functions.routes.js b/client/src/modules/functions/functions.routes.js index 550469c953..c25159c272 100644 --- a/client/src/modules/functions/functions.routes.js +++ b/client/src/modules/functions/functions.routes.js @@ -2,35 +2,35 @@ angular.module('bhima.routes') .config(['$stateProvider', ($stateProvider) => { $stateProvider .state('functions', { - url: '/functions', - controller: 'FunctionManagementController as FunctionCtrl', - templateUrl: 'modules/functions/functions.html', + url : '/functions', + controller : 'FunctionManagementController as FunctionCtrl', + templateUrl : 'modules/functions/functions.html', }) .state('functions.create', { - url: '/create', - params: { - isCreateState: { value: true }, + url : '/create', + params : { + isCreateState : { value : true }, }, - onEnter: ['$uibModal', '$transition$', functionModal], - onExit: ['$uibModalStack', closeModal], + onEnter : ['$uibModal', '$transition$', functionModal], + onExit : ['$uibModalStack', closeModal], }) .state('functions.edit', { - url: '/:id/edit', - params: { - id: { value: null }, + url : '/:id/edit', + params : { + id : { value : null }, }, - onEnter: ['$uibModal', '$transition$', functionModal], - onExit: ['$uibModalStack', closeModal], + onEnter : ['$uibModal', '$transition$', functionModal], + onExit : ['$uibModalStack', closeModal], }); }]); function functionModal($modal, $transition) { $modal.open({ - templateUrl: 'modules/functions/modals/function.modal.html', - controller: 'FunctionModalController as FunctionModalCtrl', - resolve: { params: () => $transition.params('to') }, + templateUrl : 'modules/functions/modals/function.modal.html', + controller : 'FunctionModalController as FunctionModalCtrl', + resolve : { params : () => $transition.params('to') }, }).result.catch(angular.noop); } diff --git a/client/src/modules/functions/modals/function.modal.js b/client/src/modules/functions/modals/function.modal.js index 64b903d0d8..92cfe3991f 100644 --- a/client/src/modules/functions/modals/function.modal.js +++ b/client/src/modules/functions/modals/function.modal.js @@ -51,7 +51,7 @@ function FunctionModalController($state, Functions, Notify, AppCache, params) { if (vm.batchCreate) { $state.go('functions.create'); } else { - $state.go('functions', null, { reload: true }); + $state.go('functions', null, { reload : true }); } }) .catch(Notify.handleError); diff --git a/server/controllers/admin/functions.js b/server/controllers/admin/functions.js index d595e680d8..3cbb36745b 100644 --- a/server/controllers/admin/functions.js +++ b/server/controllers/admin/functions.js @@ -52,7 +52,7 @@ async function create(req, res, next) { try { const row = await db.exec(sql, [data]); - res.status(201).json({ id: row.insertId }); + res.status(201).json({ id : row.insertId }); } catch (e) { next(e); } } diff --git a/test/integration/functions.js b/test/integration/functions.js index 620b60d348..b7597cff26 100644 --- a/test/integration/functions.js +++ b/test/integration/functions.js @@ -15,13 +15,17 @@ describe('test/integration/functions The /functions API', () => { fonction_txt: 'Anestiologiste', }; - const FUNCTION_KEY = ['id', 'fonction_txt', 'numEmployees']; + const FUNCTION_KEY = ['id', 'fonction_txt']; const NUM_FUNCTIONS = 2; it('GET /FUNCTIONS returns a list of function ', () => { return agent.get('/functions') .then((res) => { helpers.api.listed(res, NUM_FUNCTIONS); + const [firstFunction] = res.body; + expect(firstFunction).to.have.keys([...FUNCTION_KEY, 'numEmployees']); + expect(firstFunction.numEmployees).to.equal(5); + }) .catch(helpers.handler); });