Skip to content

Commit

Permalink
Added endpoints and properties to Workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulWahab3181 committed Apr 25, 2024
1 parent 719d8e2 commit f9ed4ef
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cypress/e2e/01_workspaces.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('Edit Mission', () => {
headers: { 'x-jwt': `${value}` },
body: {
uuid: Workspaces[0].uuid,
owner_pubkey: Workspaces[0].owner_pubkey,
mission: 'This is a sample mission for workspace'
}
}).then((resp) => {
Expand All @@ -59,6 +60,7 @@ describe('Edit Tactics', () => {
headers: { 'x-jwt': `${value}` },
body: {
uuid: Workspaces[0].uuid,
owner_pubkey: Workspaces[0].owner_pubkey,
mission: 'This is a sample tactics and objectives for workspace'
}
}).then((resp) => {
Expand All @@ -79,6 +81,7 @@ describe('Edit Schematics Url', () => {
headers: { 'x-jwt': `${value}` },
body: {
uuid: Workspaces[0].uuid,
owner_pubkey: Workspaces[0].owner_pubkey,
mission: 'This is a sample schematic url for workspaces'
}
}).then((resp) => {
Expand Down
3 changes: 3 additions & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ type Organization struct {
Website string `json:"website" validate:"omitempty,uri"`
Github string `json:"github" validate:"omitempty,uri"`
Description string `json:"description" validate:"omitempty,lte=120"`
Mission string `json:"mission"`
Tactics string `json:"tactics"`
SchematicUrl string `json:"schematic_url"`
}

type OrganizationShort struct {
Expand Down
156 changes: 156 additions & 0 deletions handlers/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,3 +731,159 @@ func (oh *workspaceHandler) DeleteWorkspace(w http.ResponseWriter, r *http.Reque
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(workspace)
}

func (oh *workspaceHandler) UpdateWorkspaceMission(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)
if pubKeyFromAuth == "" {
fmt.Println("no pubkey from auth")
w.WriteHeader(http.StatusUnauthorized)
return
}

workspace := db.Organization{}
body, _ := io.ReadAll(r.Body)
r.Body.Close()
err := json.Unmarshal(body, &workspace)

fmt.Println("workspace: ", workspace)
fmt.Println("body: ", body)

if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusNotAcceptable)
return
}

if pubKeyFromAuth != workspace.OwnerPubKey {
hasRole := db.UserHasAccess(pubKeyFromAuth, workspace.Uuid, db.EditOrg)
if !hasRole {
fmt.Println(pubKeyFromAuth)
fmt.Println(workspace.OwnerPubKey)
fmt.Println("mismatched pubkey")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode("Don't have access to Edit workspace")
return
}
}

// Validate struct data
err = db.Validate.Struct(workspace)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("Error: did not pass validation test : %s", err)
json.NewEncoder(w).Encode(msg)
return
}

p, err := oh.db.CreateOrEditWorkspace(workspace)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(p)
}

func (oh *workspaceHandler) UpdateWorkspaceTactics(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)
if pubKeyFromAuth == "" {
fmt.Println("no pubkey from auth")
w.WriteHeader(http.StatusUnauthorized)
return
}

workspace := db.Organization{}
body, _ := io.ReadAll(r.Body)
r.Body.Close()
err := json.Unmarshal(body, &workspace)

if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusNotAcceptable)
return
}

if pubKeyFromAuth != workspace.OwnerPubKey {
hasRole := db.UserHasAccess(pubKeyFromAuth, workspace.Uuid, db.EditOrg)
if !hasRole {
fmt.Println(pubKeyFromAuth)
fmt.Println(workspace.OwnerPubKey)
fmt.Println("mismatched pubkey")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode("Don't have access to Edit workspace")
return
}
}

// Validate struct data
err = db.Validate.Struct(workspace)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("Error: did not pass validation test : %s", err)
json.NewEncoder(w).Encode(msg)
return
}

p, err := oh.db.CreateOrEditWorkspace(workspace)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(p)
}

func (oh *workspaceHandler) UpdateWorkspaceSchematicUrl(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)
if pubKeyFromAuth == "" {
fmt.Println("no pubkey from auth")
w.WriteHeader(http.StatusUnauthorized)
return
}

workspace := db.Organization{}
body, _ := io.ReadAll(r.Body)
r.Body.Close()
err := json.Unmarshal(body, &workspace)

if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusNotAcceptable)
return
}

if pubKeyFromAuth != workspace.OwnerPubKey {
hasRole := db.UserHasAccess(pubKeyFromAuth, workspace.Uuid, db.EditOrg)
if !hasRole {
fmt.Println(pubKeyFromAuth)
fmt.Println(workspace.OwnerPubKey)
fmt.Println("mismatched pubkey")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode("Don't have access to Edit workspace")
return
}
}

// Validate struct data
err = db.Validate.Struct(workspace)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("Error: did not pass validation test : %s", err)
json.NewEncoder(w).Encode(msg)
return
}

p, err := oh.db.CreateOrEditWorkspace(workspace)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(p)
}
4 changes: 4 additions & 0 deletions routes/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func WorkspaceRoutes() chi.Router {
r.Get("/poll/invoices/{uuid}", organizationHandlers.PollBudgetInvoices)
r.Get("/invoices/count/{uuid}", handlers.GetInvoicesCount)
r.Delete("/delete/{uuid}", organizationHandlers.DeleteWorkspace)

r.Post("/mission", organizationHandlers.UpdateWorkspaceMission)
r.Post("/tactics", organizationHandlers.UpdateWorkspaceTactics)
r.Post("/schematicurl", organizationHandlers.UpdateWorkspaceSchematicUrl)
})
return r
}

0 comments on commit f9ed4ef

Please sign in to comment.