-
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 controller and serializer of get exchange rate micro…
…sservice
- Loading branch information
1 parent
7d949b7
commit d9d96b8
Showing
5 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/3-adapters/controller/exchangeRate/getExchangeRateController.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,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
14
src/3-adapters/serializers/exchangeRate/getExchangeRate/input.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,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) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/3-adapters/serializers/exchangeRate/getExchangeRate/output.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,5 @@ | ||
import { ExchangeRate } from '../../../../1-domain/entities/exchangeRate' | ||
|
||
export interface OutputGetExchangeRate { | ||
exchangeRate: ExchangeRate | ||
} |
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
60 changes: 60 additions & 0 deletions
60
test/unit/3-adpters/exchangeRate/getExchangeRateController.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,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 } | ||
}) | ||
}) | ||
}) |