Skip to content

Commit

Permalink
refactor: separating get seller quote into smaller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tiago-freire committed Dec 17, 2024
1 parent eeac6ab commit 9a7c792
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 34 deletions.
40 changes: 6 additions & 34 deletions node/resolvers/routes/seller/getSellerQuote.ts
Original file line number Diff line number Diff line change
@@ -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<Quote>({
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()
}
69 changes: 69 additions & 0 deletions node/resolvers/utils/quotes.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<Quote>({
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 }
}

0 comments on commit 9a7c792

Please sign in to comment.