refactor(UI): 🏗️ Ui changes and API changes
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"git.warky.dev/wdevs/whatshooked/pkg/config"
|
||||
"git.warky.dev/wdevs/whatshooked/pkg/handlers"
|
||||
"git.warky.dev/wdevs/whatshooked/pkg/models"
|
||||
@@ -397,6 +399,60 @@ func handleQueryCreate(w http.ResponseWriter, r *http.Request, db *bun.DB, req Q
|
||||
return
|
||||
}
|
||||
|
||||
// Initialize data map if needed
|
||||
if req.Data == nil {
|
||||
req.Data = make(map[string]interface{})
|
||||
}
|
||||
|
||||
// Auto-generate UUID for id field if not provided
|
||||
generatedID := ""
|
||||
if _, exists := req.Data["id"]; !exists {
|
||||
generatedID = uuid.New().String()
|
||||
req.Data["id"] = generatedID
|
||||
}
|
||||
|
||||
// Auto-inject user_id for tables that need it
|
||||
if userCtx != nil && userCtx.Claims != nil {
|
||||
// Get user_id from claims (it's stored as UUID string)
|
||||
if userIDClaim, ok := userCtx.Claims["user_id"]; ok {
|
||||
if userID, ok := userIDClaim.(string); ok && userID != "" {
|
||||
// Add user_id to data if the table requires it and it's not already set
|
||||
tablesWithUserID := map[string]bool{
|
||||
"hooks": true,
|
||||
"whatsapp_accounts": true,
|
||||
"api_keys": true,
|
||||
"event_logs": true,
|
||||
}
|
||||
|
||||
if tablesWithUserID[req.Table] {
|
||||
// Only set user_id if not already provided
|
||||
if _, exists := req.Data["user_id"]; !exists {
|
||||
req.Data["user_id"] = userID
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-generate session_path for WhatsApp accounts if not provided
|
||||
if req.Table == "whatsapp_accounts" {
|
||||
// Set session_path if not already provided
|
||||
if _, exists := req.Data["session_path"]; !exists {
|
||||
// Use account_id if provided, otherwise use generated id
|
||||
sessionID := ""
|
||||
if accountID, ok := req.Data["account_id"].(string); ok && accountID != "" {
|
||||
sessionID = accountID
|
||||
} else if generatedID != "" {
|
||||
sessionID = generatedID
|
||||
} else if id, ok := req.Data["id"].(string); ok && id != "" {
|
||||
sessionID = id
|
||||
}
|
||||
if sessionID != "" {
|
||||
req.Data["session_path"] = fmt.Sprintf("./sessions/%s", sessionID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert data map to model using JSON marshaling
|
||||
dataJSON, err := json.Marshal(req.Data)
|
||||
if err != nil {
|
||||
@@ -409,6 +465,22 @@ func handleQueryCreate(w http.ResponseWriter, r *http.Request, db *bun.DB, req Q
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure ID is set after unmarshaling by using model-specific handling
|
||||
if generatedID != "" {
|
||||
switch m := model.(type) {
|
||||
case *models.ModelPublicWhatsappAccount:
|
||||
m.ID.FromString(generatedID)
|
||||
case *models.ModelPublicHook:
|
||||
m.ID.FromString(generatedID)
|
||||
case *models.ModelPublicAPIKey:
|
||||
m.ID.FromString(generatedID)
|
||||
case *models.ModelPublicEventLog:
|
||||
m.ID.FromString(generatedID)
|
||||
case *models.ModelPublicUser:
|
||||
m.ID.FromString(generatedID)
|
||||
}
|
||||
}
|
||||
|
||||
// Insert into database
|
||||
_, err = db.NewInsert().Model(model).Exec(r.Context())
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user