Skip to content

Commit

Permalink
fixed delete workspace functionality and user budget functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Jun 3, 2024
1 parent a8e4905 commit e1090af
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
32 changes: 16 additions & 16 deletions db/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions handlers/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit e1090af

Please sign in to comment.