from dataclasses import dataclass from campaigns.connectors.facebook.base import FacebookClient from campaigns.meta.dtos import InstagramAccount from campaigns.meta.models import FacebookPage from .facebook_page_token import FacebookPageTokenService @dataclass class InstagramAccountService: facebook_client: FacebookClient facebook_page_token_service: FacebookPageTokenService async def get_instagram_account_for_facebook_page( self, page: FacebookPage ) -> InstagramAccount | None: page_token = await self.facebook_page_token_service.decrypt_token(page) instagram_business_account_id = ( await self.facebook_client.get_instagram_business_account_for_page( page_id=page.external_id, page_access_token=page_token, ) ) if not instagram_business_account_id: return None instagram_business_account = ( await self.facebook_client.get_instagram_business_account( instagram_business_account_id=instagram_business_account_id.id, page_access_token=page_token, ) ) return InstagramAccount.from_api_obj(instagram_business_account)