Skip to content

Commit

Permalink
Merge pull request #167 from vtex-apps/fix/add-admin-permission
Browse files Browse the repository at this point in the history
Add Admin permission and ListSize query
  • Loading branch information
cdcs0128 authored Feb 29, 2024
2 parents 23faf4d + c40603b commit 1e51d32
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added
- Admin authentication added

## [1.16.5] - 2024-02-21

### Fixed
Expand Down
1 change: 1 addition & 0 deletions dotnet/Data/IWishListRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface IWishListRepository
Task<ResponseListWrapper> GetWishList(string shopperId);
Task<bool> DeleteWishList(string documentId);
Task VerifySchema();
Task <int> GetListsSize();
Task <WishListsWrapper> GetAllLists();
}
}
43 changes: 43 additions & 0 deletions dotnet/Data/WishListRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,38 @@ private async Task<string> FirstScroll()
return responseContent;
}

private async Task<string> CountList()
{
string countList = string.Empty;

try
{
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}/search?_fields=email")
};

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);
countList = response.Headers.GetValues("REST-Content-Range").FirstOrDefault();
}
catch (Exception ex)
{
_context.Vtex.Logger.Error("First Scroll to Get The Lists", null, "Error: ", ex);
}

return countList;
}

private async Task<string> SubScroll()
{
string responseContent = string.Empty;
Expand Down Expand Up @@ -341,6 +373,17 @@ private async Task<string> SubScroll()
return responseContent;
}

public async Task<int> GetListsSize() {

await this.VerifySchema();
JArray searchResult = new JArray();
var res = await CountList();

string[] subs = res.Split('/');

return Int32.Parse(subs[1]);
}

public async Task<WishListsWrapper> GetAllLists()
{
await this.VerifySchema();
Expand Down
21 changes: 21 additions & 0 deletions dotnet/GraphQL/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,27 @@ public Query(IWishListService wishListService)
}
);

FieldAsync<IntGraphType>(
"listSize",
resolve: async context =>
{
HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser();
if (isValidAuthUser != HttpStatusCode.OK)
{
context.Errors.Add(new ExecutionError(isValidAuthUser.ToString())
{
Code = isValidAuthUser.ToString()
});
return null;
}
int AllListSize = await wishListService.GetListSizeBase();
return AllListSize;
}
);

FieldAsync<ListGraphType<WishListWrapperType>>(
"exportList",
resolve: async context =>
Expand Down
1 change: 1 addition & 0 deletions dotnet/Services/IWishListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IWishListService
Task<IList<ListItem>> LimitList(IList<ListItem> listItems, int from, int to);
Task<HttpStatusCode> IsValidAuthUser();
Task<ValidatedUser> ValidateUserToken(string token);
Task<int> GetListSizeBase();
Task<WishListsWrapper> ExportAllWishLists();
}
}
14 changes: 12 additions & 2 deletions dotnet/Services/WishListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,17 @@ public async Task<ValidatedUser> ValidateUserToken(string token)
public async Task<HttpStatusCode> IsValidAuthUser()
{

if (string.IsNullOrEmpty(_context.Vtex.StoreUserAuthToken))
if (string.IsNullOrEmpty(_context.Vtex.StoreUserAuthToken) && string.IsNullOrEmpty(_context.Vtex.AdminUserAuthToken))
{
return HttpStatusCode.Unauthorized;
}

ValidatedUser validatedUser = null;
ValidatedUser validatedAdminUser = null;

try {
validatedUser = await ValidateUserToken(_context.Vtex.StoreUserAuthToken);
validatedAdminUser = await ValidateUserToken(_context.Vtex.AdminUserAuthToken);
}
catch (Exception ex)
{
Expand All @@ -325,8 +327,9 @@ public async Task<HttpStatusCode> IsValidAuthUser()
}

bool hasPermission = validatedUser != null && validatedUser.AuthStatus.Equals("Success");
bool hasAdminPermission = validatedAdminUser != null && validatedAdminUser.AuthStatus.Equals("Success");

if (!hasPermission)
if (!hasPermission && !hasAdminPermission)
{
_context.Vtex.Logger.Warn("IsValidAuthUser", null, "User Does Not Have Permission");

Expand All @@ -336,6 +339,13 @@ public async Task<HttpStatusCode> IsValidAuthUser()
return HttpStatusCode.OK;
}

public async Task<int> GetListSizeBase()
{

int wishListAllSize = await _wishListRepository.GetListsSize();
return wishListAllSize;
}

public async Task<WishListsWrapper> ExportAllWishLists()
{
WishListsWrapper wishListsWrapper = await _wishListRepository.GetAllLists();
Expand Down
5 changes: 5 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type ListItems {
listItems: [ListItem]
isPublic: Boolean
}
type SizeLength {
lengthList: Int
}

type Query {
viewList(shopperId: String!, name: String!, from: Int, to: Int): WishList
Expand All @@ -50,6 +53,8 @@ type Query {
checkList(shopperId: String!, productId: String!, sku: String): CheckListType
@cacheControl(scope: PRIVATE)
listNames(shopperId: String!): [String]
listSize: Int
@cacheControl(scope: PRIVATE)
exportList: [WishListWrapperType]
@cacheControl(scope: PRIVATE)
}
Expand Down
3 changes: 3 additions & 0 deletions react/queries/listSize.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query ListSise {
listSise @context(provider: "vtex.wish-list")
}

0 comments on commit 1e51d32

Please sign in to comment.