From e1090af4d26a73b887206a51586fde0fcd6894e2 Mon Sep 17 00:00:00 2001 From: elraphty Date: Mon, 3 Jun 2024 10:55:21 +0100 Subject: [PATCH] fixed delete workspace functionality and user budget functionality --- db/workspaces.go | 32 ++++++++++++++++---------------- handlers/workspaces.go | 6 ++++-- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/db/workspaces.go b/db/workspaces.go index 8e0e3424b..319b29297 100644 --- a/db/workspaces.go +++ b/db/workspaces.go @@ -200,36 +200,36 @@ func (db database) GetWorkspaceBudget(workspace_uuid string) NewBountyBudget { } func (db database) GetWorkspaceStatusBudget(workspace_uuid string) StatusBudget { - orgBudget := db.GetWorkspaceBudget(workspace_uuid) + workspaceBudget := db.GetWorkspaceBudget(workspace_uuid) var openBudget uint - db.db.Model(&NewBounty{}).Where("assignee = '' ").Where("paid != true").Select("SUM(price)").Row().Scan(&openBudget) + db.db.Model(&NewBounty{}).Where("workspace_uuid = ?", workspace_uuid).Where("assignee = '' ").Where("paid != true").Select("SUM(price)").Row().Scan(&openBudget) var openCount int64 - db.db.Model(&NewBounty{}).Where("assignee = '' ").Where("paid != true").Count(&openCount) + db.db.Model(&NewBounty{}).Where("workspace_uuid = ?", workspace_uuid).Where("assignee = '' ").Where("paid != true").Count(&openCount) - var openDifference int = int(orgBudget.TotalBudget - openBudget) + var openDifference int = int(workspaceBudget.TotalBudget - openBudget) var assignedBudget uint - db.db.Model(&NewBounty{}).Where("assignee != '' ").Where("paid != true").Select("SUM(price)").Row().Scan(&assignedBudget) + db.db.Model(&NewBounty{}).Where("workspace_uuid = ?", workspace_uuid).Where("assignee != '' ").Where("paid != true").Select("SUM(price)").Row().Scan(&assignedBudget) var assignedCount int64 - db.db.Model(&NewBounty{}).Where("assignee != '' ").Where("paid != true").Count(&assignedCount) + db.db.Model(&NewBounty{}).Where("workspace_uuid = ?", workspace_uuid).Where("assignee != '' ").Where("paid != true").Count(&assignedCount) - var assignedDifference int = int(orgBudget.TotalBudget - assignedBudget) + var assignedDifference int = int(workspaceBudget.TotalBudget - assignedBudget) var completedBudget uint - db.db.Model(&NewBounty{}).Where("completed = true ").Where("paid != true").Select("SUM(price)").Row().Scan(&completedBudget) + db.db.Model(&NewBounty{}).Where("workspace_uuid = ?", workspace_uuid).Where("completed = true ").Where("paid != true").Select("SUM(price)").Row().Scan(&completedBudget) var completedCount int64 - db.db.Model(&NewBounty{}).Where("completed = true ").Where("paid != true").Count(&completedCount) + db.db.Model(&NewBounty{}).Where("workspace_uuid = ?", workspace_uuid).Where("completed = true ").Where("paid != true").Count(&completedCount) - var completedDifference int = int(orgBudget.TotalBudget - completedBudget) + var completedDifference int = int(workspaceBudget.TotalBudget - completedBudget) statusBudget := StatusBudget{ OrgUuid: workspace_uuid, WorkspaceUuid: workspace_uuid, - CurrentBudget: orgBudget.TotalBudget, + CurrentBudget: workspaceBudget.TotalBudget, OpenBudget: openBudget, OpenCount: openCount, OpenDifference: openDifference, @@ -378,19 +378,19 @@ func (db database) UpdateWorkspaceForDeletion(uuid string) error { return nil } -func (db database) DeleteAllUsersFromWorkspace(org string) error { - if org == "" { - return errors.New("no org uuid provided") +func (db database) DeleteAllUsersFromWorkspace(workspace_uuid string) error { + if workspace_uuid == "" { + return errors.New("no workspoace uuid provided") } // Delete all users associated with the Workspace - result := db.db.Where("workspace_uuid = ?", org).Delete(&WorkspaceUsers{}) + result := db.db.Where("workspace_uuid = ?", workspace_uuid).Delete(&WorkspaceUsers{}) if result.Error != nil { return result.Error } // Delete all user roles associated with the Workspace - result = db.db.Where("workspace_uuid = ?", org).Delete(&UserRoles{}) + result = db.db.Where("workspace_uuid = ?", workspace_uuid).Delete(&WorkspaceUserRoles{}) if result.Error != nil { return result.Error } diff --git a/handlers/workspaces.go b/handlers/workspaces.go index 492df6771..184ec2bed 100644 --- a/handlers/workspaces.go +++ b/handlers/workspaces.go @@ -715,15 +715,17 @@ func (oh *workspaceHandler) DeleteWorkspace(w http.ResponseWriter, r *http.Reque // Update workspace to hide and clear certain fields if err := oh.db.UpdateWorkspaceForDeletion(uuid); err != nil { - fmt.Println("[workspaces] Error updating workspace:", err) + fmt.Println("Error updating workspace:", err) w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode("Could not update workspace fields for deletion") return } // Delete all users from the workspace if err := oh.db.DeleteAllUsersFromWorkspace(uuid); err != nil { - fmt.Println("[workspaces] Error removing users from workspace:", err) + fmt.Println("Error removing users from workspace:", err) w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode("Could not delete workspace users") return }