-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provides a route for seller get a quote by id at marketplace
- Loading branch information
1 parent
021b359
commit a69ae20
Showing
5 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters