feat(automation): align attachment uploads
This commit is contained in:
@@ -3,6 +3,7 @@ package v1
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gochat/gochat/internal/automation"
|
||||
@@ -33,7 +34,7 @@ type automationRuleRequest struct {
|
||||
EventName string `json:"event_name"`
|
||||
Active *bool `json:"active"`
|
||||
Conditions []automationRuleConditionRequest `json:"conditions"`
|
||||
Actions []automationRuleActionRequest `json:"actions"`
|
||||
Actions *[]automationRuleActionRequest `json:"actions"`
|
||||
}
|
||||
|
||||
type automationRuleConditionRequest struct {
|
||||
@@ -120,6 +121,10 @@ func (h *AutomationRuleHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
if svcErr := h.svc.Create(c.Request.Context(), rule); svcErr != nil {
|
||||
if strings.Contains(svcErr.Error(), "invalid attachment") {
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "invalid attachment"})
|
||||
return
|
||||
}
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
@@ -167,6 +172,10 @@ func (h *AutomationRuleHandler) Update(c *gin.Context) {
|
||||
rule.ID = automationID
|
||||
|
||||
if svcErr := h.svc.UpdateForAccount(c.Request.Context(), accountID, rule); svcErr != nil {
|
||||
if strings.Contains(svcErr.Error(), "invalid attachment") {
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "invalid attachment"})
|
||||
return
|
||||
}
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
@@ -325,13 +334,16 @@ func buildAutomationRuleFromRequest(accountID uint, req automationRuleRequest) (
|
||||
})
|
||||
}
|
||||
|
||||
actions := make(automation.Actions, 0, len(req.Actions))
|
||||
for _, actionReq := range req.Actions {
|
||||
params, err := normalizeAutomationActionParams(actionReq.ActionName, actionReq.ActionParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
var actions automation.Actions
|
||||
if req.Actions != nil {
|
||||
actions = make(automation.Actions, 0, len(*req.Actions))
|
||||
for _, actionReq := range *req.Actions {
|
||||
params, err := normalizeAutomationActionParams(actionReq.ActionName, actionReq.ActionParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
actions = append(actions, automation.Action{ActionName: actionReq.ActionName, ActionParams: params})
|
||||
}
|
||||
actions = append(actions, automation.Action{ActionName: actionReq.ActionName, ActionParams: params})
|
||||
}
|
||||
|
||||
return &automation.AutomationRule{
|
||||
@@ -354,7 +366,7 @@ func serializeAutomationRules(rules []automation.AutomationRule) []gin.H {
|
||||
}
|
||||
|
||||
func serializeAutomationRule(rule *automation.AutomationRule) gin.H {
|
||||
return gin.H{
|
||||
serialized := gin.H{
|
||||
"id": rule.ID,
|
||||
"account_id": rule.AccountID,
|
||||
"name": rule.Name,
|
||||
@@ -365,6 +377,26 @@ func serializeAutomationRule(rule *automation.AutomationRule) gin.H {
|
||||
"created_on": rule.CreatedAt.Unix(),
|
||||
"active": rule.Active,
|
||||
}
|
||||
if len(rule.Files) > 0 {
|
||||
serialized["files"] = serializeAutomationRuleFiles(rule.Files)
|
||||
}
|
||||
return serialized
|
||||
}
|
||||
|
||||
func serializeAutomationRuleFiles(files []automation.AutomationRuleFile) []gin.H {
|
||||
result := make([]gin.H, 0, len(files))
|
||||
for _, file := range files {
|
||||
result = append(result, gin.H{
|
||||
"id": file.ID,
|
||||
"automation_rule_id": file.AutomationRuleID,
|
||||
"file_type": file.FileType,
|
||||
"account_id": file.AccountID,
|
||||
"file_url": file.FileURL,
|
||||
"blob_id": file.BlobID,
|
||||
"filename": file.Filename,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func serializeAutomationConditions(conditions automation.Conditions) []gin.H {
|
||||
|
||||
Reference in New Issue
Block a user