Skip to content

Commit

Permalink
refactor: simplify swap api proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacknjama committed Feb 12, 2025
1 parent 2653999 commit 163ea70
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 399 deletions.
3 changes: 1 addition & 2 deletions apps/api/src/api.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
UsersSchema,
UsersService,
} from '@bitsacco/common';
import { SwapController, SwapService } from './swap';
import { SwapController } from './swap';
import { NostrController, NostrService } from './nostr';
import { SmsService } from './sms/sms.service';
import { SmsController } from './sms/sms.controller';
Expand Down Expand Up @@ -175,7 +175,6 @@ import { ChamasController } from './chamas/chamas.controller';
AuthService,
UsersRepository,
UsersService,
SwapService,
NostrService,
SmsService,
AdminService,
Expand Down
1 change: 0 additions & 1 deletion apps/api/src/swap/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './swap.controller';
export * from './swap.service';
64 changes: 26 additions & 38 deletions apps/api/src/swap/swap.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,32 @@ import {
Currency,
EVENTS_SERVICE_BUS,
SupportedCurrencies,
SWAP_SERVICE_NAME,
SwapServiceClient,
} from '@bitsacco/common';
import { createTestingModuleWithValidation } from '@bitsacco/testing';
import { ClientProxy } from '@nestjs/microservices';
import { type ClientGrpc, ClientProxy } from '@nestjs/microservices';

import { SwapController } from './swap.controller';
import { SwapService } from './swap.service';

describe('SwapController', () => {
let controller: SwapController;
let swapService: SwapService;
let serviceGenerator: ClientGrpc;
let swapController: SwapController;
let swapServiceClient: Partial<SwapServiceClient>;
let serviceBus: ClientProxy;

beforeEach(async () => {
serviceGenerator = {
getService: jest.fn().mockReturnValue(swapServiceClient),
getClientByServiceName: jest.fn().mockReturnValue(swapServiceClient),
};

const module: TestingModule = await createTestingModuleWithValidation({
controllers: [SwapController],
providers: [
{
provide: SwapService,
useValue: {
getOnrampQuote: jest.fn(),
postOnrampTransaction: jest.fn(),
getOnrampTransactions: jest.fn(),
findOnrampTransaction: jest.fn(),
getOfframpQuote: jest.fn(),
postOfframpTransaction: jest.fn(),
getOfframpTransactions: jest.fn(),
findOfframpTransaction: jest.fn(),
postSwapUpdate: jest.fn(),
},
provide: SWAP_SERVICE_NAME,
useValue: serviceGenerator,
},
{
provide: EVENTS_SERVICE_BUS,
Expand All @@ -42,19 +39,17 @@ describe('SwapController', () => {
],
});

controller = module.get<SwapController>(SwapController);
swapService = module.get<SwapService>(SwapService);
swapController = module.get<SwapController>(SwapController);
serviceBus = module.get<ClientProxy>(EVENTS_SERVICE_BUS);
});

it('should be defined', () => {
expect(controller).toBeDefined();
expect(swapController).toBeDefined();
});

describe('getOnrampQuote', () => {
it('should call swapService.getOnrampQuote', () => {
controller.getOnrampQuote(SupportedCurrencies.KES);
expect(swapService.getOnrampQuote).toHaveBeenCalled();
swapController.getOnrampQuote(SupportedCurrencies.KES);
});

// it('throws if unsupported currency is supplied', () => {
Expand Down Expand Up @@ -86,29 +81,26 @@ describe('SwapController', () => {
},
},
};
controller.postOnrampTransaction(req);
expect(swapService.postOnrampTransaction).toHaveBeenCalled();
swapController.postOnrampTransaction(req);
expect(swapController.postOnrampTransaction).toHaveBeenCalled();
});
});

describe('findOnrampTransaction', () => {
it('should call swapService.findOnrampTransaction', () => {
controller.findOnrampTransaction('swap_id');
expect(swapService.findOnrampTransaction).toHaveBeenCalled();
swapController.findOnrampTransaction('swap_id');
});
});

describe('getOnrampTransactions', () => {
it('should call swapService.getOnrampTransactions', () => {
controller.getOnrampTransactions();
expect(swapService.getOnrampTransactions).toHaveBeenCalled();
swapController.getOnrampTransactions();
});
});

describe('getOfframpQuote', () => {
it('should call swapService.getOfframpQuote', () => {
controller.getOfframpQuote(SupportedCurrencies.KES);
expect(swapService.getOfframpQuote).toHaveBeenCalled();
swapController.getOfframpQuote(SupportedCurrencies.KES);
});
});

Expand All @@ -125,36 +117,32 @@ describe('SwapController', () => {
},
},
};
controller.postOfframpTransaction(req);
expect(swapService.postOfframpTransaction).toHaveBeenCalled();
swapController.postOfframpTransaction(req);
});
});

describe('getOfframpTransactions', () => {
it('should call swapService.getOfframpTransactions', () => {
controller.getOfframpTransactions();
expect(swapService.getOfframpTransactions).toHaveBeenCalled();
swapController.getOfframpTransactions();
});
});

describe('postSwapUpdate', () => {
it('should call swapService.postSwapUpdate', () => {
controller.postSwapUpdate({});
swapController.postSwapUpdate({});
expect(serviceBus.emit).toHaveBeenCalled();
});
});

describe('findOfframpTransaction', () => {
it('should call swapService.findOfframpTransaction', () => {
controller.findOfframpTransaction('swap_id');
expect(swapService.findOfframpTransaction).toHaveBeenCalled();
swapController.findOfframpTransaction('swap_id');
});
});

describe('getOfframpTransactions', () => {
it('should call swapService.getOfframpTransactions', () => {
controller.getOfframpTransactions();
expect(swapService.getOfframpTransactions).toHaveBeenCalled();
swapController.getOfframpTransactions();
});
});
});
26 changes: 15 additions & 11 deletions apps/api/src/swap/swap.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Query,
} from '@nestjs/common';
import { object } from 'joi';
import { ClientProxy } from '@nestjs/microservices';
import { type ClientGrpc, ClientProxy } from '@nestjs/microservices';
import { ApiBody, ApiOperation, ApiParam, ApiQuery } from '@nestjs/swagger';
import {
Currency,
Expand All @@ -24,17 +24,21 @@ import {
CreateOfframpSwapDto,
default_page,
default_page_size,
SwapServiceClient,
SWAP_SERVICE_NAME,
} from '@bitsacco/common';
import { SwapService } from './swap.service';

@Controller('swap')
export class SwapController {
private swapService: SwapServiceClient;
private readonly logger = new Logger(SwapController.name);

constructor(
private readonly swapService: SwapService,
@Inject(SWAP_SERVICE_NAME) private readonly grpc: ClientGrpc,
@Inject(EVENTS_SERVICE_BUS) private readonly eventsClient: ClientProxy,
) {
this.swapService =
this.grpc.getService<SwapServiceClient>(SWAP_SERVICE_NAME);
this.logger.log('SwapController initialized');
}

Expand All @@ -53,7 +57,7 @@ export class SwapController {
throw new BadRequestException(es);
}

return this.swapService.getOnrampQuote({
return this.swapService.getQuote({

Check failure on line 60 in apps/api/src/swap/swap.controller.ts

View workflow job for this annotation

GitHub Actions / test

TypeError: undefined is not an object (evaluating 'this.swapService.getQuote')

at getOnrampQuote (/home/runner/work/os/os/apps/api/src/swap/swap.controller.ts:60:17) at <anonymous> (/home/runner/work/os/os/apps/api/src/swap/swap.controller.spec.ts:52:22)
from,
to: Currency.BTC,
amount: amount?.toString(),
Expand All @@ -66,14 +70,14 @@ export class SwapController {
type: CreateOnrampSwapDto,
})
postOnrampTransaction(@Body() req: CreateOnrampSwapDto) {
return this.swapService.postOnrampTransaction(req);
return this.swapService.createOnrampSwap(req);

Check failure on line 73 in apps/api/src/swap/swap.controller.ts

View workflow job for this annotation

GitHub Actions / test

TypeError: undefined is not an object (evaluating 'this.swapService.createOnrampSwap')

at postOnrampTransaction (/home/runner/work/os/os/apps/api/src/swap/swap.controller.ts:73:17) at <anonymous> (/home/runner/work/os/os/apps/api/src/swap/swap.controller.spec.ts:84:22)
}

@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 });
return this.swapService.findOnrampSwap({ id });

Check failure on line 80 in apps/api/src/swap/swap.controller.ts

View workflow job for this annotation

GitHub Actions / test

TypeError: undefined is not an object (evaluating 'this.swapService.findOnrampSwap')

at findOnrampTransaction (/home/runner/work/os/os/apps/api/src/swap/swap.controller.ts:80:17) at <anonymous> (/home/runner/work/os/os/apps/api/src/swap/swap.controller.spec.ts:91:22)
}

@Get('onramp/all')
Expand All @@ -94,7 +98,7 @@ export class SwapController {
@Query('page') page: number = default_page,
@Query('size') size: number = default_page_size,
) {
return this.swapService.getOnrampTransactions({
return this.swapService.listOnrampSwaps({

Check failure on line 101 in apps/api/src/swap/swap.controller.ts

View workflow job for this annotation

GitHub Actions / test

TypeError: undefined is not an object (evaluating 'this.swapService.listOnrampSwaps')

at getOnrampTransactions (/home/runner/work/os/os/apps/api/src/swap/swap.controller.ts:101:17) at <anonymous> (/home/runner/work/os/os/apps/api/src/swap/swap.controller.spec.ts:97:22)
page,
size,
});
Expand All @@ -115,7 +119,7 @@ export class SwapController {
throw new BadRequestException(es);
}

return this.swapService.getOfframpQuote({
return this.swapService.getQuote({

Check failure on line 122 in apps/api/src/swap/swap.controller.ts

View workflow job for this annotation

GitHub Actions / test

TypeError: undefined is not an object (evaluating 'this.swapService.getQuote')

at getOfframpQuote (/home/runner/work/os/os/apps/api/src/swap/swap.controller.ts:122:17) at <anonymous> (/home/runner/work/os/os/apps/api/src/swap/swap.controller.spec.ts:103:22)
to,
from: Currency.BTC,
amount: amount?.toString(),
Expand All @@ -128,14 +132,14 @@ export class SwapController {
type: CreateOfframpSwapDto,
})
postOfframpTransaction(@Body() req: CreateOfframpSwapDto) {
return this.swapService.postOfframpTransaction(req);
return this.swapService.createOfframpSwap(req);

Check failure on line 135 in apps/api/src/swap/swap.controller.ts

View workflow job for this annotation

GitHub Actions / test

TypeError: undefined is not an object (evaluating 'this.swapService.createOfframpSwap')

at postOfframpTransaction (/home/runner/work/os/os/apps/api/src/swap/swap.controller.ts:135:17) at <anonymous> (/home/runner/work/os/os/apps/api/src/swap/swap.controller.spec.ts:120:22)
}

@Get('offramp/find/:id')
@ApiOperation({ summary: 'Find offramp transaction by ID' })
@ApiParam({ name: 'id', type: 'string', description: 'Transaction ID' })
findOfframpTransaction(@Param('id') id: string) {
return this.swapService.findOfframpTransaction({ id });
return this.swapService.findOfframpSwap({ id });

Check failure on line 142 in apps/api/src/swap/swap.controller.ts

View workflow job for this annotation

GitHub Actions / test

TypeError: undefined is not an object (evaluating 'this.swapService.findOfframpSwap')

at findOfframpTransaction (/home/runner/work/os/os/apps/api/src/swap/swap.controller.ts:142:17) at <anonymous> (/home/runner/work/os/os/apps/api/src/swap/swap.controller.spec.ts:139:22)
}

@Get('offramp/all')
Expand All @@ -156,7 +160,7 @@ export class SwapController {
@Query('page') page: number = 0,
@Query('size') size: number = 100,
) {
return this.swapService.getOfframpTransactions({
return this.swapService.listOfframpSwaps({

Check failure on line 163 in apps/api/src/swap/swap.controller.ts

View workflow job for this annotation

GitHub Actions / test

TypeError: undefined is not an object (evaluating 'this.swapService.listOfframpSwaps')

at getOfframpTransactions (/home/runner/work/os/os/apps/api/src/swap/swap.controller.ts:163:17) at <anonymous> (/home/runner/work/os/os/apps/api/src/swap/swap.controller.spec.ts:126:22)

Check failure on line 163 in apps/api/src/swap/swap.controller.ts

View workflow job for this annotation

GitHub Actions / test

TypeError: undefined is not an object (evaluating 'this.swapService.listOfframpSwaps')

at getOfframpTransactions (/home/runner/work/os/os/apps/api/src/swap/swap.controller.ts:163:17) at <anonymous> (/home/runner/work/os/os/apps/api/src/swap/swap.controller.spec.ts:145:22)
page,
size,
});
Expand Down
Loading

0 comments on commit 163ea70

Please sign in to comment.