Skip to content

Commit

Permalink
Fix opportunity get methods
Browse files Browse the repository at this point in the history
  • Loading branch information
shri committed Aug 3, 2024
1 parent 328a1c9 commit e7455d9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/app/db/models/opportunity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
2 changes: 1 addition & 1 deletion src/app/domain/opportunities/controllers/opportunities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
38 changes: 30 additions & 8 deletions src/app/domain/opportunities/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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."""
Expand Down
11 changes: 11 additions & 0 deletions src/app/domain/opportunities/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit e7455d9

Please sign in to comment.