Skip to content

Commit

Permalink
handle organization address case insensitive, handling duplicate key …
Browse files Browse the repository at this point in the history
…error on members invitation endpoint and api docs typos
  • Loading branch information
lucasmenendez committed Oct 31, 2024
1 parent fcd1052 commit 7243689
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions api/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ Only the following parameters can be changed. Every parameter is optional.

* **Path** `/organizations/{address}/members`
* **Method** `POST`
* **Response**
* **Request**
```json
{
"role": "admin",
Expand All @@ -602,7 +602,7 @@ Only the following parameters can be changed. Every parameter is optional.

* **Path** `/organizations/{address}/members/accept`
* **Method** `POST`
* **Response**
* **Request**
```json
{
"code": "a3f3b5",
Expand Down
4 changes: 4 additions & 0 deletions api/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ func (a *API) inviteOrganizationMemberHandler(w http.ResponseWriter, r *http.Req
CurrentUserID: user.ID,
Expiration: time.Now().Add(InvitationExpiration),
}); err != nil {
if err == db.ErrAlreadyExists {
ErrDuplicateConflict.With("user is already invited to the organization").Write(w)
return
}
ErrGenericInternalServerError.Withf("could not create invitation: %v", err).Write(w)
return
}
Expand Down
11 changes: 11 additions & 0 deletions db/organization_invites.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ func (ms *MongoStorage) CreateInvitation(invite *OrganizationInvite) error {
}
// insert the invitation in the database
_, err = ms.organizationInvites.InsertOne(ctx, invite)
// check if the user is already invited to the organization, the error is
// about the unique index
if merr, ok := err.(mongo.WriteException); ok {
for _, we := range merr.WriteErrors {
// duplicate key error has the code 11000:
// https://www.mongodb.com/docs/manual/reference/error-codes
if we.Code == 11000 {
return ErrAlreadyExists
}
}
}
return err
}

Expand Down
5 changes: 3 additions & 2 deletions db/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

func (ms *MongoStorage) organization(ctx context.Context, address string) (*Organization, error) {
// find the organization in the database
result := ms.organizations.FindOne(ctx, bson.M{"_id": address})
// find the organization in the database by its address (case insensitive)
filter := bson.M{"_id": bson.M{"$regex": address, "$options": "i"}}
result := ms.organizations.FindOne(ctx, filter)
org := &Organization{}
if err := result.Decode(org); err != nil {
// if the organization doesn't exist return a specific error
Expand Down

0 comments on commit 7243689

Please sign in to comment.