From 3bb17054067c35a6444472a68ecd3de019ec7cd2 Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Tue, 28 May 2024 22:53:34 +0800 Subject: [PATCH 1/3] refactor: add user and org id to pipeline model --- backend/src/database/models/pipeline.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/backend/src/database/models/pipeline.go b/backend/src/database/models/pipeline.go index ec48eb7f..b8d75199 100644 --- a/backend/src/database/models/pipeline.go +++ b/backend/src/database/models/pipeline.go @@ -71,14 +71,16 @@ type PipelineStepModel struct { } type PipelineModel struct { - Id primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` // unique id for the pipeline - PipelineName string `bson:"pipeline_name" json:"pipeline_name"` - Version int `bson:"version" json:"version"` - PrevVersionId primitive.ObjectID `bson:"prev_version_id" json:"prev_version_id"` - FirstStepName string `bson:"first_step_name" json:"first_step_name"` - Steps []PipelineStepModel `bson:"steps" json:"steps"` - CreatedOn time.Time `bson:"created_on" json:"created_on"` - Form Form `bson:"form" json:"form"` + Id primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` // unique id for the pipeline + UserId string `bson:"user_id" json:"user_id"` + OrganizationId int `bson:"org_id" json:"org_id"` + PipelineName string `bson:"pipeline_name" json:"pipeline_name"` + Version int `bson:"version" json:"version"` + PrevVersionId primitive.ObjectID `bson:"prev_version_id" json:"prev_version_id"` + FirstStepName string `bson:"first_step_name" json:"first_step_name"` + Steps []PipelineStepModel `bson:"steps" json:"steps"` + CreatedOn time.Time `bson:"created_on" json:"created_on"` + Form Form `bson:"form" json:"form"` } func (p *PipelineModel) GetPipelineStep(name string) *PipelineStepModel { From f97a57e47e8b35a5bd4ed2ee9aa5a1f1176ca27b Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Tue, 28 May 2024 23:45:08 +0800 Subject: [PATCH 2/3] refactor: updated handlers and created new function to get pipelines by org_id --- backend/src/database/pipeline.go | 14 ++++++++++++++ backend/src/server/handlers.go | 17 ++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/backend/src/database/pipeline.go b/backend/src/database/pipeline.go index 2e90c6bb..db8045a6 100644 --- a/backend/src/database/pipeline.go +++ b/backend/src/database/pipeline.go @@ -54,3 +54,17 @@ func (p *Pipeline) GetAll() ([]*models.PipelineModel, error) { } return pipelines, nil } + +func (p *Pipeline) GetAllByOrgId(orgId int) ([]*models.PipelineModel, error) { + res, err := p.c.Database(DatabaseName).Collection("pipelines").Find(context.Background(), bson.M{"org_id": orgId}) + if err != nil { + return nil, err + } + pipelines := []*models.PipelineModel{} + for res.Next(context.Background()) { + pipeline := &models.PipelineModel{} + res.Decode(pipeline) + pipelines = append(pipelines, pipeline) + } + return pipelines, nil +} diff --git a/backend/src/server/handlers.go b/backend/src/server/handlers.go index 573d6a09..5a655f5e 100644 --- a/backend/src/server/handlers.go +++ b/backend/src/server/handlers.go @@ -64,10 +64,9 @@ func (s *ServerHandler) registerRoutes(r *mux.Router) { r.Handle("/api/service_request/{requestId}/steps", isAuthenticated(getOrgIdUsingSrId(s.mongoClient, isOrgMember(s.psqlClient, handleGetServiceRequestStepDetails(s.logger, s.mongoClient, s.psqlClient), s.logger), s.logger), s.logger)).Methods("GET") // Pipeline - // TODO: @joshtyf need to integrate orgId in some way into these routes or the pipeline model, esp for the post method. - r.Handle("/api/pipeline", isAuthenticated(handleGetAllPipelines(s.logger, s.mongoClient), s.logger)).Methods("GET") - r.Handle("/api/pipeline/{pipelineId}", isAuthenticated(handleGetPipeline(s.logger, s.mongoClient), s.logger)).Methods("GET") - r.Handle("/api/pipeline", isAuthenticated(validateCreatePipelineRequest(handleCreatePipeline(s.logger, s.mongoClient), s.logger), s.logger)).Methods("POST").Headers("Content-Type", "application/json") + r.Handle("/api/pipeline", isAuthenticated(getOrgIdFromQuery(isOrgMember(s.psqlClient, handleGetAllPipelines(s.logger, s.mongoClient), s.logger), s.logger), s.logger)).Methods("GET") + r.Handle("/api/pipeline/{pipelineId}", isAuthenticated(getOrgIdFromQuery(isOrgMember(s.psqlClient, handleGetPipeline(s.logger, s.mongoClient), s.logger), s.logger), s.logger)).Methods("GET") + r.Handle("/api/pipeline", isAuthenticated(getOrgIdFromRequestBody(isOrgAdmin(s.psqlClient, validateCreatePipelineRequest(handleCreatePipeline(s.logger, s.mongoClient), s.logger), s.logger), s.logger), s.logger)).Methods("POST").Headers("Content-Type", "application/json") // User r.Handle("/api/user", isAuthenticated(handleGetAllUsers(s.logger, s.psqlClient), s.logger)).Methods("GET") @@ -539,8 +538,10 @@ func handleCreatePipeline(logger logger.ServerLogger, client *mongo.Client) http } } + userId := r.Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims).RegisteredClaims.Subject pipeline.CreatedOn = time.Now() pipeline.Version = 1 + pipeline.UserId = userId res, err := database.NewPipeline(client).Create(&pipeline) if err != nil { logger.Error(fmt.Sprintf("error encountered while handling API request: %s", err)) @@ -572,7 +573,13 @@ func handleGetPipeline(logger logger.ServerLogger, client *mongo.Client) http.Ha func handleGetAllPipelines(logger logger.ServerLogger, client *mongo.Client) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - pipelines, err := database.NewPipeline(client).GetAll() + orgId, err := extractQueryParam[int](r.URL.Query(), "org_id", false, -1, integerConverter) + if err != nil { + logger.Error(fmt.Sprintf("unable to extract org_id from query params: %s", err)) + encode(w, r, http.StatusBadRequest, newHandlerError(ErrInvalidOrganizationId, http.StatusBadRequest)) + return + } + pipelines, err := database.NewPipeline(client).GetAllByOrgId(orgId) if err != nil { logger.Error(fmt.Sprintf("error encountered while handling API request: %s", err)) encode(w, r, http.StatusInternalServerError, newHandlerError(ErrInternalServerError, http.StatusInternalServerError)) From aed93d87eeba92776cbe839915dd6347387fde7f Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Wed, 29 May 2024 00:06:54 +0800 Subject: [PATCH 3/3] refactor: updated mongo seed to reflect changes to pipelines and service requests --- backend/database/mongo_seed/main.go | 70 ++++++++++++++++------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/backend/database/mongo_seed/main.go b/backend/database/mongo_seed/main.go index 0474c5e8..618cd5ff 100644 --- a/backend/database/mongo_seed/main.go +++ b/backend/database/mongo_seed/main.go @@ -26,10 +26,12 @@ func main() { panic(err) } pipeline1 := models.PipelineModel{ - PipelineName: pipelineName, - Id: pipelineId, - Version: 1, - FirstStepName: "step1", + UserId: "auth0|66010ad5095367b237799680", // zheng.zhiqiang49@gmail.com + OrganizationId: 1, + PipelineName: pipelineName, + Id: pipelineId, + Version: 1, + FirstStepName: "step1", Steps: []models.PipelineStepModel{ { StepName: "step1", @@ -88,10 +90,12 @@ func main() { panic(err) } pipeline2 := models.PipelineModel{ - PipelineName: pipelineName, - Id: pipelineId, - Version: 1, - FirstStepName: "step1", + UserId: "auth0|66010ad5095367b237799680", // zheng.zhiqiang49@gmail.com + OrganizationId: 1, + PipelineName: pipelineName, + Id: pipelineId, + Version: 1, + FirstStepName: "step1", Steps: []models.PipelineStepModel{ { StepName: "step1", @@ -162,10 +166,12 @@ func main() { panic(err) } pipeline3 := models.PipelineModel{ - PipelineName: pipelineName, - Id: pipelineId, - Version: 1, - FirstStepName: "step1", + UserId: "auth0|65e9dabff2dab546ed0c231e", // josh's user ID + OrganizationId: 2, + PipelineName: pipelineName, + Id: pipelineId, + Version: 1, + FirstStepName: "step1", Steps: []models.PipelineStepModel{ { StepName: "step1", @@ -233,10 +239,12 @@ func main() { panic(err) } pipeline4 := models.PipelineModel{ - PipelineName: pipelineName, - Id: pipelineId, - Version: 1, - FirstStepName: "step1", + UserId: "auth0|6640b87db9de48e5a6243618", // test1@example.com + OrganizationId: 3, + PipelineName: pipelineName, + Id: pipelineId, + Version: 1, + FirstStepName: "step1", Steps: []models.PipelineStepModel{ { StepName: "step1", @@ -399,8 +407,8 @@ func main() { serviceRequest5 := models.ServiceRequestModel{ Id: serviceReqId, UserId: "auth0|65e9dabff2dab546ed0c231e", // josh's user ID - PipelineId: pipeline3.Id.Hex(), - PipelineName: pipeline3.PipelineName, + PipelineId: pipeline1.Id.Hex(), + PipelineName: pipeline1.PipelineName, PipelineVersion: 1, Status: models.NOT_STARTED, OrganizationId: 1, @@ -420,8 +428,8 @@ func main() { serviceRequest6 := models.ServiceRequestModel{ Id: serviceReqId, UserId: "auth0|65e9dabff2dab546ed0c231e", // josh's user ID - PipelineId: pipeline4.Id.Hex(), - PipelineName: pipeline4.PipelineName, + PipelineId: pipeline2.Id.Hex(), + PipelineName: pipeline2.PipelineName, PipelineVersion: 1, Status: models.NOT_STARTED, OrganizationId: 1, @@ -483,8 +491,8 @@ func main() { serviceRequest9 := models.ServiceRequestModel{ Id: serviceReqId, UserId: "auth0|66010ad5095367b237799680", // zheng.zhiqiang49@gmail.com - PipelineId: pipeline3.Id.Hex(), - PipelineName: pipeline3.PipelineName, + PipelineId: pipeline2.Id.Hex(), + PipelineName: pipeline2.PipelineName, PipelineVersion: 1, Status: models.NOT_STARTED, OrganizationId: 1, @@ -504,8 +512,8 @@ func main() { serviceRequest10 := models.ServiceRequestModel{ Id: serviceReqId, UserId: "auth0|66010ad5095367b237799680", // zheng.zhiqiang49@gmail.com - PipelineId: pipeline4.Id.Hex(), - PipelineName: pipeline4.PipelineName, + PipelineId: pipeline2.Id.Hex(), + PipelineName: pipeline2.PipelineName, PipelineVersion: 1, Status: models.NOT_STARTED, OrganizationId: 1, @@ -545,12 +553,12 @@ func main() { } serviceRequest12 := models.ServiceRequestModel{ Id: serviceReqId, - UserId: "auth0|66010ad5095367b237799680", // zheng.zhiqiang49@gmail.com - PipelineId: pipeline2.Id.Hex(), - PipelineName: pipeline2.PipelineName, + UserId: "auth0|6640b87e9acbec524a253e74", // test2@example.com + PipelineId: pipeline4.Id.Hex(), + PipelineName: pipeline4.PipelineName, PipelineVersion: 1, Status: models.NOT_STARTED, - OrganizationId: 1, + OrganizationId: 3, Remarks: "This is a test service request.", CreatedOn: time.Date(2024, time.January, 1, 1, 0, 0, 0, time.UTC), LastUpdated: time.Date(2024, time.January, 1, 1, 0, 0, 0, time.UTC), @@ -567,11 +575,11 @@ func main() { serviceRequest13 := models.ServiceRequestModel{ Id: serviceReqId, UserId: "auth0|6640b87db9de48e5a6243618", // test1@example.com - PipelineId: pipeline2.Id.Hex(), - PipelineName: pipeline2.PipelineName, + PipelineId: pipeline3.Id.Hex(), + PipelineName: pipeline3.PipelineName, PipelineVersion: 1, Status: models.NOT_STARTED, - OrganizationId: 1, + OrganizationId: 2, Remarks: "This is a test service request.", CreatedOn: time.Date(2024, time.January, 1, 1, 0, 0, 0, time.UTC), LastUpdated: time.Date(2024, time.January, 1, 1, 0, 0, 0, time.UTC),