From f8f29ab816be850f0a15dd9ab953218212e84dbc Mon Sep 17 00:00:00 2001 From: Eugenio Sales Date: Sat, 15 Oct 2022 19:50:12 -0300 Subject: [PATCH] feture #3 - Add function, exchante rate model and exchange rate service --- .../functions/exchangeRate/_handlers.yml | 7 ++++ .../exchangeRate/updateExchangeRate.ts | 16 ++++++++++ src/4-framework/models/dynamo/exchangeRate.ts | 32 +++++++++++++++++++ .../repositories/exchangeRateRepository.ts | 11 +++++++ .../services/exchangeRateService.ts | 28 ++++++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 src/4-framework/functions/exchangeRate/_handlers.yml create mode 100644 src/4-framework/functions/exchangeRate/updateExchangeRate.ts create mode 100644 src/4-framework/models/dynamo/exchangeRate.ts create mode 100644 src/4-framework/repositories/exchangeRateRepository.ts create mode 100644 src/4-framework/services/exchangeRateService.ts diff --git a/src/4-framework/functions/exchangeRate/_handlers.yml b/src/4-framework/functions/exchangeRate/_handlers.yml new file mode 100644 index 0000000..d5d1ca9 --- /dev/null +++ b/src/4-framework/functions/exchangeRate/_handlers.yml @@ -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 diff --git a/src/4-framework/functions/exchangeRate/updateExchangeRate.ts b/src/4-framework/functions/exchangeRate/updateExchangeRate.ts new file mode 100644 index 0000000..f7beac6 --- /dev/null +++ b/src/4-framework/functions/exchangeRate/updateExchangeRate.ts @@ -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 +} diff --git a/src/4-framework/models/dynamo/exchangeRate.ts b/src/4-framework/models/dynamo/exchangeRate.ts new file mode 100644 index 0000000..afd80a2 --- /dev/null +++ b/src/4-framework/models/dynamo/exchangeRate.ts @@ -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 = + dynamoose.model('ExchangeRate', schema, modelOptions) diff --git a/src/4-framework/repositories/exchangeRateRepository.ts b/src/4-framework/repositories/exchangeRateRepository.ts new file mode 100644 index 0000000..1a554f1 --- /dev/null +++ b/src/4-framework/repositories/exchangeRateRepository.ts @@ -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 { + return ExchangeRateModel.create(entity, { + overwrite: true + }) + } +} diff --git a/src/4-framework/services/exchangeRateService.ts b/src/4-framework/services/exchangeRateService.ts new file mode 100644 index 0000000..506f841 --- /dev/null +++ b/src/4-framework/services/exchangeRateService.ts @@ -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 { + 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 + } + } +}