| 1 | from typing import Dict |
| 2 | |
| 3 | from fastapi import APIRouter |
| 4 | from fastapi import Depends |
| 5 | from fastapi import HTTPException |
| 6 | from fastapi import Security |
| 7 | from sqlalchemy.ext.asyncio import AsyncSession |
| 8 | |
| 9 | from app.auth.utils import AuthHandler |
| 10 | from app.db.db_session import get_db |
| 11 | from app.integrations.office365.schema.provision import ProvisionOffice365AuthKeys |
| 12 | from app.integrations.office365.schema.provision import ProvisionOffice365Request |
| 13 | from app.integrations.office365.schema.provision import ProvisionOffice365Response |
| 14 | from app.integrations.office365.services.provision import provision_office365 |
| 15 | from app.integrations.routes import find_customer_integration |
| 16 | from app.integrations.routes import get_customer_integrations_by_customer_code |
| 17 | from app.integrations.schema import CustomerIntegrations |
| 18 | from app.integrations.schema import CustomerIntegrationsResponse |
| 19 | |
| 20 | integration_office365_router = APIRouter() |
| 21 | |
| 22 | |
| 23 | async def get_customer_integration_response( |
| 24 | customer_code: str, |
| 25 | session: AsyncSession, |
| 26 | ) -> CustomerIntegrationsResponse: |
| 27 | """ |
| 28 | Retrieves the integration response for a customer. |
| 29 | |
| 30 | Args: |
| 31 | customer_code (str): The code of the customer. |
| 32 | session (AsyncSession): The async session object for database operations. |
| 33 | |
| 34 | Returns: |
| 35 | CustomerIntegrationsResponse: The integration response for the customer. |
| 36 | |
| 37 | Raises: |
| 38 | HTTPException: If the customer integration settings are not found. |
| 39 | """ |
| 40 | customer_integration_response = await get_customer_integrations_by_customer_code( |
| 41 | customer_code, |
| 42 | session, |
| 43 | ) |
| 44 | if customer_integration_response.available_integrations == []: |
| 45 | raise HTTPException( |
| 46 | status_code=404, |
| 47 | detail="Customer integration settings not found.", |
| 48 | ) |
| 49 | return customer_integration_response |
| 50 | |
| 51 | |
| 52 | def extract_office365_auth_keys( |
| 53 | customer_integration: CustomerIntegrations, |
| 54 | ) -> Dict[str, str]: |
| 55 | """ |
| 56 | Extracts the authentication keys for Office365 integration from the given customer integration. |
| 57 | |
| 58 | Args: |
| 59 | customer_integration (CustomerIntegrations): The customer integration object. |
| 60 | |
| 61 | Returns: |
| 62 | Dict[str, str]: A dictionary containing the authentication keys for Office365 integration. |
| 63 | |
| 64 | Raises: |
| 65 | HTTPException: If no authentication keys are found for Office365 integration. |
| 66 | """ |
| 67 | office365_auth_keys = {} |
| 68 | for subscription in customer_integration.integration_subscriptions: |
| 69 | if subscription.integration_service.service_name == "Office365": |
| 70 | for auth_key in subscription.integration_auth_keys: |
| 71 | office365_auth_keys[auth_key.auth_key_name] = auth_key.auth_value |
| 72 | if not office365_auth_keys: |
| 73 | raise HTTPException( |
| 74 | status_code=404, |
| 75 | detail="No auth keys found for Office365 integration. Please create auth keys for Office365 integration.", |
| 76 | ) |
| 77 | return office365_auth_keys |
| 78 | |
| 79 | |
| 80 | @integration_office365_router.post( |
| 81 | "/provision", |
| 82 | response_model=ProvisionOffice365Response, |
| 83 | description="Provision Office365 integration for a customer.", |
| 84 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 85 | ) |
| 86 | async def provision_office365_route( |
| 87 | provision_office365_request: ProvisionOffice365Request, |
| 88 | session: AsyncSession = Depends(get_db), |
| 89 | ) -> ProvisionOffice365Response: |
| 90 | """ |
| 91 | Provisions Office365 integration for a customer. |
| 92 | |
| 93 | Args: |
| 94 | provision_office365_request (ProvisionOffice365Request): The request object containing the necessary information for provisioning. |
| 95 | session (AsyncSession, optional): The database session. Defaults to Depends(get_db). |
| 96 | |
| 97 | Returns: |
| 98 | ProvisionOffice365Response: The response object containing the result of the provisioning. |
| 99 | """ |
| 100 | customer_integration_response = await get_customer_integration_response( |
| 101 | provision_office365_request.customer_code, |
| 102 | session, |
| 103 | ) |
| 104 | |
| 105 | customer_integration = await find_customer_integration( |
| 106 | provision_office365_request.customer_code, |
| 107 | provision_office365_request.integration_name, |
| 108 | customer_integration_response, |
| 109 | ) |
| 110 | |
| 111 | office365_auth_keys = extract_office365_auth_keys(customer_integration) |
| 112 | |
| 113 | auth_keys = ProvisionOffice365AuthKeys(**office365_auth_keys) |
| 114 | |
| 115 | return await provision_office365( |
| 116 | provision_office365_request.customer_code, |
| 117 | auth_keys, |
| 118 | session, |
| 119 | ) |