Skip to content

Commit

Permalink
Contabilização
Browse files Browse the repository at this point in the history
  • Loading branch information
epiresdasilva committed Dec 9, 2020
1 parent 2fbbb0e commit 3a76f4e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 22 deletions.
36 changes: 27 additions & 9 deletions contabilizar.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
import json
import boto3
import logging
import os
import uuid


def main(event, context):
body = event["detail"]

logger = logging.getLogger()
logger.setLevel(logging.INFO)

body = {
"mensagem": "Contabilização incluida com sucesso"
}
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table_name = os.environ['CONTABILIZACAO_TABLE']

response = {
"statusCode": 200,
"body": json.dumps(body)
}
table = dynamodb.Table(table_name)

return response
return table.put_item(
Item={
'id': str(uuid.uuid4()),
'registros': [
{
"contaContabil": "1.0.0.0.01",
"valor": body["valor"],
"documento": body["numeroCartao"],
"natureza": "CREDITO"
},
{
"contaContabil": "2.0.0.0.01",
"valor": body["valor"],
"documento": body["numeroCartao"],
"natureza": "DEBITO"
}
]
}
)
42 changes: 29 additions & 13 deletions debitador.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ def main(event, context):
cursor.execute(f"""
update conta_corrente set saldo = saldo - {body["valor"]}
where numero_cartao = {body["numeroCartao"]}
returning saldo;
returning saldo, agencia, numero_conta;
""")
result = cursor.fetchone()
saldo = float(result[0])
agencia = result[1]
numero_conta = result[2]

if saldo < 0:
connection.rollback()
Expand All @@ -43,21 +45,21 @@ def main(event, context):
connection.rollback()
logger.error("Error while inserting", e)

status_code = 200
body = {
"saldo": saldo
conta = {
"agencia": agencia,
"numeroConta": numero_conta,
"numeroCartao": body["numeroCartao"],
"valor": body["valor"]
}

if saldo < 0:
status_code = 409
body = {
"mensagem": "Saldo insuficiente"
}
return error_response(conta)

return success_response(conta)

response = {
"statusCode": status_code,
"body": json.dumps(body)
}

def success_response(body):
status_code = 200

client = boto3.client('events')

Expand All @@ -72,4 +74,18 @@ def main(event, context):
)
print(str(bridge_response))

return response
return {
"statusCode": status_code,
"body": json.dumps(body)
}


def error_response(body):
status_code = 409
body["mensagem"] = "Saldo insuficiente"

return {
"statusCode": status_code,
"body": json.dumps(body)
}

2 changes: 2 additions & 0 deletions registrar_lancamento.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@


def main(event, context):
print(str(event))

logger = logging.getLogger()
logger.setLevel(logging.INFO)

Expand Down
47 changes: 47 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ provider:
region: us-east-1
stage: dev
stackName: ${self:service}-${self:provider.stage}
environment:
LANCAMENTO_TABLE: lancamento-${opt:stage, self:provider.stage}
CONTABILIZACAO_TABLE: contabilizacao-${opt:stage, self:provider.stage}
iamRoleStatements:
- Effect: Allow
Action:
Expand All @@ -23,6 +26,14 @@ provider:
Action:
- events:PutEvents
Resource: "*"
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- s3:GetObject
Resource: "*"

functions:
debitador:
Expand Down Expand Up @@ -124,6 +135,42 @@ resources:
GroupId: !Ref AppSecurityGroup
IpProtocol: -1
SourceSecurityGroupId: !GetAtt AppSecurityGroup.GroupId
LancamentoDynamoDbTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
-
AttributeName: "id"
AttributeType: "S"
KeySchema:
-
AttributeName: "id"
KeyType: "HASH"
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
StreamSpecification:
StreamViewType: "NEW_AND_OLD_IMAGES"
TableName: ${self:provider.environment.LANCAMENTO_TABLE}
ContabilizacaoDynamoDbTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
-
AttributeName: "id"
AttributeType: "S"
KeySchema:
-
AttributeName: "id"
KeyType: "HASH"
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
StreamSpecification:
StreamViewType: "NEW_AND_OLD_IMAGES"
TableName: ${self:provider.environment.CONTABILIZACAO_TABLE}

plugins:
- serverless-apigateway-service-proxy
Expand Down

0 comments on commit 3a76f4e

Please sign in to comment.