Skip to content

Commit

Permalink
Merge pull request #6 from vtex-apps/fix/user-token-as-header
Browse files Browse the repository at this point in the history
User token as header on settings handler
  • Loading branch information
Fabiana Ferreira Fonseca authored Jul 23, 2020
2 parents fbeb161 + 6a56fd5 commit ad7d659
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]

## [0.1.3] - 2020-07-23
### Fixed
- Adding user token to be used on the settings handler

### Fixed
- Added missing fields on the Checkout Request interface
### Changed
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ It is important to emphasize that for the first two endpoints to work, you **mus
- `settings`: a private route that is responsible for configurating a tax service in a specific account.
> It expects to receive an operation name, which can be `activate` or `deactivate`.
If you want to test your those routes, it is possible to use this [Postman collection](https://www.getpostman.com/collections/debecab7831841489998).
If you want to test your those routes, it is possible to use this [Postman collection](https://www.getpostman.com/collections/6321a1b18cde612dc027).

> **Attention!** The authorization header that it's present in the Postman collection is a mocked value to be correctly validated by the handlers. This value is defined in the `utils/constants.ts` file and it's used to configure the tax service when calling the `settings` endpoint.
4 changes: 4 additions & 0 deletions node/clients/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ export class Checkout extends VtexCommerce {

public setOrderFormConfiguration(
body: OrderFormConfiguration,
userToken: string,
tracingConfig?: RequestTracingConfig
) {
const metric = 'checkout-setOrderForm'
return this.http.post(CHECKOUT_ENDPOINT, body, {
headers: {
VtexIdclientAutCookie: userToken,
},
metric,
tracing: createTracing(metric, tracingConfig),
})
Expand Down
14 changes: 11 additions & 3 deletions node/handlers/settings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AuthenticationError, UserInputError } from '@vtex/api'

import { activateProvider, deactivateProvider } from '../utils/settings'

export async function settings(ctx: Context) {
Expand All @@ -17,14 +19,20 @@ export async function settings(ctx: Context) {
},
} = ctx

if (!ctx.req.headers.VtexIdclientAutCookie) {
throw new AuthenticationError('You must provide a VtexIdclientAutCookie')
}
const userToken = ctx.req.headers.VtexIdclientAutCookie as string
const orderForm = await checkout.getOrderFormConfiguration()

if (operation === 'activate') {
await activateProvider(orderForm, checkout, account, workspace)
await activateProvider(orderForm, checkout, account, workspace, userToken)
} else if (operation === 'deactivate') {
await deactivateProvider(orderForm, checkout)
await deactivateProvider(orderForm, checkout, userToken)
} else {
ctx.status = 400
throw new UserInputError(
"operation must be either 'activate' or 'deactivate'"
)
}
ctx.status = 200
ctx.body = 'Tax configuration has been changed'
Expand Down
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"@types/co-body": "^0.0.3",
"@types/jest": "^24.0.18",
"@types/node": "^12.0.0",
"@vtex/api": "6.35.0",
"@vtex/api": "6.35.1",
"@vtex/test-tools": "^1.0.0",
"tslint": "^5.14.0",
"tslint-config-vtex": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion node/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"settings": {
"path": "/app/tax-provider/checkout/settings/:operation",
"public": false
"public": true
}
}
}
44 changes: 26 additions & 18 deletions node/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,44 @@ export async function activateProvider(
orderForm: OrderFormConfiguration,
checkout: Checkout,
account: string,
workspace: string
workspace: string,
userToken: string
) {
if (orderForm.taxConfiguration) {
throw new UserInputError('Tax provider already configured')
}
return checkout.setOrderFormConfiguration({
...orderForm,
taxConfiguration: {
allowExecutionAfterErrors: false,
authorizationHeader: AUTHORIZATION_CODE,
integratedAuthentication: false,
url: `https://${workspace}--${account}.myvtex.com/app/tax-provider/checkout/simulation`,
return checkout.setOrderFormConfiguration(
{
...orderForm,
taxConfiguration: {
allowExecutionAfterErrors: false,
authorizationHeader: AUTHORIZATION_CODE,
integratedAuthentication: false,
url: `https://${workspace}--${account}.myvtex.com/app/tax-provider/checkout/simulation`,
},
},
})
userToken
)
}

export async function deactivateProvider(
orderForm: OrderFormConfiguration,
checkout: Checkout
checkout: Checkout,
userToken: string
) {
if (!orderForm.taxConfiguration) {
throw new UserInputError('Tax provider is not configured')
}
return checkout.setOrderFormConfiguration({
...orderForm,
taxConfiguration: {
allowExecutionAfterErrors: false,
authorizationHeader: AUTHORIZATION_CODE,
integratedAuthentication: false,
url: null,
return checkout.setOrderFormConfiguration(
{
...orderForm,
taxConfiguration: {
allowExecutionAfterErrors: false,
authorizationHeader: AUTHORIZATION_CODE,
integratedAuthentication: false,
url: null,
},
},
})
userToken
)
}
8 changes: 4 additions & 4 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1424,10 +1424,10 @@
resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.8.0.tgz#8b63ab7f1aa5321248aad5ac890a485656dcea4d"
integrity sha512-te5lMAWii1uEJ4FwLjzdlbw3+n0FZNOvFXHxQDKeT0dilh7HOzdMzV2TrJVUzq8ep7J4Na8OUYPRLSQkJHAlrg==

"@vtex/[email protected].0":
version "6.35.0"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.35.0.tgz#1970a8974f8bf9fbc70fb4e6758b78d46c8b1f87"
integrity sha512-RWH9wqpHHV0FtGlK4AxrsFcjVxhl3ZVTQRKboEgOI+7Ts9scLkBoEyYnwtQzvjfAkJgxUNCmvqsKPN2HxAnWlQ==
"@vtex/[email protected].1":
version "6.35.1"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.35.1.tgz#d18b861667b8d038fe8fd31c61e926f569305069"
integrity sha512-mRLTP9YT6aw3SNOnFRuUauWwxgiy5jh2BTzUFhiwMe2FjrqrS17f1y2mpxzZrafRs9e/W1jrqh2cpJPa8iIimQ==
dependencies:
"@types/koa" "^2.11.0"
"@types/koa-compose" "^3.2.3"
Expand Down

0 comments on commit ad7d659

Please sign in to comment.