Changeset 0.17.1 (#76)
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -20,13 +19,13 @@ import (
|
||||
func (h *TeamHandler) ListTeamProviders(c *gin.Context) {
|
||||
teamID := getTeamID(c)
|
||||
|
||||
rows, err := database.DB.Query(`
|
||||
rows, err := database.DB.Query(database.Q(`
|
||||
SELECT id, name, provider, endpoint, api_key_enc,
|
||||
model_default, config::text, is_active, is_private, created_at, updated_at
|
||||
model_default, config, is_active, is_private, created_at, updated_at
|
||||
FROM provider_configs
|
||||
WHERE scope = 'team' AND owner_id = $1
|
||||
ORDER BY name ASC
|
||||
`, teamID)
|
||||
`), teamID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list team providers"})
|
||||
return
|
||||
@@ -119,15 +118,14 @@ func (h *TeamHandler) CreateTeamProvider(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
var id string
|
||||
err := database.DB.QueryRow(`
|
||||
id, err := database.InsertReturningID(`
|
||||
INSERT INTO provider_configs (scope, owner_id, name, provider, endpoint,
|
||||
api_key_enc, key_nonce, key_scope, model_default, config, is_private)
|
||||
VALUES ('team', $1, $2, $3, $4, $5, $6, 'team', $7, $8::jsonb, $9)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, 'team', $8, $9::jsonb, $10)
|
||||
RETURNING id
|
||||
`, teamID, req.Name, req.Provider, req.Endpoint,
|
||||
`, "team", teamID, req.Name, req.Provider, req.Endpoint,
|
||||
apiKeyEnc, keyNonce, req.ModelDefault, configJSON, req.IsPrivate,
|
||||
).Scan(&id)
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("[WARN] Failed to create team provider: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create provider"})
|
||||
@@ -158,26 +156,26 @@ func (h *TeamHandler) UpdateTeamProvider(c *gin.Context) {
|
||||
|
||||
// Verify provider belongs to this team
|
||||
var count int
|
||||
database.DB.QueryRow(`SELECT COUNT(*) FROM provider_configs WHERE id = $1 AND scope = 'team' AND owner_id = $2`, providerID, teamID).Scan(&count)
|
||||
database.DB.QueryRow(database.Q(`SELECT COUNT(*) FROM provider_configs WHERE id = $1 AND scope = 'team' AND owner_id = $2`), providerID, teamID).Scan(&count)
|
||||
if count == 0 {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "provider not found in this team"})
|
||||
return
|
||||
}
|
||||
|
||||
// Build dynamic update
|
||||
query := "UPDATE provider_configs SET updated_at = NOW()"
|
||||
// Build dynamic update using ? placeholders (works on both dialects)
|
||||
setClauses := []string{"updated_at = " + database.Q("NOW()")}
|
||||
args := []interface{}{}
|
||||
argN := 1
|
||||
|
||||
addSet := func(col string, val interface{}) {
|
||||
setClauses = append(setClauses, col+" = ?")
|
||||
args = append(args, val)
|
||||
}
|
||||
|
||||
if req.Name != nil {
|
||||
query += ", name = $" + strconv.Itoa(argN)
|
||||
args = append(args, *req.Name)
|
||||
argN++
|
||||
addSet("name", *req.Name)
|
||||
}
|
||||
if req.Endpoint != nil {
|
||||
query += ", endpoint = $" + strconv.Itoa(argN)
|
||||
args = append(args, *req.Endpoint)
|
||||
argN++
|
||||
addSet("endpoint", *req.Endpoint)
|
||||
}
|
||||
if req.APIKey != nil && *req.APIKey != "" {
|
||||
if h.vault != nil {
|
||||
@@ -186,43 +184,42 @@ func (h *TeamHandler) UpdateTeamProvider(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to encrypt API key"})
|
||||
return
|
||||
}
|
||||
query += ", api_key_enc = $" + strconv.Itoa(argN)
|
||||
args = append(args, enc)
|
||||
argN++
|
||||
query += ", key_nonce = $" + strconv.Itoa(argN)
|
||||
args = append(args, nonce)
|
||||
argN++
|
||||
addSet("api_key_enc", enc)
|
||||
addSet("key_nonce", nonce)
|
||||
} else {
|
||||
query += ", api_key_enc = $" + strconv.Itoa(argN)
|
||||
args = append(args, []byte(*req.APIKey))
|
||||
argN++
|
||||
addSet("api_key_enc", []byte(*req.APIKey))
|
||||
}
|
||||
}
|
||||
if req.ModelDefault != nil {
|
||||
query += ", model_default = $" + strconv.Itoa(argN)
|
||||
args = append(args, *req.ModelDefault)
|
||||
argN++
|
||||
addSet("model_default", *req.ModelDefault)
|
||||
}
|
||||
if req.IsActive != nil {
|
||||
query += ", is_active = $" + strconv.Itoa(argN)
|
||||
args = append(args, *req.IsActive)
|
||||
argN++
|
||||
addSet("is_active", *req.IsActive)
|
||||
}
|
||||
if req.IsPrivate != nil {
|
||||
query += ", is_private = $" + strconv.Itoa(argN)
|
||||
args = append(args, *req.IsPrivate)
|
||||
argN++
|
||||
addSet("is_private", *req.IsPrivate)
|
||||
}
|
||||
if req.Config != nil {
|
||||
b, _ := json.Marshal(req.Config)
|
||||
query += ", config = $" + strconv.Itoa(argN) + "::jsonb"
|
||||
args = append(args, string(b))
|
||||
argN++
|
||||
addSet("config", string(b))
|
||||
}
|
||||
|
||||
query += " WHERE id = $" + strconv.Itoa(argN) + " AND scope = 'team' AND owner_id = $" + strconv.Itoa(argN+1)
|
||||
args = append(args, providerID, teamID)
|
||||
|
||||
// Build final query with ? placeholders, then convert to $N for Postgres
|
||||
query := "UPDATE provider_configs SET "
|
||||
for i, s := range setClauses {
|
||||
if i > 0 {
|
||||
query += ", "
|
||||
}
|
||||
query += s
|
||||
}
|
||||
query += " WHERE id = ? AND scope = 'team' AND owner_id = ?"
|
||||
|
||||
if database.IsPostgres() {
|
||||
query = convertPlaceholders(query)
|
||||
}
|
||||
|
||||
_, err := database.DB.Exec(query, args...)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update provider"})
|
||||
@@ -237,9 +234,9 @@ func (h *TeamHandler) DeleteTeamProvider(c *gin.Context) {
|
||||
teamID := getTeamID(c)
|
||||
providerID := c.Param("id")
|
||||
|
||||
result, err := database.DB.Exec(`
|
||||
result, err := database.DB.Exec(database.Q(`
|
||||
DELETE FROM provider_configs WHERE id = $1 AND scope = 'team' AND owner_id = $2
|
||||
`, providerID, teamID)
|
||||
`), providerID, teamID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to delete provider"})
|
||||
return
|
||||
@@ -263,11 +260,11 @@ func (h *TeamHandler) ListTeamProviderModels(c *gin.Context) {
|
||||
var apiKeyEnc, keyNonce []byte
|
||||
var keyScope string
|
||||
var headersJSON []byte
|
||||
err := database.DB.QueryRow(`
|
||||
err := database.DB.QueryRow(database.Q(`
|
||||
SELECT name, provider, endpoint, api_key_enc, key_nonce, key_scope, headers
|
||||
FROM provider_configs
|
||||
WHERE id = $1 AND scope = 'team' AND owner_id = $2 AND is_active = true
|
||||
`, providerID, teamID).Scan(&name, &providerType, &endpoint, &apiKeyEnc, &keyNonce, &keyScope, &headersJSON)
|
||||
`), providerID, teamID).Scan(&name, &providerType, &endpoint, &apiKeyEnc, &keyNonce, &keyScope, &headersJSON)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "provider not found"})
|
||||
return
|
||||
@@ -348,7 +345,7 @@ func isTeamProvidersAllowed(teamID string) bool {
|
||||
}
|
||||
|
||||
var settingsJSON []byte
|
||||
err = database.DB.QueryRow(`SELECT settings FROM teams WHERE id = $1`, teamID).Scan(&settingsJSON)
|
||||
err = database.DB.QueryRow(database.Q(`SELECT settings FROM teams WHERE id = $1`), teamID).Scan(&settingsJSON)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user