From 7f2d9014d899cf18333edb7a61aa50b30d72e1a3 Mon Sep 17 00:00:00 2001 From: okjodom Date: Thu, 31 Oct 2024 12:51:10 +0300 Subject: [PATCH] feat: list onramp swaps api --- apps/api/src/swap/swap.controller.ts | 30 ++++++++++++++++++++++----- apps/api/src/swap/swap.service.ts | 5 +++-- apps/swap/src/swap.service.ts | 10 ++++++--- libs/common/src/dto/find-swap.dto.ts | 4 +++- libs/common/src/dto/index.ts | 1 + libs/common/src/dto/list-swaps.dto.ts | 15 ++++++++++++++ 6 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 libs/common/src/dto/list-swaps.dto.ts diff --git a/apps/api/src/swap/swap.controller.ts b/apps/api/src/swap/swap.controller.ts index cff11aa..9fc59ff 100644 --- a/apps/api/src/swap/swap.controller.ts +++ b/apps/api/src/swap/swap.controller.ts @@ -19,6 +19,7 @@ import { process_swap_update, EVENTS_SERVICE_BUS, CreateOnrampSwapDto, + ListSwapsDto, } from '@bitsacco/common'; import { SwapService } from './swap.service'; @@ -72,11 +73,6 @@ export class SwapController { }); } - @Get('onramp/all') - getOnrampTransactions() { - return this.swapService.getOnrampTransactions(); - } - @Get('onramp/find/:id') @ApiOperation({ summary: 'Find onramp transaction by ID' }) @ApiParam({ name: 'id', type: 'string', description: 'Transaction ID' }) @@ -84,6 +80,30 @@ export class SwapController { return this.swapService.findOnrampTransaction({ id }); } + @Get('onramp/all') + @ApiOperation({ summary: 'List onramp swaps' }) + @ApiQuery({ + name: 'page', + example: '?page=0', + type: ListSwapsDto['page'], + required: false, + }) + @ApiQuery({ + name: 'size', + example: '?size=100', + type: ListSwapsDto['size'], + required: false, + }) + getOnrampTransactions( + @Query('page') page: number = 0, + @Query('size') size: number = 100, + ) { + return this.swapService.getOnrampTransactions({ + page, + size, + }); + } + @Get('offramp/quote') getOfframpQuote() { return this.swapService.getOfframpQuote(); diff --git a/apps/api/src/swap/swap.service.ts b/apps/api/src/swap/swap.service.ts index 8d8dd28..a8857ae 100644 --- a/apps/api/src/swap/swap.service.ts +++ b/apps/api/src/swap/swap.service.ts @@ -2,6 +2,7 @@ import { CreateOnrampSwapDto, Currency, FindSwapRequest, + ListSwapsDto, SWAP_SERVICE_NAME, SwapServiceClient, } from '@bitsacco/common'; @@ -30,8 +31,8 @@ export class SwapService implements OnModuleInit { return this.client.createOnrampSwap(req); } - getOnrampTransactions() { - return { status: 200 }; + getOnrampTransactions(req: ListSwapsDto) { + return this.client.listSwaps(req); } findOnrampTransaction(req: FindSwapRequest) { diff --git a/apps/swap/src/swap.service.ts b/apps/swap/src/swap.service.ts index 5f3d27d..89843f7 100644 --- a/apps/swap/src/swap.service.ts +++ b/apps/swap/src/swap.service.ts @@ -181,9 +181,13 @@ export class SwapService { size, }: PaginatedRequest): Promise { const onramps = await this.prismaService.mpesaOnrampSwap.findMany(); + const pages = Math.ceil(onramps.length / size); + + // select the last page if requested page exceeds total pages possible + const selectPage = page > pages ? pages - 1 : page; const swaps = onramps - .slice(page * size, page * size + size) + .slice(selectPage * size, (selectPage + 1) * size + size) .map((swap) => ({ id: swap.mpesaId, rate: swap.rate, @@ -192,9 +196,9 @@ export class SwapService { return { swaps, - page, + page: selectPage, size, - pages: onramps.length / size, + pages, }; } diff --git a/libs/common/src/dto/find-swap.dto.ts b/libs/common/src/dto/find-swap.dto.ts index 95f75ce..a35e918 100644 --- a/libs/common/src/dto/find-swap.dto.ts +++ b/libs/common/src/dto/find-swap.dto.ts @@ -1,10 +1,12 @@ -import { IsString, IsNotEmpty } from 'class-validator'; import { Type } from 'class-transformer'; +import { IsString, IsNotEmpty } from 'class-validator'; import { type FindSwapRequest } from '@bitsacco/common'; +import { ApiProperty } from '@nestjs/swagger'; export class FindSwapDto implements FindSwapRequest { @IsNotEmpty() @IsString() @Type(() => String) + @ApiProperty() id: string; } diff --git a/libs/common/src/dto/index.ts b/libs/common/src/dto/index.ts index cfc693c..ea5888a 100644 --- a/libs/common/src/dto/index.ts +++ b/libs/common/src/dto/index.ts @@ -1,2 +1,3 @@ export * from './create-onramp-swap.dto'; export * from './find-swap.dto'; +export * from './list-swaps.dto'; diff --git a/libs/common/src/dto/list-swaps.dto.ts b/libs/common/src/dto/list-swaps.dto.ts new file mode 100644 index 0000000..b44b819 --- /dev/null +++ b/libs/common/src/dto/list-swaps.dto.ts @@ -0,0 +1,15 @@ +import { IsNumber, Min } from 'class-validator'; +import { PaginatedRequest } from '@bitsacco/common'; +import { ApiProperty } from '@nestjs/swagger'; + +export class ListSwapsDto implements PaginatedRequest { + @ApiProperty() + @IsNumber() + @Min(0) + page: number; + + @ApiProperty() + @IsNumber() + @Min(1) + size: number; +}