Skip to content

Commit

Permalink
Merge pull request #26 from vtex-apps/feature/graphql-costcenter
Browse files Browse the repository at this point in the history
[B2BORG-78] API mutation to support updating a single cost center address
  • Loading branch information
arturmagalhaesjr authored Apr 4, 2022
2 parents 7128d50 + 301fc3d commit bcca243
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

Added 2 mutations to handle the cost center addresses
- updateCostCenterAddress
- createCostCenterAddress
see schema/schema.graphql for more details


## [0.13.0] - 2022-03-31

### Added
Expand Down
8 changes: 8 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ type Mutation {
updateCostCenter(id: ID!, input: CostCenterInput!): MutationResponse
@withSession
@withPermissions
createCostCenterAddress(
costCenterId: ID
address: AddressInput
): MutationResponse @withSession @withPermissions
updateCostCenterAddress(
costCenterId: ID
address: AddressInput
): MutationResponse @withSession @withPermissions
deleteOrganization(id: ID!): MutationResponse
deleteCostCenter(id: ID!): MutationResponse @withSession @withPermissions
saveUser(
Expand Down
4 changes: 2 additions & 2 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vtex.b2b-organizations",
"version": "0.13.0",
"dependencies": {
"@vtex/api": "6.45.11-beta",
"@vtex/api": "6.45.10",
"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.0.0",
"@types/ramda": "types/npm-ramda#dist",
"@vtex/api": "6.45.11-beta",
"@vtex/api": "6.45.10",
"@vtex/prettier-config": "^0.3.1",
"@vtex/tsconfig": "^0.6.0",
"tslint": "^5.12.0",
Expand Down
106 changes: 106 additions & 0 deletions node/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,112 @@ export const resolvers = {
}
}
},
createCostCenterAddress: async (
_: void,
{
costCenterId,
address,
}: { costCenterId: string; address: AddressInput },
ctx: Context
) => {
const {
clients: { masterdata },
vtex: { logger },
} = ctx

// create schema if it doesn't exist
await checkConfig(ctx)

const costCenter = (await masterdata.getDocument({
dataEntity: COST_CENTER_DATA_ENTITY,
fields: ['addresses'],
id: costCenterId,
})) as CostCenterInput

const addresses = costCenter.addresses ?? []

addresses.push(address)

try {
await masterdata.updatePartialDocument({
dataEntity: COST_CENTER_DATA_ENTITY,
fields: {
addresses,
},
id: costCenterId,
})

return { status: 'success', message: '' }
} catch (e) {
logger.error({
message: 'createCostCenterAddress-error',
error: e,
})
if (e.message) {
throw new GraphQLError(e.message)
} else if (e.response?.data?.message) {
throw new GraphQLError(e.response.data.message)
} else {
throw new GraphQLError(e)
}
}
},
updateCostCenterAddress: async (
_: void,
{
costCenterId,
address,
}: { costCenterId: string; address: AddressInput },
ctx: Context
) => {
const {
clients: { masterdata },
vtex: { logger },
} = ctx

// create schema if it doesn't exist
await checkConfig(ctx)

const costCenter = (await masterdata.getDocument({
dataEntity: COST_CENTER_DATA_ENTITY,
fields: ['addresses'],
id: costCenterId,
})) as CostCenterInput

let addresses = costCenter.addresses ?? []

addresses = addresses.map((current: AddressInput) => {
if (address.addressId === current.addressId) {
return address
}

return current
})

try {
await masterdata.updatePartialDocument({
dataEntity: COST_CENTER_DATA_ENTITY,
fields: {
addresses,
},
id: costCenterId,
})

return { status: 'success', message: '' }
} catch (e) {
logger.error({
message: 'updateCostCenterAddress-error',
error: e,
})
if (e.message) {
throw new GraphQLError(e.message)
} else if (e.response?.data?.message) {
throw new GraphQLError(e.response.data.message)
} else {
throw new GraphQLError(e)
}
}
},
deleteOrganization: async (
_: void,
{ id }: { id: string },
Expand Down
22 changes: 11 additions & 11 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@
dependencies:
"@types/yargs-parser" "*"

"@vtex/api@6.45.11-beta":
version "6.45.11-beta"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.11-beta.tgz#edc51a2fdd3903f33caf7d63f198005f0d5a0c98"
integrity sha512-BFHMWU4SEFKXpbW3TWj0CI944p9ysmoCtB8ewpQlyRpUZFhPx2On/qFgBrD+Gqwt1yvysuKIAOfzfpjqKy88Gg==
"@vtex/api@6.45.10":
version "6.45.10"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.10.tgz#d338d05341e176593124fa171ea5710eee802e61"
integrity sha512-1OllEdBosqGbyGSvQtJzplmOISj2jalpgTDhRWlzhCdkGYbAg0Sm4doudKfZdvizu/6D1Dz4Uy2YncT6M3Vzng==
dependencies:
"@types/koa" "^2.11.0"
"@types/koa-compose" "^3.2.3"
Expand All @@ -259,7 +259,7 @@
graphql "^14.5.8"
graphql-tools "^4.0.6"
graphql-upload "^8.1.0"
jaeger-client "^3.19.0"
jaeger-client "^3.18.0"
js-base64 "^2.5.1"
koa "^2.11.0"
koa-compose "^4.1.0"
Expand All @@ -269,7 +269,7 @@
mime-types "^2.1.12"
opentracing "^0.14.4"
p-limit "^2.2.0"
prom-client "^14.0.1"
prom-client "^12.0.0"
qs "^6.5.1"
querystring "^0.2.0"
ramda "^0.26.0"
Expand Down Expand Up @@ -1070,7 +1070,7 @@ iterall@^1.1.3, iterall@^1.2.2:
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea"
integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg==

jaeger-client@^3.19.0:
jaeger-client@^3.18.0:
version "3.19.0"
resolved "https://registry.yarnpkg.com/jaeger-client/-/jaeger-client-3.19.0.tgz#9b5bd818ebd24e818616ee0f5cffe1722a53ae6e"
integrity sha512-M0c7cKHmdyEUtjemnJyx/y9uX16XHocL46yQvyqDlPdvAcwPDbHrIbKjQdBqtiE4apQ/9dmr+ZLJYYPGnurgpw==
Expand Down Expand Up @@ -1480,10 +1480,10 @@ process@^0.10.0:
resolved "https://registry.yarnpkg.com/process/-/process-0.10.1.tgz#842457cc51cfed72dc775afeeafb8c6034372725"
integrity sha1-hCRXzFHP7XLcd1r+6vuMYDQ3JyU=

prom-client@^14.0.1:
version "14.0.1"
resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-14.0.1.tgz#bdd9583e02ec95429677c0e013712d42ef1f86a8"
integrity sha512-HxTArb6fkOntQHoRGvv4qd/BkorjliiuO2uSWC2KC17MUTKYttWdDoXX/vxOhQdkoECEM9BBH0pj2l8G8kev6w==
prom-client@^12.0.0:
version "12.0.0"
resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-12.0.0.tgz#9689379b19bd3f6ab88a9866124db9da3d76c6ed"
integrity sha512-JbzzHnw0VDwCvoqf8y1WDtq4wSBAbthMB1pcVI/0lzdqHGJI3KBJDXle70XK+c7Iv93Gihqo0a5LlOn+g8+DrQ==
dependencies:
tdigest "^0.1.1"

Expand Down

0 comments on commit bcca243

Please sign in to comment.