Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pprishchepa committed Mar 4, 2024
1 parent 3547b5d commit 933ca53
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
16 changes: 9 additions & 7 deletions internal/controller/http/v1/invites.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ func (r *InvitesRoutes) acceptInvite(c *gin.Context) {
InvitedVia: m.Code,
})
if err != nil {
r.logger.Debug().Err(err).Str("email", m.Email).Str("code", m.Code).
Msg("failed to accept invite")
switch {
case errors.Is(err, entity.ErrAlreadyExists):
if errors.Is(err, entity.ErrAlreadyExists) {
r.logger.Debug().Str("email", m.Email).Str("code", m.Code).Msg("user with the same email already exists")
c.Status(http.StatusConflict)
case errors.Is(err, entity.ErrNotAvailable):
return
}
if errors.Is(err, entity.ErrNotAvailable) {
r.logger.Debug().Str("email", m.Email).Str("code", m.Code).Msg("no invitation available")
c.Status(http.StatusGone)
default:
c.Status(http.StatusInternalServerError)
return
}
r.logger.Err(err).Str("email", m.Email).Str("code", m.Code).Msg("failed to accept invite")
c.Status(http.StatusInternalServerError)
return
}

Expand Down
11 changes: 4 additions & 7 deletions internal/usecase/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package usecase

import (
"context"
"errors"
"fmt"

"github.com/pprishchepa/go-invitecoder-example/internal/config"
Expand Down Expand Up @@ -43,13 +42,11 @@ func (s *InviteService) AcceptInvite(ctx context.Context, user entity.InvitedUse
return fmt.Errorf("inc by code: %w", err)
}

if err := s.users.SaveUser(ctx, user); err != nil {
if errors.Is(err, entity.ErrAlreadyExists) {
if err := s.stats.DecByCode(ctx, user.InvitedVia); err != nil {
s.logger.Err(err).Msg("could not rollback stats increment")
}
if saveErr := s.users.SaveUser(ctx, user); saveErr != nil {
if decErr := s.stats.DecByCode(ctx, user.InvitedVia); decErr != nil {
s.logger.Err(decErr).Msg("could not rollback stats increment")
}
return fmt.Errorf("save user: %w", err)
return fmt.Errorf("save user: %w", saveErr)
}

return nil
Expand Down

0 comments on commit 933ca53

Please sign in to comment.