Skip to content

Commit

Permalink
Merge pull request #169 from giurigaud/feat/add-permission-org
Browse files Browse the repository at this point in the history
Feat/add permission org
  • Loading branch information
ataideverton authored Oct 8, 2024
2 parents 5a28bfa + 8803e23 commit e29522a
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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 permission createQuote to organization

## [0.58.0] - 2024-10-07

Expand Down
10 changes: 10 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
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
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
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 e29522a

Please sign in to comment.