fix: stop enqueueing disabled external table exports
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user