second review

This commit is contained in:
counterweight 2025-12-20 11:58:35 +01:00
parent 23049da55a
commit 976a880312
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
7 changed files with 105 additions and 65 deletions

View file

@ -534,6 +534,22 @@ class InviteResponse(BaseModel):
revoked_at: datetime | None
def build_invite_response(invite: Invite) -> InviteResponse:
"""Build an InviteResponse from an Invite with loaded relationships."""
return InviteResponse(
id=invite.id,
identifier=invite.identifier,
godfather_id=invite.godfather_id,
godfather_email=invite.godfather.email,
status=invite.status.value,
used_by_id=invite.used_by_id,
used_by_email=invite.used_by.email if invite.used_by else None,
created_at=invite.created_at,
spent_at=invite.spent_at,
revoked_at=invite.revoked_at,
)
MAX_INVITE_COLLISION_RETRIES = 3
@ -544,17 +560,16 @@ async def create_invite(
_current_user: User = Depends(require_permission(Permission.MANAGE_INVITES)),
):
"""Create a new invite for a specified godfather user."""
# Validate godfather exists and get their info
# Validate godfather exists
result = await db.execute(
select(User.id, User.email).where(User.id == data.godfather_id)
select(User.id).where(User.id == data.godfather_id)
)
godfather_row = result.one_or_none()
if not godfather_row:
godfather_id = result.scalar_one_or_none()
if not godfather_id:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Godfather user not found",
)
godfather_id, godfather_email = godfather_row
# Try to create invite with retry on collision
invite: Invite | None = None
@ -568,7 +583,7 @@ async def create_invite(
db.add(invite)
try:
await db.commit()
await db.refresh(invite)
await db.refresh(invite, ["godfather"])
break
except IntegrityError:
await db.rollback()
@ -578,19 +593,12 @@ async def create_invite(
detail="Failed to generate unique invite code. Please try again.",
)
assert invite is not None # We either succeeded or raised an exception above
return InviteResponse(
id=invite.id,
identifier=invite.identifier,
godfather_id=invite.godfather_id,
godfather_email=godfather_email,
status=invite.status.value,
used_by_id=invite.used_by_id,
used_by_email=None,
created_at=invite.created_at,
spent_at=invite.spent_at,
revoked_at=invite.revoked_at,
)
if invite is None:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to create invite",
)
return build_invite_response(invite)
class UserInviteResponse(BaseModel):
@ -693,20 +701,7 @@ async def list_all_invites(
invites = result.scalars().all()
# Build responses using preloaded relationships
records = []
for invite in invites:
records.append(InviteResponse(
id=invite.id,
identifier=invite.identifier,
godfather_id=invite.godfather_id,
godfather_email=invite.godfather.email,
status=invite.status.value,
used_by_id=invite.used_by_id,
used_by_email=invite.used_by.email if invite.used_by else None,
created_at=invite.created_at,
spent_at=invite.spent_at,
revoked_at=invite.revoked_at,
))
records = [build_invite_response(invite) for invite in invites]
return PaginatedInviteRecords(
records=records,
@ -744,16 +739,4 @@ async def revoke_invite(
await db.commit()
await db.refresh(invite)
# Use preloaded relationships (selectin loading)
return InviteResponse(
id=invite.id,
identifier=invite.identifier,
godfather_id=invite.godfather_id,
godfather_email=invite.godfather.email,
status=invite.status.value,
used_by_id=invite.used_by_id,
used_by_email=invite.used_by.email if invite.used_by else None,
created_at=invite.created_at,
spent_at=invite.spent_at,
revoked_at=invite.revoked_at,
)
return build_invite_response(invite)

View file

@ -206,7 +206,7 @@ class Invite(Base):
godfather: Mapped[User] = relationship(
"User",
foreign_keys=[godfather_id],
lazy="selectin",
lazy="joined",
)
# User who used this invite (null until spent)
@ -216,7 +216,7 @@ class Invite(Base):
used_by: Mapped[User | None] = relationship(
"User",
foreign_keys=[used_by_id],
lazy="selectin",
lazy="joined",
)
# Timestamps