diff --git a/src/app/db/models/opportunity.py b/src/app/db/models/opportunity.py index 789fc366..ea6e8c62 100644 --- a/src/app/db/models/opportunity.py +++ b/src/app/db/models/opportunity.py @@ -81,13 +81,14 @@ class Opportunity(UUIDAuditBase, SlugKey): secondary=lambda: opportunity_person_relation, cascade="all, delete", passive_deletes=True, + lazy="selectin", ) job_posts: Mapped[list[JobPost]] = relationship( secondary=lambda: opportunity_job_post_relation, cascade="all, delete", passive_deletes=True, - lazy="select", + lazy="selectin", ) logs: Mapped[list[OpportunityAuditLog]] = relationship( - lazy="select", + lazy="selectin", ) diff --git a/src/app/domain/opportunities/controllers/opportunities.py b/src/app/domain/opportunities/controllers/opportunities.py index 0f6785b4..136701be 100644 --- a/src/app/domain/opportunities/controllers/opportunities.py +++ b/src/app/domain/opportunities/controllers/opportunities.py @@ -57,7 +57,7 @@ async def list_opportunities( filters: Annotated[list[FilterTypes], Dependency(skip_validation=True)], ) -> OffsetPagination[Opportunity]: """List opportunities that your account can access..""" - results, total = await opportunities_service.list_and_count(*filters) + results, total = await opportunities_service.get_opportunities(*filters, tenant_id=current_user.tenant_id) return opportunities_service.to_schema(data=results, total=total, schema_type=Opportunity, filters=filters) @post( diff --git a/src/app/domain/opportunities/repositories.py b/src/app/domain/opportunities/repositories.py index 3053d0ba..76f297e2 100644 --- a/src/app/domain/opportunities/repositories.py +++ b/src/app/domain/opportunities/repositories.py @@ -5,12 +5,13 @@ from advanced_alchemy.repository import SQLAlchemyAsyncSlugRepository from sqlalchemy import ColumnElement, select -from sqlalchemy.orm import joinedload +from sqlalchemy.orm import joinedload, InstrumentedAttribute from app.db.models import Opportunity, OpportunityAuditLog if TYPE_CHECKING: from advanced_alchemy.filters import FilterTypes + from advanced_alchemy.repository._util import LoadSpec __all__ = ( "OpportunityRepository", @@ -36,18 +37,39 @@ async def get_opportunities( return await self.list_and_count( *filters, statement=select(Opportunity) - .where(Opportunity.tenant_id == tenant_id) - .order_by(Opportunity.score) - .options( - joinedload(Opportunity.company, innerjoin=True), - joinedload(Opportunity.contacts, isouter=True), - joinedload(Opportunity.logs, innerjoin=True), - ), + .where(Opportunity.tenant_id == tenant_id), + #.order_by(Opportunity.score.desc(), Opportunity.created_at.desc()) auto_expunge=auto_expunge, force_basic_query_mode=force_basic_query_mode, **kwargs, ) + async def get_opportunity( + self, + opportunity_id: UUID, + tenant_id: UUID, + *, + id_attribute: str | InstrumentedAttribute[Any] | None = None, + load: LoadSpec | None = None, + execution_options: dict[str, Any] | None = None, + auto_expunge: bool | None = None, + ) -> Opportunity: + """Get an opportunity along with it's associated details.""" + return await self.repository.get_one( + item_id=opportunity_id, + auto_expunge=auto_expunge, + statement=select(Opportunity) + .where((Opportunity.id == opportunity_id) & (Opportunity.tenant_id == tenant_id)) + #.order_by(Opportunity.score.desc(), Opportunity.created_at.desc()) + .options( + joinedload(Opportunity.contacts, innerjoin=False), + joinedload(Opportunity.job_posts, innerjoin=False), + joinedload(Opportunity.logs, innerjoin=False), + ), + id_attribute=id_attribute, + load=load, + execution_options=execution_options, + ) class OpportunityAuditLogRepository(SQLAlchemyAsyncSlugRepository[OpportunityAuditLog]): """OpportunityAuditLog Repository.""" diff --git a/src/app/domain/opportunities/services.py b/src/app/domain/opportunities/services.py index dd9bb714..213427b1 100644 --- a/src/app/domain/opportunities/services.py +++ b/src/app/domain/opportunities/services.py @@ -62,6 +62,17 @@ async def get_opportunities( """Get all opportunities for a tenant.""" return await self.repository.get_opportunities(*filters, tenant_id=tenant_id, **kwargs) + async def get_opportunity( + self, + opportunity_id: UUID, + tenant_id: UUID, + **kwargs: Any, + ) -> tuple[list[Opportunity], int]: + """Get all opportunities for a tenant.""" + return await self.repository.get_opportunity( + opportunity_id=opportunity_id, tenant_id=tenant_id, **kwargs + ) + async def update( self, data: ModelDictT[Opportunity],