-
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.
refactor: separating get seller quote into smaller functions
- Loading branch information
1 parent
eeac6ab
commit 9a7c792
Showing
2 changed files
with
75 additions
and
34 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
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() | ||
} |
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