Skip to content

Commit

Permalink
Merge pull request #10 from vtex-apps/feature/B2BORG-36_share-org-cos…
Browse files Browse the repository at this point in the history
…tcenter-information

[B2BCHCKOUT-6 and B2BORG-36] Added checkout route
  • Loading branch information
wender authored Nov 4, 2021
2 parents 7cdc18c + 7d91646 commit 7a2c11d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added
- `/b2b/oms/user/checkout/` route to return `paymentTerms` and Cost Center addresses

## [0.3.2] - 2021-11-03

### Changed
Expand Down
81 changes: 73 additions & 8 deletions node/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ForbiddenError } from '@vtex/api'
Expand Down Expand Up @@ -145,6 +144,69 @@ const MUTATIONS = {

export const resolvers = {
Routes: {
checkout: async (ctx: Context) => {
const {
vtex: { storeUserAuthToken, sessionToken, logger },
clients: { session, masterdata },
} = ctx

const token: any = storeUserAuthToken
const response: any = {}

ctx.response.status = !token ? 403 : 200

if (token) {
const sessionData = await session
.getSession(sessionToken as string, ['*'])
.then((currentSession: any) => {
return currentSession.sessionData
})
.catch((error: any) => {
logger.error({
message: 'getSession-error',
error,
})

return null
})

if (sessionData?.namespaces['storefront-permissions']) {
if (
sessionData.namespaces['storefront-permissions']?.organization
?.value
) {
const organization = await masterdata.getDocument({
dataEntity: ORGANIZATION_DATA_ENTITY,
fields: ['paymentTerms'],
id:
sessionData.namespaces['storefront-permissions']?.organization
?.value,
})

response.organization = organization
}

if (
sessionData.namespaces['storefront-permissions']?.costcenter?.value
) {
const costcenter = await masterdata.getDocument({
dataEntity: COST_CENTER_DATA_ENTITY,
fields: ['addresses'],
id:
sessionData.namespaces['storefront-permissions']?.costcenter
?.value,
})

response.costcenter = costcenter
}
}
}

ctx.set('Content-Type', 'application/json')
ctx.set('Cache-Control', 'no-cache, no-store')

ctx.response.body = response
},
orders: async (ctx: Context) => {
const {
vtex: { storeUserAuthToken, sessionToken, logger },
Expand All @@ -164,8 +226,11 @@ export const resolvers = {
.then((currentSession: any) => {
return currentSession.sessionData
})
.catch((err: any) => {
logger.error(err)
.catch((error: any) => {
logger.error({
message: 'getSession-error',
error,
})

return null
})
Expand Down Expand Up @@ -199,9 +264,11 @@ export const resolvers = {
},
}
)
.catch((err: any) => {
console.log('Error quering permissions =>', err)
logger.error(err)
.catch((error: any) => {
logger.error({
message: 'checkUserPermission-error',
error,
})

return {
data: {
Expand Down Expand Up @@ -819,7 +886,6 @@ export const resolvers = {
return result.data.saveUser
})
.catch((error: any) => {
console.error(error)
logger.error({
message: 'addUser-error',
error,
Expand Down Expand Up @@ -872,7 +938,6 @@ export const resolvers = {
return result.data.deleteUser
})
.catch((error: any) => {
console.error(error)
logger.error({
message: 'deleteUser-error',
error,
Expand Down
4 changes: 4 additions & 0 deletions node/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"path": "/b2b/oms/user/orders/",
"public": true
},
"checkout": {
"path": "/b2b/oms/user/checkout/",
"public": true
},
"order": {
"path": "/b2b/oms/user/orders/:orderId",
"public": true
Expand Down

0 comments on commit 7a2c11d

Please sign in to comment.