Skip to content

Commit 7a274fe

Browse files
committed
Feedback updates.
1 parent ddd34ac commit 7a274fe

File tree

10 files changed

+179
-158
lines changed

10 files changed

+179
-158
lines changed

src/Umbraco.Cms.Integrations.Crm.Hubspot.Tests/Controllers/FormsControllerTests.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using NUnit.Framework.Internal;
1717
using Umbraco.Cms.Integrations.Crm.Hubspot.Configuration;
1818
using Umbraco.Cms.Integrations.Crm.Hubspot.Controllers;
19+
using Umbraco.Cms.Integrations.Crm.Hubspot.Models;
1920
using Umbraco.Cms.Integrations.Crm.Hubspot.Models.Dtos;
2021
using Umbraco.Cms.Integrations.Crm.Hubspot.Services;
2122
using Umbraco.Core.Composing;
@@ -54,6 +55,8 @@ public class FormsControllerTests
5455

5556
private Mock<IAppSettings> MockedAppSettingsOAuthSetup;
5657

58+
private Mock<IAppSettings> MockedAppSettingsNoSetup;
59+
5760
private Mock<ILogger> MockedLogger;
5861

5962
public FormsControllerTests()
@@ -68,29 +71,48 @@ public void Init()
6871

6972
MockedAppSettingsOAuthSetup = CreateMockedAppSettings(includeOAuthSettings: true);
7073

74+
MockedAppSettingsNoSetup = CreateMockedAppSettings();
75+
7176
MockedLogger = new Mock<ILogger>();
7277
}
7378

79+
#region CheckConfiguration
80+
7481
[Test]
75-
public void CheckApiConfiguration_GetApiKey_ShouldReturnTrue()
82+
public void CheckApiConfiguration_WithApiConfig_ShouldReturnValidConfigurationResponseObjectWithType()
7683
{
7784
var sut = new FormsController(MockedAppSettingsApiSetup.Object, Mock.Of<IHubspotService>(), Mock.Of<ITokenService>(), Mock.Of<ILogger>());
7885

79-
var result = sut.CheckApiConfiguration();
86+
var result = sut.CheckConfiguration();
8087

81-
Assert.IsTrue(result);
88+
Assert.That(result.IsValid, Is.True);
89+
Assert.AreEqual(result.Type, ConfigurationType.Api);
8290
}
8391

8492
[Test]
85-
public void CheckOAuthConfiguration_GetOAuthConfig_ShouldReturnTrue()
93+
public void CheckOAuthConfiguration_WithOAuthConfigAndNoApiConfig_ShouldReturnValidConfigurationResponseObjectWithType()
8694
{
8795
var sut = new FormsController(MockedAppSettingsOAuthSetup.Object, Mock.Of<IHubspotService>(), Mock.Of<ITokenService>(), Mock.Of<ILogger>());
8896

89-
var result = sut.CheckOAuthConfiguration();
97+
var result = sut.CheckConfiguration();
98+
99+
Assert.That(result.IsValid, Is.True);
100+
Assert.AreEqual(result.Type, ConfigurationType.OAuth);
101+
}
102+
103+
[Test]
104+
public void CheckConfiguration_WithoutApiOrOAuthConfig_ShouldReturnInvalidConfigurationResponseObjectWithType()
105+
{
106+
var sut = new FormsController(MockedAppSettingsNoSetup.Object, Mock.Of<IHubspotService>(), Mock.Of<ITokenService>(), Mock.Of<ILogger>());
107+
108+
var result = sut.CheckConfiguration();
90109

91-
Assert.IsTrue(result);
110+
Assert.That(result.IsValid, Is.False);
111+
Assert.AreEqual(result.Type, ConfigurationType.None);
92112
}
93113

114+
#endregion
115+
94116
#region GetAllUsingApiKey
95117

96118
[Test]
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,19 @@
11
angular.module("umbraco")
22
.controller("Umbraco.Cms.Integrations.Crm.Hubspot.FormPickerController",
33
function ($scope, editorService, notificationsService, umbracoCmsIntegrationsCrmHubspotResource) {
4+
5+
const oauthName = "OAuth";
6+
47
var vm = this;
58
vm.loading = true;
69
vm.hubspotFormsList = [];
710
vm.searchTerm = "";
811
vm.error = "";
12+
vm.isValid = true;
913

10-
if ($scope.model.config !== undefined && $scope.model.config.settings !== undefined) {
11-
vm.config = {
12-
useApi: $scope.model.config.settings.useApi,
13-
useOAuth: $scope.model.config.settings.useOAuth,
14-
isOverlay: false
15-
};
16-
}
17-
if ($scope.model.overlayConfig !== undefined && $scope.model.overlayConfig.isOverlay !== undefined) {
18-
vm.config = {
19-
useApi: $scope.model.overlayConfig.useApi,
20-
useOAuth: $scope.model.overlayConfig.useOAuth,
21-
isOverlay: true
22-
};
23-
}
14+
// check configuration
15+
checkConfiguration(loadForms);
2416

25-
if (vm.config !== undefined && vm.config.useApi === false && vm.config.useOAuth === false) {
26-
vm.loading = false;
27-
notificationsService.warning("Authorization", "No authorization setup has been selected");
28-
return;
29-
}
30-
31-
if (vm.config.useApi === true) {
32-
umbracoCmsIntegrationsCrmHubspotResource.getHubspotFormsList().then(function (data) {
33-
vm.loading = false;
34-
35-
vm.hubspotFormsList = data.forms;
36-
37-
if (data.isValid === false || data.isExpired == true) {
38-
notificationsService.error("Hubspot API", "Invalid API key");
39-
}
40-
});
41-
} else if (vm.config.useOAuth === true) {
42-
umbracoCmsIntegrationsCrmHubspotResource.validateAccessToken().then(function(response) {
43-
if (response.isExpired === true || response.isValid === false) {
44-
notificationsService.warning("Hubspot API", "Invalid Access Token");
45-
return;
46-
}
47-
48-
umbracoCmsIntegrationsCrmHubspotResource.getHubspotFormsListOAuth().then(function(data) {
49-
vm.loading = false;
50-
vm.hubspotFormsList = data.forms;
51-
52-
if (data.isValid === false || data.isExpired == true) {
53-
notificationsService.error("Hubspot API", "Invalid Access Token");
54-
}
55-
});
56-
});
57-
}
58-
5917
vm.remove = function () {
6018
$scope.model.value = null;
6119
};
@@ -65,13 +23,11 @@
6523
};
6624

6725
vm.openHubspotFormPickerOverlay = function () {
68-
vm.config.isOverlay = true;
6926
var options = {
7027
title: "Hubspot forms",
7128
subtitle: "Select a form",
7229
view: "/App_Plugins/UmbracoCms.Integrations/Crm/Hubspot/views/formpickereditor.html",
7330
size: "medium",
74-
overlayConfig: vm.config,
7531
pickForm: function (form) {
7632
vm.saveForm(form);
7733
editorService.close();
@@ -83,4 +39,58 @@
8339

8440
editorService.open(options);
8541
};
42+
43+
function checkConfiguration(callback) {
44+
umbracoCmsIntegrationsCrmHubspotResource.checkApiConfiguration().then(function (response) {
45+
46+
vm.status = {
47+
isValid: response.isValid === true,
48+
type: response.type,
49+
description: response.isValid === true ? `${response.type.value} is configured.` : "Invalid configuration",
50+
useOAuth: response.isValid === true && response.type.value === oauthName
51+
};
52+
53+
if (response.isValid === false) {
54+
vm.loading = false;
55+
vm.error = "Invalid configuration.";
56+
notificationsService.warning("Hubspot API",
57+
"Invalid setup. Please review the API/OAuth settings.");
58+
} else {
59+
callback();
60+
}
61+
});
62+
}
63+
64+
function loadForms() {
65+
if (vm.status.useOAuth === true) {
66+
// use OAuth
67+
umbracoCmsIntegrationsCrmHubspotResource.validateAccessToken().then(function (response) {
68+
if (response.isExpired === true || response.isValid === false) {
69+
notificationsService.warning("Hubspot API", "Invalid Access Token");
70+
return;
71+
}
72+
73+
umbracoCmsIntegrationsCrmHubspotResource.getHubspotFormsListOAuth().then(function (data) {
74+
vm.loading = false;
75+
vm.hubspotFormsList = data.forms;
76+
77+
if (data.isValid === false || data.isExpired === true) {
78+
notificationsService.error("Hubspot API", "Invalid Access Token");
79+
}
80+
});
81+
});
82+
} else {
83+
// use API
84+
umbracoCmsIntegrationsCrmHubspotResource.getHubspotFormsList().then(function (data) {
85+
vm.loading = false;
86+
87+
vm.hubspotFormsList = data.forms;
88+
89+
if (data.isValid === false || data.isExpired == true) {
90+
notificationsService.error("Hubspot API", "Invalid API key");
91+
}
92+
});
93+
94+
}
95+
}
8696
});

src/Umbraco.Cms.Integrations.Crm.Hubspot/App_Plugins/UmbracoCms.Integrations/Crm/Hubspot/js/hubspot.resource.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
angular.module('umbraco.resources').factory('umbracoCmsIntegrationsCrmHubspotResource',
22
function ($http, umbRequestHelper) {
3+
4+
const apiEndpoint = "backoffice/UmbracoCmsIntegrationsCrmHubspot/Forms";
5+
36
return {
4-
getHubspotFormsList: function (id) {
5-
return umbRequestHelper.resourcePromise(
6-
$http.get("backoffice/UmbracoCmsIntegrationsCrmHubspot/Forms/GetAll"), "");
7-
},
8-
checkApiConfiguration: function() {
7+
checkApiConfiguration: function () {
98
return umbRequestHelper.resourcePromise(
10-
$http.get("backoffice/UmbracoCmsIntegrationsCrmHubspot/Forms/CheckApiConfiguration"),
9+
$http.get(`${apiEndpoint}/CheckConfiguration`),
1110
"Failed to get resource");
1211
},
13-
checkOAuthConfiguration: function () {
12+
getHubspotFormsList: function (id) {
1413
return umbRequestHelper.resourcePromise(
15-
$http.get("backoffice/UmbracoCmsIntegrationsCrmHubspot/Forms/CheckOAuthConfiguration"),
16-
"Failed to get resource");
14+
$http.get("backoffice/UmbracoCmsIntegrationsCrmHubspot/Forms/GetAll"), "");
1715
},
1816
getAuthorizationUrl: function() {
1917
return umbRequestHelper.resourcePromise(
@@ -42,4 +40,4 @@
4240
}
4341
};
4442
}
45-
);
43+
);

src/Umbraco.Cms.Integrations.Crm.Hubspot/App_Plugins/UmbracoCms.Integrations/Crm/Hubspot/js/settings.controller.js

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,39 @@
22

33
var vm = this;
44

5-
const apiKeyName = "API Key";
6-
const oAuthName = "OAuth";
5+
const oauthName = "OAuth";
76

8-
vm.settingsValidation = {
9-
isValid: true,
10-
message: "Invalid configuration. Please review your website's configurations."
11-
};
7+
vm.status = {};
128

13-
vm.useApi = $scope.model.value.useApi;
14-
vm.useOAuth = $scope.model.value.useOAuth;
15-
vm.oauthSetup = {};
9+
umbracoCmsIntegrationsCrmHubspotResource.checkApiConfiguration().then(function (response) {
1610

17-
if (vm.useOAuth) {
18-
// validate token
19-
validateOAuthAccessToken();
20-
}
21-
22-
init();
23-
24-
$scope.$on('formSubmitting', function () {
25-
26-
$scope.model.value.useApi = vm.useApi;
27-
$scope.model.value.useOAuth = vm.useOAuth;
11+
vm.status = {
12+
isValid: response.isValid === true,
13+
type: response.type,
14+
description: response.isValid === true ? `${response.type.value} is configured.` : "Invalid configuration",
15+
useOAuth: response.isValid === true && response.type.value === oauthName
16+
};
2817

18+
if (response.isValid === true) {
19+
notificationsService.success("Configuration", `${response.type.value} setup is configured.`);
20+
} else {
21+
notificationsService.warning("Configuration",
22+
"Invalid setup. Please review the API/OAuth settings.");
23+
}
2924
});
3025

31-
vm.toggleAuthType = function (item) {
32-
33-
let selectedItem = vm.items.find(el => el.name === item.name);
26+
//if (vm.useOAuth) {
27+
// // validate token
28+
// validateOAuthAccessToken();
29+
//}
3430

35-
var deselectedItem = vm.items.find(el => el.name !== item.name);
36-
deselectedItem.disabled = false;
37-
deselectedItem.checked = false;
31+
//$scope.$on('formSubmitting', function () {
3832

39-
vm.useApi = selectedItem.name === apiKeyName;
40-
if (vm.useApi === true) {
41-
handleApiConfiguration();
42-
}
33+
// $scope.model.value.isValid = vm.status.isValid;
34+
// $scope.model.value.type = vm.status.type;
4335

44-
vm.useOAuth = selectedItem.name === oAuthName;
45-
if (vm.useOAuth === true) {
46-
handleOAuthConfiguration();
36+
//});
4737

48-
validateOAuthAccessToken();
49-
}
50-
}
5138

5239
vm.onConnectClick = function () {
5340

@@ -71,16 +58,6 @@
7158

7259

7360
// custom handlers
74-
function handleApiConfiguration() {
75-
umbracoCmsIntegrationsCrmHubspotResource.checkApiConfiguration().then(function (response) {
76-
handleConfigurationValidation(response);
77-
if (vm.settingsValidation.isValid === false) {
78-
vm.useApi = false;
79-
vm.items.find(el => el.name === apiKeyName).checked = false;
80-
}
81-
82-
});
83-
}
8461

8562
function handleOAuthConfiguration() {
8663
umbracoCmsIntegrationsCrmHubspotResource.checkOAuthConfiguration().then(function (response) {
@@ -101,18 +78,6 @@
10178
vm.settingsValidation.isValid = true;
10279
}
10380

104-
function init() {
105-
vm.items = [{
106-
name: apiKeyName,
107-
description: "Use API key based setup.",
108-
checked: vm.useApi
109-
}, {
110-
name: oAuthName,
111-
description: "Use OAuth setup.",
112-
checked: vm.useOAuth
113-
}];
114-
}
115-
11681
function validateOAuthAccessToken() {
11782
umbracoCmsIntegrationsCrmHubspotResource.validateAccessToken().then(function (response) {
11883

src/Umbraco.Cms.Integrations.Crm.Hubspot/App_Plugins/UmbracoCms.Integrations/Crm/Hubspot/views/formpicker.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
on-remove="vm.remove()">
1212
</umb-node-preview>
1313

14-
<a ng-if="!model.value && !vm.loading"
14+
<a ng-if="!model.value && !vm.loading && vm.status.isValid"
1515
class="umb-node-preview-add"
1616
href=""
1717
ng-click="vm.openHubspotFormPickerOverlay()"

0 commit comments

Comments
 (0)