Skip to content

Commit

Permalink
feature #4 - add get exchange rate use case
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeniosales committed Oct 16, 2022
1 parent dde671d commit 9828cc2
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/2-business/constants/errorData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface ErrorData {
code: string,
message: string,
shortMessage: string
}

export class ExchangeRateError extends Error {
constructor (public readonly errorData: ErrorData) {
super(errorData.message)
}
}
7 changes: 7 additions & 0 deletions src/2-business/constants/errors/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ErrorData } from '../errorData'

export class ExchangeRateError extends Error {
constructor (public readonly errorData: ErrorData) {
super(errorData.message)
}
}
5 changes: 5 additions & 0 deletions src/2-business/constants/errors/exchangeRateList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const exchangeRateNotFoundError = {
code: 'EXR-001',
message: 'Exchange Rate not found for the base',
shortMessage: 'exchangeRateNotFound'
}
5 changes: 5 additions & 0 deletions src/2-business/dto/exchangeRate/getExchangeRateInputDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CurrencyEnum } from '../../enums/currencyEnum'

export interface GetExchangeRateInputDto {
base: CurrencyEnum
}
34 changes: 34 additions & 0 deletions src/2-business/useCases/exchangeRate/getExchangeRateUseCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { IExchangeRateRepository } from '../../repositories/iExchangeRateRepository'
import { ExchangeRate } from '../../../1-domain/entities/exchangeRate'
import { GetExchangeRateInputDto } from '../../dto/exchangeRate/getExchangeRateInputDto'
import { ExchangeRateError } from '../../constants/errorData'
import { exchangeRateNotFoundError } from '../../constants/errors/exchangeRateList'

export class GetExchangeRateUseCase {

constructor (
private readonly exchangeRateRepository: IExchangeRateRepository
) {}

private logPrefix: string = 'GetExchangeRateUseCase'

async execute (input: GetExchangeRateInputDto): Promise<ExchangeRate> {
console.log(`${this.logPrefix} :: start`)
try {
const { base } = input
const exchangeRate: ExchangeRate = await this.exchangeRateRepository.get(base)

if (!exchangeRate) {
throw new ExchangeRateError(exchangeRateNotFoundError)
}

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

return exchangeRate
} catch (error) {
throw error
}
}
}

export default GetExchangeRateUseCase
61 changes: 61 additions & 0 deletions test/unit/2-business/exchangeRate/getExchangeRateUseCase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { ExchangeRate } from '../../../../src/1-domain/entities/exchangeRate'
import { GetExchangeRateUseCase } from '../../../../src/2-business/useCases/exchangeRate/getExchangeRateUseCase'
import { IExchangeRateRepository } from '../../../../src/2-business/repositories/iExchangeRateRepository'
import { CurrencyEnum } from '../../../../src/2-business/enums/currencyEnum'
import { GetExchangeRateInputDto } from '../../../../src/2-business/dto/exchangeRate/getExchangeRateInputDto'

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

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

let exchangeRateRepository: IExchangeRateRepository

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

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

test('Success::should get the exchange rates successfully', async () => {
const useCase = new GetExchangeRateUseCase(
exchangeRateRepository
)

const output = await useCase.execute(mockInput)

expect(exchangeRateRepository.get).toHaveBeenCalledWith(CurrencyEnum.BRL)
expect(output).toEqual(exchangeRateMock)
})

test('Failure::should throw error when getting exchange fails or not exists', async () => {
exchangeRateRepository = {
upsert: jest.fn().mockResolvedValue(null),
get: jest.fn().mockResolvedValue(null)
}

const useCase = new GetExchangeRateUseCase(
exchangeRateRepository
)

await expect(useCase.execute({ base: CurrencyEnum.USD })).rejects.toThrow()
})
})

0 comments on commit 9828cc2

Please sign in to comment.