Skip to content

Commit c9b1ba9

Browse files
committed
Shopify product picker OAuth and integration updates.
1 parent 7b5b461 commit c9b1ba9

File tree

14 files changed

+183
-47
lines changed

14 files changed

+183
-47
lines changed

src/Umbraco.Cms.Integrations.Commerce.Shopify/App_Plugins/UmbracoCms.Integrations/Commerce/Shopify/js/productPicker.controller.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010
// step 1. check configuration
1111
checkConfiguration(function() {
1212
// step 2. get products
13+
getProducts();
14+
});
15+
16+
function getProducts() {
1317
vm.loading = true;
18+
1419
umbracoCmsIntegrationsCommerceShopifyResource.getProductsList().then(function (response) {
1520
if (response.isValid) {
21+
1622
vm.productsList = response.result.products;
1723

1824
if ($scope.model.value != undefined && $scope.model.value.length > 0) {
@@ -21,8 +27,9 @@
2127
}
2228
vm.loading = false;
2329
});
24-
});
30+
}
2531

32+
// products table events
2633
vm.selectProduct = function (item) {
2734
if ($scope.model.selectedProducts.filter(function (i) { return i.id === item.id }).length > 0) {
2835
$scope.model.selectedProducts = $scope.model.selectedProducts.filter(function (i) { return i.id !== item.id; });
@@ -42,7 +49,7 @@
4249
description: "Select product(s)",
4350
selectedProducts: vm.selectedProducts,
4451
view: "/App_Plugins/UmbracoCms.Integrations/Commerce/Shopify/views/productPickerOverlay.html",
45-
size: "medium",
52+
size: "large",
4653
submit: function (selectedProducts) {
4754
vm.submit(selectedProducts);
4855

src/Umbraco.Cms.Integrations.Commerce.Shopify/App_Plugins/UmbracoCms.Integrations/Commerce/Shopify/js/productPickerSettings.controller.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424
useOAuth: response.isValid === true && response.type.value === oauthName
2525
};
2626

27-
console.log("STATUS: ", vm.status);
28-
29-
//if (vm.status.useOAuth) {
30-
// validateOAuthSetup();
31-
//}
27+
if (vm.status.useOAuth) {
28+
validateOAuthSetup();
29+
}
3230

3331
if (response.isValid !== true) {
3432
notificationsService.warning("Shopify Configuration",
@@ -46,10 +44,10 @@
4644
}
4745

4846
vm.onRevokeToken = function () {
49-
//umbracoCmsIntegrationsCommerceShopifyResource.revokeAccessToken().then(function (response) {
50-
// vm.oauthSetup.isConnected = false;
51-
// notificationsService.success("HubSpot Configuration", "OAuth connection revoked.");
52-
//});
47+
umbracoCmsIntegrationsCommerceShopifyResource.revokeAccessToken().then(function (response) {
48+
vm.oauthSetup.isConnected = false;
49+
notificationsService.success("Shopify Configuration", "OAuth connection revoked.");
50+
});
5351
}
5452

5553
// authorization listener
@@ -69,27 +67,26 @@
6967
}, false);
7068

7169

72-
//function validateOAuthSetup() {
73-
// umbracoCmsIntegrationsCrmHubspotResource.validateAccessToken().then(function (response) {
70+
function validateOAuthSetup() {
71+
umbracoCmsIntegrationsCommerceShopifyResource.validateAccessToken().then(function (response) {
7472

75-
// vm.oauthSetup = {
76-
// isConnected: response.isValid,
77-
// isAccessTokenExpired: response.isExpired,
78-
// isAccessTokenValid: response.isValid
79-
// }
73+
vm.oauthSetup = {
74+
isConnected: response.isValid,
75+
isAccessTokenExpired: response.isExpired,
76+
isAccessTokenValid: response.isValid
77+
}
8078

81-
// if (vm.oauthSetup.isConnected === true && vm.oauthSetup.isAccessTokenValid === true) {
82-
// vm.status.description = umbracoCmsIntegrationsCrmHubspotService.configDescription.OAuthConnected;
83-
// }
79+
if (vm.oauthSetup.isConnected === true && vm.oauthSetup.isAccessTokenValid === true) {
80+
vm.status.description = umbracoCmsIntegrationsCommerceShopifyService.configDescription.OAuthConnected;
81+
}
8482

85-
// // refresh access token
86-
// if (vm.oauthSetup.isAccessTokenExpired === true) {
87-
// umbracoCmsIntegrationsCrmHubspotResource.refreshAccessToken().then(function (response) {
88-
// });
89-
// }
83+
// refresh access token
84+
if (vm.oauthSetup.isAccessTokenExpired === true) {
85+
umbracoCmsIntegrationsCommerceShopifyService.refreshAccessToken().then(function (response) {});
86+
}
9087

91-
// });
92-
//}
88+
});
89+
}
9390
}
9491

9592
angular.module("umbraco")

src/Umbraco.Cms.Integrations.Commerce.Shopify/App_Plugins/UmbracoCms.Integrations/Commerce/Shopify/js/shopify.resource.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
$http.post(`${apiEndpoint}/GetAccessToken`, { code: authorizationCode }),
2020
"Failed");
2121
},
22+
revokeAccessToken: function () {
23+
return umbRequestHelper.resourcePromise(
24+
$http.post(`${apiEndpoint}/RevokeAccessToken`),
25+
"Failed");
26+
},
27+
validateAccessToken: function () {
28+
return umbRequestHelper.resourcePromise(
29+
$http.get(`${apiEndpoint}/ValidateAccessToken`),
30+
"Failed");
31+
},
2232
getProductsList: function() {
2333
return umbRequestHelper.resourcePromise(
2434
$http.get(`${apiEndpoint}/GetList`), "Failed to get resource");

src/Umbraco.Cms.Integrations.Commerce.Shopify/App_Plugins/UmbracoCms.Integrations/Commerce/Shopify/views/productPickerOverlay.html

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@
1919
</button>
2020
</div>
2121
<div class="umb-table-cell">
22-
<span>Body</span>
22+
<span>Vendor</span>
2323
</div>
2424
<div class="umb-table-cell">
2525
<span>Status</span>
2626
</div>
27+
<div class="umb-table-cell">
28+
<span>Tags</span>
29+
</div>
30+
<div class="umb-table-cell">
31+
<span>SKU</span>
32+
</div>
33+
<div class="umb-table-cell">
34+
<span>Barcode</span>
35+
</div>
2736
<div class="umb-table-cell">
2837
<span>Price</span>
2938
</div>
@@ -41,28 +50,27 @@
4150
{{ product.title }}
4251
</div>
4352
<div class="umb-table-cell">
44-
<span>{{ product.body_html }}</span>
53+
<span>{{ product.vendor }}</span>
4554
</div>
4655
<div class="umb-table-cell">
4756
<span>{{ product.status }}</span>
4857
</div>
58+
<div class="umb-table-cell">
59+
<span>{{ product.tags }}</span>
60+
</div>
61+
<div class="umb-table-cell">
62+
<span>{{ product.variants[0].sku }}</span>
63+
</div>
64+
<div class="umb-table-cell">
65+
<span>{{ product.variants[0].barcode }}</span>
66+
</div>
4967
<div class="umb-table-cell">
5068
<span>{{ product.variants[0].price }}</span>
5169
</div>
5270
</div>
5371
</div>
5472
</div>
5573

56-
<div class="flex justify-center" ng-show="!vm.loading">
57-
<umb-pagination page-number="vm.pagination.pageNumber"
58-
total-pages="vm.pagination.totalPages"
59-
on-next="vm.nextPage"
60-
on-prev="vm.prevPage"
61-
on-change="vm.changePage"
62-
on-go-to-page="vm.goToPage">
63-
</umb-pagination>
64-
</div>
65-
6674
<!-- If list is empty, then display -->
6775
<umb-empty-state ng-if="!vm.productsList"
6876
position="center">

src/Umbraco.Cms.Integrations.Commerce.Shopify/Controllers/ProductsController.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System.Threading.Tasks;
1+
using System.Linq;
2+
using System.Threading.Tasks;
23
using System.Web.Http;
3-
4+
using Newtonsoft.Json;
45
using Umbraco.Cms.Integrations.Commerce.Shopify.Models.Dtos;
56
using Umbraco.Cms.Integrations.Shared.Models;
67
using Umbraco.Web.Mvc;
@@ -15,9 +16,13 @@ public class ProductsController : UmbracoAuthorizedApiController
1516
{
1617
private readonly IApiService<ProductsListDto> _apiService;
1718

18-
public ProductsController(IApiService<ProductsListDto> apiService)
19+
private readonly ICacheHelper _cacheHelper;
20+
21+
public ProductsController(IApiService<ProductsListDto> apiService, ICacheHelper cacheHelper)
1922
{
2023
_apiService = apiService;
24+
25+
_cacheHelper = cacheHelper;
2126
}
2227

2328
[HttpGet]
@@ -29,9 +34,12 @@ public ProductsController(IApiService<ProductsListDto> apiService)
2934
[HttpPost]
3035
public async Task<string> GetAccessToken([FromBody] OAuthRequestDto authRequestDto) => await _apiService.GetAccessToken(authRequestDto);
3136

37+
[HttpGet]
38+
public async Task<ResponseDto<ProductsListDto>> ValidateAccessToken() => await _apiService.ValidateAccessToken();
3239

33-
public async Task<ResponseDto<ProductsListDto>> GetList() => await _apiService.GetResults();
34-
40+
[HttpPost]
41+
public void RevokeAccessToken() => _apiService.RevokeAccessToken();
3542

43+
public async Task<ResponseDto<ProductsListDto>> GetList() => await _apiService.GetResults();
3644
}
3745
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Models/Dtos/ProductDto.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ public class ProductDto
1515
[JsonProperty("body_html")]
1616
public string Body { get; set; }
1717

18+
[JsonProperty("vendor")]
19+
public string Vendor { get; set; }
20+
1821
[JsonProperty("status")]
1922
public string Status { get; set; }
2023

24+
[JsonProperty("tags")]
25+
public string Tags { get; set; }
26+
2127
[JsonProperty("variants")]
2228
public IEnumerable<ProductVariantDto> Variants { get; set; }
2329

src/Umbraco.Cms.Integrations.Commerce.Shopify/Models/Dtos/ProductVariantDto.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class ProductVariantDto
1313
[JsonProperty("position")]
1414
public int Position { get; set; }
1515

16+
[JsonProperty("barcode")]
17+
public string Barcode { get; set; }
18+
1619
[JsonProperty("inventory_quantity")]
1720
public int InventoryQuantity { get; set; }
1821
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Models/Dtos/ProductsListDto.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ public class ProductsListDto
88
{
99
[JsonProperty("products")]
1010
public IEnumerable<ProductDto> Products { get; set; }
11+
12+
[JsonProperty("totalPages")]
13+
public int TotalPages { get; set; }
1114
}
1215
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Services/ShopifyService.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,45 @@ public async Task<string> GetAccessToken(OAuthRequestDto request)
9595

9696
return "Error: An unexpected error occurred.";
9797
}
98-
98+
99+
public async Task<ResponseDto<ProductsListDto>> ValidateAccessToken()
100+
{
101+
TokenService.TryGetParameters(SettingsService.AccessTokenDbKey, out string accessToken);
102+
103+
if (string.IsNullOrEmpty(accessToken))
104+
{
105+
UmbCoreLogger.Info<ShopifyService>(message: "Cannot access Shopify - Access Token is missing.");
106+
107+
return new ResponseDto<ProductsListDto>();
108+
}
109+
110+
var requestMessage = new HttpRequestMessage
111+
{
112+
Method = HttpMethod.Get,
113+
RequestUri = new Uri(string.Format(SettingsService.ProductsApiEndpoint,
114+
AppSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyShop],
115+
AppSettings[Constants.UmbracoCmsIntegrationsCommerceShopifyApiVersion]))
116+
};
117+
requestMessage.Headers.Add("X-Shopify-Access-Token", accessToken);
118+
119+
var response = await ClientFactory().SendAsync(requestMessage);
120+
121+
return new ResponseDto<ProductsListDto>
122+
{
123+
IsValid = response.IsSuccessStatusCode,
124+
IsExpired = response.StatusCode == HttpStatusCode.Unauthorized
125+
};
126+
}
127+
128+
public void RevokeAccessToken()
129+
{
130+
TokenService.RemoveParameters(Constants.UmbracoCmsIntegrationsCommerceShopifyAccessToken);
131+
}
132+
99133
public async Task<ResponseDto<ProductsListDto>> GetResults()
100134
{
101135
string accessToken;
102-
if (GetApiConfiguration().Type == ConfigurationType.OAuth)
136+
if (GetApiConfiguration().Type.Value == ConfigurationType.OAuth.Value)
103137
TokenService.TryGetParameters(SettingsService.AccessTokenDbKey, out accessToken);
104138
else
105139
{

src/Umbraco.Cms.Integrations.Shared/Dependencies.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public void Compose(Composition composition)
1212
composition.Register<IAppSettings, AppSettingsWrapper>(Lifetime.Singleton);
1313

1414
composition.Register<ITokenService, TokenService>(Lifetime.Singleton);
15+
16+
composition.Register<ICacheHelper, CacheHelper>(Lifetime.Singleton);
1517
}
1618
}
1719
}

0 commit comments

Comments
 (0)