-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda_function.py
More file actions
75 lines (61 loc) · 3.03 KB
/
lambda_function.py
File metadata and controls
75 lines (61 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import logging
from datetime import datetime
import json
from src.infrastructure.container.dependency_container import container
from src.domain.dto.certificate_notification import CertificateNotificationBatch, CertificateNotificationResponse
from src.application.process_certificate_notification import ProcessCertificateNotification
from src.domain.dto.processed_certificate_notification import ProcessedCertificateNotification
from src.application.certificate_notification_tech_floripa import CertificateNotificationTechFloripa
from src.domain.dto.tech_floripa_notification import TechFloripaNotification
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def get_notifications_from_event(event) -> CertificateNotificationBatch:
"""
Extrai e valida as notificações de certificado do evento SQS.
Args:
event: Evento SQS contendo as notificações
Returns:
CertificateNotificationBatch: Batch de notificações validado
"""
body = event.get('Records')[0].get('body')
if isinstance(body, str):
body = json.loads(body)
# Se o body é uma lista, encapsula na estrutura esperada pelo modelo
if isinstance(body, list):
notifications_data = {"notifications": body}
else:
# Se já é um objeto, usa diretamente
notifications_data = body
logger.info(f"Notificações extraídas do evento: {notifications_data}")
return CertificateNotificationBatch.model_validate(notifications_data)
def lambda_handler(event, context):
try:
process_certificate_notification: ProcessCertificateNotification = container.get('process_certificate_notification')
certificate_notification_tech_floripa: CertificateNotificationTechFloripa = container.get('certificate_notification_tech_floripa')
result: ProcessedCertificateNotification = process_certificate_notification.execute(
get_notifications_from_event(event)
)
notifications: TechFloripaNotification = TechFloripaNotification.from_certificates(result.updated_certificates)
if certificate_notification_tech_floripa.send_notification(notifications):
logger.info(f"Notificação enviada com sucesso: {result.updated_certificates[0].product_id}")
return {
"statusCode": 200,
"body": {
"message": "Notificação enviada com sucesso",
}
}
else:
logger.error(f"Erro ao enviar notificação: {result.updated_certificates[0].product_id}")
return {
"statusCode": 500,
"message": "Erro ao enviar notificação - " + result.updated_certificates[0].product_id
}
except Exception as e:
logger.error(f"Erro ao processar notificação de certificado: {str(e)}")
return {
"statusCode": 500,
"body": json.dumps({
"message": "Erro ao processar notificação de certificado",
"error": str(e)
})
}