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