Skip to content

Commit

Permalink
Merge branch 'master' into fix/B2BTEAM-1891-add-store-user-token-vali…
Browse files Browse the repository at this point in the history
…dation
  • Loading branch information
enzomerca authored Oct 8, 2024
2 parents 00215ac + be822ba commit 7020cad
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 19 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Add new store user token validation directive to some APIs

## [0.59.0] - 2024-10-08
### Added
- Add permission createQuote to organization

## [0.58.0] - 2024-10-07

### Added
- Add new admin token validation directive to getOrganizationsWithoutSalesManager

## [0.57.1] - 2024-09-30

### Fixed
Expand Down
12 changes: 11 additions & 1 deletion graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Query {

getOrganizationsWithoutSalesManager: [Organization]
@cacheControl(scope: PRIVATE)
@auditAccess
@validateAdminUserAccess

getOrganizationById(id: ID): Organization
@cacheControl(scope: PRIVATE)
Expand Down Expand Up @@ -180,6 +180,7 @@ type Mutation {
customFields: [CustomFieldInput]
salesChannel: String
sellers: [SellerInput]
permissions: PermissionsInput
notifyUsers: Boolean
): MutationResponse @checkAdminAccess @cacheControl(scope: PRIVATE)
updateCostCenter(id: ID!, input: CostCenterInput!): MutationResponse
Expand Down Expand Up @@ -387,6 +388,7 @@ type Organization {
priceTables: [String]
sellers: [Seller]
customFields: [CustomField]
permissions: Permissions
salesChannel: String
costCenters: [ID]
status: String
Expand Down Expand Up @@ -593,6 +595,14 @@ input CostCenterInput {
stateRegistration: String
}

input PermissionsInput {
createQuote: Boolean
}

type Permissions {
createQuote: Boolean
}

input AddressInput {
addressId: String
addressType: String
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "b2b-organizations-graphql",
"vendor": "vtex",
"version": "0.57.1",
"version": "0.59.0",
"title": "B2B Organizations",
"description": "App to create and manage B2B Organizations and Cost Centers",
"mustUpdateAt": "2022-08-28",
Expand Down
10 changes: 10 additions & 0 deletions node/mdSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const ORGANIZATION_FIELDS = [
'status',
'created',
'customFields',
'permissions',
]
export const ORGANIZATION_SCHEMA_VERSION = 'v0.0.8'

Expand Down Expand Up @@ -166,6 +167,15 @@ export const schemas = [
type: 'array',
title: 'Custom Fields',
},
permissions: {
type: 'object',
title: 'Permissions',
properties: {
createQuote: {
type: 'boolean',
},
},
},
},
'v-indexed': ['name', 'status', 'created'],
'v-immediate-indexing': true,
Expand Down
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vtex.b2b-organizations",
"version": "0.57.1",
"version": "0.59.0",
"dependencies": {
"@types/lodash": "4.14.74",
"@vtex/api": "6.47.0",
Expand Down
6 changes: 6 additions & 0 deletions node/resolvers/Mutations/Organizations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ describe('given an Organization Mutation', () => {
name: input.name,
status: 'active',
tradeName: input.tradeName,
permissions: {
createQuote: true,
},
},
schema: ORGANIZATION_SCHEMA_VERSION,
})
Expand Down Expand Up @@ -365,6 +368,9 @@ describe('given an Organization Mutation', () => {
name: input.name,
status: 'active',
tradeName: input.tradeName,
permissions: {
createQuote: true,
},
},
schema: ORGANIZATION_SCHEMA_VERSION,
})
Expand Down
7 changes: 6 additions & 1 deletion node/resolvers/Mutations/Organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const createOrganization = async (
...(collections && { collections }),
customFields: customFields ?? [],
status: ORGANIZATION_STATUSES.ACTIVE,
permissions: { createQuote: true },
}

return masterdata.createDocument({
Expand Down Expand Up @@ -219,6 +220,7 @@ const createOrganizationAndCostCenterWithAdminUser = async (
defaultCostCenter: organization.defaultCostCenter,
costCenters: organization.costCenters,
customFields: organization.customFields,
permissions: { createQuote: true },
paymentTerms: organization.paymentTerms
? await findPaymentTerms(organization.paymentTerms, ctx)
: [],
Expand Down Expand Up @@ -613,6 +615,7 @@ const Organizations = {
salesChannel,
sellers,
notifyUsers = true,
permissions,
}: {
id: string
name: string
Expand All @@ -625,6 +628,7 @@ const Organizations = {
salesChannel?: string
sellers?: any[]
notifyUsers?: boolean
permissions?: Permissions
},
ctx: Context
) => {
Expand Down Expand Up @@ -680,6 +684,7 @@ const Organizations = {
...(salesChannel && { salesChannel }),
...(sellers && { sellers }),
status,
permissions,
}

await masterdata.updatePartialDocument({
Expand Down Expand Up @@ -766,7 +771,7 @@ const Organizations = {
// the following copy is fine as organizationRequest does not contain fields that need transformation
const normalizedOrganizationRequest = {
...organizationRequest,
} as NormalizedOrganizationInput
} as unknown as NormalizedOrganizationInput

const { id: organizationId } =
await createOrganizationAndCostCenterWithAdminUser(
Expand Down
6 changes: 4 additions & 2 deletions node/resolvers/Mutations/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ const Users = {

if (!user) return

const id = user.id
const userId = user.userId
const { id } = user
const { userId } = user

const fields = {
email,
Expand All @@ -402,13 +402,15 @@ const Users = {
email,
})
sendRemoveUserMetric(ctx, logger, ctx.vtex.account, fields)

return response.data.deleteUser
})
.catch((error: any) => {
logger.error({
error,
message: 'removeUser-deleteUserError',
})

return { status: 'error', message: error }
})
})
Expand Down
53 changes: 40 additions & 13 deletions node/resolvers/Queries/Organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ORGANIZATION_REQUEST_SCHEMA_VERSION,
ORGANIZATION_SCHEMA_VERSION,
} from '../../mdSchema'
import type { Organization } from '../../typings'
import GraphQLError, { getErrorMessage } from '../../utils/GraphQLError'
import checkConfig from '../config'

Expand Down Expand Up @@ -66,9 +67,7 @@ const Organizations = {
_,
{ id: orgId },
ctx
)) as {
status: string
}
)) as { status: string; permissions?: { createQuote: boolean } }

if (!organization) {
throw new Error('Organization not found')
Expand All @@ -91,11 +90,18 @@ const Organizations = {
await checkConfig(ctx)

try {
return await masterdata.getDocument({
const org: Organization = await masterdata.getDocument({
dataEntity: ORGANIZATION_DATA_ENTITY,
fields: ORGANIZATION_FIELDS,
id,
})

return {
...org,
// the previous data registered doesn't have this propertty on masterdata
// so we need to add it to the response
permissions: org.permissions ?? { createQuote: true },
}
} catch (error) {
logger.error({ error, message: 'getOrganizationById-error' })
throw new GraphQLError(getErrorMessage(error))
Expand Down Expand Up @@ -138,14 +144,32 @@ const Organizations = {
const where = whereArray.join(' AND ')

try {
return await masterdata.searchDocumentsWithPaginationInfo({
dataEntity: ORGANIZATION_DATA_ENTITY,
fields: ORGANIZATION_FIELDS,
pagination: { page, pageSize },
schema: ORGANIZATION_SCHEMA_VERSION,
sort: `${sortedBy} ${sortOrder}`,
...(where && { where }),
const organizationsDB =
(await masterdata.searchDocumentsWithPaginationInfo({
dataEntity: ORGANIZATION_DATA_ENTITY,
fields: ORGANIZATION_FIELDS,
pagination: { page, pageSize },
schema: ORGANIZATION_SCHEMA_VERSION,
sort: `${sortedBy} ${sortOrder}`,
...(where && { where }),
})) as {
data: Organization[]
pagination: { total: number; page: number; pageSize: number }
}

const mappedOrganizations = organizationsDB.data.map((org) => {
return {
...org,
// the previous data registered doesn't have this propertty on masterdata
// so we need to add it to the response
permissions: org.permissions ?? { createQuote: true },
}
})

return {
data: mappedOrganizations,
pagination: organizationsDB.pagination,
}
} catch (error) {
logger.error({
error,
Expand Down Expand Up @@ -275,7 +299,7 @@ const Organizations = {
throw new GraphQLError('operation-not-permitted')
}

const organization = await masterdata.getDocument({
const organization: Organization = await masterdata.getDocument({
dataEntity: ORGANIZATION_DATA_ENTITY,
fields: ORGANIZATION_FIELDS,
id,
Expand All @@ -286,7 +310,10 @@ const Organizations = {
}

try {
return organization
return {
...organization,
permissions: organization.permissions ?? { createQuote: true },
}
} catch (error) {
logger.error({
error,
Expand Down
1 change: 1 addition & 0 deletions node/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ interface Organization {
costCenters: string[]
paymentTerms: PaymentTerm[]
priceTables?: string[]
permissions?: Permissions
status: string
created: string
customFields?: CustomField[]
Expand Down

0 comments on commit 7020cad

Please sign in to comment.