-
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 #4 - add get exchange rate use case
- Loading branch information
1 parent
dde671d
commit 9828cc2
Showing
6 changed files
with
123 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,11 @@ | ||
export interface ErrorData { | ||
code: string, | ||
message: string, | ||
shortMessage: string | ||
} | ||
|
||
export class ExchangeRateError extends Error { | ||
constructor (public readonly errorData: ErrorData) { | ||
super(errorData.message) | ||
} | ||
} |
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,7 @@ | ||
import { ErrorData } from '../errorData' | ||
|
||
export class ExchangeRateError extends Error { | ||
constructor (public readonly errorData: ErrorData) { | ||
super(errorData.message) | ||
} | ||
} |
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 @@ | ||
export const exchangeRateNotFoundError = { | ||
code: 'EXR-001', | ||
message: 'Exchange Rate not found for the base', | ||
shortMessage: 'exchangeRateNotFound' | ||
} |
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 { CurrencyEnum } from '../../enums/currencyEnum' | ||
|
||
export interface GetExchangeRateInputDto { | ||
base: CurrencyEnum | ||
} |
34 changes: 34 additions & 0 deletions
34
src/2-business/useCases/exchangeRate/getExchangeRateUseCase.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,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
61
test/unit/2-business/exchangeRate/getExchangeRateUseCase.test.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,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() | ||
}) | ||
}) |