Skip to content

Commit

Permalink
Merge pull request #164 from vtex-apps/feat/user-directives
Browse files Browse the repository at this point in the history
Feat/user directives
  • Loading branch information
cdcs0128 authored Jan 31, 2024
2 parents 1ac3957 + f2f4e57 commit 09ddcba
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 15 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
- Authentication yo queries and mutation added

## [1.16.3] - 2024-01-22

## [1.16.2] - 2024-01-10
Expand Down
38 changes: 34 additions & 4 deletions dotnet/GraphQL/Mutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
using WishList.Models;
using WishList.Services;

// using Newtonsoft.Json;
using System;
using System.Linq;
using System.Net;

namespace WishList.GraphQL
{
[GraphQLMetadata("Mutation")]
Expand All @@ -14,16 +19,28 @@ public Mutation(IWishListService wishListService)
{
Name = "Mutation";

Field<IntGraphType>(
FieldAsync<IntGraphType>(
"addToList",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<ListItemInputType>> { Name = "listItem" },
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "shopperId" },
new QueryArgument<StringGraphType> { Name = "name" },
new QueryArgument<BooleanGraphType> { Name = "public" }
),
resolve: context =>
resolve: async context =>
{

HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser();
if (isValidAuthUser != HttpStatusCode.OK)
{
context.Errors.Add(new ExecutionError(isValidAuthUser.ToString())
{
Code = isValidAuthUser.ToString()
});

return null;
}

var listItem = context.GetArgument<ListItem>("listItem");
string shopperId = context.GetArgument<string>("shopperId");
string listName = context.GetArgument<string>("name");
Expand All @@ -32,15 +49,28 @@ public Mutation(IWishListService wishListService)
return wishListService.SaveItem(listItem, shopperId, listName, isPublic);
});

Field<BooleanGraphType>(
FieldAsync<BooleanGraphType>(
"removeFromList",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<IdGraphType>> { Name = "id" },
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "shopperId" },
new QueryArgument<StringGraphType> { Name = "name" }
),
resolve: context =>
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 id = context.GetArgument<int>("id");
string shopperId = context.GetArgument<string>("shopperId");
string listName = context.GetArgument<string>("name");
Expand Down
47 changes: 47 additions & 0 deletions dotnet/GraphQL/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ public Query(IWishListService wishListService)
),
resolve: async context =>
{

HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser();
if (isValidAuthUser != HttpStatusCode.OK)
{
context.Errors.Add(new ExecutionError(isValidAuthUser.ToString())
{
Code = isValidAuthUser.ToString()
});

return null;
}

string shopperId = context.GetArgument<string>("shopperId");
string name = context.GetArgument<string>("name");
int from = context.GetArgument<int>("from");
Expand Down Expand Up @@ -84,6 +96,18 @@ public Query(IWishListService wishListService)
),
resolve: async context =>
{

HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser();
if (isValidAuthUser != HttpStatusCode.OK)
{
context.Errors.Add(new ExecutionError(isValidAuthUser.ToString())
{
Code = isValidAuthUser.ToString()
});

return null;
}

string shopperId = context.GetArgument<string>("shopperId");
int from = context.GetArgument<int>("from");
int to = context.GetArgument<int>("to");
Expand Down Expand Up @@ -136,6 +160,7 @@ public Query(IWishListService wishListService)

return resultLists;
}

);

FieldAsync<CheckListType>(
Expand All @@ -147,6 +172,18 @@ public Query(IWishListService wishListService)
),
resolve: async context =>
{

HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser();
if (isValidAuthUser != HttpStatusCode.OK)
{
context.Errors.Add(new ExecutionError(isValidAuthUser.ToString())
{
Code = isValidAuthUser.ToString()
});

return null;
}

string shopperId = context.GetArgument<string>("shopperId");
string productId = context.GetArgument<string>("productId");
string sku = context.GetArgument<string>("sku");
Expand Down Expand Up @@ -205,6 +242,16 @@ public Query(IWishListService wishListService)
),
resolve: async context =>
{
HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser();
if (isValidAuthUser != HttpStatusCode.OK)
{
context.Errors.Add(new ExecutionError(isValidAuthUser.ToString())
{
Code = isValidAuthUser.ToString()
});

return null;
}
string shopperId = context.GetArgument<string>("shopperId");
ResponseListWrapper allLists = await wishListService.GetLists(shopperId);
IList<ListItemsWrapper> listItemsWrappers = allLists.ListItemsWrapper;
Expand Down
5 changes: 3 additions & 2 deletions dotnet/Services/WishListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,16 @@ public async Task<ValidatedUser> ValidateUserToken(string token)

public async Task<HttpStatusCode> IsValidAuthUser()
{
if (string.IsNullOrEmpty(_context.Vtex.AdminUserAuthToken))

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

ValidatedUser validatedUser = null;

try {
validatedUser = await ValidateUserToken(_context.Vtex.AdminUserAuthToken);
validatedUser = await ValidateUserToken(_context.Vtex.StoreUserAuthToken);
}
catch (Exception ex)
{
Expand Down
13 changes: 4 additions & 9 deletions react/AddProductBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React, {
} from 'react'
import { useMutation, useLazyQuery } from 'react-apollo'
import { defineMessages, useIntl } from 'react-intl'
import { useProduct } from 'vtex.product-context'
import { ProductContext } from 'vtex.product-context'
import { Button, ToastContext } from 'vtex.styleguide'
import { useRuntime, NoSSR } from 'vtex.render-runtime'
import { useCssHandles } from 'vtex.css-handles'
Expand Down Expand Up @@ -153,17 +153,14 @@ const AddBtn: FC<AddBtnProps> = ({ toastURL = '/account/#wishlist' }) => {
const { push } = usePixel()
const handles = useCssHandles(CSS_HANDLES)
const { showToast } = useContext(ToastContext)
const productContext = useProduct()
const { selectedItem, product } = productContext
const { selectedItem, product } = useContext(ProductContext) as any

Check warning on line 156 in react/AddProductBtn.tsx

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
const sessionResponse: any = useSessionResponse()
const [handleCheck, { data, loading, called }] = useLazyQuery(checkItem)

const [productId] = String(product?.productId).split('-')
const sku = product?.items?.[0]?.itemId
const sku = product?.sku?.itemId
wishListed = JSON.parse(localStore.getItem('wishlist_wishlisted')) ?? []

const productContextScoped = useProduct()

const toastMessage = (messsageKey: string, linkWishlist: string) => {
let action: any
if (messsageKey === 'notLogged') {
Expand Down Expand Up @@ -307,14 +304,12 @@ const AddBtn: FC<AddBtnProps> = ({ toastURL = '/account/#wishlist' }) => {
})
pixelEvent.event = 'removeToWishlist'
} else {
const { selectedItem: selectedItemScoped } = productContextScoped

addProduct({
variables: {
listItem: {
productId,
title: product.productName,
sku: selectedItemScoped.itemId,
sku: selectedItem.itemId,
},
shopperId,
name: defaultValues.LIST_NAME,
Expand Down

0 comments on commit 09ddcba

Please sign in to comment.