generated from VLADIMIR/template
add email
This commit is contained in:
@@ -1384,6 +1384,7 @@ func (s *server) AddTeam(ctx context.Context, req *proto.AddTeamReq) (*proto.Add
|
||||
claims.UserID,
|
||||
int(req.GameId),
|
||||
req.Name,
|
||||
req.CaptainEmail,
|
||||
)
|
||||
if err != nil {
|
||||
return &proto.AddTeamRsp{
|
||||
|
||||
@@ -118,4 +118,18 @@ func TestRegistrationPostgres(t *testing.T) {
|
||||
if otherID, err := repo.RegisterTeam(ctx, 2, "delivery", "Team", "captain@example.org", 5, "abcdefgh"); err != nil || otherID == id {
|
||||
t.Fatalf("other game: %d %v", otherID, err)
|
||||
}
|
||||
for _, email := range []string{"", "manual@example.org"} {
|
||||
manualID, err := repo.AddTeamWithParticipation(ctx, 1, 2, "Manual", "password", email, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manual, err := repo.GetTeamByID(ctx, manualID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if manual.CaptainEmail != email || manual.ParticipationStatus != "pending" {
|
||||
t.Fatalf("incorrect manual team: %+v", manual)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -115,10 +115,10 @@ func (r *TeamsRepo) AddTeam(
|
||||
name string,
|
||||
password string,
|
||||
) (int, error) {
|
||||
return r.AddTeamWithParticipation(ctx, creatorId, gameId, name, password, false)
|
||||
return r.AddTeamWithParticipation(ctx, creatorId, gameId, name, password, "", false)
|
||||
}
|
||||
|
||||
func (r *TeamsRepo) AddTeamWithParticipation(ctx context.Context, creatorId, gameId int, name, password string, autoConfirm bool) (int, error) {
|
||||
func (r *TeamsRepo) AddTeamWithParticipation(ctx context.Context, creatorId, gameId int, name, password, captainEmail string, autoConfirm bool) (int, error) {
|
||||
id := 0
|
||||
status, source := "pending", ""
|
||||
if autoConfirm {
|
||||
@@ -126,8 +126,8 @@ func (r *TeamsRepo) AddTeamWithParticipation(ctx context.Context, creatorId, gam
|
||||
}
|
||||
err := r.pool.QueryRow(
|
||||
ctx,
|
||||
`INSERT INTO teams (name, creator_id, game_id, password, participation_status, participation_source, participation_answered_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, CASE WHEN $5 = 'confirmed' THEN NOW() ELSE NULL END)
|
||||
`INSERT INTO teams (name, creator_id, game_id, password, participation_status, participation_source, participation_answered_at, captain_email)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, CASE WHEN $5 = 'confirmed' THEN NOW() ELSE NULL END, $7)
|
||||
RETURNING id`,
|
||||
name,
|
||||
creatorId,
|
||||
@@ -135,6 +135,7 @@ func (r *TeamsRepo) AddTeamWithParticipation(ctx context.Context, creatorId, gam
|
||||
password,
|
||||
status,
|
||||
source,
|
||||
captainEmail,
|
||||
).Scan(&id)
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package game_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestManualTeamEmail(t *testing.T) {
|
||||
for _, email := range []string{"captain@example.org", "captain+team@example.org"} {
|
||||
if !validCaptainEmail(email) {
|
||||
t.Errorf("rejected %q", email)
|
||||
}
|
||||
}
|
||||
for _, email := range []string{"invalid", "Captain <captain@example.org>", "a@example.org\r\nBcc: other@example.org", strings.Repeat("a", 250) + "@example.org"} {
|
||||
service := &GameService{}
|
||||
if _, err := service.AddTeam(context.Background(), 1, 1, "Team", email); err == nil {
|
||||
t.Errorf("accepted invalid email %q", email)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,12 +51,16 @@ func (r *Registration) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validCaptainEmail(email string) bool {
|
||||
address, err := mail.ParseAddress(email)
|
||||
return err == nil && address.Address == email && len(email) <= 254
|
||||
}
|
||||
|
||||
func (r *Registration) Validate() error {
|
||||
r.Name = strings.TrimSpace(r.Name)
|
||||
r.Email = strings.TrimSpace(r.Email)
|
||||
address, err := mail.ParseAddress(r.Email)
|
||||
if r.Name == "" || !utf8.ValidString(r.Name) || strings.ContainsRune(r.Name, 0) || utf8.RuneCountInString(r.Name) > 255 ||
|
||||
err != nil || address.Address != r.Email || len(r.Email) > 254 || r.ParticipantsCount <= 0 {
|
||||
!validCaptainEmail(r.Email) || r.ParticipantsCount <= 0 {
|
||||
return ErrInvalidRegistration
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"evening_detective_server/internal/repos/team_answers_repo"
|
||||
"evening_detective_server/internal/repos/teams_repo"
|
||||
"evening_detective_server/internal/services/scenarios_service"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -217,7 +218,12 @@ func (s *GameService) AddTeam(
|
||||
creatorId int,
|
||||
gameId int,
|
||||
name string,
|
||||
captainEmail string,
|
||||
) (*Team, error) {
|
||||
captainEmail = strings.TrimSpace(captainEmail)
|
||||
if captainEmail != "" && !validCaptainEmail(captainEmail) {
|
||||
return nil, errors.New("Укажите корректную почту капитана")
|
||||
}
|
||||
password, err := s.passwordGenerator.Generate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -226,7 +232,7 @@ func (s *GameService) AddTeam(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id, err := s.teamsRepo.AddTeamWithParticipation(ctx, creatorId, gameId, name, password, !time.Now().Before(game.StartAt.Add(-24*time.Hour)))
|
||||
id, err := s.teamsRepo.AddTeamWithParticipation(ctx, creatorId, gameId, name, password, captainEmail, !time.Now().Before(game.StartAt.Add(-24*time.Hour)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user