All files / src/composables useMatchLineup.js

98.97% Statements 193/195
94.23% Branches 49/52
100% Functions 7/7
98.97% Lines 193/195

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 2261x 1x 1x 1x 1x 1x           106x 106x 106x   1x 84x   84x 84x 84x   84x 84x 84x 84x   84x 84x 84x 84x 84x 52x 52x   51x 52x   50x 50x 50x 50x 52x   52x 52x 52x 52x 52x 52x 52x 52x 52x   52x 52x   52x 52x 52x 52x 52x 1x 1x 1x 1x 1x 52x   84x 84x 84x 84x 94x 94x 94x 94x 92x 92x 2x 2x 2x 2x 94x   84x 84x 84x 84x 48x 48x   47x 47x   47x 47x 47x 47x 47x   46x 46x 46x 1x 1x 1x 48x 47x 47x 48x   84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 6x 6x 6x 6x   2x 6x 6x     6x 6x   6x 6x 6x 6x 6x 6x 6x 6x 6x   2x 2x 6x 6x 6x 6x 2x 6x 6x 6x 3x 3x 3x 6x 6x 1x 1x 1x 1x 1x 1x 6x   84x 84x 84x 84x 6x 6x 6x 1x 1x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   4x 4x 4x 4x 3x 4x 1x 1x 4x   4x 6x 1x 1x 1x 6x   84x 84x 84x 84x   84x 84x 84x 84x   84x 84x 84x 84x 84x 84x 84x  
/**
 * Match Lineup Composable
 *
 * Shared logic for fetching and saving team rosters and lineups.
 * Used by both useLiveMatch (live matches) and MatchDetailView (pre-match).
 */
 
import { ref, isRef } from 'vue';
import { useAuthStore } from '../stores/auth';
import { getApiBaseUrl } from '../config/api';
 
function unwrapMatchData(matchData) {
  return isRef(matchData) ? matchData.value : matchData;
}
 
export function useMatchLineup(matchId, matchData) {
  const authStore = useAuthStore();
 
  // Roster state
  const homeRoster = ref([]);
  const awayRoster = ref([]);
 
  // Lineup state
  const homeLineup = ref(null);
  const awayLineup = ref(null);
  const lineupLoading = ref(false);
 
  /**
   * Fetch rosters for both teams in the match.
   * Returns { home: [], away: [] } for compatibility with LiveAdminControls.
   */
  async function fetchTeamRosters() {
    const match = unwrapMatchData(matchData);
    if (!match) return { home: [], away: [] };
 
    const { home_team_id, away_team_id, season_id, age_group_id } = match;
    if (!season_id) return { home: [], away: [] };
 
    // SB-68: pass the match's age_group_id so the live-match path filters
    // strictly by age group. Without this, an IFA U15 match would pull
    // the IFA U14 roster (team_id is age-agnostic for umbrella clubs).
    const ageGroupParam =
      age_group_id != null ? `&age_group_id=${age_group_id}` : '';
 
    try {
      const [homeResponse, awayResponse] = await Promise.all([
        authStore.apiRequest(
          `${getApiBaseUrl()}/api/teams/${home_team_id}/roster?season_id=${season_id}${ageGroupParam}`
        ),
        authStore.apiRequest(
          `${getApiBaseUrl()}/api/teams/${away_team_id}/roster?season_id=${season_id}${ageGroupParam}`
        ),
      ]);
 
      homeRoster.value = homeResponse?.roster || [];
      awayRoster.value = awayResponse?.roster || [];
 
      return {
        home: homeRoster.value,
        away: awayRoster.value,
      };
    } catch (err) {
      // SB-118: propagate so callers can show an error + retry instead of
      // silently treating a failed fetch as an empty roster.
      console.error('Error fetching rosters:', err);
      throw err;
    }
  }
 
  /**
   * Fetch lineup for a single team.
   */
  async function fetchLineup(teamId) {
    try {
      const response = await authStore.apiRequest(
        `${getApiBaseUrl()}/api/matches/${matchId}/lineup/${teamId}`
      );
      return response;
    } catch (err) {
      // SB-118: propagate — a failed fetch is not the same as "no lineup"
      console.error('Error fetching lineup:', err);
      throw err;
    }
  }
 
  /**
   * Fetch lineups for both teams, updating homeLineup/awayLineup refs.
   */
  async function fetchLineups() {
    const match = unwrapMatchData(matchData);
    if (!match) return;
 
    const { home_team_id, away_team_id } = match;
    lineupLoading.value = true;
 
    try {
      const [homeResponse, awayResponse] = await Promise.all([
        fetchLineup(home_team_id),
        fetchLineup(away_team_id),
      ]);
 
      homeLineup.value = homeResponse;
      awayLineup.value = awayResponse;
    } catch (err) {
      // SB-118: propagate so callers can show an error + retry
      console.error('Error fetching lineups:', err);
      throw err;
    } finally {
      lineupLoading.value = false;
    }
  }
 
  /**
   * Materialize placeholder players before saving a lineup.
   *
   * A jersey number typed into the picker for a player who isn't on the roster
   * yet is held as a negative player_id (-jersey). On save those numbers are
   * bulk-created as roster rows (the backend skips ones that already exist),
   * then the lineup positions are remapped to the new real ids. Mirrors the
   * android LineupScreen flow.
   *
   * Returns { ok, positions } or { ok: false, error }.
   */
  async function materializePlaceholders(teamId, positions) {
    const placeholderNumbers = [
      ...new Set(positions.filter(p => p.player_id < 0).map(p => -p.player_id)),
    ];
    if (placeholderNumbers.length === 0) return { ok: true, positions };
 
    const match = unwrapMatchData(matchData);
    const season_id = match?.season_id;
    if (!season_id) {
      return { ok: false, error: 'No season set for this match' };
    }
    const age_group_id = match?.age_group_id ?? null;
    const base = getApiBaseUrl();
 
    // Create the missing jersey numbers (duplicates are skipped server-side).
    await authStore.apiRequest(`${base}/api/teams/${teamId}/roster/bulk`, {
      method: 'POST',
      body: JSON.stringify({
        season_id,
        age_group_id,
        players: placeholderNumbers.map(n => ({ jersey_number: n })),
      }),
    });
 
    // Refetch (age-scoped when known) and remap number -> new id.
    const ageParam =
      age_group_id != null ? `&age_group_id=${age_group_id}` : '';
    const rosterResponse = await authStore.apiRequest(
      `${base}/api/teams/${teamId}/roster?season_id=${season_id}${ageParam}`
    );
    const idByNumber = new Map(
      (rosterResponse?.roster || []).map(p => [p.jersey_number, p.id])
    );
    const remapped = positions.map(p =>
      p.player_id < 0
        ? { ...p, player_id: idByNumber.get(-p.player_id) ?? p.player_id }
        : p
    );
    if (remapped.some(p => p.player_id < 0)) {
      return {
        ok: false,
        error: 'Could not create roster entries for the entered numbers',
      };
    }
    return { ok: true, positions: remapped };
  }
 
  /**
   * Save lineup for a team. Updates the corresponding local ref.
   */
  async function saveLineup(teamId, formationName, positions) {
    try {
      const materialized = await materializePlaceholders(teamId, positions);
      if (!materialized.ok) {
        return { success: false, error: materialized.error };
      }
 
      const response = await authStore.apiRequest(
        `${getApiBaseUrl()}/api/matches/${matchId}/lineup/${teamId}`,
        {
          method: 'PUT',
          body: JSON.stringify({
            formation_name: formationName,
            positions: materialized.positions,
          }),
        }
      );
 
      // Update local state
      const match = unwrapMatchData(matchData);
      if (match) {
        if (teamId === match.home_team_id) {
          homeLineup.value = response;
        } else if (teamId === match.away_team_id) {
          awayLineup.value = response;
        }
      }
 
      return { success: true, lineup: response };
    } catch (err) {
      console.error('Error saving lineup:', err);
      return { success: false, error: err.message };
    }
  }
 
  return {
    // Roster state
    homeRoster,
    awayRoster,
 
    // Lineup state
    homeLineup,
    awayLineup,
    lineupLoading,
 
    // Methods
    fetchTeamRosters,
    fetchLineup,
    fetchLineups,
    saveLineup,
  };
}