diff --git a/workspace-server/internal/handlers/workspace_broadcast.go b/workspace-server/internal/handlers/workspace_broadcast.go index 27ce7a8ea..60155038f 100644 --- a/workspace-server/internal/handlers/workspace_broadcast.go +++ b/workspace-server/internal/handlers/workspace_broadcast.go @@ -82,19 +82,7 @@ func (h *BroadcastHandler) Broadcast(c *gin.Context) { // Find the sender's org root by walking the parent_id chain. // Workspaces with parent_id = NULL are org roots; every other workspace // belongs to the org identified by its topmost ancestor. - var orgRootID string - err = db.DB.QueryRowContext(ctx, ` - WITH RECURSIVE org_chain AS ( - SELECT id, parent_id, id AS root_id - FROM workspaces - WHERE id = $1 - UNION ALL - SELECT w.id, w.parent_id, c.root_id - FROM workspaces w - JOIN org_chain c ON w.id = c.parent_id - ) - SELECT root_id FROM org_chain WHERE parent_id IS NULL LIMIT 1 - `, senderID).Scan(&orgRootID) + rootID, err := orgRootID(ctx, db.DB, senderID) if err != nil { log.Printf("Broadcast: org root lookup for %s: %v", senderID, err) c.JSON(http.StatusInternalServerError, gin.H{"error": "internal error"}) @@ -121,7 +109,7 @@ func (h *BroadcastHandler) Broadcast(c *gin.Context) { SELECT 1 FROM workspaces w WHERE w.id = c.id AND w.status != 'removed' ) - `, orgRootID, senderID) + `, rootID, senderID) if err != nil { log.Printf("Broadcast: recipient query failed for %s: %v", senderID, err) c.JSON(http.StatusInternalServerError, gin.H{"error": "internal error"})