Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add route to return the rectification config value #678

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}
Loading