<commit_before>package teamserver

import (
	"errors"
	"net/http"

	"github.com/concourse/atc/auth"
)

func (s *Server) DestroyTeam(w http.ResponseWriter, r *http.Request) {
	hLog := s.logger.Session("destroy-team")
	hLog.Debug("destroying-team")

	authTeam, authTeamFound := auth.GetTeam(r)
	if !authTeamFound {
		hLog.Error("failed-to-get-team-from-auth", errors.New("failed-to-get-team-from-auth"))
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	teamName := r.FormValue(":team_name")
	teamDB := s.teamDBFactory.GetTeamDB(teamName)

	if !authTeam.IsAdmin() {
		w.WriteHeader(http.StatusForbidden)
		return
	}

	savedTeam, found, err := teamDB.GetTeam()
	if err != nil {
		hLog.Error("failed-to-get-team", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	if !found {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	if savedTeam.Admin {
		allTeams, err := s.teamsDB.GetTeams()
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		adminTeams := 0
		for _, candidate := range allTeams {
			if candidate.Admin {
				adminTeams = adminTeams + 1
			}
		}

		if adminTeams == 1 {
			w.WriteHeader(http.StatusForbidden)
			return
		}
	}

	err = s.teamsDB.DeleteTeamByName(teamName)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusNoContent)
}<commit_msg>add logging for sad paths<commit_after>package teamserver

import (
	"errors"
	"net/http"

	"github.com/concourse/atc/auth"
)

func (s *Server) DestroyTeam(w http.ResponseWriter, r *http.Request) {
	hLog := s.logger.Session("destroy-team")
	hLog.Debug("destroying-team")

	authTeam, authTeamFound := auth.GetTeam(r)
	if !authTeamFound {
		hLog.Error("failed-to-get-team-from-auth", errors.New("failed-to-get-team-from-auth"))
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	teamName := r.FormValue(":team_name")
	teamDB := s.teamDBFactory.GetTeamDB(teamName)

	if !authTeam.IsAdmin() {
		hLog.Info("requesting-team-is-not-admin")
		w.WriteHeader(http.StatusForbidden)
		return
	}

	savedTeam, found, err := teamDB.GetTeam()
	if err != nil {
		hLog.Error("failed-to-get-team", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	if !found {
		hLog.Info("team-not-found")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	if savedTeam.Admin {
		allTeams, err := s.teamsDB.GetTeams()
		if err != nil {
			hLog.Error("failed-to-get-teams", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		adminTeams := 0
		for _, candidate := range allTeams {
			if candidate.Admin {
				adminTeams = adminTeams + 1
			}
		}

		if adminTeams == 1 {
			hLog.Info("team-is-last-admin-team")
			w.WriteHeader(http.StatusForbidden)
			return
		}
	}

	err = s.teamsDB.DeleteTeamByName(teamName)
	if err != nil {
		hLog.Error("failed-to-delete-team", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusNoContent)
}
{'fraction_non_alphanumeric': 0.14063029481531683, 'fraction_numerical': 0.0020332090816672314, 'mean_word_length': 3.961344537815126, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5253588', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1106, 'n_words': 175}