Skip to content

Commit

Permalink
feat: use seller quote controller
Browse files Browse the repository at this point in the history
  • Loading branch information
guidobernal-cubos committed Dec 18, 2024
1 parent f0d1c55 commit 5d6c1c5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 81 deletions.
78 changes: 8 additions & 70 deletions node/resolvers/routes/seller/getSellerQuotesPaginated.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,23 @@
import { NotFoundError, UserInputError } from '@vtex/api'
import pLimit from 'p-limit'

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

export async function getSellerQuotesPaginated(ctx: Context, next: NextFn) {
const { seller } = ctx.state
const { query } = ctx

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

// default page = 1
const page = parseInt(
Array.isArray(ctx.query.page) ? ctx.query.page[0] : ctx.query.page || '1',
Array.isArray(query.page) ? query.page[0] : query.page || '1',
10
)

// default pageSize = 15
const pageSize = parseInt(
Array.isArray(ctx.query.pageSize)
? ctx.query.pageSize[0]
: ctx.query.pageSize || '15',
Array.isArray(query.pageSize) ? query.pageSize[0] : query.pageSize || '15',
10
)

if (page < 1 || pageSize < 1) {
throw new UserInputError('get-seller-quote-invalid-pagination-params')
}

const {
data,
pagination,
} = await ctx.clients.masterdata.searchDocumentsWithPaginationInfo<Quote>({
dataEntity: QUOTE_DATA_ENTITY,
fields: QUOTE_FIELDS,
pagination: { page, pageSize },
schema: SCHEMA_VERSION,
where: `seller=${seller}`,
})
const validPage = page >= 0 ? page : 1
const validPageSize = pageSize >= 0 ? pageSize : 15

if (!data || !data.length) {
throw new NotFoundError('seller-quotes-not-found')
}

const limit = pLimit(15)
const enrichedQuotes = await Promise.all(
data.map((quote) =>
limit(async () => {
const organizationName = await getOrganizationName(
{ organization: quote.organization },
null,
ctx
)

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

return { ...quote, organizationName, costCenterName }
})
)
ctx.body = await ctx.vtex.sellerQuotesController?.getSellerQuotesPaginated(
validPage,
validPageSize
)

ctx.body = {
data: enrichedQuotes,
pagination: {
page,
pageSize,
total: pagination.total,
},
}

await next()
}
49 changes: 38 additions & 11 deletions node/resolvers/utils/sellerQuotesController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NotFoundError } from '@vtex/api'
import pLimit from 'p-limit'

import {
QUOTE_DATA_ENTITY,
Expand All @@ -23,21 +24,24 @@ export default class SellerQuotesController {
private async getSellerQuotes({
page = 1,
pageSize = 1,
where,
sort,
where = '',
sort = '',
}: GetQuotesArgs) {
return this.ctx.clients.masterdata.searchDocuments<Quote>({
dataEntity: QUOTE_DATA_ENTITY,
fields: QUOTE_FIELDS,
schema: SCHEMA_VERSION,
pagination: { page, pageSize },
where: `seller=${this.seller} AND (${where})`,
sort,
})
return this.ctx.clients.masterdata.searchDocumentsWithPaginationInfo<Quote>(
{
dataEntity: QUOTE_DATA_ENTITY,
fields: QUOTE_FIELDS,
schema: SCHEMA_VERSION,
pagination: { page, pageSize },
where: `seller=${this.seller} AND (${where})`,
sort,
}
)
}

private async getSellerQuote(id: string) {
const [quote] = await this.getSellerQuotes({ where: `id=${id}` })
const { data } = await this.getSellerQuotes({ where: `id=${id}` })
const quote = data[0]

if (!quote) {
throw new NotFoundError('seller-quote-not-found')
Expand All @@ -63,4 +67,27 @@ export default class SellerQuotesController {

return { ...quote, organizationName, costCenterName }
}

public async getSellerQuotesPaginated(page: number, pageSize: number) {
const { data, pagination } = await this.getSellerQuotes({ page, pageSize })

const limit = pLimit(15)
const enrichedQuotes = await Promise.all(
data.map((quote) =>
limit(async () => {
const {
organizationName,
costCenterName,
} = await this.getOrganizationData(quote)

return { ...quote, organizationName, costCenterName }
})
)
)

return {
data: enrichedQuotes,
pagination,
}
}
}

0 comments on commit 5d6c1c5

Please sign in to comment.