From 39ea1c863433f197423a427bd924730759013aff Mon Sep 17 00:00:00 2001 From: Aura Esteban Date: Tue, 26 Sep 2017 17:07:40 +0800 Subject: [PATCH 1/4] [DD 2450] Accept api from state and call correct path for adding new user in the web app --- client/app/dashboard/model/edit/ModelEdit.js | 28 ++++++++++++++----- client/common/services/GeneralModelService.js | 8 +++++- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/client/app/dashboard/model/edit/ModelEdit.js b/client/app/dashboard/model/edit/ModelEdit.js index bce18795..d2f6a78f 100644 --- a/client/app/dashboard/model/edit/ModelEdit.js +++ b/client/app/dashboard/model/edit/ModelEdit.js @@ -9,7 +9,7 @@ angular.module('dashboard.Dashboard.Model.Edit', [ 'ui.bootstrap', 'ui.bootstrap.datepicker', 'ui.bootstrap.modal', - 'ngCookies' + 'ngCookies' ]) .config(function config($stateProvider) { @@ -22,6 +22,18 @@ angular.module('dashboard.Dashboard.Model.Edit', [ pageTitle: 'Edit' } }) + .state('dashboard.model.action.add', { + url: '/add', + templateUrl: 'app/dashboard/model/edit/ModelEdit.html', + data: { + pageTitle: 'Add' + }, + params: { + model: null, + action: null, + api: null + } + }); ; }) @@ -35,7 +47,8 @@ angular.module('dashboard.Dashboard.Model.Edit', [ if (!$scope.action) $scope.action = {}; if (!$scope.action.options) $scope.action.options = { model: $stateParams.model, key: $stateParams.key }; - + if (!$scope.action.options.api && $stateParams.api) $scope.action.options.api = $stateParams.api; + $scope.model = Config.serverParams.models[$scope.action.options.model]; //Make Key field readonly @@ -69,7 +82,7 @@ angular.module('dashboard.Dashboard.Model.Edit', [ //Loop through fields and check for forced default fields GeneralModelService.checkDefaultValues($scope.model, $scope.data); - + //Check to see if editing model var id = null; if ($stateParams.id && $stateParams.id > 0) id = $stateParams.id; @@ -125,6 +138,7 @@ angular.module('dashboard.Dashboard.Model.Edit', [ */ function save(callback) { var id = $scope.data[$scope.action.options.key]; + if ($scope.action.options.api) $scope.data.api = $scope.action.options.api; GeneralModelService.saveWithFiles($scope.model.name, id, $scope.data) .then(function(response) { if (modalInstance) modalInstance.close(); @@ -181,7 +195,7 @@ angular.module('dashboard.Dashboard.Model.Edit', [ } }); }; - + $scope.clickDeleteModel = function(data, formParams) { $scope.deleteDialogText = (formParams && formParams.deleteDialogText) ? formParams.deleteDialogText : $scope.deleteDialogText; if (!confirm($scope.deleteDialogText)) return; @@ -213,7 +227,7 @@ angular.module('dashboard.Dashboard.Model.Edit', [ }); } }; - + /** * Checks if the user access to edit the field for this Model */ @@ -247,7 +261,7 @@ angular.module('dashboard.Dashboard.Model.Edit', [ if (!$cookies.roles) { return false; //user has no role access } - + var userRoles = JSON.parse($cookies.roles); for (var i in userRoles) { var role = userRoles[i]; @@ -257,7 +271,7 @@ angular.module('dashboard.Dashboard.Model.Edit', [ } return false; }; - + init(); }) diff --git a/client/common/services/GeneralModelService.js b/client/common/services/GeneralModelService.js index 52cc3042..76e34c55 100644 --- a/client/common/services/GeneralModelService.js +++ b/client/common/services/GeneralModelService.js @@ -98,7 +98,13 @@ angular.module('dashboard.services.GeneralModel', [ * in hierarchical format */ this.save = function(model, id, params) { - var path = Config.serverParams.cmsBaseUrl + '/model/save'; + var path; + if (params.api) { + path = params.api + delete params.api; + } else { + path = Config.serverParams.cmsBaseUrl + '/model/save'; + } params.__model = model; params.__id = id; params.__accessToken = $cookies.accessToken; From 97d485d2c0d71b3a026bcb8719ef0b5158d809ca Mon Sep 17 00:00:00 2001 From: Aura Esteban Date: Thu, 28 Sep 2017 13:36:59 +0800 Subject: [PATCH 2/4] [DD-2450] Accept options object for save and saveWithFiles --- client/app/dashboard/model/edit/ModelEdit.js | 18 ++---------------- client/common/services/GeneralModelService.js | 10 ++-------- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/client/app/dashboard/model/edit/ModelEdit.js b/client/app/dashboard/model/edit/ModelEdit.js index d2f6a78f..f690cfa9 100644 --- a/client/app/dashboard/model/edit/ModelEdit.js +++ b/client/app/dashboard/model/edit/ModelEdit.js @@ -22,18 +22,6 @@ angular.module('dashboard.Dashboard.Model.Edit', [ pageTitle: 'Edit' } }) - .state('dashboard.model.action.add', { - url: '/add', - templateUrl: 'app/dashboard/model/edit/ModelEdit.html', - data: { - pageTitle: 'Add' - }, - params: { - model: null, - action: null, - api: null - } - }); ; }) @@ -47,8 +35,7 @@ angular.module('dashboard.Dashboard.Model.Edit', [ if (!$scope.action) $scope.action = {}; if (!$scope.action.options) $scope.action.options = { model: $stateParams.model, key: $stateParams.key }; - if (!$scope.action.options.api && $stateParams.api) $scope.action.options.api = $stateParams.api; - + $scope.model = Config.serverParams.models[$scope.action.options.model]; //Make Key field readonly @@ -138,8 +125,7 @@ angular.module('dashboard.Dashboard.Model.Edit', [ */ function save(callback) { var id = $scope.data[$scope.action.options.key]; - if ($scope.action.options.api) $scope.data.api = $scope.action.options.api; - GeneralModelService.saveWithFiles($scope.model.name, id, $scope.data) + GeneralModelService.saveWithFiles($scope.action.options, id, $scope.data) .then(function(response) { if (modalInstance) modalInstance.close(); $rootScope.$broadcast('modelEditSaved'); diff --git a/client/common/services/GeneralModelService.js b/client/common/services/GeneralModelService.js index 76e34c55..3b0534b0 100644 --- a/client/common/services/GeneralModelService.js +++ b/client/common/services/GeneralModelService.js @@ -98,13 +98,7 @@ angular.module('dashboard.services.GeneralModel', [ * in hierarchical format */ this.save = function(model, id, params) { - var path; - if (params.api) { - path = params.api - delete params.api; - } else { - path = Config.serverParams.cmsBaseUrl + '/model/save'; - } + var path = typeof model === 'object' && model.upsertApi ? model.upsertApi : Config.serverParams.cmsBaseUrl + '/model/save'; params.__model = model; params.__id = id; params.__accessToken = $cookies.accessToken; @@ -121,7 +115,7 @@ angular.module('dashboard.services.GeneralModel', [ * @returns {promise.promise|Function|deferred.promise|{then, catch, finally}|*|r.promise} */ this.saveWithFiles = function(model, id, data) { - var modelDef = Config.serverParams.models[model]; + var modelDef = Config.serverParams.models[typeof model === 'object' ? model.model : model]; var deferred = $q.defer(); var uploadImages = function(callback) { From e965246df22c4d43840257d0f9afca688f78bf28 Mon Sep 17 00:00:00 2001 From: Aura Esteban Date: Fri, 29 Sep 2017 13:44:41 +0800 Subject: [PATCH 3/4] DD-2450: Refactor code --- client/app/dashboard/model/edit/ModelEdit.js | 2 +- client/common/services/GeneralModelService.js | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/client/app/dashboard/model/edit/ModelEdit.js b/client/app/dashboard/model/edit/ModelEdit.js index f690cfa9..046f1860 100644 --- a/client/app/dashboard/model/edit/ModelEdit.js +++ b/client/app/dashboard/model/edit/ModelEdit.js @@ -125,7 +125,7 @@ angular.module('dashboard.Dashboard.Model.Edit', [ */ function save(callback) { var id = $scope.data[$scope.action.options.key]; - GeneralModelService.saveWithFiles($scope.action.options, id, $scope.data) + GeneralModelService.saveWithFiles($scope.model.name, id, $scope.data, $scope.action.options.upsertApi) .then(function(response) { if (modalInstance) modalInstance.close(); $rootScope.$broadcast('modelEditSaved'); diff --git a/client/common/services/GeneralModelService.js b/client/common/services/GeneralModelService.js index 3b0534b0..0b198c80 100644 --- a/client/common/services/GeneralModelService.js +++ b/client/common/services/GeneralModelService.js @@ -97,8 +97,8 @@ angular.module('dashboard.services.GeneralModel', [ * The CMS exposes the /model/save API that can take in model data * in hierarchical format */ - this.save = function(model, id, params) { - var path = typeof model === 'object' && model.upsertApi ? model.upsertApi : Config.serverParams.cmsBaseUrl + '/model/save'; + this.save = function(model, id, params, upsertApi) { + var path = upsertApi ? upsertApi : Config.serverParams.cmsBaseUrl + '/model/save'; params.__model = model; params.__id = id; params.__accessToken = $cookies.accessToken; @@ -112,10 +112,11 @@ angular.module('dashboard.services.GeneralModel', [ * @param model * @param id * @param data + * @param upsertApi * @returns {promise.promise|Function|deferred.promise|{then, catch, finally}|*|r.promise} */ - this.saveWithFiles = function(model, id, data) { - var modelDef = Config.serverParams.models[typeof model === 'object' ? model.model : model]; + this.saveWithFiles = function(model, id, data, upsertApi) { + var modelDef = Config.serverParams.models[model]; var deferred = $q.defer(); var uploadImages = function(callback) { @@ -179,7 +180,7 @@ angular.module('dashboard.services.GeneralModel', [ uploadFiles(function() { //Loop through fields and check for forced default fields self.checkDefaultValues(modelDef, data); - self.save(model, id, data).then( + self.save(model, id, data, upsertApi).then( function(result) { deferred.resolve(result); }, From 04169ca13231e8c956e09c2e849e2d8a5fbb499f Mon Sep 17 00:00:00 2001 From: Aura Esteban Date: Mon, 2 Oct 2017 09:07:05 +0800 Subject: [PATCH 4/4] Refactor setting of api path in save function of GeneralModelService --- client/common/services/GeneralModelService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/common/services/GeneralModelService.js b/client/common/services/GeneralModelService.js index 0b198c80..3314f5cf 100644 --- a/client/common/services/GeneralModelService.js +++ b/client/common/services/GeneralModelService.js @@ -98,7 +98,7 @@ angular.module('dashboard.services.GeneralModel', [ * in hierarchical format */ this.save = function(model, id, params, upsertApi) { - var path = upsertApi ? upsertApi : Config.serverParams.cmsBaseUrl + '/model/save'; + var path = upsertApi || Config.serverParams.cmsBaseUrl + '/model/save'; params.__model = model; params.__id = id; params.__accessToken = $cookies.accessToken;