diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e0c4b0b..753c833a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/graphql/schema.graphql b/graphql/schema.graphql index a0d6b86a..1c51f3a2 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -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( diff --git a/node/package.json b/node/package.json index c3315522..3bd45e81 100644 --- a/node/package.json +++ b/node/package.json @@ -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", @@ -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", diff --git a/node/resolvers/index.ts b/node/resolvers/index.ts index 517bcb88..97b7c331 100644 --- a/node/resolvers/index.ts +++ b/node/resolvers/index.ts @@ -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 }, diff --git a/node/yarn.lock b/node/yarn.lock index 64c297d9..07c51fbb 100644 --- a/node/yarn.lock +++ b/node/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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== @@ -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"