Skip to content

Commit

Permalink
feat: manage case where account is created but sending confirmation m…
Browse files Browse the repository at this point in the history
…ail fails (#9)

* chore: Improve lint section of Makefile

* chore: remove unused function

* feat: manage case where account is created but sending confirmation mail fails
  • Loading branch information
Angak0k authored Feb 5, 2024
1 parent 3554f39 commit a294200
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ build: test
go build

lint:
docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:v1.52 golangci-lint run -v
@echo "Running staticcheck..."
@staticcheck ./...
@echo "Running golangci-lint..."
@golangci-lint run --timeout=5m --out-format=colored-line-number
26 changes: 15 additions & 11 deletions pkg/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,50 +48,54 @@ func Register(c *gin.Context) {
user.Created_at = time.Now().Truncate(time.Second)
user.Updated_at = time.Now().Truncate(time.Second)

err := saveUser(user)
err, emailSended := registerUser(user)

if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, gin.H{"message": "registration success"})
if !emailSended {
c.JSON(http.StatusAccepted, gin.H{"message": "registration succeed but failed to send confirmation email"})
return
}

c.JSON(http.StatusOK, gin.H{"message": "registration succeed, please check your email to confirm your account"})

}

func saveUser(u dataset.User) error {
func registerUser(u dataset.User) (error, bool) {
var id int

confirmationCode, err := helper.GenerateRandomCode(30)
if err != nil {
return fmt.Errorf("failed to generate confirmation code: %w", err)
return fmt.Errorf("failed to generate confirmation code: %w", err), false
}

err = database.Db().QueryRow("INSERT INTO account (username, email, firstname, lastname, role, status, confirmation_code, created_at, updated_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8, $9) RETURNING id;", u.Username, u.Email, u.Firstname, u.Lastname, u.Role, u.Status, confirmationCode, u.Created_at, u.Updated_at).Scan(&id)
if err != nil {
return fmt.Errorf("failed to insert user: %w", err)
return fmt.Errorf("failed to insert user: %w", err), false
}

u.ID = uint(id)

hashedPassword, err := security.HashPassword(u.Password)
if err != nil {
return fmt.Errorf("failed to hash password: %w", err)
return fmt.Errorf("failed to hash password: %w", err), false
}

err = database.Db().QueryRow("INSERT INTO password (user_id, password, updated_at) VALUES ($1,$2,$3) RETURNING id;", id, hashedPassword, u.Updated_at).Scan(&id)
if err != nil {
return fmt.Errorf("failed to insert password: %w", err)
return fmt.Errorf("failed to insert password: %w", err), false
}

err = sendConfirmationEmail(u, confirmationCode)
if err != nil {
// deactivate email sending error throwing for now
// return fmt.Errorf("failed to send confirmation email: %w", err)
return nil
// we haven't succed to send the email but the user is created
return nil, false
}

return nil
return nil, true
}

// Send confirmation email
Expand Down
4 changes: 2 additions & 2 deletions pkg/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func TestRegister(t *testing.T) {
newAccount := dataset.RegisterInput{
Username: fmt.Sprintf("user-%s", random.UniqueId()),
Password: "password",
Email: "jane.doe@pmp.com",
Email: "jane.doe@exemple.com",
Firstname: "Jane",
Lastname: "Doe",
}
Expand All @@ -404,7 +404,7 @@ func TestRegister(t *testing.T) {
router.ServeHTTP(w, req)

// Check the HTTP status code
if w.Code != http.StatusOK {
if w.Code != http.StatusOK && w.Code != http.StatusAccepted {
t.Errorf("Expected status code %d but got %d", http.StatusOK, w.Code)
}

Expand Down
10 changes: 0 additions & 10 deletions pkg/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strconv"

"github.com/Angak0k/pimpmypack/pkg/dataset"
"github.com/joho/godotenv"
)

func StringToUint(s string) (uint, error) {
Expand All @@ -30,15 +29,6 @@ func ConvertWeightUnit(unit string) string {
return "METRIC"
}

func EnvInit() error {
// Load environment variables from .env file
err := godotenv.Load()
if err != nil {
return err
}
return nil
}

func FinUserIDByUsername(users []dataset.User, username string) uint {
// Find a user ID by username
for _, user := range users {
Expand Down

0 comments on commit a294200

Please sign in to comment.