Skip to content

Commit

Permalink
feature #4 - add controller and serializer of get exchange rate micro…
Browse files Browse the repository at this point in the history
…sservice
  • Loading branch information
Eugeniosales committed Oct 16, 2022
1 parent 7d949b7 commit d9d96b8
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { IExchangeRateRepository } from '../../../2-business/repositories/iExchangeRateRepository'
import { GetExchangeRateUseCase } from '../../../2-business/useCases/exchangeRate/getExchangeRateUseCase'
import { ControllerBase } from '../../controllerBase'
import { Output } from '../../../2-business/dto/output'
import { ExchangeRate } from '../../../1-domain/entities/exchangeRate'
import { InputGetExchangeRate } from '../../serializers/exchangeRate/getExchangeRate/input'
import { OutputGetExchangeRate } from '../../serializers/exchangeRate/getExchangeRate/output'
import { ExchangeRateError } from '../../../2-business/constants/errorData'

export class GetExchangeRateController extends ControllerBase<InputGetExchangeRate, OutputGetExchangeRate> {
constructor (
private readonly exchangeRateRepository: IExchangeRateRepository
) { super() }

async run (input: InputGetExchangeRate): Promise<Output<OutputGetExchangeRate>> {
const logPrefix = 'GetExchangeRateController'
console.log(`${logPrefix} :: start`)
console.log(`${logPrefix} ::`, input)

try {
const updateExchangeRateUseCase = new GetExchangeRateUseCase(
this.exchangeRateRepository
)

const exchangeRate: ExchangeRate = await updateExchangeRateUseCase.execute(input)

console.log(`${logPrefix} :: end`)

return {
httpCode: 200,
status: 'success',
data: { exchangeRate }
}

} catch (error) {
console.error(error)

if (error instanceof ExchangeRateError) {
return {
httpCode: 404,
status: 'fail',
message: error.message
}
}

return {
httpCode: 400,
status: 'error',
message: 'general error'
}
}
}
}
14 changes: 14 additions & 0 deletions src/3-adapters/serializers/exchangeRate/getExchangeRate/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { InputClassValidator } from '../../../../4-framework/utility/classValidator'
import { IsNotEmpty, IsString } from 'class-validator'
import { CurrencyEnum } from '../../../../2-business/enums/currencyEnum'

export class InputGetExchangeRate extends InputClassValidator {
@IsNotEmpty()
@IsString()
baseCurrency!: CurrencyEnum

constructor (obj: Partial<InputGetExchangeRate>) {
super()
Object.assign(this, obj)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ExchangeRate } from '../../../../1-domain/entities/exchangeRate'

export interface OutputGetExchangeRate {
exchangeRate: ExchangeRate
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GetExchangeRateInputDto } from '../../../../src/2-business/dto/exchange

describe('GetExchangeRateUseCase', () => {
const exchangeRateMock: ExchangeRate = {
baseCurrency: 'BRL',
baseCurrency: CurrencyEnum.BRL,
rates: {
'AUD': 1.566015,
'CAD': 1.560132,
Expand All @@ -19,7 +19,7 @@ describe('GetExchangeRateUseCase', () => {
}

const mockInput: GetExchangeRateInputDto = {
base: CurrencyEnum.BRL
baseCurrency: CurrencyEnum.BRL
}

let exchangeRateRepository: IExchangeRateRepository
Expand Down Expand Up @@ -56,6 +56,6 @@ describe('GetExchangeRateUseCase', () => {
exchangeRateRepository
)

await expect(useCase.execute({ base: CurrencyEnum.USD })).rejects.toThrow()
await expect(useCase.execute({ baseCurrency: CurrencyEnum.USD })).rejects.toThrow()
})
})
60 changes: 60 additions & 0 deletions test/unit/3-adpters/exchangeRate/getExchangeRateController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { GetExchangeRateController } from '../../../../src/3-adapters/controller/exchangeRate/getExchangeRateController'
import { IExchangeRateRepository } from '../../../../src/2-business/repositories/iExchangeRateRepository'
import { ExchangeRate } from '../../../../src/1-domain/entities/exchangeRate'
import { CurrencyEnum } from '../../../../src/2-business/enums/currencyEnum'
import { InputGetExchangeRate } from '../../../../src/3-adapters/serializers/exchangeRate/getExchangeRate/input'

jest.mock('../../../../src/2-business/useCases/exchangeRate/updateExchangeRateUseCase')

describe('UpdateExchangeRateController', () => {
const exchangeRateMock: ExchangeRate = {
baseCurrency: CurrencyEnum.BRL,
rates: {
'AUD': 1.566015,
'CAD': 1.560132,
'CHF': 1.154727,
'CNY': 7.827874,
'GBP': 0.882047,
'JPY': 132.360679,
'USD': 1.23396
}
}

const inputMock = new InputGetExchangeRate({
baseCurrency: CurrencyEnum.BRL
})

let exchangeRateRepository: IExchangeRateRepository
let GetExchangeRateUseCase = jest.fn()

const setMocks = () => {
exchangeRateRepository = {
upsert: jest.fn().mockResolvedValue(null),
get: jest.fn().mockResolvedValue(exchangeRateMock)
}
GetExchangeRateUseCase.mockClear()
}

beforeEach(() => {
setMocks()
})

test('Success::should get the exchange rates successfully', async () => {
GetExchangeRateUseCase.mockImplementation(() => {
return {
execute: jest.fn().mockResolvedValue(exchangeRateMock)
}
})
const controller = new GetExchangeRateController(
exchangeRateRepository
)

const response = await controller.run(inputMock)

expect(response).toEqual({
httpCode: 200,
status: 'success',
data: { exchangeRate: exchangeRateMock }
})
})
})

0 comments on commit d9d96b8

Please sign in to comment.