diff --git a/node/resolvers/routes/seller/getSellerQuote.ts b/node/resolvers/routes/seller/getSellerQuote.ts index da81b4c..9633f55 100644 --- a/node/resolvers/routes/seller/getSellerQuote.ts +++ b/node/resolvers/routes/seller/getSellerQuote.ts @@ -1,47 +1,19 @@ -import { NotFoundError, UserInputError } from '@vtex/api' +import { UserInputError } from '@vtex/api' -import { - QUOTE_DATA_ENTITY, - QUOTE_FIELDS, - SCHEMA_VERSION, -} from '../../../constants' -import { - costCenterName as getCostCenterName, - organizationName as getOrganizationName, -} from '../../fieldResolvers' +import { getFullSellerQuote } from '../../utils/quotes' 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)) { + if (invalidParam(id)) { throw new UserInputError('get-seller-quote-invalid-params') } - const [quote] = await ctx.clients.masterdata.searchDocuments({ - dataEntity: QUOTE_DATA_ENTITY, - fields: QUOTE_FIELDS, - pagination: { page: 1, pageSize: 1 }, - schema: SCHEMA_VERSION, - where: `id=${id} AND seller=${seller}`, - }) + const { seller } = ctx.state as { seller: string } + const quote = await getFullSellerQuote(ctx, seller, id) - 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 } + ctx.body = quote await next() } diff --git a/node/resolvers/utils/quotes.ts b/node/resolvers/utils/quotes.ts index 7ec7d6d..bf371ca 100644 --- a/node/resolvers/utils/quotes.ts +++ b/node/resolvers/utils/quotes.ts @@ -1,3 +1,15 @@ +import { NotFoundError } from '@vtex/api' + +import { + QUOTE_DATA_ENTITY, + QUOTE_FIELDS, + SCHEMA_VERSION, +} from '../../constants' +import { + costCenterName as getCostCenterName, + organizationName as getOrganizationName, +} from '../fieldResolvers' + export async function splitItemsBySeller({ ctx, items, @@ -136,3 +148,60 @@ export const createQuoteObject = ({ hasChildren, } } + +type GetQuotesArgs = { + ctx: Context + where?: string + sort?: string + page?: number + pageSize?: number +} + +export async function getQuotes({ + ctx, + page = 1, + pageSize = 1, + where, + sort, +}: GetQuotesArgs) { + return ctx.clients.masterdata.searchDocuments({ + dataEntity: QUOTE_DATA_ENTITY, + fields: QUOTE_FIELDS, + schema: SCHEMA_VERSION, + pagination: { page, pageSize }, + where, + sort, + }) +} + +export async function getSellerQuote(ctx: Context, seller: string, id: string) { + const [quote] = await getQuotes({ + ctx, + where: `id=${id} AND seller=${seller}`, + }) + + if (!quote) { + throw new NotFoundError('seller-quote-not-found') + } + + return quote +} + +export async function getFullSellerQuote( + ctx: Context, + seller: string, + id: string +) { + const quote = await getSellerQuote(ctx, id, seller) + const { organization, costCenter } = quote + + const organizationName = await getOrganizationName( + { organization }, + null, + ctx + ) + + const costCenterName = await getCostCenterName({ costCenter }, null, ctx) + + return { ...quote, organizationName, costCenterName } +}