generated from VLADIMIR/template
213 lines
7.2 KiB
Go
213 lines
7.2 KiB
Go
package team_service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"html"
|
|
"strings"
|
|
"time"
|
|
|
|
"evening_detective_server/internal/modules/email_sender"
|
|
"evening_detective_server/internal/modules/password_generator"
|
|
"evening_detective_server/internal/modules/team_qr_pdf"
|
|
"evening_detective_server/internal/repos"
|
|
"evening_detective_server/internal/repos/games_repo"
|
|
"evening_detective_server/internal/repos/teams_repo"
|
|
"evening_detective_server/internal/services/game_model"
|
|
)
|
|
|
|
// TeamService handles team operations.
|
|
type TeamService struct {
|
|
gamesRepo ITeamGamesRepository
|
|
teamsRepo ITeamsRepository
|
|
passwordGenerator password_generator.IPasswordGenerator
|
|
appBaseURL string
|
|
emailSender email_sender.IEmailSender
|
|
venueMode bool
|
|
receiptPrinter IReceiptPrinter
|
|
}
|
|
|
|
// ITeamGamesRepository reads games used by team operations.
|
|
type ITeamGamesRepository interface {
|
|
GetGameByID(context.Context, int) (*repos.Game, error)
|
|
}
|
|
|
|
// ITeamsRepository stores teams used by this service.
|
|
type ITeamsRepository interface {
|
|
GetTeamByID(context.Context, string) (*repos.Team, error)
|
|
GetActiveTeamsByGameID(context.Context, int) ([]*repos.Team, error)
|
|
AddTeamWithParticipation(context.Context, int, int, string, string, string, int32, bool) (string, error)
|
|
UpdateTeam(context.Context, string, string) error
|
|
DeleteTeam(context.Context, string) error
|
|
RegisterTeamWithParticipation(context.Context, int, string, string, string, int32, string, bool) (string, error)
|
|
}
|
|
|
|
// NewTeamService creates a team service.
|
|
func NewTeamService(
|
|
gamesRepo ITeamGamesRepository,
|
|
teamsRepo ITeamsRepository,
|
|
passwordGenerator password_generator.IPasswordGenerator,
|
|
appBaseURL string,
|
|
emailSender email_sender.IEmailSender,
|
|
venueMode bool,
|
|
receiptPrinter IReceiptPrinter,
|
|
) *TeamService {
|
|
return &TeamService{
|
|
gamesRepo: gamesRepo,
|
|
teamsRepo: teamsRepo,
|
|
passwordGenerator: passwordGenerator,
|
|
appBaseURL: appBaseURL,
|
|
emailSender: emailSender,
|
|
venueMode: venueMode,
|
|
receiptPrinter: receiptPrinter,
|
|
}
|
|
}
|
|
|
|
// AddTeam creates a team.
|
|
func (s *TeamService) AddTeam(
|
|
ctx context.Context,
|
|
creatorID int,
|
|
gameID int,
|
|
name string,
|
|
captainEmail string,
|
|
participantsCount int32,
|
|
) (*game_model.Team, error) {
|
|
captainEmail = strings.TrimSpace(captainEmail)
|
|
if captainEmail != "" && !validCaptainEmail(captainEmail) {
|
|
return nil, ErrInvalidCaptainEmail
|
|
}
|
|
if participantsCount < 0 || participantsCount > 8 {
|
|
return nil, ErrInvalidParticipantsCount
|
|
}
|
|
password, err := s.passwordGenerator.Generate()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
game, err := s.gamesRepo.GetGameByID(ctx, gameID)
|
|
if err != nil {
|
|
if errors.Is(err, games_repo.ErrGameNotFound) {
|
|
return nil, ErrGameNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
id, err := s.teamsRepo.AddTeamWithParticipation(ctx, creatorID, gameID, name, password, captainEmail, participantsCount, !time.Now().Before(game.StartAt.Add(-24*time.Hour)))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &game_model.Team{
|
|
ID: id,
|
|
Password: password,
|
|
}, nil
|
|
}
|
|
|
|
// UpdateTeam changes a team's name.
|
|
func (s *TeamService) UpdateTeam(ctx context.Context, teamID string, name string) error {
|
|
err := s.teamsRepo.UpdateTeam(ctx, teamID, name)
|
|
if errors.Is(err, teams_repo.ErrTeamNotFound) {
|
|
return ErrTeamNotFound
|
|
}
|
|
return err
|
|
}
|
|
|
|
// DeleteTeam removes a team.
|
|
func (s *TeamService) DeleteTeam(ctx context.Context, teamID string) error {
|
|
err := s.teamsRepo.DeleteTeam(ctx, teamID)
|
|
if errors.Is(err, teams_repo.ErrTeamNotFound) {
|
|
return ErrTeamNotFound
|
|
}
|
|
return err
|
|
}
|
|
|
|
// RegisterTeam registers a team for a game.
|
|
func (s *TeamService) RegisterTeam(ctx context.Context, gameID int, deliveryID string, registration Registration) (string, error) {
|
|
if err := registration.Validate(); err != nil {
|
|
return "", err
|
|
}
|
|
if gameID <= 0 || deliveryID == "" || len(deliveryID) > 255 || strings.ContainsRune(deliveryID, 0) {
|
|
return "", ErrInvalidRegistration
|
|
}
|
|
password, err := s.passwordGenerator.Generate()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
game, err := s.gamesRepo.GetGameByID(ctx, gameID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
autoConfirm := !time.Now().Before(game.StartAt.Add(-24 * time.Hour))
|
|
id, err := s.teamsRepo.RegisterTeamWithParticipation(ctx, gameID, deliveryID, registration.Name, registration.Email, registration.ParticipantsCount, password, autoConfirm)
|
|
if err != nil {
|
|
if errors.Is(err, teams_repo.ErrRegistrationClosed) {
|
|
return "", ErrRegistrationClosed
|
|
}
|
|
return "", err
|
|
}
|
|
if s.emailSender == nil {
|
|
return id, nil
|
|
}
|
|
team, err := s.teamsRepo.GetTeamByID(ctx, id)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if err := s.emailSender.Send(ctx, registrationEmail(team, game.Name, s.appBaseURL)); err != nil {
|
|
return "", fmt.Errorf("email send: %w", err)
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
func registrationEmail(team *repos.Team, gameName, baseURL string) email_sender.Message {
|
|
link := strings.TrimRight(baseURL, "/") + "/team-story/" + team.ID + "?password=" + team.Password
|
|
body := fmt.Sprintf("Регистрация команды «%s» в игре «%s» прошла успешно.\n\nУчастников: %d\n\nСсылка для входа в игру: %s", team.Name, gameName, team.ParticipantsCount, link)
|
|
content := fmt.Sprintf("<p class=\"email-text\">Регистрация команды <strong>%s</strong> в игре <strong>%s</strong> прошла успешно.</p><p class=\"email-text\">Участников: %d</p>%s", html.EscapeString(team.Name), html.EscapeString(gameName), team.ParticipantsCount, email_sender.ButtonHTML(link, "Войти в игру"))
|
|
markup := email_sender.WrapHTML(content, strings.TrimRight(baseURL, "/"))
|
|
return email_sender.Message{To: team.CaptainEmail, Subject: "Регистрация команды в «Вечернем детективе»", Body: body, HTML: markup}
|
|
}
|
|
|
|
// DownloadTeamQR returns a PDF with team QR codes.
|
|
func (s *TeamService) DownloadTeamQR(ctx context.Context, id int) ([]byte, string, error) {
|
|
game, err := s.gamesRepo.GetGameByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, games_repo.ErrGameNotFound) {
|
|
return nil, "", ErrGameNotFound
|
|
}
|
|
return nil, "", err
|
|
}
|
|
teams, err := s.teamsRepo.GetActiveTeamsByGameID(ctx, id)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
if len(teams) == 0 {
|
|
return nil, "", ErrNoCards
|
|
}
|
|
return s.GenerateTeamQR(ctx, game, teams)
|
|
}
|
|
|
|
// GenerateTeamQR uses the provided teams so a print package shares one team list.
|
|
func (s *TeamService) GenerateTeamQR(ctx context.Context, game *repos.Game, teams []*repos.Team) ([]byte, string, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, "", err
|
|
}
|
|
cards, filename, err := teamQRData(s.appBaseURL, game, teams)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
data, err := team_qr_pdf.Generate(cards)
|
|
return data, filename, err
|
|
}
|
|
|
|
// CheckPresence validates team credentials without loading game progress or a scenario.
|
|
func (s *TeamService) CheckPresence(ctx context.Context, teamID string, password string) (int, error) {
|
|
team, err := s.teamsRepo.GetTeamByID(ctx, teamID)
|
|
if errors.Is(err, teams_repo.ErrTeamNotFound) {
|
|
return 0, ErrTeamNotFound
|
|
}
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if team.Password != password || team.ParticipationStatus == "cancelled" {
|
|
return 0, ErrTeamNotFound
|
|
}
|
|
return team.GameID, nil
|
|
}
|