Skip to content

Commit

Permalink
Added workspaces endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulWahab3181 committed Apr 26, 2024
1 parent 4aeae88 commit 2dd3bb6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 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
31 changes: 17 additions & 14 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,20 +495,23 @@ type Organization struct {
}

type Workspace struct {
ID uint `json:"id"`
Uuid string `json:"uuid"`
Name string `gorm:"unique;not null" json:"name"`
OwnerPubKey string `json:"owner_pubkey"`
Img string `json:"img"`
Created *time.Time `json:"created"`
Updated *time.Time `json:"updated"`
Show bool `json:"show"`
Deleted bool `gorm:"default:false" json:"deleted"`
BountyCount int64 `json:"bounty_count,omitempty"`
Budget uint `json:"budget,omitempty"`
Website string `json:"website" validate:"omitempty,uri"`
Github string `json:"github" validate:"omitempty,uri"`
Description string `json:"description" validate:"omitempty,lte=120"`
ID uint `json:"id"`
Uuid string `json:"uuid"`
Name string `gorm:"unique;not null" json:"name"`
OwnerPubKey string `json:"owner_pubkey"`
Img string `json:"img"`
Created *time.Time `json:"created"`
Updated *time.Time `json:"updated"`
Show bool `json:"show"`
Deleted bool `gorm:"default:false" json:"deleted"`
BountyCount int64 `json:"bounty_count,omitempty"`
Budget uint `json:"budget,omitempty"`
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 WorkspaceShort struct {
Expand Down
51 changes: 51 additions & 0 deletions handlers/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,54 @@ func (oh *workspaceHandler) DeleteWorkspace(w http.ResponseWriter, r *http.Reque
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(workspace)
}

func (oh *workspaceHandler) UpdateWorkspace(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.Workspace{}
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}", workspaceHandlers.PollBudgetInvoices)
r.Get("/invoices/count/{uuid}", handlers.GetInvoicesCount)
r.Delete("/delete/{uuid}", workspaceHandlers.DeleteWorkspace)

r.Post("/mission", workspaceHandlers.UpdateWorkspace)
r.Post("/tactics", workspaceHandlers.UpdateWorkspace)
r.Post("/schematicurl", workspaceHandlers.UpdateWorkspace)
})
return r
}

0 comments on commit 2dd3bb6

Please sign in to comment.