Skip to content

Commit

Permalink
fix/force setProfile to use valid costCenter - [KI 945609] (#162)
Browse files Browse the repository at this point in the history
* fix: force setProfile to use valid costCenter

* feat: increase timeout
  • Loading branch information
giurigaud authored Oct 15, 2024
1 parent c23f936 commit a0a068c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
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.12] - 2024-10-14

### Added
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
20 changes: 19 additions & 1 deletion 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
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,
"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

0 comments on commit a0a068c

Please sign in to comment.