Skip to content

Commit

Permalink
Merge pull request #95 from vtex-apps/bugfix/WISHLISTS-25_fix-wishlis…
Browse files Browse the repository at this point in the history
…t-api

[WISHLISTS-25] fix the wishlist api
  • Loading branch information
auroraaaashen authored Feb 25, 2022
2 parents 20ca961 + 28dc87a commit 7df6f7f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions dotnet/Controlers/RoutesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ public RoutesController(IIOServiceContext context, IWishListRepository wishListR
public async Task<IActionResult> ExportAllLists()
{
WishListsWrapper wishListsWrapper = await _wishListRepository.GetAllLists();
var queryString = HttpContext.Request.Query;
int from = int.Parse(queryString["from"]);
int to = int.Parse(queryString["to"]);
IList<WishListWrapper> wishListsWrappers = wishListsWrapper.WishLists;
wishListsWrapper.WishLists = wishListsWrappers.Skip(from - 1).Take(to - from).ToList();


return Json(wishListsWrapper);
}
}
Expand Down
76 changes: 71 additions & 5 deletions dotnet/Data/WishListRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -229,12 +240,13 @@ public async Task VerifySchema()
}
}

public async Task<WishListsWrapper> GetAllLists()
private async Task<string> 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];
Expand All @@ -244,19 +256,73 @@ public async Task<WishListsWrapper> 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<string> 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<WishListsWrapper> 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>();
WishListWrapper responseListWrapper = new WishListWrapper();

try
{
JArray searchResult = JArray.Parse(responseContent);
for (int l = 0; l < searchResult.Count; l++)
{
JToken listWrapper = searchResult[l];
Expand Down
32 changes: 5 additions & 27 deletions react/WishlistAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ const WishlistAdmin: FC<any> = ({ 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 = []
Expand Down Expand Up @@ -54,28 +45,15 @@ const WishlistAdmin: FC<any> = ({ 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 })
}

Expand Down

0 comments on commit 7df6f7f

Please sign in to comment.