Skip to content

Commit 962175b

Browse files
committed
Added new API GetProjectOwners.
Signed-off-by: aryan <aryan1bhokare@gmail.com>
1 parent 78b82ab commit 962175b

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

chaoscenter/authentication/api/handlers/rest/project_handler.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ func GetActiveProjectMembers(service services.ApplicationService) gin.HandlerFun
195195
}
196196
}
197197

198+
// GetActiveProjectOwners godoc
199+
//
200+
// @Summary Get active project Owners.
201+
// @Description Return list of active project owners.
202+
// @Tags ProjectRouter
203+
// @Param state path string true "State"
204+
// @Accept json
205+
// @Produce json
206+
// @Failure 500 {object} response.ErrServerError
207+
// @Success 200 {object} response.Response{}
208+
// @Router /get_project_owners/:project_id/:state [get]
209+
func GetActiveProjectOwners(service services.ApplicationService) gin.HandlerFunc {
210+
return func(c *gin.Context) {
211+
projectID := c.Param("project_id")
212+
// state := c.Param("state")
213+
owners, err := service.GetProjectOwners(projectID)
214+
if err != nil {
215+
c.JSON(utils.ErrorStatusCodes[utils.ErrServerError], presenter.CreateErrorResponse(utils.ErrServerError))
216+
return
217+
}
218+
c.JSON(http.StatusOK, gin.H{"data": owners})
219+
}
220+
}
221+
198222
// getInvitation returns the Invitation status
199223
func getInvitation(service services.ApplicationService, member entities.MemberInput) (entities.Invitation, error) {
200224
project, err := service.GetProjectByProjectID(member.ProjectID)

chaoscenter/authentication/api/routes/project_router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func ProjectRouter(router *gin.Engine, service services.ApplicationService) {
1313
router.Use(middleware.JwtMiddleware(service))
1414
router.GET("/get_project/:project_id", rest.GetProject(service))
1515
router.GET("/get_project_members/:project_id/:state", rest.GetActiveProjectMembers(service))
16+
router.GET("/get_project_owners/:project_id", rest.GetActiveProjectOwners(service))
1617
router.GET("/get_user_with_project/:username", rest.GetUserWithProject(service))
1718
router.GET("/get_owner_projects", rest.GetOwnerProjects(service))
1819
router.GET("/get_project_role/:project_id", rest.GetProjectRole(service))

chaoscenter/authentication/pkg/entities/project.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ type Project struct {
1010
}
1111

1212
type Owner struct {
13-
UserID string `bson:"user_id" json:"userID"`
14-
Username string `bson:"username" json:"username"`
13+
UserID string `bson:"user_id" json:"userID"`
14+
Username string `bson:"username" json:"username"`
1515
Invitation Invitation `bson:"invitation" json:"invitation"`
1616
JoinedAt int64 `bson:"joined_at" json:"joinedAt"`
1717
DeactivatedAt *int64 `bson:"deactivated_at,omitempty" json:"deactivatedAt,omitempty"`

chaoscenter/authentication/pkg/project/repository.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Repository interface {
3030
GetOwnerProjects(ctx context.Context, userID string) ([]*entities.Project, error)
3131
GetProjectRole(projectID string, userID string) (*entities.MemberRole, error)
3232
GetProjectMembers(projectID string, state string) ([]*entities.Member, error)
33+
GetProjectOwners(projectID string) ([]*entities.Member, error)
3334
ListInvitations(userID string, invitationState entities.Invitation) ([]*entities.Project, error)
3435
}
3536

@@ -381,6 +382,28 @@ func (r repository) GetOwnerProjects(ctx context.Context, userID string) ([]*ent
381382
return projects, nil
382383
}
383384

385+
// GetProjectOwners takes projectID and returns the owners
386+
func (r repository) GetProjectOwners(projectID string) ([]*entities.Member, error) {
387+
filter := bson.D{{"_id", projectID}}
388+
389+
var project struct {
390+
Members []*entities.Member `bson:"members"`
391+
}
392+
err := r.Collection.FindOne(context.TODO(), filter).Decode(&project)
393+
if err != nil {
394+
return err, nil
395+
}
396+
397+
// Filter the members to include only the owners
398+
var owners []*entities.Member
399+
for _, member := range project.Members {
400+
if member.Role == entities.RoleOwner {
401+
owners = append(owners, member)
402+
}
403+
}
404+
return owners, nil
405+
}
406+
384407
// GetProjectRole returns the role of a user in the project
385408
func (r repository) GetProjectRole(projectID string, userID string) (*entities.MemberRole, error) {
386409
filter := bson.D{

chaoscenter/authentication/pkg/services/project_service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type projectService interface {
2525
GetOwnerProjectIDs(ctx context.Context, userID string) ([]*entities.Project, error)
2626
GetProjectRole(projectID string, userID string) (*entities.MemberRole, error)
2727
GetProjectMembers(projectID string, state string) ([]*entities.Member, error)
28+
GetProjectOwners(projectID string) ([]*entities.Member, error)
2829
ListInvitations(userID string, invitationState entities.Invitation) ([]*entities.Project, error)
2930
}
3031

@@ -82,6 +83,10 @@ func (a applicationService) GetProjectMembers(projectID string, state string) ([
8283
return a.projectRepository.GetProjectMembers(projectID, state)
8384
}
8485

86+
func (a applicationService) GetProjectOwners(projectID string) ([]*entities.Member, error) {
87+
return a.projectRepository.GetProjectOwners(projectID)
88+
}
89+
8590
func (a applicationService) ListInvitations(userID string, invitationState entities.Invitation) ([]*entities.Project, error) {
8691
return a.projectRepository.ListInvitations(userID, invitationState)
8792
}

0 commit comments

Comments
 (0)