Skip to content

Commit

Permalink
Merge branch 'main' into f/admin_invitations
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Oct 29, 2024
2 parents 4bf295f + a8b166e commit a2bc2bd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ This endpoint only returns the addresses of the organizations where the current
| `400` | `40002` | `email malformed` |
| `400` | `40003` | `password too short` |
| `400` | `40004` | `malformed JSON body` |
| `409` | `40901` | `duplicate conflict` |
| `500` | `50002` | `internal server error` |

### ✅ Verify user
Expand Down
6 changes: 3 additions & 3 deletions api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ func (a *API) registerHandler(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
if err == db.ErrAlreadyExists {
ErrMalformedBody.WithErr(err).Write(w)
ErrDuplicateConflict.With("user already exists").Write(w)
return
}
log.Warnw("could not create user", "error", err)
ErrGenericInternalServerError.Write(w)
ErrGenericInternalServerError.WithErr(err).Write(w)
return
}
// compose the new user and send the verification code
Expand All @@ -113,7 +113,7 @@ func (a *API) registerHandler(w http.ResponseWriter, r *http.Request) {
}
if err := a.sendUserCode(r.Context(), newUser, db.CodeTypeAccountVerification); err != nil {
log.Warnw("could not send verification code", "error", err)
ErrGenericInternalServerError.Write(w)
ErrGenericInternalServerError.WithErr(err).Write(w)
return
}
// send the token back to the user
Expand Down
4 changes: 4 additions & 0 deletions db/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"context"
"strings"
"time"

"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -160,6 +161,9 @@ func (ms *MongoStorage) SetUser(user *User) (uint64, error) {
// if the user doesn't exist, create it setting the ID first
user.ID = nextID
if _, err := ms.users.InsertOne(ctx, user); err != nil {
if strings.Contains(err.Error(), "duplicate key error") {
return 0, ErrAlreadyExists
}
return 0, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion db/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var usersCollectionValidator = bson.M{
"email": bson.M{
"bsonType": "string",
"description": "must be an email and is required",
"pattern": `^[\w.\-]+@([\w\-]+\.)+[\w]{2,}$`,
"pattern": `^[\w.\+\.\-]+@([\w\-]+\.)+[\w]{2,}$`,
},
"password": bson.M{
"bsonType": "string",
Expand Down

0 comments on commit a2bc2bd

Please sign in to comment.