feat(search): reindex contact conversations
This commit is contained in:
@@ -63,6 +63,24 @@ func (s *ContactService) SetWorkerPool(wp *worker.WorkerPool) {
|
||||
func (s *ContactService) indexContact(ctx context.Context, contact *model.Contact) {
|
||||
if s.searchIndexer != nil {
|
||||
logSearchIndexError("contact", contact.ID, s.searchIndexer.IndexContact(ctx, contact))
|
||||
s.indexContactConversations(ctx, contact)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ContactService) indexContactConversations(ctx context.Context, contact *model.Contact) {
|
||||
if s == nil || s.repo == nil || s.searchIndexer == nil || contact == nil || contact.ID == 0 {
|
||||
return
|
||||
}
|
||||
var conversations []model.Conversation
|
||||
if err := s.repo.DB().WithContext(ctx).
|
||||
Preload("Contact").
|
||||
Where("account_id = ? AND contact_id = ?", contact.AccountID, contact.ID).
|
||||
Find(&conversations).Error; err != nil {
|
||||
applogger.L().Warnf("search index sync failed for contact conversations %d: %v", contact.ID, err)
|
||||
return
|
||||
}
|
||||
for i := range conversations {
|
||||
logSearchIndexError("conversation", conversations[i].ID, s.searchIndexer.IndexConversation(ctx, &conversations[i]))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,12 +12,20 @@ import (
|
||||
)
|
||||
|
||||
type mockServiceSearchIndexer struct {
|
||||
indexed []string
|
||||
deleted []string
|
||||
indexed []string
|
||||
deleted []string
|
||||
indexedConversationIDs []uint
|
||||
indexedConversationNames []string
|
||||
}
|
||||
|
||||
func (m *mockServiceSearchIndexer) IndexConversation(ctx context.Context, conversation *model.Conversation) error {
|
||||
m.indexed = append(m.indexed, "conversation")
|
||||
if conversation != nil {
|
||||
m.indexedConversationIDs = append(m.indexedConversationIDs, conversation.ID)
|
||||
if conversation.Contact != nil {
|
||||
m.indexedConversationNames = append(m.indexedConversationNames, conversation.Contact.Name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -124,6 +132,29 @@ func TestContactService_SearchIndexHooks(t *testing.T) {
|
||||
assert.Equal(t, []string{"contact"}, indexer.deleted)
|
||||
}
|
||||
|
||||
func TestContactService_SearchIndexHooksReindexContactConversations(t *testing.T) {
|
||||
db := setupServiceTestDB(t)
|
||||
repo := NewContactService(repository.NewContactRepo(db), nil, nil)
|
||||
indexer := &mockServiceSearchIndexer{}
|
||||
repo.SetSearchIndexer(indexer)
|
||||
|
||||
account := createTestAccount(t, db)
|
||||
contact, err := repo.Create(context.Background(), account.ID, CreateContactRequest{Name: "Ada", Email: "ada@example.com"})
|
||||
require.NoError(t, err)
|
||||
inbox := createTestInbox(t, db, account.ID, "web_widget")
|
||||
conversation := createTestConversation(t, db, account.ID, inbox.ID, contact.ID)
|
||||
indexer.indexed = nil
|
||||
indexer.indexedConversationIDs = nil
|
||||
indexer.indexedConversationNames = nil
|
||||
|
||||
_, err = repo.Update(context.Background(), account.ID, contact.ID, UpdateContactRequest{Name: "Ada Lovelace"})
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []string{"contact", "conversation"}, indexer.indexed)
|
||||
assert.Equal(t, []uint{conversation.ID}, indexer.indexedConversationIDs)
|
||||
assert.Equal(t, []string{"Ada Lovelace"}, indexer.indexedConversationNames)
|
||||
}
|
||||
|
||||
func TestCompanyService_SearchIndexHooks(t *testing.T) {
|
||||
db, _, _, _, svc := setupCompanyServiceTest(t)
|
||||
indexer := &mockServiceSearchIndexer{}
|
||||
|
||||
Reference in New Issue
Block a user