Skip to content

Commit

Permalink
Models/Schemas: ConditionType and class Condition - remove sponsee_ad…
Browse files Browse the repository at this point in the history
…dress, add user_id tie to contract_id
  • Loading branch information
lykimq committed Feb 5, 2024
1 parent 2cc8de0 commit ccc3ae2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
31 changes: 17 additions & 14 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,14 @@ class Condition(Base):

id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
type = Column(Enum(ConditionType))
sponsee_address = Column(
String,
CheckConstraint(
"(type = 'MAX_CALLS_PER_SPONSEE') = (sponsee_address IS NOT NULL)",
name="sponsee_address_not_null_constraint",
),
nullable=True,
)
contract_id = Column(
UUID(as_uuid=True),
CheckConstraint(
"(type = 'MAX_CALLS_PER_ENTRYPOINT') = (contract_id IS NOT NULL)",
name="contract_id_not_null_constraint",
),
# Check if the condition type is either for entrypoint or for new users
CheckConstraint("type IN ('MAX_CALLS_PER_ENTRYPOINT', 'MAX_CALLS_PER_CONTRACT_FOR_NEW_USERS') AND contract_id IS NOT NULL"
),
ForeignKey("contracts.id"),
nullable=True,
)
nullable=True,)

entrypoint_id = Column(
UUID(as_uuid=True),
CheckConstraint(
Expand All @@ -159,6 +150,17 @@ class Condition(Base):
ForeignKey("entrypoints.id"),
nullable=True,
)

# Foreign key column reference the ID of the associated user if the condition is
# MAX_CALLS_PER_CONTRACT_FOR_NEW_USERS
user_id = Column(
UUID(as_uuid=True),
CheckConstraint("(type = 'MAX_CALLS_PER_CONTRACT_FOR_NEW_USERS') = (user_id IS NOT NULL)",
name="user_id_not_null_constraint",
),
ForeignKey("users.id"), nullable=True,
)

vault_id = Column(UUID(as_uuid=True), ForeignKey(
"credits.id"), nullable=False)
max = Column(Integer, nullable=False)
Expand All @@ -169,4 +171,5 @@ class Condition(Base):

contract = relationship("Contract", back_populates="conditions")
entrypoint = relationship("Entrypoint", back_populates="conditions")
user = relationship("User", back_populates="users")
vault = relationship("Credit", back_populates="conditions")
2 changes: 1 addition & 1 deletion src/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# -- UTILITY TYPES --
class ConditionType(enum.Enum):
MAX_CALLS_PER_ENTRYPOINT = "MAX_CALLS_PER_ENTRYPOINT"
MAX_CALLS_PER_SPONSEE = "MAX_CALLS_PER_SPONSEE"
MAX_CALLS_PER_CONTRACT_FOR_NEW_USERS = "MAX_CALLS_PER_CONTRACT_FOR_NEW_USERS"


# Users
Expand Down

0 comments on commit ccc3ae2

Please sign in to comment.