feat: invalidate cache after inbox members or team members update (#10869)

At the moment, when updating the inbox members, or team members the
account cache used for IndexedDB is not invalidated. This can cause
inconsistencies in the UI. This PR fixes this by adding explicit
invalidation after performing the member changes

### Summary of changes

1. Added a new method `add_members` and `remove_members` to both `team`
and `inbox` models. The change was necessary for two reasons
- Since the individual `add_member` and `remove_member` is called in a
loop, it's wasteful to run the cache invalidation in the method.
- Moving the account cache invalidation call in the controller pollutes
the controller business logic
2. Updated tests across the board

### More improvements

We can make a concern called `Memberable` with usage like
`memberable_with :inbox_members`, that can encapsulate the functionality

---

Related: https://github.com/chatwoot/chatwoot/issues/10578
This commit is contained in:
Shivam Mishra
2025-02-21 10:58:38 +05:30
committed by GitHub
parent 27f7e0921e
commit c88447c11f
7 changed files with 124 additions and 36 deletions

View File

@@ -41,29 +41,50 @@ RSpec.describe Inbox do
it_behaves_like 'avatarable'
end
describe '#add_member' do
describe '#add_members' do
let(:inbox) { FactoryBot.create(:inbox) }
let(:user) { FactoryBot.create(:user) }
it do
expect(inbox.inbox_members.size).to eq(0)
before do
allow(Rails.configuration.dispatcher).to receive(:dispatch)
end
inbox.add_member(user.id)
expect(inbox.reload.inbox_members.size).to eq(1)
it 'handles adds all members and resets cache keys' do
users = FactoryBot.create_list(:user, 3)
inbox.add_members(users.map(&:id))
expect(inbox.reload.inbox_members.size).to eq(3)
expect(Rails.configuration.dispatcher).to have_received(:dispatch).at_least(:once)
.with(
'account.cache_invalidated',
kind_of(Time),
account: inbox.account,
cache_keys: inbox.account.cache_keys
)
end
end
describe '#remove_member' do
describe '#remove_members' do
let(:inbox) { FactoryBot.create(:inbox) }
let(:user) { FactoryBot.create(:user) }
let(:users) { FactoryBot.create_list(:user, 3) }
before { inbox.add_member(user.id) }
before do
inbox.add_members(users.map(&:id))
allow(Rails.configuration.dispatcher).to receive(:dispatch)
end
it do
expect(inbox.inbox_members.size).to eq(1)
it 'removes the members and resets cache keys' do
expect(inbox.reload.inbox_members.size).to eq(3)
inbox.remove_member(user.id)
inbox.remove_members(users.map(&:id))
expect(inbox.reload.inbox_members.size).to eq(0)
expect(Rails.configuration.dispatcher).to have_received(:dispatch).at_least(:once)
.with(
'account.cache_invalidated',
kind_of(Time),
account: inbox.account,
cache_keys: inbox.account.cache_keys
)
end
end