-
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.
feture #3 - Add function, exchante rate model and exchange rate service
- Loading branch information
1 parent
eff658d
commit f8f29ab
Showing
5 changed files
with
94 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,7 @@ | ||
updateExchangeRate: | ||
handler: ${self:custom.funcDir}/exchangeRate/updateExchangeRate.handler | ||
timeout: 30 | ||
events: | ||
- sqs: | ||
arn: ${self:provider.environment.SQS_UPDATE_EXCHANGE_RATE_ARN} | ||
batchSize: 1 |
16 changes: 16 additions & 0 deletions
16
src/4-framework/functions/exchangeRate/updateExchangeRate.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,16 @@ | ||
'use strict' | ||
|
||
import { UpdateExchangeRateController } from '../../../3-adapters/controller/exchangeRate/updateExchangeRateController' | ||
import { ExchangeRateRepository } from '../../repositories/exchangeRateRepository' | ||
import { ExchangeRateService } from '../../services/exchangeRateService' | ||
|
||
exports.handler = async () => { | ||
const updateExchangeRateController = new UpdateExchangeRateController( | ||
new ExchangeRateRepository(), | ||
new ExchangeRateService() | ||
) | ||
|
||
const response = await updateExchangeRateController.run() | ||
|
||
return response | ||
} |
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,32 @@ | ||
import dynamoose, { Schema } from 'dynamoose' | ||
import { Document } from 'dynamoose/dist/Document' | ||
import { SchemaDefinition } from 'dynamoose/dist/Schema' | ||
import { Model, ModelOptionsOptional } from 'dynamoose/dist/Model' | ||
import { ExchangeRate } from '../../../1-domain/entities/exchangeRate' | ||
|
||
export interface ExchangeRateEntity extends Document, ExchangeRate { } | ||
|
||
const schemaDefinition: SchemaDefinition = { | ||
baseCurrency: { | ||
type: String, | ||
hashKey: true | ||
}, | ||
rates: { | ||
type: Object, | ||
required: true | ||
} | ||
} | ||
|
||
const schema = new Schema(schemaDefinition, { | ||
timestamps: true, | ||
saveUnknown: true | ||
}) | ||
|
||
const modelOptions: ModelOptionsOptional = { | ||
throughput: 'ON_DEMAND', | ||
create: false, | ||
waitForActive: false | ||
} | ||
|
||
export const ExchangeRateModel: Model<ExchangeRateEntity> = | ||
dynamoose.model('ExchangeRate', schema, modelOptions) |
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 @@ | ||
import { ExchangeRate } from '../../1-domain/entities/exchangeRate' | ||
import { ExchangeRateModel } from '../models/dynamo/exchangeRate' | ||
import { IExchangeRateRepository } from '../../2-business/repositories/iExchangeRateRepository' | ||
|
||
export class ExchangeRateRepository implements IExchangeRateRepository { | ||
upsert (entity: ExchangeRate): Promise<ExchangeRate> { | ||
return ExchangeRateModel.create(entity, { | ||
overwrite: true | ||
}) | ||
} | ||
} |
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,28 @@ | ||
import { ILatestRatesResponse } from '../../1-domain/models/iExchangeRateResponse' | ||
import { IExchangeRateService } from '../../2-business/services/iExchangeRateService' | ||
import { CurrencyEnum } from '../../2-business/enums/currencyEnum' | ||
import axios, { AxiosResponse } from 'axios' | ||
|
||
export class ExchangeRateService implements IExchangeRateService { | ||
private readonly baseUrl = 'https://api.apilayer.com/exchangerates_data' | ||
private readonly apiKey = process.env.EXCHANGE_RATE_API_KEY | ||
|
||
async getLatestRates (base: CurrencyEnum = CurrencyEnum.BRL): Promise<ILatestRatesResponse> { | ||
const logPrefix = 'getLatestRates' | ||
|
||
try { | ||
const response: AxiosResponse = await axios.get(`${this.baseUrl}/latest`, { | ||
params: { | ||
apikey: this.apiKey, | ||
base: base | ||
} | ||
}) | ||
|
||
console.log(`${logPrefix} :: success ::`, { data: response.data }) | ||
return response.data | ||
} catch (error) { | ||
console.error(`${logPrefix} :: error ::`, error) | ||
throw error | ||
} | ||
} | ||
} |