From be48c2ad4cb1b9609d732d007de316f027e9081b Mon Sep 17 00:00:00 2001 From: aurorahey Date: Thu, 24 Feb 2022 23:36:03 -0500 Subject: [PATCH 1/2] [WISHLISTS-25] fix the wishlist api --- CHANGELOG.md | 4 ++ dotnet/Controlers/RoutesController.cs | 7 +-- dotnet/Data/WishListRepository.cs | 76 +++++++++++++++++++++++++-- react/WishlistAdmin.tsx | 32 ++--------- 4 files changed, 81 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d4ed05..41451ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Fixed + +- Fix the api for downloading all the wishlist records + ## [1.10.0] - 2022-01-28 ### Added diff --git a/dotnet/Controlers/RoutesController.cs b/dotnet/Controlers/RoutesController.cs index abbdec6..6a4f3ef 100644 --- a/dotnet/Controlers/RoutesController.cs +++ b/dotnet/Controlers/RoutesController.cs @@ -26,12 +26,7 @@ public RoutesController(IIOServiceContext context, IWishListRepository wishListR public async Task ExportAllLists() { WishListsWrapper wishListsWrapper = await _wishListRepository.GetAllLists(); - var queryString = HttpContext.Request.Query; - int from = int.Parse(queryString["from"]); - int to = int.Parse(queryString["to"]); - IList wishListsWrappers = wishListsWrapper.WishLists; - wishListsWrapper.WishLists = wishListsWrappers.Skip(from - 1).Take(to - from).ToList(); - + return Json(wishListsWrapper); } } diff --git a/dotnet/Data/WishListRepository.cs b/dotnet/Data/WishListRepository.cs index 9f0787f..0c85cd1 100644 --- a/dotnet/Data/WishListRepository.cs +++ b/dotnet/Data/WishListRepository.cs @@ -24,7 +24,18 @@ public class WishListRepository : IWishListRepository private readonly IHttpClientFactory _clientFactory; private readonly IIOServiceContext _context; private readonly string _applicationName; - + private static string _tokenResponse; + public static string tokenResponse + { + get + { + return _tokenResponse; + } + set + { + _tokenResponse = value; + } + } public WishListRepository(IVtexEnvironmentVariableProvider environmentVariableProvider, IHttpContextAccessor httpContextAccessor, IHttpClientFactory clientFactory, IIOServiceContext context) { @@ -229,12 +240,13 @@ public async Task VerifySchema() } } - public async Task GetAllLists() + private async Task firstScroll() { + var client = _clientFactory.CreateClient(); var request = new HttpRequestMessage { Method = HttpMethod.Get, - RequestUri = new Uri($"http://{this._httpContextAccessor.HttpContext.Request.Headers[WishListConstants.VTEX_ACCOUNT_HEADER_NAME]}.vtexcommercestable.com.br/api/dataentities/{WishListConstants.DATA_ENTITY}/scroll?_fields=email,ListItemsWrapper") + RequestUri = new Uri($"http://{this._httpContextAccessor.HttpContext.Request.Headers[WishListConstants.VTEX_ACCOUNT_HEADER_NAME]}.vtexcommercestable.com.br/api/dataentities/{WishListConstants.DATA_ENTITY}/scroll?_size=200&_fields=email,ListItemsWrapper") }; string authToken = this._httpContextAccessor.HttpContext.Request.Headers[WishListConstants.HEADER_VTEX_CREDENTIAL]; @@ -244,19 +256,73 @@ public async Task GetAllLists() request.Headers.Add(WishListConstants.VtexIdCookie, authToken); request.Headers.Add(WishListConstants.PROXY_AUTHORIZATION_HEADER_NAME, authToken); } - request.Headers.Add("Cache-Control", "no-cache"); + var response = await client.SendAsync(request); + tokenResponse = response.Headers.GetValues("X-VTEX-MD-TOKEN").FirstOrDefault(); + + string responseContent = await response.Content.ReadAsStringAsync(); + _context.Vtex.Logger.Debug("GetAllLists", null, $"[{response.StatusCode}]"); + + return responseContent; + } + private async Task subScroll() + { var client = _clientFactory.CreateClient(); + var request = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri($"http://{this._httpContextAccessor.HttpContext.Request.Headers[WishListConstants.VTEX_ACCOUNT_HEADER_NAME]}.vtexcommercestable.com.br/api/dataentities/{WishListConstants.DATA_ENTITY}/scroll?_token={tokenResponse}") + }; + + string authToken = this._httpContextAccessor.HttpContext.Request.Headers[WishListConstants.HEADER_VTEX_CREDENTIAL]; + if (authToken != null) + { + request.Headers.Add(WishListConstants.AUTHORIZATION_HEADER_NAME, authToken); + request.Headers.Add(WishListConstants.VtexIdCookie, authToken); + request.Headers.Add(WishListConstants.PROXY_AUTHORIZATION_HEADER_NAME, authToken); + } + request.Headers.Add("Cache-Control", "no-cache"); var response = await client.SendAsync(request); + string responseContent = await response.Content.ReadAsStringAsync(); _context.Vtex.Logger.Debug("GetAllLists", null, $"[{response.StatusCode}]"); + + return responseContent; + } + public async Task GetAllLists() + { + var i = 0; + var status = true; + JArray searchResult = new JArray(); + + while (status) + { + if( i == 0) + { + var res = await firstScroll(); + JArray resArray = JArray.Parse(res); + searchResult.Merge(resArray); + } + else + { + var res = await subScroll(); + JArray resArray = JArray.Parse(res); + if (resArray.Count < 200) + { + status = false; + } + searchResult.Merge(resArray); + } + i++; + } + WishListsWrapper wishListsWrapper = new WishListsWrapper(); wishListsWrapper.WishLists = new List(); WishListWrapper responseListWrapper = new WishListWrapper(); + try { - JArray searchResult = JArray.Parse(responseContent); for (int l = 0; l < searchResult.Count; l++) { JToken listWrapper = searchResult[l]; diff --git a/react/WishlistAdmin.tsx b/react/WishlistAdmin.tsx index 49db33a..980a53d 100644 --- a/react/WishlistAdmin.tsx +++ b/react/WishlistAdmin.tsx @@ -18,15 +18,6 @@ const WishlistAdmin: FC = ({ intl }) => { const { loading } = state - const fetchWishlists = async (from: number, to: number) => { - const response: any = await fetch( - `/_v/wishlist/export-lists?from=${from}&to=${to}`, - { mode: 'no-cors' } - ) - - return response.json() - } - const downloadWishlist = (allWishlists: any) => { const header = ['Email', 'Product ID', 'SKU', 'Title'] const data: any = [] @@ -54,28 +45,15 @@ const WishlistAdmin: FC = ({ intl }) => { XLSX.writeFile(wb, exportFileName) } - let allWishlists: any = [] - const getAllWishlists = async () => { - let status = true - let i = 0 - const chunkLength = 100 setState({ ...state, loading: true }) - while (status) { - await fetchWishlists(i, i + chunkLength).then(data => { - const wishlistArr = data.wishLists - - if (!wishlistArr.length) { - status = false - } - allWishlists = [...allWishlists, ...wishlistArr] - }) - - i += 100 - } + const data: any = await fetch(`/_v/wishlist/export-lists`).then(response => + response.json() + ) + const wishlistArr = data.wishLists - downloadWishlist(allWishlists) + downloadWishlist(wishlistArr) setState({ ...state, loading: false }) } From 28dc87a54a04d4fe8f49639e806cf0560ed7704f Mon Sep 17 00:00:00 2001 From: aurorahey Date: Fri, 25 Feb 2022 10:54:18 -0500 Subject: [PATCH 2/2] changes from code review --- dotnet/Data/WishListRepository.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dotnet/Data/WishListRepository.cs b/dotnet/Data/WishListRepository.cs index 0c85cd1..39aacfe 100644 --- a/dotnet/Data/WishListRepository.cs +++ b/dotnet/Data/WishListRepository.cs @@ -240,7 +240,7 @@ public async Task VerifySchema() } } - private async Task firstScroll() + private async Task FirstScroll() { var client = _clientFactory.CreateClient(); var request = new HttpRequestMessage @@ -266,7 +266,7 @@ private async Task firstScroll() return responseContent; } - private async Task subScroll() + private async Task SubScroll() { var client = _clientFactory.CreateClient(); var request = new HttpRequestMessage @@ -300,13 +300,13 @@ public async Task GetAllLists() { if( i == 0) { - var res = await firstScroll(); + var res = await FirstScroll(); JArray resArray = JArray.Parse(res); searchResult.Merge(resArray); } else { - var res = await subScroll(); + var res = await SubScroll(); JArray resArray = JArray.Parse(res); if (resArray.Count < 200) {