Skip to content

Commit

Permalink
Merge pull request #2321 from MahtabBukhari/Add_Feature_Status_Manage…
Browse files Browse the repository at this point in the history
…ment_to_WorkspaceFeatures

Add Feature Status Management to WorkspaceFeatures
  • Loading branch information
humansinstitute authored Jan 3, 2025
2 parents 3073cb9 + 1dbc875 commit 6c160c5
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 182 deletions.
23 changes: 18 additions & 5 deletions db/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (

func (db database) GetFeaturesByWorkspaceUuid(uuid string, r *http.Request) []WorkspaceFeatures {
offset, limit, sortBy, direction, _ := utils.GetPaginationParams(r)
statusFilter := r.URL.Query().Get("status")
if statusFilter == "" {
statusFilter = string(ActiveFeature)
}

orderQuery := ""
limitQuery := ""
Expand All @@ -28,13 +32,10 @@ func (db database) GetFeaturesByWorkspaceUuid(uuid string, r *http.Request) []Wo
limitQuery = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset)
}

query := `SELECT * FROM public.workspace_features WHERE workspace_uuid = '` + uuid + `'`

query := `SELECT * FROM public.workspace_features WHERE workspace_uuid = ? AND feat_status = ? `
allQuery := query + " " + orderQuery + " " + limitQuery

theQuery := db.db.Raw(allQuery)

theQuery.Scan(&ms)
db.db.Raw(allQuery, uuid, statusFilter).Scan(&ms)

return ms
}
Expand Down Expand Up @@ -356,3 +357,15 @@ func (db database) GetFeatureBrief(featureUuid string) (string, error) {

return featureBrief, nil
}

func (db database) UpdateFeatureStatus(uuid string, status FeatureStatus) (WorkspaceFeatures, error) {
var feature WorkspaceFeatures

result := db.db.Model(&WorkspaceFeatures{}).Where("uuid = ?", uuid).Update("feat_status", status)
if result.RowsAffected == 0 {
return feature, errors.New("feature not found or status unchanged")
}

db.db.Where("uuid = ?", uuid).First(&feature)
return feature, nil
}
1 change: 1 addition & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,5 @@ type Database interface {
CloseBountyTiming(bountyID uint) error
UpdateBountyTimingOnProof(bountyID uint) error
GetWorkspaceBountyCardsData(r *http.Request) []NewBounty
UpdateFeatureStatus(uuid string, status FeatureStatus) (WorkspaceFeatures, error)
}
40 changes: 24 additions & 16 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,23 +588,31 @@ type WorkspaceCodeGraph struct {
UpdatedBy string `json:"updated_by"`
}

type FeatureStatus string

const (
ActiveFeature FeatureStatus = "active"
ArchivedFeature FeatureStatus = "archived"
)

type WorkspaceFeatures struct {
ID uint `json:"id"`
Uuid string `gorm:"unique;not null" json:"uuid"`
WorkspaceUuid string `gorm:"not null" json:"workspace_uuid"`
Name string `gorm:"not null" json:"name"`
Brief string `json:"brief"`
Requirements string `json:"requirements"`
Architecture string `json:"architecture"`
Url string `json:"url"`
Priority int `json:"priority"`
Created *time.Time `json:"created"`
Updated *time.Time `json:"updated"`
CreatedBy string `json:"created_by"`
UpdatedBy string `json:"updated_by"`
BountiesCountCompleted int `gorm:"-" json:"bounties_count_completed"`
BountiesCountAssigned int `gorm:"-" json:"bounties_count_assigned"`
BountiesCountOpen int `gorm:"-" json:"bounties_count_open"`
ID uint `json:"id"`
Uuid string `gorm:"unique;not null" json:"uuid"`
WorkspaceUuid string `gorm:"not null" json:"workspace_uuid"`
Name string `gorm:"not null" json:"name"`
Brief string `json:"brief"`
Requirements string `json:"requirements"`
Architecture string `json:"architecture"`
Url string `json:"url"`
Priority int `json:"priority"`
Created *time.Time `json:"created"`
Updated *time.Time `json:"updated"`
CreatedBy string `json:"created_by"`
UpdatedBy string `json:"updated_by"`
BountiesCountCompleted int `gorm:"-" json:"bounties_count_completed"`
BountiesCountAssigned int `gorm:"-" json:"bounties_count_assigned"`
BountiesCountOpen int `gorm:"-" json:"bounties_count_open"`
FeatStatus FeatureStatus `gorm:"type:varchar(20);default:'active';not null" json:"feat_status"`
}

type FeaturePhase struct {
Expand Down
38 changes: 38 additions & 0 deletions handlers/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (oh *featureHandler) CreateOrEditFeatures(w http.ResponseWriter, r *http.Re

if features.Uuid == "" {
features.Uuid = xid.New().String()
features.FeatStatus = db.ActiveFeature
} else {
features.UpdatedBy = pubKeyFromAuth
}
Expand Down Expand Up @@ -698,3 +699,40 @@ func (oh *featureHandler) BriefSend(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(resp.StatusCode)
w.Write(respBody)
}

func (oh *featureHandler) UpdateFeatureStatus(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)
if pubKeyFromAuth == "" {
logger.Log.Info("no pubkey from auth")
w.WriteHeader(http.StatusUnauthorized)
return
}

uuid := chi.URLParam(r, "uuid")
var req struct {
Status db.FeatureStatus `json:"status"`
}

if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
logger.Log.Error("invalid request body", err)
w.WriteHeader(http.StatusBadRequest)
return
}

if req.Status != db.ActiveFeature && req.Status != db.ArchivedFeature {
logger.Log.Info("invalid feature status")
w.WriteHeader(http.StatusBadRequest)
return
}

updatedFeature, err := oh.db.UpdateFeatureStatus(uuid, req.Status)
if err != nil {
logger.Log.Error("failed to update feature status", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(updatedFeature)
}
Loading

0 comments on commit 6c160c5

Please sign in to comment.