Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion backend/controllers/team_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,18 @@ func GetTeam(c *gin.Context) {
return
}

// Only members may see the full team (invite code, members' emails).
userID, exists := c.Get("userID")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "User not authenticated"})
return
}
requesterID, ok := userID.(primitive.ObjectID)
if !ok || !team.HasMember(requesterID) {
c.JSON(http.StatusForbidden, gin.H{"error": "You are not a member of this team"})
return
}

c.JSON(http.StatusOK, team)
}

Expand Down Expand Up @@ -655,7 +667,14 @@ func GetAvailableTeams(c *gin.Context) {
return
}

c.JSON(http.StatusOK, teams)
// Any signed-in user can list open teams, so return only public fields:
// no invite codes and no email addresses.
summaries := make([]models.TeamSummary, 0, len(teams))
for _, team := range teams {
summaries = append(summaries, team.Summary())
}

c.JSON(http.StatusOK, summaries)
}

// TransferTeamCaptain transfers captaincy to an existing team member.
Expand Down
56 changes: 56 additions & 0 deletions backend/models/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,62 @@ func (tm TeamMember) MarshalJSON() ([]byte, error) {
})
}

// TeamSummaryMember is the public view of a team member: no email address.
type TeamSummaryMember struct {
UserID string `json:"userId"`
DisplayName string `json:"displayName"`
AvatarURL string `json:"avatarUrl,omitempty"`
Elo float64 `json:"elo"`
}

// TeamSummary is the public view of a team, safe to show to users who are
// not in it. It has no invite code and no email addresses.
type TeamSummary struct {
ID string `json:"id"`
Name string `json:"name"`
CaptainID string `json:"captainId"`
CaptainName string `json:"captainName"`
Members []TeamSummaryMember `json:"members"`
MaxSize int `json:"maxSize"`
AverageElo float64 `json:"averageElo"`
}

// Summary returns the public view of the team.
func (t Team) Summary() TeamSummary {
members := make([]TeamSummaryMember, 0, len(t.Members))
captainName := ""
for _, m := range t.Members {
if m.UserID == t.CaptainID {
captainName = m.DisplayName
}
members = append(members, TeamSummaryMember{
UserID: m.UserID.Hex(),
DisplayName: m.DisplayName,
AvatarURL: m.AvatarURL,
Elo: m.Elo,
})
}
return TeamSummary{
ID: t.ID.Hex(),
Name: t.Name,
CaptainID: t.CaptainID.Hex(),
CaptainName: captainName,
Members: members,
MaxSize: t.MaxSize,
AverageElo: t.AverageElo,
}
}

// HasMember reports whether the user is a member of the team.
func (t Team) HasMember(userID primitive.ObjectID) bool {
for _, m := range t.Members {
if m.UserID == userID {
return true
}
}
return false
}

// TeamDebate represents a debate between two teams
type TeamDebate struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/Pages/TeamBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
updateTeamSize,
transferTeamCaptain,
} from "@/services/teamService";
import type { TeamSummary, TeamSummaryMember } from "@/services/teamService";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
Expand Down Expand Up @@ -91,7 +92,7 @@ const TeamBuilder: React.FC = () => {
const [teamName, setTeamName] = useState("");
const [maxSize, setMaxSize] = useState<number>(4);
const [isCreating, setIsCreating] = useState(false);
const [availableTeams, setAvailableTeams] = useState<Team[]>([]);
const [availableTeams, setAvailableTeams] = useState<TeamSummary[]>([]);
const [userTeams, setUserTeams] = useState<Team[]>([]);
const [error, setError] = useState<string>("");
const [success, setSuccess] = useState<string>("");
Expand Down Expand Up @@ -1089,7 +1090,7 @@ const TeamBuilder: React.FC = () => {
<div className="flex items-center gap-1 min-w-0 max-w-[200px] sm:max-w-xs">
<FaCrown className="text-primary flex-shrink-0" />
<div className="font-medium overflow-x-auto whitespace-nowrap pb-0.5 custom-scrollbar">
{team.captainEmail}
{team.captainName || "Team leader"}
</div>
</div>
<div className="flex items-center gap-1">
Expand Down Expand Up @@ -1117,7 +1118,7 @@ const TeamBuilder: React.FC = () => {
Current Members:
</div>
<div className="flex flex-wrap gap-2">
{(team.members || []).map((member: TeamMember) => (
{(team.members || []).map((member: TeamSummaryMember) => (
<Badge
key={member.userId}
variant="secondary"
Expand Down
21 changes: 20 additions & 1 deletion frontend/src/services/teamService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ export interface TeamMember {
joinedAt: string;
}

// Public view of a team returned by GET /teams/available:
// no invite code and no email addresses.
export interface TeamSummaryMember {
userId: string;
displayName: string;
avatarUrl?: string;
elo: number;
}

export interface TeamSummary {
id: string;
name: string;
captainId: string;
captainName: string;
members: TeamSummaryMember[];
maxSize: number;
averageElo: number;
}

export interface TeamDebate {
id: string;
team1Id: string;
Expand Down Expand Up @@ -138,7 +157,7 @@ export const getUserTeams = async (): Promise<Team[]> => {
};

// Get available teams
export const getAvailableTeams = async (): Promise<Team[]> => {
export const getAvailableTeams = async (): Promise<TeamSummary[]> => {
const token = getAuthToken();
const response = await fetch(`${API_BASE_URL}/teams/available`, {
headers: {
Expand Down