diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e1c64e..a0ca278 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Fixed + +- Force setProfile to use a valid cost center +- Increase timeout to 45 seconds + ## [1.44.12] - 2024-10-14 ### Added diff --git a/node/clients/Organizations.ts b/node/clients/Organizations.ts index 3dc105a..7965bd7 100644 --- a/node/clients/Organizations.ts +++ b/node/clients/Organizations.ts @@ -3,7 +3,10 @@ import { AppGraphQLClient } from '@vtex/api' import { QUERIES } from '../resolvers/Routes/utils' import { getTokenToHeader } from './index' -import type { GetCostCenterType } from '../typings/custom' +import type { + GetCostCenterType, + GetOrganizationsByEmailResponse, +} from '../typings/custom' const getPersistedQuery = () => { return { @@ -57,12 +60,12 @@ export class OrganizationsGraphQLClient extends AppGraphQLClient { }) } - public getOrganizationsByEmail = async (email: string): Promise => { + public getOrganizationsByEmail = async (email: string) => { return this.query({ extensions: getPersistedQuery(), query: QUERIES.getOrganizationsByEmail, variables: { email }, - }) + }) as Promise } private query = async (param: { diff --git a/node/resolvers/Routes/index.ts b/node/resolvers/Routes/index.ts index 2ea7f1d..fc5a98a 100644 --- a/node/resolvers/Routes/index.ts +++ b/node/resolvers/Routes/index.ts @@ -186,7 +186,6 @@ export const Routes = { userId: string name: string } - email = user.email let { userId } = user @@ -268,6 +267,25 @@ export const Routes = { organizations.getB2BSettings(), ]) + // in case the cost center is not found, we need to find a valid cost center for the user + if (!costCenterResponse.data.getCostCenterById.businessDocument) { + try { + const usersByEmail = await organizations.getOrganizationsByEmail(email) + + // when cost center comes without a name, it's because the cost center is deleted + const usersData = usersByEmail.data.getOrganizationsByEmail.find( + (userByEmail) => userByEmail.costCenterName !== null + ) + + user.costId = usersData?.costId ?? user.costId + } catch (error) { + logger.error({ + error, + message: 'setProfile.graphqlGetOrganizationById', + }) + } + } + let organization = organizationResponse?.data?.getOrganizationById // prevent login if org is inactive diff --git a/node/resolvers/Routes/utils/index.ts b/node/resolvers/Routes/utils/index.ts index 6530db0..998ebca 100644 --- a/node/resolvers/Routes/utils/index.ts +++ b/node/resolvers/Routes/utils/index.ts @@ -73,11 +73,12 @@ export const QUERIES = { } }`, getOrganizationsByEmail: `query Organizations($email: String!) { - getOrganizationsByEmail(email: $email) { + getOrganizationsByEmail(email: $email){ id organizationStatus costId orgId + costCenterName } }`, } diff --git a/node/service.json b/node/service.json index d6f136b..8a3a2d4 100644 --- a/node/service.json +++ b/node/service.json @@ -2,7 +2,7 @@ "stack": "nodejs", "memory": 256, "ttl": 60, - "timeout": 30, + "timeout": 45, "cpu": { "type": "shared", "value": 5, diff --git a/node/typings/custom.d.ts b/node/typings/custom.d.ts index 5c1a123..8c6b4d4 100644 --- a/node/typings/custom.d.ts +++ b/node/typings/custom.d.ts @@ -192,6 +192,17 @@ export interface DeliveryId { quantity: number | null } +export interface GetOrganizationsByEmailResponse { + data: { + getOrganizationsByEmail: Array<{ + id: string + organizationStatus: string + orgId: string + costId: string + costCenterName: string + }> + } +} export interface GetCostCenterType { getCostCenterById: { name: string | null