More management tools
This commit is contained in:
@@ -536,25 +536,41 @@ func handleQueryUpdate(w http.ResponseWriter, r *http.Request, db *bun.DB, req Q
|
||||
return
|
||||
}
|
||||
|
||||
// Convert data map to model
|
||||
dataJSON, err := json.Marshal(req.Data)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid data", http.StatusBadRequest)
|
||||
if req.Data == nil || len(req.Data) == 0 {
|
||||
http.Error(w, "No update data provided", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(dataJSON, model); err != nil {
|
||||
http.Error(w, "Invalid data format", http.StatusBadRequest)
|
||||
updateQuery := db.NewUpdate().Model(model).Where("id = ?", req.ID)
|
||||
updatedColumns := 0
|
||||
|
||||
for column, value := range req.Data {
|
||||
// Protect immutable/audit columns from accidental overwrite.
|
||||
if column == "id" || column == "created_at" {
|
||||
continue
|
||||
}
|
||||
updateQuery = updateQuery.Set("? = ?", bun.Ident(column), value)
|
||||
updatedColumns++
|
||||
}
|
||||
|
||||
if updatedColumns == 0 {
|
||||
http.Error(w, "No mutable fields to update", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Update in database
|
||||
_, err = db.NewUpdate().Model(model).Where("id = ?", req.ID).Exec(r.Context())
|
||||
// Update only the provided fields.
|
||||
_, err := updateQuery.Exec(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Update failed: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Return the latest database row after update.
|
||||
if err := db.NewSelect().Model(model).Where("id = ?", req.ID).Scan(r.Context()); err != nil {
|
||||
http.Error(w, fmt.Sprintf("Update succeeded but reload failed: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, model)
|
||||
}
|
||||
|
||||
|
||||
72
pkg/serverembed/dist/assets/index-Cj4Q_Qgu.js
vendored
72
pkg/serverembed/dist/assets/index-Cj4Q_Qgu.js
vendored
File diff suppressed because one or more lines are too long
73
pkg/serverembed/dist/assets/index-_R1QOTag.js
vendored
Normal file
73
pkg/serverembed/dist/assets/index-_R1QOTag.js
vendored
Normal file
File diff suppressed because one or more lines are too long
2
pkg/serverembed/dist/index.html
vendored
2
pkg/serverembed/dist/index.html
vendored
@@ -5,7 +5,7 @@
|
||||
<link rel="icon" type="image/svg+xml" href="/ui/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>web</title>
|
||||
<script type="module" crossorigin src="/ui/assets/index-Cj4Q_Qgu.js"></script>
|
||||
<script type="module" crossorigin src="/ui/assets/index-_R1QOTag.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="/ui/assets/index-Bfia8Lvm.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -12,8 +12,9 @@ import (
|
||||
|
||||
// ListCatalogs returns all product catalogs linked to the business account.
|
||||
func (c *Client) ListCatalogs(ctx context.Context) (*CatalogListResponse, error) {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return nil, errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
params := url.Values{
|
||||
@@ -21,7 +22,7 @@ func (c *Client) ListCatalogs(ctx context.Context) (*CatalogListResponse, error)
|
||||
}
|
||||
|
||||
var resp CatalogListResponse
|
||||
if err := c.graphAPIGet(ctx, c.config.BusinessAccountID+"/catalogs", params, &resp); err != nil {
|
||||
if err := c.graphAPIGet(ctx, wabaID+"/product_catalogs", params, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
|
||||
@@ -19,6 +19,8 @@ import (
|
||||
"go.mau.fi/whatsmeow/types"
|
||||
)
|
||||
|
||||
const defaultBusinessAPIMediaTimeout = 5 * time.Minute
|
||||
|
||||
// Client represents a WhatsApp Business API client
|
||||
type Client struct {
|
||||
id string
|
||||
@@ -61,7 +63,7 @@ func NewClient(cfg config.WhatsAppConfig, eventBus *events.EventBus, mediaConfig
|
||||
phoneNumber: cfg.PhoneNumber,
|
||||
config: *cfg.BusinessAPI,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
Timeout: defaultBusinessAPIMediaTimeout,
|
||||
},
|
||||
eventBus: eventBus,
|
||||
mediaConfig: mediaConfig,
|
||||
|
||||
@@ -7,8 +7,9 @@ import (
|
||||
|
||||
// ListFlows returns all flows for the business account.
|
||||
func (c *Client) ListFlows(ctx context.Context) (*FlowListResponse, error) {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return nil, errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
params := url.Values{
|
||||
@@ -16,7 +17,7 @@ func (c *Client) ListFlows(ctx context.Context) (*FlowListResponse, error) {
|
||||
}
|
||||
|
||||
var resp FlowListResponse
|
||||
if err := c.graphAPIGet(ctx, c.config.BusinessAccountID+"/flows", params, &resp); err != nil {
|
||||
if err := c.graphAPIGet(ctx, wabaID+"/flows", params, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
@@ -24,12 +25,13 @@ func (c *Client) ListFlows(ctx context.Context) (*FlowListResponse, error) {
|
||||
|
||||
// CreateFlow creates a new flow and returns its ID.
|
||||
func (c *Client) CreateFlow(ctx context.Context, flow FlowCreateRequest) (*FlowCreateResponse, error) {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return nil, errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp FlowCreateResponse
|
||||
if err := c.graphAPIPost(ctx, c.config.BusinessAccountID+"/flows", flow, &resp); err != nil {
|
||||
if err := c.graphAPIPost(ctx, wabaID+"/flows", flow, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// UploadMedia uploads a media file to Meta and returns the media ID.
|
||||
@@ -26,8 +28,16 @@ func (c *Client) uploadMedia(ctx context.Context, data []byte, mimeType string)
|
||||
var requestBody bytes.Buffer
|
||||
writer := multipart.NewWriter(&requestBody)
|
||||
|
||||
// Add the file
|
||||
part, err := writer.CreateFormFile("file", "media")
|
||||
if strings.TrimSpace(mimeType) == "" {
|
||||
mimeType = "application/octet-stream"
|
||||
}
|
||||
|
||||
// Add the file with explicit MIME type so Meta does not treat it as octet-stream.
|
||||
fileHeader := make(textproto.MIMEHeader)
|
||||
fileHeader.Set("Content-Disposition", `form-data; name="file"; filename="media"`)
|
||||
fileHeader.Set("Content-Type", mimeType)
|
||||
|
||||
part, err := writer.CreatePart(fileHeader)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create form file: %w", err)
|
||||
}
|
||||
|
||||
@@ -2,22 +2,43 @@ package businessapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (c *Client) resolveWABAID(ctx context.Context) (string, error) {
|
||||
if c.wabaID != "" {
|
||||
return c.wabaID, nil
|
||||
}
|
||||
|
||||
if c.config.WABAId != "" {
|
||||
c.wabaID = c.config.WABAId
|
||||
return c.wabaID, nil
|
||||
}
|
||||
|
||||
id, err := c.fetchWABAID(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("could not resolve WABA ID: %w", err)
|
||||
}
|
||||
|
||||
c.wabaID = id
|
||||
return c.wabaID, nil
|
||||
}
|
||||
|
||||
// ListTemplates returns all message templates for the business account.
|
||||
// Requires BusinessAccountID in the client config.
|
||||
// Uses the WhatsApp Business Account (WABA) ID.
|
||||
func (c *Client) ListTemplates(ctx context.Context) (*TemplateListResponse, error) {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return nil, errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
params := url.Values{
|
||||
"fields": {"id,name,status,language,category,created_at,components,rejection_reasons,quality_score"},
|
||||
"fields": {"id,name,status,language,category,created_at,components,quality_score"},
|
||||
}
|
||||
|
||||
var resp TemplateListResponse
|
||||
if err := c.graphAPIGet(ctx, c.config.BusinessAccountID+"/message_templates", params, &resp); err != nil {
|
||||
if err := c.graphAPIGet(ctx, wabaID+"/message_templates", params, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
@@ -25,12 +46,13 @@ func (c *Client) ListTemplates(ctx context.Context) (*TemplateListResponse, erro
|
||||
|
||||
// UploadTemplate creates a new message template.
|
||||
func (c *Client) UploadTemplate(ctx context.Context, tmpl TemplateUploadRequest) (*TemplateUploadResponse, error) {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return nil, errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp TemplateUploadResponse
|
||||
if err := c.graphAPIPost(ctx, c.config.BusinessAccountID+"/message_templates", tmpl, &resp); err != nil {
|
||||
if err := c.graphAPIPost(ctx, wabaID+"/message_templates", tmpl, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
@@ -38,13 +60,14 @@ func (c *Client) UploadTemplate(ctx context.Context, tmpl TemplateUploadRequest)
|
||||
|
||||
// DeleteTemplate deletes a template by name and language.
|
||||
func (c *Client) DeleteTemplate(ctx context.Context, name, language string) error {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params := url.Values{
|
||||
"name": {name},
|
||||
"language": {language},
|
||||
}
|
||||
return c.graphAPIDelete(ctx, c.config.BusinessAccountID+"/message_templates", params)
|
||||
return c.graphAPIDelete(ctx, wabaID+"/message_templates", params)
|
||||
}
|
||||
|
||||
@@ -622,7 +622,7 @@ type TemplateInfo struct {
|
||||
CreatedAt string `json:"created_at"`
|
||||
Components []TemplateComponentDef `json:"components"`
|
||||
RejectionReasons []string `json:"rejection_reasons,omitempty"`
|
||||
QualityScore string `json:"quality_score,omitempty"`
|
||||
QualityScore any `json:"quality_score,omitempty"`
|
||||
}
|
||||
|
||||
type TemplateComponentDef struct {
|
||||
|
||||
Reference in New Issue
Block a user