Skip to content

Commit

Permalink
feat: allow create cost center with an user provided id (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
enzomerca authored Nov 28, 2023
1 parent 675f079 commit ea88577
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- allow creation of cost center with an user provided id

## [0.44.1] - 2023-11-09

### Fixed
Expand Down
7 changes: 7 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ type Mutation {
@withPermissions
@checkUserAccess
@cacheControl(scope: PRIVATE)
createCostCenterWithId(
organizationId: ID
input: CostCenterInput!
): MasterDataResponse
@checkAdminAccess
@cacheControl(scope: PRIVATE)
updateOrganization(
id: ID!
name: String!
Expand Down Expand Up @@ -489,6 +495,7 @@ input DefaultCostCenterInput {
}

input CostCenterInput {
id: String
name: String
addresses: [AddressInput]
paymentTerms: [PaymentTermInput]
Expand Down
4 changes: 2 additions & 2 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.44.1",
"dependencies": {
"@types/lodash": "4.14.74",
"@vtex/api": "6.45.22",
"@vtex/api": "6.46.0",
"atob": "^2.1.2",
"co-body": "^6.0.0",
"graphql": "^14.5.0",
Expand All @@ -20,7 +20,7 @@
"@types/jsonwebtoken": "^8.5.0",
"@types/node": "^12.12.21",
"@types/ramda": "types/npm-ramda#dist",
"@vtex/api": "6.45.22",
"@vtex/api": "6.46.0",
"@vtex/prettier-config": "^0.3.1",
"@vtex/tsconfig": "^0.6.0",
"jest": "27.5.1",
Expand Down
79 changes: 78 additions & 1 deletion node/resolvers/Mutations/CostCenters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import {
COST_CENTER_DATA_ENTITY,
ORGANIZATION_DATA_ENTITY,
} from '../../mdSchema'
import type { AddressInput, CostCenterInput } from '../../typings'
import type {
AddressInput,
CostCenterInput,
CostCenterInputWithId,
} from '../../typings'
import GraphQLError, { getErrorMessage } from '../../utils/GraphQLError'
import Organizations from '../Queries/Organizations'
import costCenters from '../Queries/CostCenters'
import checkConfig from '../config'
import CostCenterRepository from '../repository/CostCenterRepository'

Expand All @@ -13,6 +19,7 @@ const CostCenters = {
{
organizationId,
input: {
id,
name,
addresses,
phoneNumber,
Expand Down Expand Up @@ -58,6 +65,7 @@ const CostCenters = {
businessDocument,
customFields,
marketingTags,
id,
name,
phoneNumber,
sellers,
Expand All @@ -79,6 +87,75 @@ const CostCenters = {
}
},

createCostCenterWithId: async (
_: void,
{
organizationId,
input: {
id,
name,
addresses,
phoneNumber,
businessDocument,
stateRegistration,
customFields,
marketingTags,
sellers,
},
}: { organizationId: string; input: CostCenterInputWithId },
ctx: Context
) => {
// create schema if it doesn't exist
await checkConfig(ctx)

// check if organization exists
const organization = (await Organizations.getOrganizationById(
_,
{ id: organizationId },
ctx
)) as {
status: string
}

if (!organization) {
throw new Error('Organization not found')
}

// check if cost center id already exists
let costCenter = null
try {
costCenter = await costCenters.getCostCenterById(_, { id }, ctx)
} catch (error) {
costCenter = null // cost center does not exist so we don't need to do anything
}

if (costCenter) {
throw new Error('Cost Center already exists')
}

// create cost center
const newCostCenter: CostCenterInput = {
addresses,
businessDocument,
customFields,
marketingTags,
id,
name,
phoneNumber,
sellers,
stateRegistration,
}

const { id: costCenterId } = await CostCenterRepository.createCostCenter(
_,
organizationId,
newCostCenter,
ctx
)

return { id: costCenterId }
},

createCostCenterAddress: async (
_: void,
{ costCenterId, address }: { costCenterId: string; address: AddressInput },
Expand Down
14 changes: 14 additions & 0 deletions node/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ interface CostCenterInput {
sellers?: Seller[]
}

interface CostCenterInputWithId {
id: string
name: string
addresses?: AddressInput[]
paymentTerms?: PaymentTerm[]
phoneNumber?: string
businessDocument?: string
customFields?: CustomField[]
stateRegistration?: string
marketingTags?: string[]
user?: B2BCustomerInput
sellers?: Seller[]
}

interface AddressInput {
addressId: string
addressType: string
Expand Down
8 changes: 4 additions & 4 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -836,10 +836,10 @@
dependencies:
"@types/yargs-parser" "*"

"@vtex/api@6.45.22":
version "6.45.22"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.22.tgz#fa9bbfde1a4d4fbbaf6cce9f6dbc9bb9ee929ba3"
integrity sha512-g5cGUDhF4FADgSMpQmce/bnIZumwGlPG2cabwbQKIQ+cCFMZqOEM/n+YQb1+S8bCyHkzW3u/ZABoyCKi5/nxxg==
"@vtex/api@6.46.0":
version "6.46.0"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.46.0.tgz#208d14b96cbc8fd5eb6bd18fbd0c8424886e6154"
integrity sha512-XAvJlD1FG1GynhPXiMcayunahFCL2r3ilO5MHAWKxYvB/ljyxi4+U+rVpweeaQGpxHfhKHdfPe7qNEEh2oa2lw==
dependencies:
"@types/koa" "^2.11.0"
"@types/koa-compose" "^3.2.3"
Expand Down

0 comments on commit ea88577

Please sign in to comment.