From 8261b82b980d16226fe94668126fc52f80b3ecf9 Mon Sep 17 00:00:00 2001
From: Martin Hradil <mhradil@redhat.com>
Date: Thu, 10 Oct 2019 12:27:45 +0000
Subject: [PATCH] DialogUser - use DialogData.outputConversion before submit

right now, when submitting a service dialog,
we simply take the dialogData object, convert to JSON and send to the API.

To allow for output conversions, adding a DialogData.outputConversion method,
which should return the dialogData in a suitable format.

(Right now, all this really means is that it converts Dates to strings before JSON stringification.)

https://bugzilla.redhat.com/show_bug.cgi?id=1744413
---
 .../dialog_user/dialog_user_controller.js            | 12 ++++++------
 .../dialog_user_reconfigure_controller.js            |  7 +++++--
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/app/assets/javascripts/controllers/dialog_user/dialog_user_controller.js b/app/assets/javascripts/controllers/dialog_user/dialog_user_controller.js
index 0137dac9511..7491679e41e 100644
--- a/app/assets/javascripts/controllers/dialog_user/dialog_user_controller.js
+++ b/app/assets/javascripts/controllers/dialog_user/dialog_user_controller.js
@@ -1,4 +1,4 @@
-ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefreshService', 'miqService', 'dialogUserSubmitErrorHandlerService', 'dialogId', 'apiSubmitEndpoint', 'apiAction', 'finishSubmitEndpoint', 'cancelEndpoint', 'resourceActionId', 'targetId', 'targetType', 'realTargetType', 'openUrl', '$http', '$window', 'dialogReplaceData', function(API, dialogFieldRefreshService, miqService, dialogUserSubmitErrorHandlerService, dialogId, apiSubmitEndpoint, apiAction, finishSubmitEndpoint, cancelEndpoint, resourceActionId, targetId, targetType, realTargetType, openUrl, $http, $window, dialogReplaceData) {
+ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefreshService', 'miqService', 'dialogUserSubmitErrorHandlerService', 'dialogId', 'apiSubmitEndpoint', 'apiAction', 'finishSubmitEndpoint', 'cancelEndpoint', 'resourceActionId', 'targetId', 'targetType', 'realTargetType', 'openUrl', '$http', '$window', 'dialogReplaceData', 'DialogData', function(API, dialogFieldRefreshService, miqService, dialogUserSubmitErrorHandlerService, dialogId, apiSubmitEndpoint, apiAction, finishSubmitEndpoint, cancelEndpoint, resourceActionId, targetId, targetType, realTargetType, openUrl, $http, $window, dialogReplaceData, DialogData) {
   var vm = this;
 
   vm.$onInit = function() {
@@ -68,14 +68,14 @@ ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefr
   function submitButtonClicked() {
     vm.dialogData.action = apiAction;
     miqService.sparkleOn();
-    var apiData;
+
+    var apiData = DialogData.outputConversion(vm.dialogData);
     if (apiSubmitEndpoint.match(/generic_objects/)) {
-      apiData = {action: apiAction, parameters: _.omit(vm.dialogData, 'action')};
+      apiData = {action: apiAction, parameters: _.omit(apiData, 'action')};
     } else if (apiAction === 'reconfigure') {
-      apiData = {action: apiAction, resource: _.omit(vm.dialogData, 'action')};
-    } else {
-      apiData = vm.dialogData;
+      apiData = {action: apiAction, resource: _.omit(apiData, 'action')};
     }
+
     return API.post(apiSubmitEndpoint, apiData, {skipErrors: [400]})
       .then(function(response) {
 
diff --git a/app/assets/javascripts/controllers/dialog_user/dialog_user_reconfigure_controller.js b/app/assets/javascripts/controllers/dialog_user/dialog_user_reconfigure_controller.js
index a01e7953816..0b6ab05460f 100644
--- a/app/assets/javascripts/controllers/dialog_user/dialog_user_reconfigure_controller.js
+++ b/app/assets/javascripts/controllers/dialog_user/dialog_user_reconfigure_controller.js
@@ -1,4 +1,4 @@
-ManageIQ.angular.app.controller('dialogUserReconfigureController', ['API', 'dialogFieldRefreshService', 'miqService', 'dialogUserSubmitErrorHandlerService', 'resourceActionId', 'targetId', function(API, dialogFieldRefreshService, miqService, dialogUserSubmitErrorHandlerService, resourceActionId, targetId) {
+ManageIQ.angular.app.controller('dialogUserReconfigureController', ['API', 'dialogFieldRefreshService', 'miqService', 'dialogUserSubmitErrorHandlerService', 'resourceActionId', 'targetId', 'DialogData', function(API, dialogFieldRefreshService, miqService, dialogUserSubmitErrorHandlerService, resourceActionId, targetId, DialogData) {
   var vm = this;
 
   vm.$onInit = function() {
@@ -46,7 +46,10 @@ ManageIQ.angular.app.controller('dialogUserReconfigureController', ['API', 'dial
   function submitButtonClicked() {
     miqService.sparkleOn();
 
-    var apiData = {action: 'reconfigure', resource: _.omit(vm.dialogData, 'action')};
+    var apiData = {
+      action: 'reconfigure',
+      resource: _.omit(DialogData.outputConversion(vm.dialogData), 'action'),
+    };
     var apiSubmitEndpoint = '/api/services/' + targetId;
 
     return API.post(apiSubmitEndpoint, apiData, {skipErrors: [400]}).then(function() {