Skip to content

Commit

Permalink
feat: list onramp swaps api
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Oct 31, 2024
1 parent 67c47ec commit 7f2d901
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
30 changes: 25 additions & 5 deletions apps/api/src/swap/swap.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
process_swap_update,
EVENTS_SERVICE_BUS,
CreateOnrampSwapDto,
ListSwapsDto,
} from '@bitsacco/common';
import { SwapService } from './swap.service';

Expand Down Expand Up @@ -72,18 +73,37 @@ 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' })
findOnrampTransaction(@Param('id') id: string) {
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();
Expand Down
5 changes: 3 additions & 2 deletions apps/api/src/swap/swap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CreateOnrampSwapDto,
Currency,
FindSwapRequest,
ListSwapsDto,
SWAP_SERVICE_NAME,
SwapServiceClient,
} from '@bitsacco/common';
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions apps/swap/src/swap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,13 @@ export class SwapService {
size,
}: PaginatedRequest): Promise<PaginatedSwapResponse> {
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,
Expand All @@ -192,9 +196,9 @@ export class SwapService {

return {
swaps,
page,
page: selectPage,
size,
pages: onramps.length / size,
pages,
};
}

Expand Down
4 changes: 3 additions & 1 deletion libs/common/src/dto/find-swap.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions libs/common/src/dto/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './create-onramp-swap.dto';
export * from './find-swap.dto';
export * from './list-swaps.dto';
15 changes: 15 additions & 0 deletions libs/common/src/dto/list-swaps.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 7f2d901

Please sign in to comment.