iachat/spec/enterprise
Tanmay Deep Sharma 0592cccca9
fix: prevent lost custom_attributes updates from concurrent jsonb writes (#14040)
## Linear ticket
https://linear.app/chatwoot/issue/CW-6834/billing-upgrade-didnt-work

## Description
A `customer.subscription.updated` Stripe webhook for account 76162
returned 200 OK but did not persist the new `subscribed_quantity`. Root
cause: a race condition between the webhook handler and
`increment_response_usage` (Captain usage counter), both doing
read-modify-write on the `custom_attributes` JSONB column. The webhook
wrote `quantity: 6`, then a concurrent `save` from
`increment_response_usage` overwrote the entire hash with stale data —
restoring `quantity: 5`.

Fix: use atomic `jsonb_set` so usage counter updates only touch the
single key they care about, instead of rewriting the whole
`custom_attributes` hash. `increment_custom_attribute` also performs the
increment in SQL, making concurrent increments correct as well.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## How Has This Been Tested?

- New regression spec in `handle_stripe_event_service_spec.rb` that
simulates concurrent webhook + `increment_response_usage` and asserts
both `subscribed_quantity` and `captain_responses_usage` survive
- Existing account, billing, captain, and topup specs all pass locally

## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules
2026-04-13 19:03:37 +07:00
..
builders fix: restrict existing user sign-in to account members (#13793) 2026-03-13 12:22:25 +05:30
controllers feat: captain custom tools v1 (#13890) 2026-04-02 12:40:11 +05:30
drops feat: Add push/email notification support for SLA (#9140) 2024-03-29 20:27:21 +11:00
jobs fix(captain): display handoff message to customer in V2 flow (#13885) 2026-04-08 17:30:07 +05:30
lib fix: log only on system api key failures (#13968) 2026-04-09 18:04:52 +05:30
listeners feat: captain decides if conversation should be resolved or kept open (#13336) 2026-03-13 10:03:58 +05:30
mailers feat: setup invite to handle SAML enabled account [CW-5613] (#12439) 2025-09-17 19:33:38 +05:30
models feat: allow zero conversation limit capacity policy (#13964) 2026-04-06 11:39:14 +05:30
policies chore: Enforce custom role permissions on conversation access (#12583) 2025-10-22 20:23:37 -07:00
presenters/conversations feat: Conversation API to return applied_sla and sla_events (#9174) 2024-04-01 23:30:07 +05:30
services fix: prevent lost custom_attributes updates from concurrent jsonb writes (#14040) 2026-04-13 19:03:37 +07:00