From 6feb7ee388e23097288327949fcb17d66955a42a Mon Sep 17 00:00:00 2001 From: Anders Gustafsson <34234789+anders-photowall@users.noreply.github.com> Date: Tue, 23 May 2023 08:40:44 +0200 Subject: [PATCH] 3769 - Differentiate forbidden and unauthenticated - add product group filter (#153) --- Dockerfile | 2 +- Dockerfile-dev | 2 +- README.md | 2 +- src/cognito/access-control.ts | 2 +- src/cognito/cognito-client.ts | 2 +- src/resolvers/products-resolver.ts | 12 ++++++++++++ src/schema.graphql | 2 +- 7 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index aed1b0f..c8c83b0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,4 +12,4 @@ ENV NEW_RELIC_NO_CONFIG_FILE=true # Installs npm dependencies on container RUN npm ci # Command container will actually run when called -CMD ["npx", "nodemon", "-w", "src", "--ext", "ts", "--exec", "TZ=Europe/Paris", "ts-node", "src/index.ts"] +CMD ["npx", "nodemon", "--exec", "TZ=Europe/Paris", "ts-node", "src/index.ts"] diff --git a/Dockerfile-dev b/Dockerfile-dev index 82f8bc8..b33d80e 100644 --- a/Dockerfile-dev +++ b/Dockerfile-dev @@ -9,4 +9,4 @@ WORKDIR /app ENV NODE_ENV=development ENV NEW_RELIC_NO_CONFIG_FILE=true # Command container will actually run when called -CMD ["npx", "nodemon", "-w", "src", "--ext", "ts", "--exec", "TZ=Europe/Paris", "ts-node", "src/index.ts"] +CMD ["npx", "nodemon", "-w", "src", "--ext", "ts, graphql", "--exec", "TZ=Europe/Paris", "ts-node", "src/index.ts"] diff --git a/README.md b/README.md index 2693c4d..7303964 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ COGNITO_LOGIN_CLIENT_ID and COGNITO_LOGIN_CLIENT_SECRET found in cognito `photow ### Development -This project runs in docker, so when developing start the docker environment and it will restart automatically when files are changed. Sometimes the watch for files doesn't trigger. Like when you edit the .graphql file. Then just edit a .ts file and it will reload. +This project runs in docker, so when developing start the docker environment and it will restart automatically when .graphql (schema) or .ts files are changed. Run diff --git a/src/cognito/access-control.ts b/src/cognito/access-control.ts index 0d0c3d8..6b531ac 100644 --- a/src/cognito/access-control.ts +++ b/src/cognito/access-control.ts @@ -51,7 +51,7 @@ function throwForbidden() { extensions: { code: 'FORBIDDEN', http: { - status: 401, + status: 403, }, }, }); diff --git a/src/cognito/cognito-client.ts b/src/cognito/cognito-client.ts index a1fb9c9..175d6e7 100644 --- a/src/cognito/cognito-client.ts +++ b/src/cognito/cognito-client.ts @@ -187,7 +187,7 @@ const verifyAuthentication = async (req: IncomingMessage) => { const notAuthorizedError = () => { throw new GraphQLError('You are not authorized', { extensions: { - code: 'FORBIDDEN', + code: 'UNAUTHENTICATED', http: { status: 401, }, diff --git a/src/resolvers/products-resolver.ts b/src/resolvers/products-resolver.ts index e94722f..79a4f64 100644 --- a/src/resolvers/products-resolver.ts +++ b/src/resolvers/products-resolver.ts @@ -3,6 +3,7 @@ import { Orientation, PrintProduct, Product, + ProductGroup, ProductListResult, ProductsFilterInput, ProductsSearchResult, @@ -32,6 +33,17 @@ const ProductBlacklist = { }; const Product = { + async printProducts( + product: { printProducts: PrintProduct[] }, + input: { groups: ProductGroup[] }, + ) { + if (!input.groups) { + return product.printProducts; + } + return product.printProducts.filter((printProduct) => { + return input.groups.includes(printProduct.group); + }); + }, async categories({ id }, _args, { dataSources }): Promise> { return (dataSources.categoryApi).getProductCategories(id); }, diff --git a/src/schema.graphql b/src/schema.graphql index 8e19919..8d94293 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -254,7 +254,7 @@ type Product { inserted: DateTime! updated: DateTime publishingDate: DateTime! - printProducts: [PrintProduct] + printProducts(groups: [ProductGroup]): [PrintProduct] blacklisting: [ProductBlacklist] categories: [Category] designerId: Int