Skip to content

Commit

Permalink
Add icp table and use the criteria when scaing for leads
Browse files Browse the repository at this point in the history
  • Loading branch information
shrir committed Sep 11, 2024
1 parent ba11fcf commit 487281a
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# type: ignore
"""Add icp table
Revision ID: 110d329b0992
Revises: 50dc3def711b
Create Date: 2024-09-11 15:57:47.240984+00:00
"""
from __future__ import annotations

import warnings
from typing import TYPE_CHECKING

import sqlalchemy as sa
from alembic import op
from advanced_alchemy.types import EncryptedString, EncryptedText, GUID, ORA_JSONB, DateTimeUTC
from sqlalchemy import Text # noqa: F401

from app.db.models.custom_types import CompanyCriteriaType, ToolCriteriaType, PersonCriteriaType

if TYPE_CHECKING:
from collections.abc import Sequence

__all__ = ["downgrade", "upgrade", "schema_upgrades", "schema_downgrades", "data_upgrades", "data_downgrades"]

sa.GUID = GUID
sa.DateTimeUTC = DateTimeUTC
sa.ORA_JSONB = ORA_JSONB
sa.EncryptedString = EncryptedString
sa.EncryptedText = EncryptedText

# revision identifiers, used by Alembic.
revision = "110d329b0992"
down_revision = "50dc3def711b"
branch_labels = None
depends_on = None


def upgrade() -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
with op.get_context().autocommit_block():
schema_upgrades()
data_upgrades()


def downgrade() -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
with op.get_context().autocommit_block():
data_downgrades()
schema_downgrades()


def schema_upgrades() -> None:
"""schema upgrade migrations go here."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"icp",
sa.Column("id", sa.GUID(length=16), nullable=False),
sa.Column("company", CompanyCriteriaType(), nullable=True),
sa.Column("tool", ToolCriteriaType(), nullable=True),
sa.Column("person", PersonCriteriaType(), nullable=True),
sa.Column("tenant_id", sa.GUID(length=16), nullable=False),
sa.Column("sa_orm_sentinel", sa.Integer(), nullable=True),
sa.Column("created_at", sa.DateTimeUTC(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTimeUTC(timezone=True), nullable=False),
sa.ForeignKeyConstraint(["tenant_id"], ["tenant.id"], name=op.f("fk_icp_tenant_id_tenant")),
sa.PrimaryKeyConstraint("id", name=op.f("pk_icp")),
)
with op.batch_alter_table("icp", schema=None) as batch_op:
batch_op.create_index(batch_op.f("ix_icp_person"), ["person"], unique=False)
batch_op.create_index(batch_op.f("ix_icp_tenant_id"), ["tenant_id"], unique=False)

# ### end Alembic commands ###


def schema_downgrades() -> None:
"""schema downgrade migrations go here."""
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("icp", schema=None) as batch_op:
batch_op.drop_index(batch_op.f("ix_icp_tenant_id"))
batch_op.drop_index(batch_op.f("ix_icp_person"))

op.drop_table("icp")
# ### end Alembic commands ###


def data_upgrades() -> None:
"""Add any optional data upgrade migrations here!"""


def data_downgrades() -> None:
"""Add any optional data downgrade migrations here!"""
2 changes: 2 additions & 0 deletions src/app/db/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .job_post import JobPost
from .person import Person
from .opportunity import Opportunity, OpportunityAuditLog, opportunity_person_relation, opportunity_job_post_relation
from .icp import ICP

__all__ = (
"User",
Expand All @@ -33,4 +34,5 @@
"OpprtunityAuditLog",
"opportunity_person_relation",
"opportunity_job_post_relation",
"ICP",
)
45 changes: 45 additions & 0 deletions src/app/db/models/custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
OrgSize,
Tool,
Scale,
OrgSizeCriteria,
CompanyCriteria,
ToolCriteria,
PersonCriteria,
)


Expand Down Expand Up @@ -116,3 +120,44 @@ def process_result_value(self, value, dialect):
objs.append(obj)
return objs
return None


class OrgSizeCriteriaType(JSONBType):
def process_result_value(self, value, dialect):
"""Convert JSON format to Python object when reading from the database."""
if value and isinstance(value, dict):
return OrgSizeCriteria.from_dict(value)
return None


class PersonCriteriaType(JSONBType):
def process_result_value(self, value, dialect):
"""Convert JSON format to Python object when reading from the database."""
if value and isinstance(value, dict):
return PersonCriteria.from_dict(value)
return None


class ToolCriteriaType(JSONBType):
def process_result_value(self, value, dialect):
"""Convert JSON format to Python object when reading from the database."""
if value and isinstance(value, dict):
return ToolCriteria.from_dict(value)
return None


class CompanyCriteriaType(JSONBType):
def process_result_value(self, value, dialect):
"""Convert JSON format to Python object when reading from the database."""
if value and isinstance(value, dict):
obj = CompanyCriteria.from_dict(value)
funding_rounds = []
for funding_round in obj.funding:
try:
funding_rounds.append(FundingRound(funding_round))
except ValueError:
pass
obj.funding = funding_rounds
obj.org_size = OrgSizeCriteria.from_dict(obj.org_size)
return obj
return None
21 changes: 21 additions & 0 deletions src/app/db/models/icp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

from uuid import UUID

from advanced_alchemy.base import UUIDAuditBase
from sqlalchemy import String, ForeignKey
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.lib.schema import CompanyCriteria, ToolCriteria, PersonCriteria
from .custom_types import CompanyCriteriaType, ToolCriteriaType, PersonCriteriaType


class ICP(UUIDAuditBase):
"""ICP criteria."""

__tablename__ = "icp"
company: Mapped[CompanyCriteria] = mapped_column(CompanyCriteriaType, nullable=True)
tool: Mapped[ToolCriteria] = mapped_column(ToolCriteriaType, nullable=True)
person: Mapped[PersonCriteria] = mapped_column(PersonCriteriaType, nullable=True, default="identified", index=True)
tenant_id: Mapped[UUID] = mapped_column(ForeignKey("tenant.id"), nullable=False, index=True)
1 change: 1 addition & 0 deletions src/app/domain/opportunities/controllers/icp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""ICP Controllers."""
13 changes: 10 additions & 3 deletions src/app/domain/opportunities/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
from typing import TYPE_CHECKING, Any
from uuid import UUID # noqa: TCH003

from advanced_alchemy.repository import SQLAlchemyAsyncSlugRepository
from advanced_alchemy.repository import SQLAlchemyAsyncSlugRepository, SQLAlchemyAsyncRepository
from sqlalchemy import ColumnElement, select
from sqlalchemy.orm import joinedload, InstrumentedAttribute

from app.db.models import Opportunity, OpportunityAuditLog
from app.db.models import Opportunity, OpportunityAuditLog, ICP

if TYPE_CHECKING:
from advanced_alchemy.filters import FilterTypes
from advanced_alchemy.repository._util import LoadSpec

__all__ = ("OpportunityRepository", "OpportunityAuditLogRepository")
__all__ = ("OpportunityRepository", "OpportunityAuditLogRepository", "ICPRepository")


class OpportunityRepository(SQLAlchemyAsyncSlugRepository[Opportunity]):
Expand Down Expand Up @@ -66,3 +66,10 @@ class OpportunityAuditLogRepository(SQLAlchemyAsyncSlugRepository[OpportunityAud
"""OpportunityAuditLog Repository."""

model_type = OpportunityAuditLog


class ICPRepository(SQLAlchemyAsyncRepository[ICP]):
"""JobPost Repository."""

model_type = ICP
match_fields = ["id"]
26 changes: 25 additions & 1 deletion src/app/domain/opportunities/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from app.domain.companies.schemas import Company
from app.domain.people.schemas import Person
from app.domain.jobs.schemas import JobPost
from app.lib.schema import CamelizedBaseStruct, OpportunityStage
from app.lib.schema import CamelizedBaseStruct, OpportunityStage, CompanyCriteria, ToolCriteria, PersonCriteria


class OpportunityAuditLog(CamelizedBaseStruct):
Expand Down Expand Up @@ -66,3 +66,27 @@ class OpportunityUpdate(CamelizedBaseStruct):
stage: OpportunityStage | None | msgspec.UnsetType = msgspec.UNSET
notes: str | None | msgspec.UnsetType = msgspec.UNSET
owner_id: UUID | None = None


class ICP(CamelizedBaseStruct):
"""An ICP."""

company: CompanyCriteria | None = None
tool: ToolCriteria | None = None
person: PersonCriteria | None = None


class ICPCreate(CamelizedBaseStruct):
"""An ICP create schema."""

company: CompanyCriteria | None = None
tool: ToolCriteria | None = None
person: PersonCriteria | None = None


class ICPUpdate(CamelizedBaseStruct):
"""An ICP update schema."""

company: CompanyCriteria | None | msgspec.UnsetType = msgspec.UNSET
tool: ToolCriteria | None | msgspec.UnsetType = msgspec.UNSET
person: PersonCriteria | None | msgspec.UnsetType = msgspec.UNSET
Loading

0 comments on commit 487281a

Please sign in to comment.