Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/user directives #164

Merged
merged 6 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -179,7 +179,7 @@
return wasRemoved;
}

public async Task<IList<ListItem>> LimitList(IList<ListItem> listItems, int from, int to)

Check warning on line 182 in dotnet/Services/WishListService.cs

View workflow job for this annotation

GitHub Actions / QE / Lint .Net

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 182 in dotnet/Services/WishListService.cs

View workflow job for this annotation

GitHub Actions / QE / Lint .Net

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
int take = MaximumReturnedRecords;
if (to > 0)
Expand Down Expand Up @@ -237,15 +237,16 @@

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 @@
} 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 All @@ -21,7 +21,7 @@
import removeFromList from './queries/removeFromList.gql'
import styles from './styles.css'

const localStore: any = storageFactory(() => sessionStorage)

Check warning on line 24 in react/AddProductBtn.tsx

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
const CSS_HANDLES = ['wishlistIconContainer', 'wishlistIcon'] as const

type AddBtnProps = {
Expand All @@ -32,7 +32,7 @@
JSON.parse(String(localStore.getItem('wishlist_isAuthenticated'))) ?? false
let shopperId = localStore.getItem('wishlist_shopperId') ?? null
let addAfterLogin = localStore.getItem('wishlist_addAfterLogin') ?? null
let wishListed: any =

Check warning on line 35 in react/AddProductBtn.tsx

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
JSON.parse(localStore.getItem('wishlist_wishlisted')) ?? []

const productCheck: {
Expand Down Expand Up @@ -94,10 +94,10 @@
return session
}

const addWishlisted = (productId: any, sku: any) => {

Check warning on line 97 in react/AddProductBtn.tsx

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type

Check warning on line 97 in react/AddProductBtn.tsx

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
if (
wishListed.find(
(item: any) =>

Check warning on line 100 in react/AddProductBtn.tsx

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
item.productId &&
item.sku &&
item.productId === productId &&
Expand All @@ -112,14 +112,14 @@
saveToLocalStorageItem(wishListed)
}

const saveToLocalStorageItem = (data: any): any => {

Check warning on line 115 in react/AddProductBtn.tsx

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type

Check warning on line 115 in react/AddProductBtn.tsx

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
localStore.setItem('wishlist_wishlisted', JSON.stringify(data))
return data
}

const AddBtn: FC<AddBtnProps> = ({ toastURL = '/account/#wishlist' }) => {
const intl = useIntl()
const [state, setState] = useState<any>({

Check warning on line 122 in react/AddProductBtn.tsx

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
isLoading: true,
isWishlistPage: null,
})
Expand All @@ -138,7 +138,7 @@
}

wishListed = wishListed.filter(
(item: any) => item.productId !== productId && item.sku !== sku

Check warning on line 141 in react/AddProductBtn.tsx

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
)
saveToLocalStorageItem(wishListed)

Expand All @@ -153,17 +153,14 @@
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 @@
})
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
Loading