|
| 1 | +from fastapi import APIRouter, Depends, HTTPException |
| 2 | + |
| 3 | +from api.db import db_client |
| 4 | +from api.db.models import UserModel |
| 5 | +from api.enums import OrganizationConfigurationKey |
| 6 | +from api.schemas.telephony_config import ( |
| 7 | + TelephonyConfigurationResponse, |
| 8 | + TwilioConfigurationRequest, |
| 9 | + TwilioConfigurationResponse, |
| 10 | +) |
| 11 | +from api.services.auth.depends import get_user |
| 12 | +from api.services.configuration.masking import is_mask_of, mask_key |
| 13 | + |
| 14 | +router = APIRouter(prefix="/organizations", tags=["organizations"]) |
| 15 | + |
| 16 | + |
| 17 | +@router.get("/telephony-config", response_model=TelephonyConfigurationResponse) |
| 18 | +async def get_telephony_configuration(user: UserModel = Depends(get_user)): |
| 19 | + """Get telephony configuration for the user's organization with masked sensitive fields.""" |
| 20 | + if not user.selected_organization_id: |
| 21 | + raise HTTPException(status_code=400, detail="No organization selected") |
| 22 | + |
| 23 | + config = await db_client.get_configuration( |
| 24 | + user.selected_organization_id, |
| 25 | + OrganizationConfigurationKey.TWILIO_CONFIGURATION.value, |
| 26 | + ) |
| 27 | + |
| 28 | + if not config or not config.value: |
| 29 | + return TelephonyConfigurationResponse(twilio=None) |
| 30 | + |
| 31 | + # Mask sensitive fields (account_sid and auth_token) before returning |
| 32 | + account_sid = config.value.get("account_sid", "") |
| 33 | + auth_token = config.value.get("auth_token", "") |
| 34 | + |
| 35 | + return TelephonyConfigurationResponse( |
| 36 | + twilio=TwilioConfigurationResponse( |
| 37 | + provider="twilio", |
| 38 | + account_sid=mask_key(account_sid) if account_sid else "", |
| 39 | + auth_token=mask_key(auth_token) if auth_token else "", |
| 40 | + from_numbers=config.value.get("from_numbers", []), |
| 41 | + ) |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +@router.post("/telephony-config") |
| 46 | +async def save_telephony_configuration( |
| 47 | + request: TwilioConfigurationRequest, user: UserModel = Depends(get_user) |
| 48 | +): |
| 49 | + """Save telephony configuration for the user's organization.""" |
| 50 | + if not user.selected_organization_id: |
| 51 | + raise HTTPException(status_code=400, detail="No organization selected") |
| 52 | + |
| 53 | + # Fetch existing configuration to handle masked values |
| 54 | + existing_config = await db_client.get_configuration( |
| 55 | + user.selected_organization_id, |
| 56 | + OrganizationConfigurationKey.TWILIO_CONFIGURATION.value, |
| 57 | + ) |
| 58 | + |
| 59 | + # Build new configuration |
| 60 | + config_value = { |
| 61 | + "provider": request.provider, |
| 62 | + "account_sid": request.account_sid, |
| 63 | + "auth_token": request.auth_token, |
| 64 | + "from_numbers": request.from_numbers, |
| 65 | + } |
| 66 | + |
| 67 | + # If incoming values are masked (same as stored masked value), keep the original |
| 68 | + if existing_config and existing_config.value: |
| 69 | + # Check if account_sid is unchanged (masked value matches) |
| 70 | + stored_account_sid = existing_config.value.get("account_sid", "") |
| 71 | + if stored_account_sid and is_mask_of(request.account_sid, stored_account_sid): |
| 72 | + config_value["account_sid"] = stored_account_sid # Keep original |
| 73 | + |
| 74 | + # Check if auth_token is unchanged (masked value matches) |
| 75 | + stored_auth_token = existing_config.value.get("auth_token", "") |
| 76 | + if stored_auth_token and is_mask_of(request.auth_token, stored_auth_token): |
| 77 | + config_value["auth_token"] = stored_auth_token # Keep original |
| 78 | + |
| 79 | + await db_client.upsert_configuration( |
| 80 | + user.selected_organization_id, |
| 81 | + OrganizationConfigurationKey.TWILIO_CONFIGURATION.value, |
| 82 | + config_value, |
| 83 | + ) |
| 84 | + |
| 85 | + return {"message": "Telephony configuration saved successfully"} |
0 commit comments