From 4679ef0cd68c2670ec9d32766cc7b729a72db41f Mon Sep 17 00:00:00 2001 From: giurigaud Date: Fri, 18 Oct 2024 12:33:02 -0300 Subject: [PATCH 1/4] fix: getOrganizationsByEmail to return only active organizations --- CHANGELOG.md | 4 +++ node/resolvers/Queries/Organizations.ts | 34 +++++++++++++++++++++---- node/typings.d.ts | 9 +++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64b869c9..6b899bab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added + +- Add getActiveOrganizationsByEmail to return only active organizations ## [0.61.1] - 2024-10-29 ### Fixed - Avoid calls to checkUserPermissions when session data is not available + ## [0.61.0] - 2024-10-16 ### Added diff --git a/node/resolvers/Queries/Organizations.ts b/node/resolvers/Queries/Organizations.ts index f541e1fe..75c3bafc 100644 --- a/node/resolvers/Queries/Organizations.ts +++ b/node/resolvers/Queries/Organizations.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-await-in-loop */ import { ORGANIZATION_DATA_ENTITY, ORGANIZATION_FIELDS, @@ -6,9 +7,13 @@ import { ORGANIZATION_REQUEST_SCHEMA_VERSION, ORGANIZATION_SCHEMA_VERSION, } from '../../mdSchema' -import type { Organization } from '../../typings' +import type { + GetOrganizationsByEmailWithStatus, + Organization, +} from '../../typings' import GraphQLError, { getErrorMessage } from '../../utils/GraphQLError' import checkConfig from '../config' +import { organizationStatus } from '../fieldResolvers' const getWhereByStatus = ({ status }: { status: string[] }) => { const whereArray = [] @@ -182,11 +187,13 @@ const Organizations = { getOrganizationsByEmail: async ( _: void, { email }: { email: string }, - { + ctx: Context + ) => { + const { clients: { storefrontPermissions, session }, vtex: { logger, sessionToken, adminUserAuthToken }, - }: any - ) => { + } = ctx + const organizationFilters: string[] = [] let fromSession = false @@ -259,8 +266,25 @@ const Organizations = { ) }) + const organizationsWithStatus: GetOrganizationsByEmailWithStatus[] = + await Promise.all( + organizations.map(async (organization: { orgId: string }) => { + const status = await organizationStatus( + { orgId: organization.orgId }, + _, + ctx + ) + + return { ...organization, status } + }) + ) + + const activeOrganizations = organizationsWithStatus.filter( + (organization) => organization.status === 'active' + ) + try { - return organizations + return activeOrganizations } catch (error) { logger.error({ error, diff --git a/node/typings.d.ts b/node/typings.d.ts index 29113e57..71468229 100644 --- a/node/typings.d.ts +++ b/node/typings.d.ts @@ -163,6 +163,15 @@ interface Organization { sellers?: Seller[] } +export type GetOrganizationsByEmailWithStatus = { + costId: string + orgId: string + roleId: string + id: string + clId: string + status: string +} + interface Collection { id: string name: string From ba1285f0b343d17d61f1b6e4323951d26aafdf26 Mon Sep 17 00:00:00 2001 From: giurigaud Date: Fri, 25 Oct 2024 10:07:55 -0300 Subject: [PATCH 2/4] feat: add new method getActiveOrganizationsByEmail --- graphql/schema.graphql | 4 +++ node/package.json | 4 +-- node/resolvers/Queries/Organizations.ts | 40 ++++++++++++++++++++++--- node/typings.d.ts | 9 ------ node/yarn.lock | 10 +++---- 5 files changed, 47 insertions(+), 20 deletions(-) diff --git a/graphql/schema.graphql b/graphql/schema.graphql index a6a49414..ced55741 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -93,6 +93,10 @@ type Query { @checkUserAccess @cacheControl(scope: PRIVATE) + getActiveOrganizationsByEmail(email: String): [B2BOrganization] + @checkUserAccess + @cacheControl(scope: PRIVATE) + checkOrganizationIsActive(id: String): Boolean @cacheControl(scope: PRIVATE) @auditAccess diff --git a/node/package.json b/node/package.json index d6e0fc76..fc2c379b 100644 --- a/node/package.json +++ b/node/package.json @@ -3,7 +3,7 @@ "version": "0.61.1", "dependencies": { "@types/lodash": "4.14.74", - "@vtex/api": "6.47.0", + "@vtex/api": "6.48.0", "atob": "^2.1.2", "co-body": "^6.0.0", "graphql": "^14.5.0", @@ -20,7 +20,7 @@ "@types/jsonwebtoken": "^8.5.0", "@types/node": "^12.12.21", "@types/ramda": "types/npm-ramda#dist", - "@vtex/api": "6.47.0", + "@vtex/api": "6.48.0", "@vtex/prettier-config": "^0.3.1", "@vtex/tsconfig": "^0.6.0", "jest": "27.5.1", diff --git a/node/resolvers/Queries/Organizations.ts b/node/resolvers/Queries/Organizations.ts index 75c3bafc..1901732c 100644 --- a/node/resolvers/Queries/Organizations.ts +++ b/node/resolvers/Queries/Organizations.ts @@ -7,14 +7,20 @@ import { ORGANIZATION_REQUEST_SCHEMA_VERSION, ORGANIZATION_SCHEMA_VERSION, } from '../../mdSchema' -import type { - GetOrganizationsByEmailWithStatus, - Organization, -} from '../../typings' +import type { Organization } from '../../typings' import GraphQLError, { getErrorMessage } from '../../utils/GraphQLError' import checkConfig from '../config' import { organizationStatus } from '../fieldResolvers' +export interface GetOrganizationsByEmailWithStatus { + costId: string + orgId: string + roleId: string + id: string + clId: string + status: string +} + const getWhereByStatus = ({ status }: { status: string[] }) => { const whereArray = [] @@ -266,6 +272,32 @@ const Organizations = { ) }) + try { + return organizations + } catch (error) { + logger.error({ + error, + message: 'getOrganizationsByEmail-error', + }) + throw new GraphQLError(getErrorMessage(error)) + } + }, + + getActiveOrganizationsByEmail: async ( + _: void, + { email }: { email: string }, + ctx: Context + ) => { + const { + vtex: { logger }, + } = ctx + + const organizations = await Organizations.getOrganizationsByEmail( + _, + { email }, + ctx + ) + const organizationsWithStatus: GetOrganizationsByEmailWithStatus[] = await Promise.all( organizations.map(async (organization: { orgId: string }) => { diff --git a/node/typings.d.ts b/node/typings.d.ts index 71468229..29113e57 100644 --- a/node/typings.d.ts +++ b/node/typings.d.ts @@ -163,15 +163,6 @@ interface Organization { sellers?: Seller[] } -export type GetOrganizationsByEmailWithStatus = { - costId: string - orgId: string - roleId: string - id: string - clId: string - status: string -} - interface Collection { id: string name: string diff --git a/node/yarn.lock b/node/yarn.lock index afd8369f..54d10540 100644 --- a/node/yarn.lock +++ b/node/yarn.lock @@ -836,10 +836,10 @@ dependencies: "@types/yargs-parser" "*" -"@vtex/api@6.47.0": - version "6.47.0" - resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.47.0.tgz#6910455d593d8bb76f1f4f2b7660023853fda35e" - integrity sha512-t9gt7Q89EMbSj3rLhho+49Fv+/lQgiy8EPVRgtmmXFp1J4v8hIAZF7GPjCPie111KVs4eG0gfZFpmhA5dafKNA== +"@vtex/api@6.48.0": + version "6.48.0" + resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.48.0.tgz#67f9f11d197d543d4f854b057d31a8d6999241e9" + integrity sha512-mAdT7gbV0/BwiuqUkNH1E7KZqTUczT5NbBBZcPJq5kmTr73PUjbR9wh//70ryJo2EAdHlqIgqgwsCVpozenlhg== dependencies: "@types/koa" "^2.11.0" "@types/koa-compose" "^3.2.3" @@ -3475,7 +3475,7 @@ stack-utils@^2.0.3: dependencies: escape-string-regexp "^2.0.0" -stats-lite@vtex/node-stats-lite#dist: +"stats-lite@github:vtex/node-stats-lite#dist": version "2.2.0" resolved "https://codeload.github.com/vtex/node-stats-lite/tar.gz/1b0d39cc41ef7aaecfd541191f877887a2044797" dependencies: From 5214c99ac7d6726f96339cda4d1869c6d15bf856 Mon Sep 17 00:00:00 2001 From: giurigaud Date: Tue, 29 Oct 2024 09:56:00 -0300 Subject: [PATCH 3/4] feat: code review comments --- graphql/schema.graphql | 1 + node/resolvers/Queries/Organizations.ts | 17 +++++------------ node/typings.d.ts | 8 ++++++++ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/graphql/schema.graphql b/graphql/schema.graphql index ced55741..08a869cc 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -96,6 +96,7 @@ type Query { getActiveOrganizationsByEmail(email: String): [B2BOrganization] @checkUserAccess @cacheControl(scope: PRIVATE) + @validateStoreUserAccess checkOrganizationIsActive(id: String): Boolean @cacheControl(scope: PRIVATE) diff --git a/node/resolvers/Queries/Organizations.ts b/node/resolvers/Queries/Organizations.ts index 1901732c..7c78c8d3 100644 --- a/node/resolvers/Queries/Organizations.ts +++ b/node/resolvers/Queries/Organizations.ts @@ -1,4 +1,3 @@ -/* eslint-disable no-await-in-loop */ import { ORGANIZATION_DATA_ENTITY, ORGANIZATION_FIELDS, @@ -7,20 +6,14 @@ import { ORGANIZATION_REQUEST_SCHEMA_VERSION, ORGANIZATION_SCHEMA_VERSION, } from '../../mdSchema' -import type { Organization } from '../../typings' +import type { + GetOrganizationsByEmailWithStatus, + Organization, +} from '../../typings' import GraphQLError, { getErrorMessage } from '../../utils/GraphQLError' import checkConfig from '../config' import { organizationStatus } from '../fieldResolvers' -export interface GetOrganizationsByEmailWithStatus { - costId: string - orgId: string - roleId: string - id: string - clId: string - status: string -} - const getWhereByStatus = ({ status }: { status: string[] }) => { const whereArray = [] @@ -320,7 +313,7 @@ const Organizations = { } catch (error) { logger.error({ error, - message: 'getOrganizationsByEmail-error', + message: 'getActiveOrganizationsByEmail-error', }) throw new GraphQLError(getErrorMessage(error)) } diff --git a/node/typings.d.ts b/node/typings.d.ts index 29113e57..a2fce388 100644 --- a/node/typings.d.ts +++ b/node/typings.d.ts @@ -167,6 +167,14 @@ interface Collection { id: string name: string } +interface GetOrganizationsByEmailWithStatus { + costId: string + orgId: string + roleId: string + id: string + clId: string + status: string +} interface CostCenter { id: string From 456d279954f5cfef31512c61a8ddbc9cc87e99ba Mon Sep 17 00:00:00 2001 From: giurigaud Date: Tue, 29 Oct 2024 11:39:50 -0300 Subject: [PATCH 4/4] feat: remove checkUseerAccess --- graphql/schema.graphql | 1 - 1 file changed, 1 deletion(-) diff --git a/graphql/schema.graphql b/graphql/schema.graphql index 08a869cc..f9c43640 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -94,7 +94,6 @@ type Query { @cacheControl(scope: PRIVATE) getActiveOrganizationsByEmail(email: String): [B2BOrganization] - @checkUserAccess @cacheControl(scope: PRIVATE) @validateStoreUserAccess