feat: validate and activate AI table bindings
This commit is contained in:
@@ -13,10 +13,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type faqConfig struct {
|
type faqConfig struct {
|
||||||
BaseID string
|
BaseID string
|
||||||
SheetID string
|
SheetID string
|
||||||
ViewID string
|
ViewID string
|
||||||
OperatorID string
|
OperatorID string
|
||||||
|
FieldMapping map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func readFAQConfig() (faqConfig, error) {
|
func readFAQConfig() (faqConfig, error) {
|
||||||
@@ -46,7 +47,7 @@ func buildFAQDispatcher(repo faqtable.ExportRepository, clientID, clientSecret s
|
|||||||
}
|
}
|
||||||
client, err := questiontable.NewClient(questiontable.Config{
|
client, err := questiontable.NewClient(questiontable.Config{
|
||||||
ClientID: clientID, Secret: clientSecret, BaseID: cfg.BaseID,
|
ClientID: clientID, Secret: clientSecret, BaseID: cfg.BaseID,
|
||||||
SheetID: cfg.SheetID, OperatorID: cfg.OperatorID,
|
SheetID: cfg.SheetID, OperatorID: cfg.OperatorID, FieldMapping: cfg.FieldMapping,
|
||||||
}, nil)
|
}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
+15
-1
@@ -107,10 +107,17 @@ func main() {
|
|||||||
if err := dingCfg.Validate(); err != nil {
|
if err := dingCfg.Validate(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
activeBindings, err := settingsRuntime.repository.ActiveBindings(storeCtx, dingCfg.TenantKey)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
faqCfg, err := readFAQConfig()
|
faqCfg, err := readFAQConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
if binding, ok := activeBindings["faq_table"]; ok {
|
||||||
|
faqCfg = faqConfig{BaseID: binding.BaseID, SheetID: binding.SheetID, ViewID: binding.ViewID, OperatorID: binding.OperatorID, FieldMapping: binding.FieldMapping}
|
||||||
|
}
|
||||||
var faqWorkflowDone chan struct{}
|
var faqWorkflowDone chan struct{}
|
||||||
var faqRecordLinks *faqtable.RecordLinkBuilder
|
var faqRecordLinks *faqtable.RecordLinkBuilder
|
||||||
if faqCfg.BaseID != "" {
|
if faqCfg.BaseID != "" {
|
||||||
@@ -134,11 +141,15 @@ func main() {
|
|||||||
log.Printf("DingTalk FAQ table dispatcher started")
|
log.Printf("DingTalk FAQ table dispatcher started")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
supportConversationID := os.Getenv("DINGTALK_SUPPORT_GROUP_OPEN_CONVERSATION_ID")
|
||||||
|
if binding, ok := activeBindings["support_group"]; ok {
|
||||||
|
supportConversationID = binding.ConversationID
|
||||||
|
}
|
||||||
supportDispatcher, err := buildSupportDispatcher(
|
supportDispatcher, err := buildSupportDispatcher(
|
||||||
store,
|
store,
|
||||||
dingCfg.ClientID,
|
dingCfg.ClientID,
|
||||||
dingCfg.ClientSecret,
|
dingCfg.ClientSecret,
|
||||||
os.Getenv("DINGTALK_SUPPORT_GROUP_OPEN_CONVERSATION_ID"),
|
supportConversationID,
|
||||||
faqRecordLinks,
|
faqRecordLinks,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -153,6 +164,9 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
if binding, ok := activeBindings["question_table"]; ok {
|
||||||
|
qtCfg = questionTableConfig{BaseID: binding.BaseID, SheetID: binding.SheetID, OperatorID: binding.OperatorID, FieldMapping: binding.FieldMapping}
|
||||||
|
}
|
||||||
var questionTableDone chan struct{}
|
var questionTableDone chan struct{}
|
||||||
if qtCfg.BaseID != "" {
|
if qtCfg.BaseID != "" {
|
||||||
qtRepo, err := postgres.NewQuestionTableExportRepository(store)
|
qtRepo, err := postgres.NewQuestionTableExportRepository(store)
|
||||||
|
|||||||
@@ -19,9 +19,10 @@ type questionTablePollerRunner interface {
|
|||||||
|
|
||||||
// questionTableConfig holds the optional Notable question-table env values.
|
// questionTableConfig holds the optional Notable question-table env values.
|
||||||
type questionTableConfig struct {
|
type questionTableConfig struct {
|
||||||
BaseID string
|
BaseID string
|
||||||
SheetID string
|
SheetID string
|
||||||
OperatorID string
|
OperatorID string
|
||||||
|
FieldMapping map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
// readQuestionTableConfig reads the three dedicated env vars and returns
|
// readQuestionTableConfig reads the three dedicated env vars and returns
|
||||||
@@ -61,11 +62,12 @@ func buildQuestionTableDispatcher(repo questiontable.ExportRepository, clientID,
|
|||||||
}
|
}
|
||||||
|
|
||||||
client, err := questiontable.NewClient(questiontable.Config{
|
client, err := questiontable.NewClient(questiontable.Config{
|
||||||
ClientID: clientID,
|
ClientID: clientID,
|
||||||
Secret: clientSecret,
|
Secret: clientSecret,
|
||||||
BaseID: cfg.BaseID,
|
BaseID: cfg.BaseID,
|
||||||
SheetID: cfg.SheetID,
|
SheetID: cfg.SheetID,
|
||||||
OperatorID: cfg.OperatorID,
|
OperatorID: cfg.OperatorID,
|
||||||
|
FieldMapping: cfg.FieldMapping,
|
||||||
}, nil)
|
}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/questiontable"
|
||||||
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/settings"
|
||||||
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/store/postgres"
|
||||||
|
)
|
||||||
|
|
||||||
|
type validatedSettingsRepository struct {
|
||||||
|
*postgres.SettingsRepository
|
||||||
|
clientID string
|
||||||
|
secret string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *validatedSettingsRepository) SaveBinding(ctx context.Context, binding settings.ResourceBinding) error {
|
||||||
|
switch binding.Kind {
|
||||||
|
case "question_table", "faq_table":
|
||||||
|
client, err := questiontable.NewClient(questiontable.Config{
|
||||||
|
ClientID: r.clientID, Secret: r.secret, BaseID: binding.BaseID,
|
||||||
|
SheetID: binding.SheetID, OperatorID: binding.OperatorID,
|
||||||
|
}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("table_binding_invalid_config")
|
||||||
|
}
|
||||||
|
fields, err := client.ReadFields(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
required := questiontable.QuestionSchema()
|
||||||
|
if binding.Kind == "faq_table" {
|
||||||
|
required = questiontable.FAQSchema()
|
||||||
|
}
|
||||||
|
mapping, err := questiontable.MapRequiredFields(fields, required)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
binding.FieldMapping = mapping
|
||||||
|
binding.ValidationCode = "schema_ok"
|
||||||
|
binding.Activate = true
|
||||||
|
case "support_group":
|
||||||
|
binding.ValidationCode = "pending_test_message"
|
||||||
|
binding.Activate = false
|
||||||
|
default:
|
||||||
|
return errors.New("unsupported_binding_kind")
|
||||||
|
}
|
||||||
|
return r.SettingsRepository.SaveBinding(ctx, binding)
|
||||||
|
}
|
||||||
@@ -83,5 +83,6 @@ func buildSettingsRuntime(ctx context.Context, store *postgres.Store, tenantKey
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &settingsRuntime{repository: repo, controller: settings.NewController(repo), admin: control, clientID: clientID, secret: secret}, nil
|
validatedRepo := &validatedSettingsRepository{SettingsRepository: repo, clientID: clientID, secret: secret}
|
||||||
|
return &settingsRuntime{repository: repo, controller: settings.NewController(validatedRepo), admin: control, clientID: clientID, secret: secret}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,11 +19,12 @@ const defaultBase = "https://api.dingtalk.com"
|
|||||||
|
|
||||||
// Config holds the required parameters for the DingTalk question-table client.
|
// Config holds the required parameters for the DingTalk question-table client.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
ClientID string
|
ClientID string
|
||||||
Secret string
|
Secret string
|
||||||
BaseID string
|
BaseID string
|
||||||
SheetID string
|
SheetID string
|
||||||
OperatorID string
|
OperatorID string
|
||||||
|
FieldMapping map[string]string
|
||||||
|
|
||||||
// TokenURL overrides the token endpoint. Empty → default.
|
// TokenURL overrides the token endpoint. Empty → default.
|
||||||
TokenURL string
|
TokenURL string
|
||||||
@@ -54,11 +55,12 @@ var defaultTransport = func() *http.Transport {
|
|||||||
// Client is a stdlib-only DingTalk HTTP client that inserts records into a
|
// Client is a stdlib-only DingTalk HTTP client that inserts records into a
|
||||||
// Notable v1 base. It owns a mutex-protected in-memory access-token cache.
|
// Notable v1 base. It owns a mutex-protected in-memory access-token cache.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
clientID string
|
clientID string
|
||||||
secret string
|
secret string
|
||||||
baseID string
|
baseID string
|
||||||
sheetID string
|
sheetID string
|
||||||
operatorID string
|
operatorID string
|
||||||
|
fieldMapping map[string]string
|
||||||
|
|
||||||
tokenURL string
|
tokenURL string
|
||||||
baseURL string
|
baseURL string
|
||||||
@@ -113,14 +115,15 @@ func NewClient(cfg Config, httpClient *http.Client) (*Client, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
clientID: clientID,
|
clientID: clientID,
|
||||||
secret: secret,
|
secret: secret,
|
||||||
baseID: baseID,
|
baseID: baseID,
|
||||||
sheetID: sheetID,
|
sheetID: sheetID,
|
||||||
operatorID: operatorID,
|
operatorID: operatorID,
|
||||||
tokenURL: tokenURL,
|
fieldMapping: cloneFieldMapping(cfg.FieldMapping),
|
||||||
baseURL: baseURL,
|
tokenURL: tokenURL,
|
||||||
http: httpClient,
|
baseURL: baseURL,
|
||||||
|
http: httpClient,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,6 +164,7 @@ func (c *Client) InsertRecord(ctx context.Context, row Row) (string, error) {
|
|||||||
// InsertFields sends one already-typed field map to DingTalk Notable v1 and
|
// InsertFields sends one already-typed field map to DingTalk Notable v1 and
|
||||||
// returns the record ID. It is shared by other reviewed Notable table schemas.
|
// returns the record ID. It is shared by other reviewed Notable table schemas.
|
||||||
func (c *Client) InsertFields(ctx context.Context, fields map[string]interface{}) (string, error) {
|
func (c *Client) InsertFields(ctx context.Context, fields map[string]interface{}) (string, error) {
|
||||||
|
fields = c.remapFields(fields)
|
||||||
token, err := c.getAccessToken(ctx)
|
token, err := c.getAccessToken(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("questiontable token: %w", err)
|
return "", fmt.Errorf("questiontable token: %w", err)
|
||||||
@@ -213,6 +217,37 @@ func (c *Client) InsertFields(ctx context.Context, fields map[string]interface{}
|
|||||||
return ir.Value[0].ID, nil
|
return ir.Value[0].ID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cloneFieldMapping(in map[string]string) map[string]string {
|
||||||
|
out := make(map[string]string, len(in))
|
||||||
|
for key, value := range in {
|
||||||
|
out[key] = value
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
var providerFieldLogical = map[string]string{
|
||||||
|
"ZaMZF6j": "chinese_question", "SCug9My": "asked_time", "S2XA1uZ": "asker",
|
||||||
|
"bETcL3r": "original_question", "Y4cpmxU": "answer", "JgY2f1z": "trigger_person",
|
||||||
|
"DNIV3mQ": "language", "eBc6a6b": "department", "fPNLeVW": "region",
|
||||||
|
"01ZM8y7": "standard_question", "fj3JQ1C": "original_question", "07YYagS": "language", "Eu7gkKu": "asker",
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) remapFields(in map[string]interface{}) map[string]interface{} {
|
||||||
|
if len(c.fieldMapping) == 0 {
|
||||||
|
return in
|
||||||
|
}
|
||||||
|
out := make(map[string]interface{}, len(in))
|
||||||
|
for providerID, value := range in {
|
||||||
|
logical := providerFieldLogical[providerID]
|
||||||
|
if mapped := c.fieldMapping[logical]; logical != "" && mapped != "" {
|
||||||
|
out[mapped] = value
|
||||||
|
} else {
|
||||||
|
out[providerID] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
// safeDoErr wraps an http.Do error without exposing the raw URL or credentials.
|
// safeDoErr wraps an http.Do error without exposing the raw URL or credentials.
|
||||||
// If the context is already done, returns a wrapped ctx.Err() so errors.Is works.
|
// If the context is already done, returns a wrapped ctx.Err() so errors.Is works.
|
||||||
func safeDoErr(ctx context.Context, err error) error {
|
func safeDoErr(ctx context.Context, err error) error {
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package questiontable
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestRemapFieldsUsesValidatedLogicalMapping(t *testing.T) {
|
||||||
|
client := &Client{fieldMapping: map[string]string{"answer": "new-answer", "original_question": "new-question"}}
|
||||||
|
got := client.remapFields(map[string]interface{}{FieldAnswer: "A", FieldOriginalQuestion: "Q"})
|
||||||
|
if got["new-answer"] != "A" || got["new-question"] != "Q" {
|
||||||
|
t.Fatalf("got=%#v", got)
|
||||||
|
}
|
||||||
|
if _, ok := got[FieldAnswer]; ok {
|
||||||
|
t.Fatal("legacy field id must not remain when mapping exists")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package questiontable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Field struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RequiredField struct {
|
||||||
|
Logical string
|
||||||
|
Name string
|
||||||
|
Type string
|
||||||
|
}
|
||||||
|
|
||||||
|
func QuestionSchema() []RequiredField {
|
||||||
|
return []RequiredField{
|
||||||
|
{"chinese_question", "中文问题", "text"},
|
||||||
|
{"asked_time", "提问时间", "date"},
|
||||||
|
{"asker", "提问人", "user"},
|
||||||
|
{"original_question", "原始问题", "text"},
|
||||||
|
{"answer", "AI答案", "text"},
|
||||||
|
{"trigger_person", "流程触发人员信息", "text"},
|
||||||
|
{"language", "语言", "text"},
|
||||||
|
{"department", "部门", "text"},
|
||||||
|
{"region", "区域", "text"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func FAQSchema() []RequiredField {
|
||||||
|
return []RequiredField{
|
||||||
|
{"standard_question", "标准问题(必填)", "text"},
|
||||||
|
{"original_question", "原始问题", "text"},
|
||||||
|
{"language", "语言", "text"},
|
||||||
|
{"asker", "提问人", "user"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func MapRequiredFields(fields []Field, required []RequiredField) (map[string]string, error) {
|
||||||
|
out := make(map[string]string, len(required))
|
||||||
|
for _, want := range required {
|
||||||
|
nameMatches := make([]Field, 0, 1)
|
||||||
|
for _, got := range fields {
|
||||||
|
if strings.TrimSpace(got.Name) == want.Name {
|
||||||
|
nameMatches = append(nameMatches, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(nameMatches) == 0 {
|
||||||
|
return nil, errors.New("missing_field")
|
||||||
|
}
|
||||||
|
if len(nameMatches) > 1 {
|
||||||
|
return nil, errors.New("duplicate_field")
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(nameMatches[0].Type) != want.Type {
|
||||||
|
return nil, errors.New("field_type_mismatch")
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(nameMatches[0].ID) == "" {
|
||||||
|
return nil, errors.New("missing_field_id")
|
||||||
|
}
|
||||||
|
out[want.Logical] = strings.TrimSpace(nameMatches[0].ID)
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) ReadFields(ctx context.Context) ([]Field, error) {
|
||||||
|
token, err := c.getAccessToken(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("schema_token_failed")
|
||||||
|
}
|
||||||
|
endpoint := fmt.Sprintf("%s/v1.0/notable/bases/%s/sheets/%s/fields?operatorId=%s", c.baseURL, url.PathEscape(c.baseID), url.PathEscape(c.sheetID), url.QueryEscape(c.operatorID))
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("schema_request_failed")
|
||||||
|
}
|
||||||
|
req.Header.Set("x-acs-dingtalk-access-token", token)
|
||||||
|
resp, err := c.http.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("schema_network_failed")
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
raw, err := io.ReadAll(io.LimitReader(resp.Body, 256<<10))
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("schema_read_failed")
|
||||||
|
}
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
return nil, fmt.Errorf("schema_http_%d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
var payload struct {
|
||||||
|
Value []Field `json:"value"`
|
||||||
|
}
|
||||||
|
if json.Unmarshal(raw, &payload) != nil {
|
||||||
|
return nil, errors.New("schema_decode_failed")
|
||||||
|
}
|
||||||
|
if len(payload.Value) == 0 {
|
||||||
|
return nil, errors.New("schema_empty")
|
||||||
|
}
|
||||||
|
return payload.Value, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package questiontable
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestReadFieldsUsesOperatorAndMapsQuestionSchema(t *testing.T) {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/token", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
_, _ = w.Write([]byte(`{"accessToken":"token","expireIn":7200}`))
|
||||||
|
})
|
||||||
|
mux.HandleFunc("/v1.0/notable/bases/base/sheets/sheet/fields", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Query().Get("operatorId") != "union" {
|
||||||
|
t.Fatalf("operator=%q", r.URL.Query().Get("operatorId"))
|
||||||
|
}
|
||||||
|
_, _ = w.Write([]byte(`{"value":[
|
||||||
|
{"id":"q","name":"中文问题","type":"text"},{"id":"time","name":"提问时间","type":"date"},
|
||||||
|
{"id":"asker","name":"提问人","type":"user"},{"id":"orig","name":"原始问题","type":"text"},
|
||||||
|
{"id":"answer","name":"AI答案","type":"text"},{"id":"trigger","name":"流程触发人员信息","type":"text"},
|
||||||
|
{"id":"lang","name":"语言","type":"text"},{"id":"dept","name":"部门","type":"text"},{"id":"region","name":"区域","type":"text"}]}`))
|
||||||
|
})
|
||||||
|
server := httptest.NewServer(mux)
|
||||||
|
defer server.Close()
|
||||||
|
client, err := NewClient(Config{ClientID: "id", Secret: "secret", BaseID: "base", SheetID: "sheet", OperatorID: "union", TokenURL: server.URL + "/token", BaseURL: server.URL}, server.Client())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
fields, err := client.ReadFields(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
mapping, err := MapRequiredFields(fields, QuestionSchema())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if mapping["answer"] != "answer" || len(mapping) != 9 {
|
||||||
|
t.Fatalf("mapping=%v", mapping)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMapRequiredFieldsRejectsMissingDuplicateAndWrongType(t *testing.T) {
|
||||||
|
required := []RequiredField{{Logical: "question", Name: "问题", Type: "text"}}
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
fields []Field
|
||||||
|
code string
|
||||||
|
}{
|
||||||
|
{"missing", nil, "missing_field"},
|
||||||
|
{"duplicate", []Field{{ID: "1", Name: "问题", Type: "text"}, {ID: "2", Name: "问题", Type: "text"}}, "duplicate_field"},
|
||||||
|
{"wrong type", []Field{{ID: "1", Name: "问题", Type: "user"}}, "field_type_mismatch"},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
_, err := MapRequiredFields(tc.fields, required)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), tc.code) {
|
||||||
|
t.Fatalf("err=%v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFAQSchemaUsesFourCurrentInsertFields(t *testing.T) {
|
||||||
|
required := FAQSchema()
|
||||||
|
if len(required) != 4 {
|
||||||
|
t.Fatalf("len=%d", len(required))
|
||||||
|
}
|
||||||
|
want := map[string]string{"standard_question": "text", "original_question": "text", "language": "text", "asker": "user"}
|
||||||
|
for _, field := range required {
|
||||||
|
if want[field.Logical] != field.Type {
|
||||||
|
t.Fatalf("field=%+v", field)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,9 @@ type ResourceBinding struct {
|
|||||||
Kind, TenantKey, ConversationID, ConversationTitle string
|
Kind, TenantKey, ConversationID, ConversationTitle string
|
||||||
BaseID, SheetID, ViewID, OperatorID string
|
BaseID, SheetID, ViewID, OperatorID string
|
||||||
CreatedByStaffID string
|
CreatedByStaffID string
|
||||||
|
FieldMapping map[string]string
|
||||||
|
ValidationCode string
|
||||||
|
Activate bool
|
||||||
}
|
}
|
||||||
type BootstrapCandidate struct {
|
type BootstrapCandidate struct {
|
||||||
TenantKey, Code, StaffID, UnionID, DisplayName, Department string
|
TenantKey, Code, StaffID, UnionID, DisplayName, Department string
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func (r *SettingsRepository) DeleteSettingSession(ctx context.Context, tenant, s
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
func (r *SettingsRepository) SaveBinding(ctx context.Context, b settings.ResourceBinding) error {
|
func (r *SettingsRepository) SaveBinding(ctx context.Context, b settings.ResourceBinding) error {
|
||||||
payload, err := json.Marshal(map[string]string{"conversation_id": b.ConversationID, "conversation_title": b.ConversationTitle, "base_id": b.BaseID, "sheet_id": b.SheetID, "view_id": b.ViewID, "operator_id": b.OperatorID})
|
payload, err := json.Marshal(map[string]interface{}{"conversation_id": b.ConversationID, "conversation_title": b.ConversationTitle, "base_id": b.BaseID, "sheet_id": b.SheetID, "view_id": b.ViewID, "operator_id": b.OperatorID, "field_mapping": b.FieldMapping})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -75,10 +75,20 @@ func (r *SettingsRepository) SaveBinding(ctx context.Context, b settings.Resourc
|
|||||||
if err = tx.QueryRow(ctx, `SELECT COALESCE(MAX(revision),0)+1 FROM system_resource_bindings WHERE tenant_key=$1 AND kind=$2`, b.TenantKey, b.Kind).Scan(&rev); err != nil {
|
if err = tx.QueryRow(ctx, `SELECT COALESCE(MAX(revision),0)+1 FROM system_resource_bindings WHERE tenant_key=$1 AND kind=$2`, b.TenantKey, b.Kind).Scan(&rev); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err = tx.Exec(ctx, `UPDATE system_resource_bindings SET state='retired' WHERE tenant_key=$1 AND kind=$2 AND state='active'`, b.TenantKey, b.Kind); err != nil {
|
state := "candidate"
|
||||||
return err
|
var validatedAt, activatedAt interface{}
|
||||||
|
if b.Activate {
|
||||||
|
state = "active"
|
||||||
|
validatedAt, activatedAt = time.Now(), time.Now()
|
||||||
|
if _, err = tx.Exec(ctx, `UPDATE system_resource_bindings SET state='retired' WHERE tenant_key=$1 AND kind=$2 AND state='active'`, b.TenantKey, b.Kind); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_, err = tx.Exec(ctx, `INSERT INTO system_resource_bindings(id,tenant_key,kind,revision,state,public_config,validation_code,validated_at,activated_at,created_by_staff_id) VALUES($1,$2,$3,$4,'active',$5,'pending_external_probe',NOW(),NOW(),$6)`, newSettingsID(), b.TenantKey, b.Kind, rev, payload, b.CreatedByStaffID)
|
validationCode := strings.TrimSpace(b.ValidationCode)
|
||||||
|
if validationCode == "" {
|
||||||
|
validationCode = "pending_external_probe"
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(ctx, `INSERT INTO system_resource_bindings(id,tenant_key,kind,revision,state,public_config,validation_code,validated_at,activated_at,created_by_staff_id) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, newSettingsID(), b.TenantKey, b.Kind, rev, state, payload, validationCode, validatedAt, activatedAt, b.CreatedByStaffID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -208,11 +218,19 @@ func (r *SettingsRepository) ActiveBindings(ctx context.Context, tenant string)
|
|||||||
if err = rows.Scan(&kind, &raw); err != nil {
|
if err = rows.Scan(&kind, &raw); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var v map[string]string
|
var v struct {
|
||||||
|
ConversationID string `json:"conversation_id"`
|
||||||
|
ConversationTitle string `json:"conversation_title"`
|
||||||
|
BaseID string `json:"base_id"`
|
||||||
|
SheetID string `json:"sheet_id"`
|
||||||
|
ViewID string `json:"view_id"`
|
||||||
|
OperatorID string `json:"operator_id"`
|
||||||
|
FieldMapping map[string]string `json:"field_mapping"`
|
||||||
|
}
|
||||||
if json.Unmarshal(raw, &v) != nil {
|
if json.Unmarshal(raw, &v) != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
out[kind] = settings.ResourceBinding{Kind: kind, TenantKey: tenant, ConversationID: v["conversation_id"], ConversationTitle: v["conversation_title"], BaseID: v["base_id"], SheetID: v["sheet_id"], ViewID: v["view_id"], OperatorID: v["operator_id"]}
|
out[kind] = settings.ResourceBinding{Kind: kind, TenantKey: tenant, ConversationID: v.ConversationID, ConversationTitle: v.ConversationTitle, BaseID: v.BaseID, SheetID: v.SheetID, ViewID: v.ViewID, OperatorID: v.OperatorID, FieldMapping: v.FieldMapping, Activate: true}
|
||||||
}
|
}
|
||||||
return out, rows.Err()
|
return out, rows.Err()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user