From 163b9bdca62a1f43c1b6cc167fd245e70a34aeef Mon Sep 17 00:00:00 2001 From: wanyaland Date: Tue, 26 Jan 2021 03:31:51 +0300 Subject: [PATCH] [#2515] Feature/ Get Events Signing Entity Name - Factored in internal companyID based on signing entity name to fetch corresponding events Signed-off-by: wanyaland --- cla-backend-go/events/mockrepo.go | 4 ++-- cla-backend-go/events/repository.go | 32 +++++++++++++++++++--------- cla-backend-go/events/service.go | 12 +++++------ cla-backend-go/swagger/cla.v2.yaml | 4 ++-- cla-backend-go/v2/events/handlers.go | 17 ++++++++++----- 5 files changed, 44 insertions(+), 25 deletions(-) diff --git a/cla-backend-go/events/mockrepo.go b/cla-backend-go/events/mockrepo.go index f74ae1373..4f82ab047 100644 --- a/cla-backend-go/events/mockrepo.go +++ b/cla-backend-go/events/mockrepo.go @@ -19,11 +19,11 @@ func (repo *mockRepository) AddDataToEvent(eventID, foundationSFID, projectSFID, panic("implement me") } -func (repo *mockRepository) GetCompanyFoundationEvents(companySFID, foundationSFID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { +func (repo *mockRepository) GetCompanyFoundationEvents(companySFID, companyID, foundationSFID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { panic("implement me") } -func (repo *mockRepository) GetCompanyClaGroupEvents(companySFID, claGroupID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { +func (repo *mockRepository) GetCompanyClaGroupEvents(companySFID, companyID, claGroupID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { panic("implement me") } diff --git a/cla-backend-go/events/repository.go b/cla-backend-go/events/repository.go index e0204a371..01a825b73 100644 --- a/cla-backend-go/events/repository.go +++ b/cla-backend-go/events/repository.go @@ -59,8 +59,8 @@ type Repository interface { SearchEvents(params *eventOps.SearchEventsParams, pageSize int64) (*models.EventList, error) GetRecentEvents(pageSize int64) (*models.EventList, error) - GetCompanyFoundationEvents(companySFID, foundationSFID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) - GetCompanyClaGroupEvents(companySFID, claGroupID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) + GetCompanyFoundationEvents(companySFID, companyID, foundationSFID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) + GetCompanyClaGroupEvents(companySFID, companyID, claGroupID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) GetCompanyEvents(companyID, eventType string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) GetFoundationEvents(foundationSFID string, nextKey *string, paramPageSize *int64, all bool, searchTerm *string) (*models.EventList, error) GetClaGroupEvents(claGroupID string, nextKey *string, paramPageSize *int64, all bool, searchTerm *string) (*models.EventList, error) @@ -308,7 +308,7 @@ func (repo *repository) SearchEvents(params *eventOps.SearchEventsParams, pageSi } // queryEventsTable queries events table on index -func (repo *repository) queryEventsTable(indexName string, condition expression.KeyConditionBuilder, nextKey *string, pageSize *int64, all bool, searchTerm *string) (*models.EventList, error) { +func (repo *repository) queryEventsTable(indexName string, condition expression.KeyConditionBuilder, filter *expression.ConditionBuilder, nextKey *string, pageSize *int64, all bool, searchTerm *string) (*models.EventList, error) { f := logrus.Fields{ "functionName": "events.queryEventsTable", "indexName": indexName, @@ -320,10 +320,14 @@ func (repo *repository) queryEventsTable(indexName string, condition expression. log.WithFields(f).Debug("querying events table") builder := expression.NewBuilder() // .WithProjection(buildProjection()) + // The table we're interested in tableName := fmt.Sprintf("cla-%s-events", repo.stage) builder = builder.WithKeyCondition(condition) + if filter != nil { + builder = builder.WithFilter(*filter) + } // Use the nice builder to create the expression expr, err := builder.Build() if err != nil { @@ -464,17 +468,25 @@ func buildNextKey(indexName string, event *models.Event) (string, error) { } // GetCompanyFoundationEvents returns the list of events for foundation and company -func (repo *repository) GetCompanyFoundationEvents(companySFID, foundationSFID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { +func (repo *repository) GetCompanyFoundationEvents(companySFID, companyID, foundationSFID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { key := fmt.Sprintf("%s#%s", companySFID, foundationSFID) keyCondition := expression.Key("company_sfid_foundation_sfid").Equal(expression.Value(key)) - return repo.queryEventsTable(CompanySFIDFoundationSFIDEpochIndex, keyCondition, nextKey, paramPageSize, all, nil) + var filter expression.ConditionBuilder + if companyID != "" { + filter = expression.Name("company_id").Equal(expression.Value(companyID)) + } + return repo.queryEventsTable(CompanySFIDFoundationSFIDEpochIndex, keyCondition, &filter, nextKey, paramPageSize, all, nil) } // GetCompanyClaGroupEvents returns the list of events for cla group and the company -func (repo *repository) GetCompanyClaGroupEvents(companySFID, claGroupID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { +func (repo *repository) GetCompanyClaGroupEvents(companySFID, companyID, claGroupID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { key := fmt.Sprintf("%s#%s", companySFID, claGroupID) keyCondition := expression.Key("company_sfid_project_id").Equal(expression.Value(key)) - return repo.queryEventsTable(CompanySFIDProjectIDEpochIndex, keyCondition, nextKey, paramPageSize, all, nil) + var filter expression.ConditionBuilder + if companyID != "" { + filter = expression.Name("company_id").Equal(expression.Value(companyID)) + } + return repo.queryEventsTable(CompanySFIDProjectIDEpochIndex, keyCondition, &filter, nextKey, paramPageSize, all, nil) } // GetCompanyEvents returns the list of events for given company id and event types @@ -482,19 +494,19 @@ func (repo *repository) GetCompanyEvents(companyID, eventType string, nextKey *s keyCondition := expression.Key("company_id").Equal(expression.Value(companyID)).And( expression.Key("event_type").Equal(expression.Value(eventType))) - return repo.queryEventsTable(CompanyIDEventTypeIndex, keyCondition, nextKey, paramPageSize, all, nil) + return repo.queryEventsTable(CompanyIDEventTypeIndex, keyCondition, nil, nextKey, paramPageSize, all, nil) } // GetFoundationEvents returns the list of foundation events func (repo *repository) GetFoundationEvents(foundationSFID string, nextKey *string, paramPageSize *int64, all bool, searchTerm *string) (*models.EventList, error) { keyCondition := expression.Key("event_foundation_sfid").Equal(expression.Value(foundationSFID)) - return repo.queryEventsTable(EventFoundationSFIDEpochIndex, keyCondition, nextKey, paramPageSize, all, searchTerm) + return repo.queryEventsTable(EventFoundationSFIDEpochIndex, keyCondition, nil, nextKey, paramPageSize, all, searchTerm) } // GetClaGroupEvents returns the list of cla-group events func (repo *repository) GetClaGroupEvents(claGroupID string, nextKey *string, paramPageSize *int64, all bool, searchTerm *string) (*models.EventList, error) { keyCondition := expression.Key("event_project_id").Equal(expression.Value(claGroupID)) - return repo.queryEventsTable(EventProjectIDEpochIndex, keyCondition, nextKey, paramPageSize, all, searchTerm) + return repo.queryEventsTable(EventProjectIDEpochIndex, keyCondition, nil, nextKey, paramPageSize, all, searchTerm) } // toString encodes the map as a string diff --git a/cla-backend-go/events/service.go b/cla-backend-go/events/service.go index 622fdf056..34d0439d7 100644 --- a/cla-backend-go/events/service.go +++ b/cla-backend-go/events/service.go @@ -30,8 +30,8 @@ type Service interface { GetFoundationEvents(foundationSFID string, nextKey *string, paramPageSize *int64, all bool, searchTerm *string) (*models.EventList, error) GetClaGroupEvents(claGroupID string, nextKey *string, paramPageSize *int64, all bool, searchTerm *string) (*models.EventList, error) - GetCompanyFoundationEvents(companySFID, foundationSFID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) - GetCompanyClaGroupEvents(companySFID, claGroupID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) + GetCompanyFoundationEvents(companySFID, companyID, foundationSFID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) + GetCompanyClaGroupEvents(companySFID, companyID, claGroupID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) GetCompanyEvents(companyID, eventType string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) } @@ -91,13 +91,13 @@ func (s *service) GetClaGroupEvents(projectSFDC string, nextKey *string, paramPa } // GetCompanyFoundationEvents returns list of events for company and foundation -func (s *service) GetCompanyFoundationEvents(companySFID, foundationSFID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { - return s.repo.GetCompanyFoundationEvents(companySFID, foundationSFID, nextKey, paramPageSize, all) +func (s *service) GetCompanyFoundationEvents(companySFID, companyID, foundationSFID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { + return s.repo.GetCompanyFoundationEvents(companySFID, companyID, foundationSFID, nextKey, paramPageSize, all) } // GetCompanyClaGroupEvents returns list of events for company and cla group -func (s *service) GetCompanyClaGroupEvents(companySFID, claGroupID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { - return s.repo.GetCompanyClaGroupEvents(companySFID, claGroupID, nextKey, paramPageSize, all) +func (s *service) GetCompanyClaGroupEvents(companySFID, companyID, claGroupID string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { + return s.repo.GetCompanyClaGroupEvents(companySFID, companyID, claGroupID, nextKey, paramPageSize, all) } func (s *service) GetCompanyEvents(companyID, eventType string, nextKey *string, paramPageSize *int64, all bool) (*models.EventList, error) { diff --git a/cla-backend-go/swagger/cla.v2.yaml b/cla-backend-go/swagger/cla.v2.yaml index 37b390f4f..51f1cf4d9 100644 --- a/cla-backend-go/swagger/cla.v2.yaml +++ b/cla-backend-go/swagger/cla.v2.yaml @@ -1068,7 +1068,7 @@ paths: tags: - events - /company/{companySFID}/project/{projectSFID}/events: + /company/{companyID}/project/{projectSFID}/events: get: summary: Get recent events of company and project description: Returns list of events of company and project @@ -1080,7 +1080,7 @@ paths: - $ref: "#/parameters/x-email" - $ref: '#/parameters/pageSize' - $ref: '#/parameters/path-projectSFID' - - $ref: '#/parameters/path-companySFID' + - $ref: '#/parameters/path-companyID' - $ref: '#/parameters/nextKey' - $ref: '#/parameters/returnAllEvents' produces: diff --git a/cla-backend-go/v2/events/handlers.go b/cla-backend-go/v2/events/handlers.go index c210a045c..365bad29f 100644 --- a/cla-backend-go/v2/events/handlers.go +++ b/cla-backend-go/v2/events/handlers.go @@ -279,13 +279,20 @@ func Configure(api *operations.EasyclaAPI, service v1Events.Service, v1CompanyRe "authUserName": authUser.UserName, "authUserEmail": authUser.Email, "projectSFID": params.ProjectSFID, - "companySFID": params.CompanySFID, + "companyID": params.CompanyID, } - if !utils.IsUserAuthorizedForOrganization(authUser, params.CompanySFID, utils.ALLOW_ADMIN_SCOPE) { + + v1Company, compErr := v1CompanyRepo.GetCompany(ctx, params.CompanyID) + if compErr != nil { + log.WithFields(f).Warnf("unable to fetch company by ID:%s ", params.CompanyID) + return events.NewGetCompanyProjectEventsBadRequest().WithPayload(errorResponse(reqID, compErr)) + } + + if !utils.IsUserAuthorizedForOrganization(authUser, v1Company.CompanyExternalID, utils.ALLOW_ADMIN_SCOPE) { return events.NewGetCompanyProjectEventsForbidden().WithPayload(&models.ErrorResponse{ Code: "403", Message: fmt.Sprintf("EasyCLA - 403 Forbidden - user %s does not have access to GetCompanyProject Events with Organization scope of %s", - authUser.UserName, params.CompanySFID), + authUser.UserName, v1Company.CompanyExternalID), XRequestID: reqID, }) } @@ -300,7 +307,7 @@ func Configure(api *operations.EasyclaAPI, service v1Events.Service, v1CompanyRe var result *v1Models.EventList if projectDetails.ProjectType == utils.ProjectTypeProjectGroup { - result, err = service.GetCompanyFoundationEvents(params.CompanySFID, params.ProjectSFID, params.NextKey, params.PageSize, aws.BoolValue(params.ReturnAllEvents)) + result, err = service.GetCompanyFoundationEvents(v1Company.CompanyExternalID, params.CompanyID, params.ProjectSFID, params.NextKey, params.PageSize, aws.BoolValue(params.ReturnAllEvents)) } else { pm, perr := projectsClaGroupsRepo.GetClaGroupIDForProject(params.ProjectSFID) if perr != nil { @@ -314,7 +321,7 @@ func Configure(api *operations.EasyclaAPI, service v1Events.Service, v1CompanyRe log.WithFields(f).WithError(perr).Warnf("problem determining CLA Group for project SFID: %s", params.ProjectSFID) return events.NewGetCompanyProjectEventsInternalServerError().WithPayload(errorResponse(reqID, perr)) } - result, err = service.GetCompanyClaGroupEvents(params.CompanySFID, pm.ClaGroupID, params.NextKey, params.PageSize, aws.BoolValue(params.ReturnAllEvents)) + result, err = service.GetCompanyClaGroupEvents(v1Company.CompanyExternalID, params.CompanyID, pm.ClaGroupID, params.NextKey, params.PageSize, aws.BoolValue(params.ReturnAllEvents)) } if err != nil { log.WithFields(f).WithError(err).Warn("problem loading events")