from collections.abc import Iterable, Sequence import sqlalchemy as sa from pydantic import TypeAdapter from dmp.adapters.db import Repository from dmp.google.dtos import GoogleUserAdAccountLabel from dmp.google.models import GoogleAdAccount GoogleUserAdAccountLabelAssociationListMapper = TypeAdapter( list[GoogleUserAdAccountLabel] ) class GoogleAdAccountRepository(Repository[GoogleAdAccount]): def find_by_external_ids( self, external_ids: Iterable[str] ) -> Sequence[GoogleAdAccount]: query = sa.select(GoogleAdAccount).where( GoogleAdAccount.external_id.in_(external_ids) ) result = self.db.session.execute(query) return result.scalars().all() def count_by_ids(self, ids: list[str]) -> int: query = sa.select(sa.func.count()).where(GoogleAdAccount.id.in_(ids)) result = self.db.session.execute(query) return result.scalar_one() def find_by_identity_id_and_user_id_for_assigment( self, identity_id: str, user_id: str ) -> list[GoogleUserAdAccountLabel]: query = self.db.query_from_template( "google-ad-account/find-by-identity-id-and-user-id-for-assigment.sql", context={ "identity_id": identity_id, "user_id": user_id, }, ) return GoogleUserAdAccountLabelAssociationListMapper.validate_python( self.db.session.execute(query).mappings().all() ) def exists_by_id(self, id_: str) -> bool: query = sa.select(sa.select(1).exists().where(GoogleAdAccount.id == id_)) result = self.db.session.execute(query) return bool(result.scalar_one())