Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope pipelines to orgs #166

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions backend/database/mongo_seed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ func main() {
panic(err)
}
pipeline1 := models.PipelineModel{
PipelineName: pipelineName,
Id: pipelineId,
Version: 1,
FirstStepName: "step1",
UserId: "auth0|66010ad5095367b237799680", // [email protected]
OrganizationId: 1,
PipelineName: pipelineName,
Id: pipelineId,
Version: 1,
FirstStepName: "step1",
Steps: []models.PipelineStepModel{
{
StepName: "step1",
Expand Down Expand Up @@ -88,10 +90,12 @@ func main() {
panic(err)
}
pipeline2 := models.PipelineModel{
PipelineName: pipelineName,
Id: pipelineId,
Version: 1,
FirstStepName: "step1",
UserId: "auth0|66010ad5095367b237799680", // [email protected]
OrganizationId: 1,
PipelineName: pipelineName,
Id: pipelineId,
Version: 1,
FirstStepName: "step1",
Steps: []models.PipelineStepModel{
{
StepName: "step1",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -233,10 +239,12 @@ func main() {
panic(err)
}
pipeline4 := models.PipelineModel{
PipelineName: pipelineName,
Id: pipelineId,
Version: 1,
FirstStepName: "step1",
UserId: "auth0|6640b87db9de48e5a6243618", // [email protected]
OrganizationId: 3,
PipelineName: pipelineName,
Id: pipelineId,
Version: 1,
FirstStepName: "step1",
Steps: []models.PipelineStepModel{
{
StepName: "step1",
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -483,8 +491,8 @@ func main() {
serviceRequest9 := models.ServiceRequestModel{
Id: serviceReqId,
UserId: "auth0|66010ad5095367b237799680", // [email protected]
PipelineId: pipeline3.Id.Hex(),
PipelineName: pipeline3.PipelineName,
PipelineId: pipeline2.Id.Hex(),
PipelineName: pipeline2.PipelineName,
PipelineVersion: 1,
Status: models.NOT_STARTED,
OrganizationId: 1,
Expand All @@ -504,8 +512,8 @@ func main() {
serviceRequest10 := models.ServiceRequestModel{
Id: serviceReqId,
UserId: "auth0|66010ad5095367b237799680", // [email protected]
PipelineId: pipeline4.Id.Hex(),
PipelineName: pipeline4.PipelineName,
PipelineId: pipeline2.Id.Hex(),
PipelineName: pipeline2.PipelineName,
PipelineVersion: 1,
Status: models.NOT_STARTED,
OrganizationId: 1,
Expand Down Expand Up @@ -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),
Expand All @@ -567,11 +575,11 @@ func main() {
serviceRequest13 := models.ServiceRequestModel{
Id: serviceReqId,
UserId: "auth0|6640b87db9de48e5a6243618", // [email protected]
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),
Expand Down
18 changes: 10 additions & 8 deletions backend/src/database/models/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions backend/src/database/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
17 changes: 12 additions & 5 deletions backend/src/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -572,7 +573,13 @@ func handleGetPipeline(logger logger.ServerLogger, client *mongo.Client) http.Ha

func handleGetAllPipelines(logger logger.ServerLogger, client *mongo.Client) http.Handler {
Ziyang-98 marked this conversation as resolved.
Show resolved Hide resolved
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))
Expand Down
Loading