<commit_before>package account

import (
	"fmt"
	"strings"

	"github.com/backstage/maestro/errors"
	. "github.com/backstage/maestro/log"
)

type Service struct {
	Subdomain     string   `json:"subdomain"`
	Description   string   `json:"description,omitempty"`
	Disabled      bool     `json:"disabled,omitempty"`
	Documentation string   `json:"documentation,omitempty"`
	Endpoint      string   `json:"endpoint,omitempty"`
	Transformers  []string `json:"transformers,omitempty"`
	Owner         string   `json:"owner,omitempty"`
	Team          string   `json:"team"`
	Timeout       int      `json:"timeout,omitempty"`
}

func (service *Service) Create(owner User, team Team) error {
	service.Owner = owner.Email
	service.Subdomain = strings.ToLower(service.Subdomain)
	service.Team = team.Alias

	if !service.valid() {
		return errors.NewValidationError(errors.ErrServiceMissingRequiredFields)
	}

	if service.Exists() {
		return errors.NewValidationError(errors.ErrServiceDuplicateEntry)
	}

	sendHook(newServiceEvent("service.create", service))

	return store.UpsertService(*service)
}

func (service *Service) Update() error {
	if !service.valid() {
		return errors.NewValidationError(errors.ErrServiceMissingRequiredFields)
	}

	if !service.Exists() {
		return errors.NewNotFoundError(errors.ErrServiceNotFound)
	}

	sendHook(newServiceEvent("service.update", service))
	return store.UpsertService(*service)
}

func (service *Service) Delete(owner User) error {
	if service.Owner != owner.Email {
		Logger.Warn(fmt.Sprintf("Only the owner has permission to delete the servce %s.", service.Subdomain))
		return errors.NewForbiddenError(errors.ErrOnlyOwnerHasPermission)
	}
	sendHook(newServiceEvent("service.delete", service))

	return store.DeleteService(*service)
}

func DeleteServicesByTeam(team Team, owner User) error {
	services, err := store.TeamServices(team)
	if err != nil {
		return err
	}
	for _, s := range services {
		s.Delete(owner)
	}
	return nil
}

func (service Service) Exists() bool {
	_, err := store.FindServiceBySubdomain(service.Subdomain)
	if err != nil {
		return false
	}
	return true
}

func FindServiceBySubdomain(subdomain string) (*Service, error) {
	service, err := store.FindServiceBySubdomain(subdomain)
	if err != nil {
		return nil, err
	}
	return &service, nil
}

func (service *Service) valid() bool {
	if service.Subdomain == "" || service.Endpoint == "" || service.Team == "" {
		return false
	}
	return true
}<commit_msg>Method valid raise error in service<commit_after>package account

import (
	"fmt"
	"strings"

	"github.com/backstage/maestro/errors"
	. "github.com/backstage/maestro/log"
)

type Service struct {
	Subdomain     string   `json:"subdomain"`
	Description   string   `json:"description,omitempty"`
	Disabled      bool     `json:"disabled,omitempty"`
	Documentation string   `json:"documentation,omitempty"`
	Endpoint      string   `json:"endpoint,omitempty"`
	Transformers  []string `json:"transformers,omitempty"`
	Owner         string   `json:"owner,omitempty"`
	Team          string   `json:"team"`
	Timeout       int      `json:"timeout,omitempty"`
}

func (service *Service) Create(owner User, team Team) error {
	service.Owner = owner.Email
	service.Subdomain = strings.ToLower(service.Subdomain)
	service.Team = team.Alias

	if err := service.valid(); err != nil {
		return err
	}

	if service.Exists() {
		return errors.NewValidationError(errors.ErrServiceDuplicateEntry)
	}

	sendHook(newServiceEvent("service.create", service))

	return store.UpsertService(*service)
}

func (service *Service) Update() error {
	if err := service.valid(); err != nil {
		return err
	}

	if !service.Exists() {
		return errors.NewNotFoundError(errors.ErrServiceNotFound)
	}

	sendHook(newServiceEvent("service.update", service))
	return store.UpsertService(*service)
}

func (service *Service) Delete(owner User) error {
	if service.Owner != owner.Email {
		Logger.Warn(fmt.Sprintf("Only the owner has permission to delete the servce %s.", service.Subdomain))
		return errors.NewForbiddenError(errors.ErrOnlyOwnerHasPermission)
	}
	sendHook(newServiceEvent("service.delete", service))

	return store.DeleteService(*service)
}

func DeleteServicesByTeam(team Team, owner User) error {
	services, err := store.TeamServices(team)
	if err != nil {
		return err
	}
	for _, s := range services {
		s.Delete(owner)
	}
	return nil
}

func (service Service) Exists() bool {
	_, err := store.FindServiceBySubdomain(service.Subdomain)
	if err != nil {
		return false
	}
	return true
}

func FindServiceBySubdomain(subdomain string) (*Service, error) {
	service, err := store.FindServiceBySubdomain(subdomain)
	if err != nil {
		return nil, err
	}
	return &service, nil
}

func (service *Service) valid() error {
	if service.Subdomain == "" || service.Endpoint == "" || service.Team == "" {
		return errors.NewValidationError(errors.ErrServiceMissingRequiredFields)
	}
	return nil
}
{'fraction_non_alphanumeric': 0.12190166598943519, 'fraction_numerical': 0.0, 'mean_word_length': 4.645642201834862, 'pattern_counts': {'":': 0, '<': 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': '1155954', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1501, 'n_words': 353}