All files / src/utils matchPermissions.js

100% Statements 58/58
94.59% Branches 35/37
100% Functions 4/4
100% Lines 58/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 661x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1481x 1480x 1481x 1481x 1481x 1481x   1x 1x 1x 1x 1x 14x 13x 13x 14x 14x 14x 14x 14x 14x   1x 1x 1x 1x 1x 1x 1x 1x 5004x 5004x   1465x   5004x 10x 10x 10x 10x   5004x 21x 21x 21x   1446x 1446x  
/**
 * Who may edit a given match.
 *
 * The rule itself is old — admin edits anything, a club manager edits any
 * match involving a team in their club, a team manager edits their own team's
 * matches — but it had been copy-pasted per screen. The copies drifted:
 * `MatchDetailView` granted *every* club manager, without checking the club.
 * Tournament rows needed the same rule again (SB-906), so it lives here once.
 *
 * Match shapes differ by endpoint. The league list carries flat
 * `home_team_id` / `away_team_id`; tournament rows carry nested
 * `home_team: { id }` and `home_team_club: { id }`. Both are read here so
 * callers don't have to normalise first.
 */
 
/** Team ids on both sides, from either match shape. */
export function matchTeamIds(match) {
  if (!match) return { homeTeamId: null, awayTeamId: null };
  return {
    homeTeamId: match.home_team_id ?? match.home_team?.id ?? null,
    awayTeamId: match.away_team_id ?? match.away_team?.id ?? null,
  };
}
 
/**
 * Club ids on both sides. Tournament matches embed the club; league matches
 * don't, so a caller with a teams list passes `resolveClubId` to look one up.
 */
export function matchClubIds(match, resolveClubId = null) {
  if (!match) return { homeClubId: null, awayClubId: null };
  const { homeTeamId, awayTeamId } = matchTeamIds(match);
  const lookup =
    typeof resolveClubId === 'function' ? resolveClubId : () => null;
  return {
    homeClubId: match.home_team_club?.id ?? lookup(homeTeamId) ?? null,
    awayClubId: match.away_team_club?.id ?? lookup(awayTeamId) ?? null,
  };
}
 
/**
 * @param {object} match
 * @param {object} viewer  { isAdmin, isClubManager, isTeamManager, clubId, teamId }
 * @param {object} [options]
 * @param {(teamId:number)=>number|null} [options.resolveClubId]
 * @returns {boolean}
 */
export function canEditMatch(match, viewer, { resolveClubId = null } = {}) {
  if (!match || !viewer) return false;
  if (viewer.isAdmin) return true;
 
  const { homeTeamId, awayTeamId } = matchTeamIds(match);
 
  if (viewer.isClubManager && viewer.clubId != null) {
    const { homeClubId, awayClubId } = matchClubIds(match, resolveClubId);
    if (homeClubId === viewer.clubId || awayClubId === viewer.clubId)
      return true;
  }
 
  if (viewer.isTeamManager && viewer.teamId != null) {
    if (homeTeamId === viewer.teamId || awayTeamId === viewer.teamId)
      return true;
  }
 
  return false;
}