feat(inboxes): deepen channel config parity
This commit is contained in:
@@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -220,7 +221,7 @@ func (h *InboxHandler) Delete(c *gin.Context) {
|
||||
|
||||
func bindCreateInboxRequest(c *gin.Context, req *service.CreateInboxRequest) error {
|
||||
if strings.Contains(c.ContentType(), "json") {
|
||||
return c.ShouldBindJSON(req)
|
||||
return bindCreateInboxJSON(c, req)
|
||||
}
|
||||
values, err := inboxFormValues(c)
|
||||
if err != nil {
|
||||
@@ -232,7 +233,7 @@ func bindCreateInboxRequest(c *gin.Context, req *service.CreateInboxRequest) err
|
||||
|
||||
func bindUpdateInboxRequest(c *gin.Context, req *service.UpdateInboxRequest) error {
|
||||
if strings.Contains(c.ContentType(), "json") {
|
||||
return c.ShouldBindJSON(req)
|
||||
return bindUpdateInboxJSON(c, req)
|
||||
}
|
||||
values, err := inboxFormValues(c)
|
||||
if err != nil {
|
||||
@@ -268,6 +269,70 @@ func inboxFormValues(c *gin.Context) (map[string][]string, error) {
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func bindCreateInboxJSON(c *gin.Context, req *service.CreateInboxRequest) error {
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.Unmarshal(body, req); err != nil {
|
||||
return err
|
||||
}
|
||||
var raw map[string]any
|
||||
if err := json.Unmarshal(body, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
hoistInboxChannelJSONFields(&req.Channel, raw)
|
||||
return nil
|
||||
}
|
||||
|
||||
func bindUpdateInboxJSON(c *gin.Context, req *service.UpdateInboxRequest) error {
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.Unmarshal(body, req); err != nil {
|
||||
return err
|
||||
}
|
||||
var raw map[string]any
|
||||
if err := json.Unmarshal(body, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
hoistInboxChannelJSONFields(&req.Channel, raw)
|
||||
return nil
|
||||
}
|
||||
|
||||
func hoistInboxChannelJSONFields(channel *map[string]any, raw map[string]any) {
|
||||
for key, value := range raw {
|
||||
if !isInboxChannelConfigKey(key) || value == nil {
|
||||
continue
|
||||
}
|
||||
if *channel == nil {
|
||||
*channel = map[string]any{}
|
||||
}
|
||||
if _, exists := (*channel)[key]; !exists {
|
||||
(*channel)[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isInboxChannelConfigKey(key string) bool {
|
||||
switch key {
|
||||
case "website_url", "widget_color", "welcome_title", "welcome_tagline", "reply_time",
|
||||
"pre_chat_form_enabled", "pre_chat_form_options", "continuity_via_email", "hmac_mandatory",
|
||||
"allowed_domains", "selected_feature_flags", "webhook_url", "additional_attributes",
|
||||
"email", "forward_to_email", "imap_login", "imap_password", "imap_address", "imap_port",
|
||||
"imap_enabled", "imap_enable_ssl", "imap_authentication", "smtp_login", "smtp_password",
|
||||
"smtp_address", "smtp_port", "smtp_enabled", "smtp_domain", "smtp_enable_ssl_tls",
|
||||
"smtp_enable_starttls_auto", "smtp_openssl_verify_mode", "smtp_authentication", "provider",
|
||||
"provider_config", "phone_number", "message_templates", "account_sid", "auth_token", "api_key_sid",
|
||||
"api_key_secret", "messaging_service_sid", "medium", "content_templates", "twiml_app_sid",
|
||||
"voice_enabled", "line_channel_id", "line_channel_secret", "line_channel_token", "bot_token":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func applyCreateInboxForm(req *service.CreateInboxRequest, values map[string][]string) {
|
||||
req.Channel = map[string]any{}
|
||||
for key, list := range values {
|
||||
|
||||
Reference in New Issue
Block a user