Skip to content

Commit

Permalink
feature #4 - add function, event adapters and class validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeniosales committed Oct 16, 2022
1 parent d9d96b8 commit fa76bc8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ custom:
funcDir: src/4-framework/functions
baseApi: sbf/v1
baseApiInternal: api/v1
albHttpListenerArn: ${ssm:/exchangerate/infra/alb/arn}
serverless-offline:
port: 4000
stage: dev
Expand Down
14 changes: 14 additions & 0 deletions src/4-framework/functions/exchangeRate/_handlers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@ updateExchangeRate:
- sqs:
arn: ${self:provider.environment.SQS_UPDATE_EXCHANGE_RATE_ARN}
batchSize: 1

getExchangeRate:
handler: ${self:custom.funcDir}/exchangeRate/getExchangeRate.handler
events:
- alb:
listenerArn: ${self:custom.albHttpListenerArn}
priority: 1
conditions:
method: GET
path: /${self:custom.baseApi}/exchangerate
request:
parameters:
querystrings:
baseCurrency: true
25 changes: 25 additions & 0 deletions src/4-framework/functions/exchangeRate/getExchangeRate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

import { ALBEvent } from 'aws-lambda'
import { GetExchangeRateController } from '../../../3-adapters/controller/exchangeRate/getExchangeRateController'
import { InputGetExchangeRate } from '../../../3-adapters/serializers/exchangeRate/getExchangeRate/input'
import { ExchangeRateRepository } from '../../repositories/exchangeRateRepository'
import { albHttpEventNormalizer } from '../../utility/eventAdapters'

function eventAdapter (event: ALBEvent): InputGetExchangeRate {
const body = albHttpEventNormalizer(event)
return new InputGetExchangeRate(body)

}

exports.handler = async (event: ALBEvent) => {
const getExchangeRateController = new GetExchangeRateController(
new ExchangeRateRepository()
)

const input = eventAdapter(event)

const response = await getExchangeRateController.run(input)

return response
}
5 changes: 5 additions & 0 deletions src/4-framework/repositories/exchangeRateRepository.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { ExchangeRate } from '../../1-domain/entities/exchangeRate'
import { ExchangeRateModel } from '../models/dynamo/exchangeRate'
import { IExchangeRateRepository } from '../../2-business/repositories/iExchangeRateRepository'
import { CurrencyEnum } from '../../2-business/enums/currencyEnum'

export class ExchangeRateRepository implements IExchangeRateRepository {
upsert (entity: ExchangeRate): Promise<ExchangeRate> {
return ExchangeRateModel.create(entity, {
overwrite: true
})
}

get (baseCurrency: CurrencyEnum): Promise<ExchangeRate> {
return ExchangeRateModel.get({ baseCurrency })
}
}
11 changes: 11 additions & 0 deletions src/4-framework/utility/classValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { validateSync } from 'class-validator'

export class InputClassValidator {
errors () {
const validationErrors = validateSync(this)
return validationErrors.map((e) => ({
property: e.property,
constraints: e.constraints
}))
}
}
15 changes: 15 additions & 0 deletions src/4-framework/utility/eventAdapters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ALBEvent } from 'aws-lambda'

export const albHttpEventNormalizer = (event: ALBEvent) => {
let payload = {}
if (event.queryStringParameters) {
payload = { ...event.queryStringParameters }
}

if (event.body) {
const body = JSON.parse(event.body)
payload = { ...payload, ...body }
}

return payload
}

0 comments on commit fa76bc8

Please sign in to comment.