Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add paging support to getSellers query #168

Merged
merged 10 commits into from
Aug 12, 2024
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added
- Add new `getSellersPaginated` query to allow pagination on sellers query

## [0.51.2] - 2024-07-30

### Fixed
Expand Down
19 changes: 19 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ type Query {
@cacheControl(scope: PUBLIC, maxAge: SHORT)
@auditAccess
getSellers: [Seller] @checkAdminAccess @cacheControl(scope: PRIVATE)
getSellersPaginated(
args: GetSellersPaginatedArgsInput
enzomerca marked this conversation as resolved.
Show resolved Hide resolved
): GetSellersPaginatedResponse @checkAdminAccess @cacheControl(scope: PRIVATE)
}

input GetSellersPaginatedArgsInput {
page: Int
pageSize: Int
}

type PaginationResponse {
page: Int
pageSize: Int
total: Int
}

type GetSellersPaginatedResponse {
items: [Seller]
pagination: PaginationResponse
}

type Mutation {
Expand Down
86 changes: 84 additions & 2 deletions node/clients/sellers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { InstanceOptions, IOContext } from '@vtex/api'
import { JanusClient } from '@vtex/api'
import { isNaN, orderBy } from 'lodash'

const SELLERS_PATH = '/api/seller-register/pvt/sellers'

Expand All @@ -9,6 +10,25 @@ export interface Seller {
email: string
}

export interface GetSellersResponse {
items: Seller[]
pagination: {
page: number
pageSize: number
total: number
}
}

export interface GetSellersOpts {
page: number
pageSize: number
}

const INITIAL_LIST_OPTIONS = {
page: 1,
pageSize: 100,
}

export default class SellersClient extends JanusClient {
constructor(context: IOContext, options?: InstanceOptions) {
super(context, {
Expand All @@ -19,7 +39,69 @@ export default class SellersClient extends JanusClient {
})
}

public async getSellers(): Promise<{ items: Seller[] }> {
return this.http.get(SELLERS_PATH)
public async getSellers(args?: GetSellersOpts): Promise<GetSellersResponse> {
const argsWithDefaults = {
...INITIAL_LIST_OPTIONS,
...args,
}

const result = await this.http.get<{
paging: {
from: number
to: number
total: number
}
items: Seller[]
}>(SELLERS_PATH, {
metric: 'sellers-get',
enzomerca marked this conversation as resolved.
Show resolved Hide resolved
params: {
from: argsWithDefaults?.page ?? INITIAL_LIST_OPTIONS.page,
to: argsWithDefaults?.pageSize ?? INITIAL_LIST_OPTIONS.pageSize,
enzomerca marked this conversation as resolved.
Show resolved Hide resolved
},
})

if (!result.items) {
return {
items: [],
pagination: {
page: 0,
enzomerca marked this conversation as resolved.
Show resolved Hide resolved
pageSize: 0,
total: 0,
},
}
}

if (result.items.length > 1) {
result.items = this.sortSellers(result.items)
}

return {
items: result.items,
pagination: {
page: argsWithDefaults.page,
pageSize: argsWithDefaults.pageSize,
total: result.paging.total,
},
}
}

private sortSellers(sellers: Seller[]) {
return orderBy(
sellers,
[
(seller: Seller) => {
const name = seller.name ?? ''
const isNumber = !isNaN(name) && name !== ''

return isNumber ? 1 : 0
},
(seller: Seller) => {
const name = seller.name ?? ''

return isNaN(name) ? name : Number(name)
},
],
['asc', 'asc']
)
enzomerca marked this conversation as resolved.
Show resolved Hide resolved
}
}
34 changes: 33 additions & 1 deletion node/resolvers/Queries/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import GraphQLError from '../../utils/GraphQLError'
import checkConfig from '../config'
import type { B2BSettingsInput } from '../../typings'
import type { GetSellersOpts } from '../../clients/sellers'

const B2BSettings = {
getB2BSettings: async (_: void, __: void, ctx: Context) => {
Expand Down Expand Up @@ -52,7 +53,38 @@ const B2BSettings = {
clients: { sellers },
} = ctx

return (await sellers.getSellers())?.items
try {
return (await sellers.getSellers())?.items
} catch (e) {
if (e.message) {
throw new GraphQLError(e.message)
} else if (e.response?.data?.message) {
throw new GraphQLError(e.response.data.message)
} else {
throw new GraphQLError(e)
}
}
},
enzomerca marked this conversation as resolved.
Show resolved Hide resolved
getSellersPaginated: async (
_: void,
{ args }: { args: GetSellersOpts },
ctx: Context
) => {
const {
clients: { sellers },
} = ctx

try {
return await sellers.getSellers(args)
} catch (e) {
if (e.message) {
throw new GraphQLError(e.message)
} else if (e.response?.data?.message) {
throw new GraphQLError(e.response.data.message)
} else {
throw new GraphQLError(e)
}
}
},
}

Expand Down
2 changes: 1 addition & 1 deletion node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3475,7 +3475,7 @@ stack-utils@^2.0.3:
dependencies:
escape-string-regexp "^2.0.0"

"stats-lite@github:vtex/node-stats-lite#dist":
stats-lite@vtex/node-stats-lite#dist:
version "2.2.0"
resolved "https://codeload.github.com/vtex/node-stats-lite/tar.gz/1b0d39cc41ef7aaecfd541191f877887a2044797"
dependencies:
Expand Down