diff --git a/CHANGELOG.md b/CHANGELOG.md index 359d336..a736994 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/README.md b/docs/README.md index 1cf8187..028f058 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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. diff --git a/node/clients/checkout.ts b/node/clients/checkout.ts index f44f825..36e4079 100644 --- a/node/clients/checkout.ts +++ b/node/clients/checkout.ts @@ -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), }) diff --git a/node/handlers/settings.ts b/node/handlers/settings.ts index 40d8b00..baa2451 100644 --- a/node/handlers/settings.ts +++ b/node/handlers/settings.ts @@ -1,3 +1,5 @@ +import { AuthenticationError, UserInputError } from '@vtex/api' + import { activateProvider, deactivateProvider } from '../utils/settings' export async function settings(ctx: Context) { @@ -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' diff --git a/node/package.json b/node/package.json index 262dbfa..8320695 100644 --- a/node/package.json +++ b/node/package.json @@ -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", diff --git a/node/service.json b/node/service.json index d9a1d45..e824f80 100644 --- a/node/service.json +++ b/node/service.json @@ -14,7 +14,7 @@ }, "settings": { "path": "/app/tax-provider/checkout/settings/:operation", - "public": false + "public": true } } } diff --git a/node/utils/settings.ts b/node/utils/settings.ts index 06d36a4..423b236 100644 --- a/node/utils/settings.ts +++ b/node/utils/settings.ts @@ -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 + ) } diff --git a/node/yarn.lock b/node/yarn.lock index c26b83c..ace8aff 100644 --- a/node/yarn.lock +++ b/node/yarn.lock @@ -1424,10 +1424,10 @@ resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.8.0.tgz#8b63ab7f1aa5321248aad5ac890a485656dcea4d" integrity sha512-te5lMAWii1uEJ4FwLjzdlbw3+n0FZNOvFXHxQDKeT0dilh7HOzdMzV2TrJVUzq8ep7J4Na8OUYPRLSQkJHAlrg== -"@vtex/api@6.35.0": - version "6.35.0" - resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.35.0.tgz#1970a8974f8bf9fbc70fb4e6758b78d46c8b1f87" - integrity sha512-RWH9wqpHHV0FtGlK4AxrsFcjVxhl3ZVTQRKboEgOI+7Ts9scLkBoEyYnwtQzvjfAkJgxUNCmvqsKPN2HxAnWlQ== +"@vtex/api@6.35.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"