| 1 | from datetime import datetime |
| 2 | |
| 3 | from dotenv import load_dotenv |
| 4 | from loguru import logger |
| 5 | from sqlalchemy import select |
| 6 | |
| 7 | from app.db.db_session import get_db_session |
| 8 | from app.db.db_session import get_sync_db_session |
| 9 | from app.integrations.mimecast.routes.mimecast import invoke_mimecast_route |
| 10 | from app.integrations.mimecast.routes.mimecast import mimecast_ttp_url_route |
| 11 | from app.integrations.mimecast.schema.mimecast import MimecastRequest |
| 12 | from app.integrations.mimecast.schema.mimecast import MimecastResponse |
| 13 | from app.integrations.models.customer_integration_settings import CustomerIntegrations |
| 14 | from app.schedulers.models.scheduler import JobMetadata |
| 15 | |
| 16 | load_dotenv() |
| 17 | |
| 18 | |
| 19 | async def invoke_mimecast_integration() -> MimecastResponse: |
| 20 | """ |
| 21 | Invokes the Mimecast integration. |
| 22 | """ |
| 23 | logger.info("Invoking Mimecast integration scheduled job.") |
| 24 | customer_codes = [] |
| 25 | async with get_db_session() as session: |
| 26 | stmt = select(CustomerIntegrations).where( |
| 27 | CustomerIntegrations.integration_service_name == "Mimecast", |
| 28 | ) |
| 29 | result = await session.execute(stmt) |
| 30 | customer_codes = [row.customer_code for row in result.scalars()] |
| 31 | logger.info(f"customer_codes: {customer_codes}") |
| 32 | for customer_code in customer_codes: |
| 33 | await invoke_mimecast_route( |
| 34 | MimecastRequest( |
| 35 | customer_code=customer_code, |
| 36 | integration_name="Mimecast", |
| 37 | ), |
| 38 | session, |
| 39 | ) |
| 40 | # Close the session |
| 41 | await session.close() |
| 42 | with get_sync_db_session() as session: |
| 43 | # Synchronous ORM operations |
| 44 | job_metadata = session.query(JobMetadata).filter_by(job_id="invoke_mimecast_integration").one_or_none() |
| 45 | if job_metadata: |
| 46 | job_metadata.last_success = datetime.utcnow() |
| 47 | session.add(job_metadata) |
| 48 | session.commit() |
| 49 | else: |
| 50 | # Handle the case where job_metadata does not exist |
| 51 | print("JobMetadata for 'invoke_mimecast_integration' not found.") |
| 52 | |
| 53 | return MimecastResponse(success=True, message="Mimecast integration invoked.") |
| 54 | |
| 55 | |
| 56 | async def invoke_mimecast_integration_ttp() -> MimecastResponse: |
| 57 | """ |
| 58 | Invokes the Mimecast integration. |
| 59 | """ |
| 60 | customer_codes = [] |
| 61 | async with get_db_session() as session: |
| 62 | stmt = select(CustomerIntegrations).where( |
| 63 | CustomerIntegrations.integration_service_name == "Mimecast", |
| 64 | ) |
| 65 | result = await session.execute(stmt) |
| 66 | customer_codes = [row.customer_code for row in result.scalars()] |
| 67 | logger.info(f"customer_codes: {customer_codes}") |
| 68 | for customer_code in customer_codes: |
| 69 | await mimecast_ttp_url_route( |
| 70 | MimecastRequest( |
| 71 | customer_code=customer_code, |
| 72 | integration_name="Mimecast", |
| 73 | ), |
| 74 | session, |
| 75 | ) |
| 76 | # Close the session |
| 77 | await session.close() |
| 78 | with get_sync_db_session() as session: |
| 79 | # Synchronous ORM operations |
| 80 | job_metadata = session.query(JobMetadata).filter_by(job_id="invoke_mimecast_integration_ttp").one_or_none() |
| 81 | if job_metadata: |
| 82 | job_metadata.last_success = datetime.utcnow() |
| 83 | session.add(job_metadata) |
| 84 | session.commit() |
| 85 | else: |
| 86 | # Handle the case where job_metadata does not exist |
| 87 | print("JobMetadata for 'invoke_mimecast_integration_ttp' not found.") |
| 88 | return MimecastResponse(success=True, message="Mimecast integration invoked.") |