Skip to content

Commit

Permalink
feat: Add route to return the retification config value
Browse files Browse the repository at this point in the history
  • Loading branch information
rerissondaniel authored Jan 30, 2025
1 parent 6f85bfb commit a37a729
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- '/_v/private/rectification-config' route to check if rectification is enabled for the current user.

## [2.172.1] - 2024-10-16

### Added

- Validation for customerID due to email rectification

## [2.172.0] - 2024-10-10
Expand Down
6 changes: 6 additions & 0 deletions node/clients/oms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,10 @@ export class OMS extends JanusClient {
order: (id: string) => `${base}/pvt/orders/${id}`,
}
}

public getEmailRetificationConfig() {
return this.http.get('/api/oms/configuration/email-rectification-enabled', {
headers: { VtexIdclientAutCookie: this.context.authToken },
})
}
}
8 changes: 7 additions & 1 deletion node/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import './globals'

import { LRUCache, Service } from '@vtex/api'
import { LRUCache, method, Service } from '@vtex/api'

import { Clients } from './clients'
import { dataSources } from './dataSources'
import { schemaDirectives } from './directives'
import { resolvers } from './resolvers'
import { getEmailRetificationConfig } from './routes'

const TWO_SECONDS_MS = 2 * 1000
const THREE_SECONDS_MS = 3 * 1000
Expand Down Expand Up @@ -62,4 +63,9 @@ export default new Service<Clients, void, CustomContext>({
resolvers,
schemaDirectives,
},
routes: {
'rectification-config': method({
GET: [getEmailRetificationConfig],
}),
},
})
49 changes: 49 additions & 0 deletions node/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/*
Disabling because `status` and `body` are not defined in the `Context` type provided by VTEX.
However, they are dynamically added by the middleware in the runtime, and using them works as expected.
*/
export async function getEmailRetificationConfig(
ctx: Context,
next: () => Promise<any>
) {
try {
const { storeUserAuthToken } = ctx.vtex
const { account } = decodeToken(storeUserAuthToken ?? '')

if (account !== ctx.vtex.account) {
setForbiddenStatus(ctx)

return
}
} catch (e) {
setForbiddenStatus(ctx)

return
}

// @ts-ignore
ctx.status = 200
// @ts-ignore
ctx.body = await ctx.clients.oms.getEmailRetificationConfig()

await next()
}

function decodeToken(token: string) {
if (!token || !token.includes('.')) {
throw new Error('Invalid token')
}

const [, encodedPayload] = token.split('.')
const payload = Buffer.from(encodedPayload, 'base64').toString()

return JSON.parse(payload)
}

function setForbiddenStatus(ctx: Context) {
// @ts-ignore
ctx.status = 403
// @ts-ignore
ctx.body = 'Forbidden'
}
8 changes: 7 additions & 1 deletion node/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@
"timeout": 40,
"minReplicas": 50,
"maxReplicas": 200,
"cpu": 30
"cpu": 30,
"routes": {
"rectification-config": {
"path": "/_v/private/rectification-config",
"public": true
}
}
}

0 comments on commit a37a729

Please sign in to comment.