Skip to content

Commit

Permalink
feture #3 - Add function, exchante rate model and exchange rate service
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeniosales committed Oct 15, 2022
1 parent eff658d commit f8f29ab
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/4-framework/functions/exchangeRate/_handlers.yml
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 src/4-framework/functions/exchangeRate/updateExchangeRate.ts
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
}
32 changes: 32 additions & 0 deletions src/4-framework/models/dynamo/exchangeRate.ts
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)
11 changes: 11 additions & 0 deletions src/4-framework/repositories/exchangeRateRepository.ts
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
})
}
}
28 changes: 28 additions & 0 deletions src/4-framework/services/exchangeRateService.ts
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
}
}
}

0 comments on commit f8f29ab

Please sign in to comment.