added free agents
This commit is contained in:
@@ -128,6 +128,7 @@ func AddPlayerToTeam(ctx context.Context, tx bun.Tx, seasonID, leagueID, teamID,
|
||||
|
||||
// ManageTeamRoster replaces the entire roster for a team in a season/league.
|
||||
// It deletes all existing roster entries and inserts the new ones.
|
||||
// Also auto-removes free agent registrations and nominations for players joining a team.
|
||||
func ManageTeamRoster(ctx context.Context, tx bun.Tx, seasonID, leagueID, teamID, managerID int, playerIDs []int, audit *AuditMeta) error {
|
||||
// Delete all existing roster entries for this team/season/league
|
||||
_, err := tx.NewDelete().
|
||||
@@ -140,6 +141,56 @@ func ManageTeamRoster(ctx context.Context, tx bun.Tx, seasonID, leagueID, teamID
|
||||
return errors.Wrap(err, "delete existing roster")
|
||||
}
|
||||
|
||||
// Collect all player IDs being added (including manager)
|
||||
allPlayerIDs := make([]int, 0, len(playerIDs)+1)
|
||||
if managerID > 0 {
|
||||
allPlayerIDs = append(allPlayerIDs, managerID)
|
||||
}
|
||||
for _, pid := range playerIDs {
|
||||
if pid != managerID {
|
||||
allPlayerIDs = append(allPlayerIDs, pid)
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-remove free agent registrations and nominations for players joining a team
|
||||
for _, playerID := range allPlayerIDs {
|
||||
// Check if the player is a registered free agent
|
||||
isFA, err := IsFreeAgentRegistered(ctx, tx, seasonID, leagueID, playerID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "IsFreeAgentRegistered")
|
||||
}
|
||||
if isFA {
|
||||
// Remove all nominations for this player
|
||||
err = RemoveAllFreeAgentNominationsForPlayer(ctx, tx, playerID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "RemoveAllFreeAgentNominationsForPlayer")
|
||||
}
|
||||
// Remove free agent registration
|
||||
err = RemoveFreeAgentRegistrationForPlayer(ctx, tx, playerID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "RemoveFreeAgentRegistrationForPlayer")
|
||||
}
|
||||
// Log the cascade action
|
||||
if audit != nil {
|
||||
cascadeInfo := &AuditInfo{
|
||||
"free_agents.auto_removed_on_team_join",
|
||||
"season_league_free_agent",
|
||||
fmt.Sprintf("%d-%d-%d", seasonID, leagueID, playerID),
|
||||
map[string]any{
|
||||
"season_id": seasonID,
|
||||
"league_id": leagueID,
|
||||
"player_id": playerID,
|
||||
"team_id": teamID,
|
||||
},
|
||||
}
|
||||
err = LogSuccess(ctx, tx, audit, cascadeInfo)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "LogSuccess cascade")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Insert manager if provided
|
||||
if managerID > 0 {
|
||||
tr := &TeamRoster{
|
||||
|
||||
Reference in New Issue
Block a user