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

fix/force setProfile to use valid costCenter - [KI 945609] #162

Merged
merged 12 commits into from
Oct 15, 2024
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.11] - 2024-10-10

### Fixed
Expand Down
9 changes: 6 additions & 3 deletions node/clients/Organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -57,12 +60,12 @@ export class OrganizationsGraphQLClient extends AppGraphQLClient {
})
}

public getOrganizationsByEmail = async (email: string): Promise<unknown> => {
public getOrganizationsByEmail = async (email: string) => {
return this.query({
extensions: getPersistedQuery(),
query: QUERIES.getOrganizationsByEmail,
variables: { email },
})
}) as Promise<GetOrganizationsByEmailResponse>
}

private query = async (param: {
Expand Down
22 changes: 20 additions & 2 deletions node/resolvers/Routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ export const Routes = {
userId: string
name: string
}

email = user.email
let { userId } = user

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -386,7 +404,7 @@ export const Routes = {
// Only require CPF if cost center contains an address in Brazil
// This is a workaround to avoid setting CPF as documentType for countries other than Brazil
if (
costCenterResponse?.data?.getCostCenterById?.addresses.some(
costCenterResponse?.data?.getCostCenterById?.addresses?.some(
(address: { country: string }) => address.country === 'BRA'
)
) {
Expand Down
3 changes: 2 additions & 1 deletion node/resolvers/Routes/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ export const QUERIES = {
}
}`,
getOrganizationsByEmail: `query Organizations($email: String!) {
getOrganizationsByEmail(email: $email) {
getOrganizationsByEmail(email: $email){
id
organizationStatus
costId
orgId
costCenterName
}
}`,
}
Expand Down
2 changes: 1 addition & 1 deletion node/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"stack": "nodejs",
"memory": 256,
"ttl": 60,
"timeout": 30,
"timeout": 45,
enzomerca marked this conversation as resolved.
Show resolved Hide resolved
"cpu": {
"type": "shared",
"value": 5,
Expand Down
11 changes: 11 additions & 0 deletions node/typings/custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading