from apps.users.schemas import UserOut from apps.bounties.schemas import BountyOut, BountyWithDetailsOut def serialize_user(user): """Serialize user to UserOut.""" if not user: return None return UserOut( id=user.id, open_id=user.open_id, name=user.name, email=user.email, avatar=user.avatar, role=user.role, created_at=user.created_at, updated_at=user.updated_at, ) def serialize_bounty(bounty, include_counts: bool = False): """Serialize bounty to BountyOut or BountyWithDetailsOut.""" data = { "id": bounty.id, "title": bounty.title, "description": bounty.description, "reward": bounty.reward, "currency": bounty.currency, "publisher_id": bounty.publisher_id, "publisher": serialize_user(bounty.publisher), "acceptor_id": bounty.acceptor_id, "acceptor": serialize_user(bounty.acceptor) if bounty.acceptor else None, "status": bounty.status, "deadline": bounty.deadline, "completed_at": bounty.completed_at, "is_paid": bounty.is_paid, "is_escrowed": bounty.is_escrowed, "created_at": bounty.created_at, "updated_at": bounty.updated_at, } if include_counts: applications_count = getattr(bounty, "applications_count", None) comments_count = getattr(bounty, "comments_count", None) data["applications_count"] = ( applications_count if applications_count is not None else bounty.applications.count() ) data["comments_count"] = ( comments_count if comments_count is not None else bounty.comments.count() ) return BountyWithDetailsOut(**data) return BountyOut(**data)