Skip to content

Commit

Permalink
feat: provides a route for seller get a quote by id at marketplace
Browse files Browse the repository at this point in the history
  • Loading branch information
tiago-freire committed Dec 15, 2024
1 parent 021b359 commit a69ae20
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
9 changes: 8 additions & 1 deletion node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ const clients: ClientsConfig<Clients> = {

declare global {
// We declare a global Context type just to avoid re-writing ServiceContext<Clients, State> in every handler and resolver
type Context = ServiceContext<Clients>
type Context = ServiceContext<
Clients,
RecorderState & {
seller?: string
}
>

type NextFn = () => Promise<unknown>

// The shape of our State object found in `ctx.state`. This is used as state bag to communicate between middlewares.
interface State {
Expand Down
18 changes: 17 additions & 1 deletion node/resolvers/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { processQueue } from '../../utils/Queue'
import { method } from '@vtex/api'

import { getAppId } from '../../constants'
import { processQueue } from '../../utils/Queue'
import { getSellerQuote } from './seller/getSellerQuote'
import {
setSellerResponseMetadata,
validateSellerRequest,
} from './seller/utils'

function createSellerHandlers(
mainHandler: (ctx: Context, next: NextFn) => Promise<void>
) {
return [validateSellerRequest, mainHandler, setSellerResponseMetadata]
}

export const Routes = {
host: async (ctx: Context) => {
Expand All @@ -18,4 +31,7 @@ export const Routes = {
ctx.response.body = { date, appId: getAppId() }
ctx.response.status = 200
},
getSellerQuote: method({
GET: createSellerHandlers(getSellerQuote),
}),
}
47 changes: 47 additions & 0 deletions node/resolvers/routes/seller/getSellerQuote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { NotFoundError, UserInputError } from '@vtex/api'

import {
QUOTE_DATA_ENTITY,
QUOTE_FIELDS,
SCHEMA_VERSION,
} from '../../../constants'
import {
costCenterName as getCostCenterName,
organizationName as getOrganizationName,
} from '../../fieldResolvers'
import { invalidParam } from './utils'

export async function getSellerQuote(ctx: Context, next: NextFn) {
const { id } = ctx.vtex.route.params
const { seller } = ctx.state

if (!seller || invalidParam(id)) {
throw new UserInputError('get-seller-quote-invalid-params')
}

const [quote] = await ctx.clients.masterdata.searchDocuments<Quote>({
dataEntity: QUOTE_DATA_ENTITY,
fields: QUOTE_FIELDS,
pagination: { page: 1, pageSize: 1 },
schema: SCHEMA_VERSION,
where: `id=${id} AND seller=${seller}`,
})

if (!quote) {
throw new NotFoundError('seller-quote-not-found')
}

const { organization, costCenter } = quote

const organizationName = await getOrganizationName(
{ organization },
null,
ctx
)

const costCenterName = await getCostCenterName({ costCenter }, null, ctx)

ctx.body = { ...quote, organizationName, costCenterName }

await next()
}
31 changes: 31 additions & 0 deletions node/resolvers/routes/seller/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ForbiddenError } from '@vtex/api'

const USER_AGENT_REGEX = /^vtex\.b2b-seller-quotes@\d+.\d+.\d+$/

export function invalidParam(
param?: string | string[]
): param is string[] | undefined {
return !param || Array.isArray(param)
}

export async function validateSellerRequest(ctx: Context, next: NextFn) {
const { seller } = ctx.vtex.route.params

if (
invalidParam(seller) ||
seller !== ctx.headers['x-vtex-origin-account'] ||
!ctx.headers['user-agent']?.match(USER_AGENT_REGEX)
) {
throw new ForbiddenError('request-not-allowed')
}

ctx.state.seller = seller

await next()
}

export async function setSellerResponseMetadata(ctx: Context) {
ctx.set('Access-Control-Allow-Origin', '*')
ctx.set('Content-Type', 'application/json')
ctx.status = 200
}
12 changes: 12 additions & 0 deletions node/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@
"principals": ["vrn:apps:*:*:*:app/vtex.scheduler@*"]
}
]
},
"getSellerQuote": {
"path": "/b2b-quotes-graphql/_v/0/get-seller-quote/:seller/:id",
"public": true,
"access": "authorized",
"policies": [
{
"effect": "allow",
"actions": ["get"],
"principals": ["vrn:apps:*:*:*:app/vtex.b2b-seller-quotes@*"]
}
]
}
}
}

0 comments on commit a69ae20

Please sign in to comment.