Skip to content

Commit

Permalink
feat: add option to paginate in desc order (#2090)
Browse files Browse the repository at this point in the history
* feat: added a sortOrder parameter for paginated queries and set the default order to DESC

---------

Co-authored-by: Max Kurapov <[email protected]>
  • Loading branch information
JoblersTune and mkurapov authored Nov 4, 2023
1 parent c5ed376 commit 48e5af4
Show file tree
Hide file tree
Showing 39 changed files with 546 additions and 169 deletions.
17 changes: 17 additions & 0 deletions localenv/mock-account-servicing-entity/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/backend/src/asset/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { v4 as uuid } from 'uuid'

import { AssetError, isAssetError } from './errors'
import { AssetService } from './service'
import { Pagination } from '../shared/baseModel'
import { Pagination, SortOrder } from '../shared/baseModel'
import { getPageTests } from '../shared/baseModel.test'
import { createTestApp, TestContainer } from '../tests/app'
import { createAsset, randomAsset } from '../tests/asset'
Expand Down Expand Up @@ -210,7 +210,8 @@ describe('Asset Service', (): void => {
describe('getPage', (): void => {
getPageTests({
createModel: () => createAsset(deps),
getPage: (pagination?: Pagination) => assetService.getPage(pagination)
getPage: (pagination?: Pagination, sortOrder?: SortOrder) =>
assetService.getPage(pagination, sortOrder)
})
})

Expand Down
12 changes: 7 additions & 5 deletions packages/backend/src/asset/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NotFoundError, UniqueViolationError } from 'objection'

import { AssetError } from './errors'
import { Asset } from './model'
import { Pagination } from '../shared/baseModel'
import { Pagination, SortOrder } from '../shared/baseModel'
import { BaseService } from '../shared/baseService'
import { AccountingService, LiquidityAccountType } from '../accounting/service'

Expand All @@ -26,7 +26,7 @@ export interface AssetService {
create(options: CreateOptions): Promise<Asset | AssetError>
update(options: UpdateOptions): Promise<Asset | AssetError>
get(id: string): Promise<void | Asset>
getPage(pagination?: Pagination): Promise<Asset[]>
getPage(pagination?: Pagination, sortOrder?: SortOrder): Promise<Asset[]>
getAll(): Promise<Asset[]>
}

Expand All @@ -51,7 +51,8 @@ export async function createAssetService({
create: (options) => createAsset(deps, options),
update: (options) => updateAsset(deps, options),
get: (id) => getAsset(deps, id),
getPage: (pagination?) => getAssetsPage(deps, pagination),
getPage: (pagination?, sortOrder?) =>
getAssetsPage(deps, pagination, sortOrder),
getAll: () => getAll(deps)
}
}
Expand Down Expand Up @@ -119,9 +120,10 @@ async function getAsset(

async function getAssetsPage(
deps: ServiceDependencies,
pagination?: Pagination
pagination?: Pagination,
sortOrder?: SortOrder
): Promise<Asset[]> {
return await Asset.query(deps.knex).getPage(pagination)
return await Asset.query(deps.knex).getPage(pagination, sortOrder)
}

async function getAll(deps: ServiceDependencies): Promise<Asset[]> {
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/fee/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { v4 } from 'uuid'
import { FeeError } from './errors'
import { getPageTests } from '../shared/baseModel.test'
import { createFee } from '../tests/fee'
import { Pagination } from '../shared/baseModel'
import { Pagination, SortOrder } from '../shared/baseModel'

describe('Fee Service', (): void => {
let deps: IocContract<AppServices>
Expand Down Expand Up @@ -160,8 +160,8 @@ describe('Fee Service', (): void => {
describe('Fee pagination', (): void => {
getPageTests({
createModel: () => createFee(deps, asset.id),
getPage: (pagination?: Pagination) =>
feeService.getPage(asset.id, pagination)
getPage: (pagination?: Pagination, sortOrder?: SortOrder) =>
feeService.getPage(asset.id, pagination, sortOrder)
})
})
})
Expand Down
22 changes: 16 additions & 6 deletions packages/backend/src/fee/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ForeignKeyViolationError } from 'objection'
import { BaseService } from '../shared/baseService'
import { FeeError } from './errors'
import { Fee, FeeType } from './model'
import { Pagination } from '../shared/baseModel'
import { Pagination, SortOrder } from '../shared/baseModel'

export interface CreateOptions {
assetId: string
Expand All @@ -15,7 +15,11 @@ export interface CreateOptions {

export interface FeeService {
create(CreateOptions: CreateOptions): Promise<Fee | FeeError>
getPage(assetId: string, pagination?: Pagination): Promise<Fee[]>
getPage(
assetId: string,
pagination?: Pagination,
sortOrder?: SortOrder
): Promise<Fee[]>
getLatestFee(assetId: string, type: FeeType): Promise<Fee | undefined>
}

Expand All @@ -33,8 +37,11 @@ export async function createFeeService({
}
return {
create: (options: CreateOptions) => createFee(deps, options),
getPage: (assetId: string, pagination: Pagination) =>
getFeesPage(deps, assetId, pagination),
getPage: (
assetId: string,
pagination: Pagination,
sortOrder = SortOrder.Desc
) => getFeesPage(deps, assetId, pagination, sortOrder),
getLatestFee: (assetId: string, type: FeeType) =>
getLatestFee(deps, assetId, type)
}
Expand All @@ -43,9 +50,12 @@ export async function createFeeService({
async function getFeesPage(
deps: ServiceDependencies,
assetId: string,
pagination?: Pagination
pagination?: Pagination,
sortOrder?: SortOrder
): Promise<Fee[]> {
const query = Fee.query(deps.knex).where({ assetId }).getPage(pagination)
const query = Fee.query(deps.knex)
.where({ assetId })
.getPage(pagination, sortOrder)

return await query
}
Expand Down
Loading

0 comments on commit 48e5af4

Please sign in to comment.