Skip to content

Commit

Permalink
feature #3 - Add update exchange rate use case
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeniosales committed Oct 14, 2022
1 parent f50fb93 commit 70713ce
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/1-domain/entities/exchangeRate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface ExchangeRate {
baseCurrency: string
rates: {
[currency: string]: number
}
createdAt?: Date
updatedAt?: Date
}
9 changes: 9 additions & 0 deletions src/1-domain/models/iExchangeRateResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface IExchangeRateResponse {
success: boolean
timestamp: number
base: string
date: string
rates: {
[currency: string]: number
}
}
6 changes: 6 additions & 0 deletions src/2-business/enums/currencyEnum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum CurrencyEnum {
BRL = 'BRL',
USD = 'USD',
EUR = 'EUR',
CAD = 'CAD'
}
5 changes: 5 additions & 0 deletions src/2-business/repositories/iExchangeRateRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ExchangeRate } from '../../1-domain/entities/exchangeRate'

export interface IExchangeRateRepository {
create (entity: ExchangeRate): Promise<void>
}
6 changes: 6 additions & 0 deletions src/2-business/services/iExchangeRateService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IExchangeRateResponse } from '../../1-domain/models/iExchangeRateResponse'
import { CurrencyEnum } from '../../2-business/enums/currencyEnum'

export interface IExchangeRateService {
get (base: CurrencyEnum): Promise<IExchangeRateResponse>
}
30 changes: 30 additions & 0 deletions src/2-business/useCases/exchangeRate/updateExchangeRateUseCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { IExchangeRateRepository } from '../../repositories/iExchangeRateRepository'
import { IExchangeRateService } from '../../services/iExchangeRateService'
import { ExchangeRate } from '../../../1-domain/entities/exchangeRate'
import { CurrencyEnum } from '../../enums/currencyEnum'
import { IExchangeRateResponse } from '../../../1-domain/models/iExchangeRateResponse'

export class UpdateExchangeRateUseCase {

constructor (
private exchangeRateRepository: IExchangeRateRepository,
private exchangeRateService: IExchangeRateService
) {}

private logPrefix: string = 'UpdateExchangeRateUseCase'

async execute (): Promise<void> {
console.log(`${this.logPrefix} :: start`)
try {
const exchangeRatesResponse: IExchangeRateResponse = await this.exchangeRateService.get(CurrencyEnum.BRL)
const exchangeRates: ExchangeRate = {
baseCurrency: exchangeRatesResponse.base,
rates: exchangeRatesResponse.rates
}
await this.exchangeRateRepository.create(exchangeRates)
console.log(`${this.logPrefix} :: end`)
} catch (error) {
throw error
}
}
}
77 changes: 77 additions & 0 deletions test/unit/2-business/updateExchangeRateUseCase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { ExchangeRate } from '../../../src/1-domain/entities/exchangeRate'
import { IExchangeRateResponse } from '../../../src/1-domain/models/iExchangeRateResponse'
import { UpdateExchangeRateUseCase } from '../../../src/2-business/useCases/exchangeRate/updateExchangeRateUseCase'
import { IExchangeRateRepository } from '../../../src/2-business/repositories/iExchangeRateRepository'
import { IExchangeRateService } from '../../../src/2-business/services/iExchangeRateService'
import { CurrencyEnum } from '../../../src/2-business/enums/currencyEnum'

describe('UpdateExchangeRateUseCase', () => {
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 exchangeRateResponseMock: IExchangeRateResponse = {
success: true,
timestamp: 1519296206,
base: 'BRL',
date: '2021-03-17',
rates: {
AUD: 1.566015,
CAD: 1.560132,
CHF: 1.154727,
CNY: 7.827874,
GBP: 0.882047,
JPY: 132.360679,
USD: 1.23396
}
}

let exchangeRateRepository: IExchangeRateRepository
let exchangeRateService: IExchangeRateService

const setMocks = () => {
exchangeRateRepository = {
create: jest.fn().mockResolvedValue(null)
}
exchangeRateService = {
get: jest.fn().mockResolvedValue(exchangeRateResponseMock)
}
}

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

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

await expect(useCase.execute()).resolves.not.toThrow()
expect(exchangeRateService.get).toHaveBeenCalledWith(CurrencyEnum.BRL)
expect(exchangeRateRepository.create).toHaveBeenCalledWith(exchangeRateMock)
})

test('Failure::should throw error when exchange rate service fails', async () => {
exchangeRateService = {
get: jest.fn().mockRejectedValue(new Error())
}

const useCase = new UpdateExchangeRateUseCase(
exchangeRateRepository,
exchangeRateService
)

await expect(useCase.execute()).rejects.toThrow()
})
})

0 comments on commit 70713ce

Please sign in to comment.