From e5be9ee7800125dc3964e736ecb5dc6ff9cf8b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Thu, 13 Aug 2026 14:49:44 +0800 Subject: [PATCH] fix: stop enqueueing disabled external table exports --- cmd/dobotai/main.go | 1 + internal/store/postgres/coverage.go | 13 +++++++++--- .../store/postgres/coverage_contract_test.go | 9 +++++++++ .../store/postgres/outbox_contract_test.go | 9 ++++++--- internal/store/postgres/store.go | 20 +++++++++++++++---- 5 files changed, 42 insertions(+), 10 deletions(-) diff --git a/cmd/dobotai/main.go b/cmd/dobotai/main.go index 713a97b..6728d8f 100644 --- a/cmd/dobotai/main.go +++ b/cmd/dobotai/main.go @@ -191,6 +191,7 @@ func main() { if binding, ok := activeBindings["question_table"]; ok { qtCfg = questionTableConfig{BaseID: binding.BaseID, SheetID: binding.SheetID, OperatorID: binding.OperatorID, FieldMapping: binding.FieldMapping} } + store.SetExternalTableExports(qtCfg.BaseID != "", faqCfg.BaseID != "") var questionTableDone chan struct{} if qtCfg.BaseID != "" { qtRepo, err := postgres.NewQuestionTableExportRepository(store) diff --git a/internal/store/postgres/coverage.go b/internal/store/postgres/coverage.go index d004fe2..4fcc3d7 100644 --- a/internal/store/postgres/coverage.go +++ b/internal/store/postgres/coverage.go @@ -49,10 +49,10 @@ func (s *Store) SaveCoverageDecision(ctx context.Context, d coverage.Decision) e return fmt.Errorf("coverage case id: %w", err) } _, err = tx.Exec(ctx, `INSERT INTO support_cases - (id, tenant_key, channel, turn_id, coverage_decision_id, status) - VALUES($1,$2,$3,$4,$5,$6) + (id, tenant_key, channel, turn_id, coverage_decision_id, status, faq_export_status) + VALUES($1,$2,$3,$4,$5,$6,$7) ON CONFLICT (tenant_key, channel, turn_id) DO NOTHING`, - caseID, d.TenantKey, d.Channel, d.TurnID, persistedDecisionID, "pending") + caseID, d.TenantKey, d.Channel, d.TurnID, persistedDecisionID, "pending", faqExportStatus(s.faqExportEnabled)) if err != nil { return fmt.Errorf("coverage insert support case: %w", err) } @@ -64,6 +64,13 @@ func (s *Store) SaveCoverageDecision(ctx context.Context, d coverage.Decision) e return nil } +func faqExportStatus(enabled bool) string { + if enabled { + return "pending" + } + return "not_applicable" +} + // newUUID produces a 32-char hex id. func newUUID() (string, error) { var b [16]byte diff --git a/internal/store/postgres/coverage_contract_test.go b/internal/store/postgres/coverage_contract_test.go index 87a1b9d..0b53bfa 100644 --- a/internal/store/postgres/coverage_contract_test.go +++ b/internal/store/postgres/coverage_contract_test.go @@ -127,6 +127,15 @@ func TestCoverageRepoInsertsSupportCaseOnlyForUncovered(t *testing.T) { } } +func TestCoverageRepoFAQExportStatusFollowsRuntimeFlag(t *testing.T) { + if got := faqExportStatus(false); got != "not_applicable" { + t.Fatalf("disabled status=%q", got) + } + if got := faqExportStatus(true); got != "pending" { + t.Fatalf("enabled status=%q", got) + } +} + func TestCoverageRepoUsesSingleTransaction(t *testing.T) { src := readCoverageSource(t) // Must use BeginTx diff --git a/internal/store/postgres/outbox_contract_test.go b/internal/store/postgres/outbox_contract_test.go index ca7c144..1671911 100644 --- a/internal/store/postgres/outbox_contract_test.go +++ b/internal/store/postgres/outbox_contract_test.go @@ -59,9 +59,12 @@ func TestShouldEnqueueQuestionTableExportOnlyForDingTalkSucceededAnswers(t *test for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - if got := shouldEnqueueQuestionTableExport(tc.turn); got != tc.want { + if got := shouldEnqueueQuestionTableExport(tc.turn, true); got != tc.want { t.Fatalf("shouldEnqueueQuestionTableExport()=%v, want %v", got, tc.want) } + if got := shouldEnqueueQuestionTableExport(tc.turn, false); got { + t.Fatal("disabled question-table export must not enqueue") + } }) } } @@ -188,8 +191,8 @@ func TestFinishTurnOutboxGuardedByShouldCreateAnswerMessage(t *testing.T) { if !strings.Contains(guardBlock, "question_table_exports") { t.Error("question_table_exports INSERT must be inside the shouldCreateAnswerMessage guard block") } - if !strings.Contains(guardBlock, "if shouldEnqueueQuestionTableExport(turn) {") { - t.Error("question_table_exports INSERT must be guarded to DingTalk turns explicitly") + if !strings.Contains(guardBlock, "if shouldEnqueueQuestionTableExport(turn, s.questionTableExportEnabled) {") { + t.Error("question_table_exports INSERT must require both runtime enablement and a DingTalk turn") } // The guard block must be the one that contains the messages INSERT diff --git a/internal/store/postgres/store.go b/internal/store/postgres/store.go index dd4b07d..06b3e32 100644 --- a/internal/store/postgres/store.go +++ b/internal/store/postgres/store.go @@ -13,7 +13,19 @@ import ( "gitea.zheromiku.top/Weknora/dobotai-platform/internal/core" ) -type Store struct{ pool *pgxpool.Pool } +type Store struct { + pool *pgxpool.Pool + questionTableExportEnabled bool + faqExportEnabled bool +} + +func (s *Store) SetExternalTableExports(questionEnabled, faqEnabled bool) { + if s == nil { + return + } + s.questionTableExportEnabled = questionEnabled + s.faqExportEnabled = faqEnabled +} func Open(ctx context.Context, databaseURL string) (*Store, error) { if databaseURL == "" { @@ -213,7 +225,7 @@ func (s *Store) FinishTurn(ctx context.Context, turn core.Turn) error { } answerMessageID = turn.AnswerMessageID - if shouldEnqueueQuestionTableExport(turn) { + if shouldEnqueueQuestionTableExport(turn, s.questionTableExportEnabled) { exportID, err := newUUID() if err != nil { return err @@ -366,8 +378,8 @@ func shouldCreateAnswerMessage(turn core.Turn) bool { strings.TrimSpace(turn.AnswerMessageID) != "" } -func shouldEnqueueQuestionTableExport(turn core.Turn) bool { - return shouldCreateAnswerMessage(turn) && strings.EqualFold(strings.TrimSpace(turn.Channel), "dingtalk") +func shouldEnqueueQuestionTableExport(turn core.Turn, enabled bool) bool { + return enabled && shouldCreateAnswerMessage(turn) && strings.EqualFold(strings.TrimSpace(turn.Channel), "dingtalk") } func validateDeliveryUpdate(update core.DeliveryUpdate) error {