Skip to content

Commit

Permalink
working endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Aug 21, 2024
1 parent dc0a87b commit 4f4e937
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ func (a *API) initRouter() http.Handler {
// get organization information
log.Infow("new route", "method", "GET", "path", organizationEndpoint)
r.Get(organizationEndpoint, a.organizationInfoHandler)
// get organization members
log.Infow("new route", "method", "GET", "path", organizationMembersEndpoint)
r.Get(organizationMembersEndpoint, a.organizationMembersHandler)
})
a.router = r
return r
Expand Down
40 changes: 40 additions & 0 deletions api/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,46 @@ func (a *API) organizationInfoHandler(w http.ResponseWriter, r *http.Request) {
httpWriteJSON(w, organizationFromDB(org, parent))
}

// organizationMembersHandler handles the request to get the members of an
// organization. It returns the list of members with their role in the
// organization with the address provided in the request.
func (a *API) organizationMembersHandler(w http.ResponseWriter, r *http.Request) {
// get the organization info from the request context
org, _, ok := a.organizationFromRequest(r)
if !ok {
ErrUnauthorized.Write(w)
return
}
// send the organization back to the user
members, err := a.db.OrganizationsMembers(org.Address)
if err != nil {
ErrGenericInternalServerError.Withf("could not get organization members: %v", err).Write(w)
return
}
orgMembers := []OrganizationMember{}
for _, member := range members {
var role string
for _, userOrg := range member.Organizations {
if userOrg.Address == org.Address {
role = string(userOrg.Role)
break
}
}
if role == "" {
continue
}
orgMembers = append(orgMembers, OrganizationMember{
Info: &UserInfo{
Email: member.Email,
FirstName: member.FirstName,
LastName: member.LastName,
},
Role: role,
})
}
httpWriteJSON(w, orgMembers)
}

// updateOrganizationHandler handles the request to update the information of an
// organization. Only the admin of the organization can update the information.
// Only certain fields can be updated, and they will be updated only if they are
Expand Down
2 changes: 2 additions & 0 deletions api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ const (
organizationsEndpoint = "/organizations"
// GET /organizations/{address} to get the organization information
organizationEndpoint = "/organizations/{address}"
// GET /organizations/{address}/members to get the organization members
organizationMembersEndpoint = "/organizations/{address}/members"
)
7 changes: 7 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ type OrganizationInfo struct {
Parent *OrganizationInfo `json:"parent"`
}

// OrganizationMember is the struct that represents a members of organizations
// with his role in the API.
type OrganizationMember struct {
Info *UserInfo `json:"info"`
Role string `json:"role"`
}

// OrganizationAddresses is the struct that represents a list of addresses of
// organizations in the API.
type OrganizationAddresses struct {
Expand Down
28 changes: 28 additions & 0 deletions db/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,31 @@ func (ms *MongoStorage) ReplaceCreatorEmail(oldEmail, newEmail string) error {
}
return nil
}

// OrganizationsMembers method returns the users that are members of the
// organization with the given address. If an error occurs, it returns the
// error.
func (ms *MongoStorage) OrganizationsMembers(address string) ([]User, error) {
ms.keysLock.RLock()
defer ms.keysLock.RUnlock()
// create a context with a timeout
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// find the organization in the database
filter := bson.M{
"organizations": bson.M{
"$elemMatch": bson.M{
"_id": address,
},
},
}
users := []User{}
cursor, err := ms.users.Find(ctx, filter)
if err != nil {
return nil, err
}
if err := cursor.All(ctx, &users); err != nil {
return nil, err
}
return users, nil
}
29 changes: 29 additions & 0 deletions db/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,32 @@ func TestReplaceCreatorEmail(t *testing.T) {
c.Assert(org.Name, qt.Equals, name)
c.Assert(org.Creator, qt.Equals, newCreator)
}

func TestOrganizationsMembers(t *testing.T) {
defer func() {
if err := db.Reset(); err != nil {
t.Error(err)
}
}()
c := qt.New(t)
// create a new organization with a creator
address := "orgToReplaceCreator"
name := "Organization to replace creator"
c.Assert(db.SetUser(&User{
Email: testUserEmail,
Password: testUserPass,
}), qt.IsNil)
c.Assert(db.SetOrganization(&Organization{
Address: address,
Name: name,
Creator: testUserEmail,
}), qt.IsNil)
_, _, err := db.Organization(address, false)
c.Assert(err, qt.IsNil)
// get the organization members
members, err := db.OrganizationsMembers(address)
c.Assert(err, qt.IsNil)
c.Assert(members, qt.HasLen, 1)
singleMember := members[0]
c.Assert(singleMember.Email, qt.Equals, testUserEmail)
}

0 comments on commit 4f4e937

Please sign in to comment.