-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #3 - Add update exchange rate use case
- Loading branch information
1 parent
f50fb93
commit 70713ce
Showing
7 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/2-business/useCases/exchangeRate/updateExchangeRateUseCase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |