refactor: separate subscriptions from customer cache

- Split subscription data into separate cache keys for better performance
- Updated cache utilities to handle subscriptions independently
- Modified Lua scripts to manage separate subscription cache entries
- Updated API models and expand logic for separated subscriptions
- Fixed test utilities to match new data structure

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
John Yeo
2025-11-26 10:28:02 +00:00
parent 736dbccd56
commit a005f6d879
30 changed files with 252 additions and 86 deletions

View File

@@ -73,6 +73,27 @@ local function getCustomerObject(orgId, env, customerId, skipEntityMerge)
-- Merge subscriptions by plan ID and normalized status
baseCustomer.subscriptions = mergeSubscriptions(allSubscriptions)
-- Collect all scheduled_subscriptions: start with customer's scheduled_subscriptions, then add all entity scheduled_subscriptions
local allScheduledSubscriptions = {}
if baseCustomer.scheduled_subscriptions then
for _, subscription in ipairs(baseCustomer.scheduled_subscriptions) do
table.insert(allScheduledSubscriptions, subscription)
end
end
-- Add scheduled_subscriptions from each entity
for _, entityId in ipairs(entityIds) do
local entityBase = entityBaseData[entityId]
if entityBase and entityBase.scheduled_subscriptions then
for _, subscription in ipairs(entityBase.scheduled_subscriptions) do
table.insert(allScheduledSubscriptions, subscription)
end
end
end
-- Merge scheduled_subscriptions by plan ID and normalized status
baseCustomer.scheduled_subscriptions = mergeSubscriptions(allScheduledSubscriptions)
-- Merge invoices
-- Build final customer object
@@ -135,21 +156,26 @@ local function getEntityObject(orgId, env, customerId, entityId, skipCustomerMer
-- Get entity subscriptions (start with entity's own subscriptions)
local entitySubscriptions = baseEntity.subscriptions or {}
local entityScheduledSubscriptions = baseEntity.scheduled_subscriptions or {}
if not skipCustomerMerge then
-- Get customer subscriptions
-- Get customer subscriptions and scheduled_subscriptions
local customerSubscriptions = nil
local customerScheduledSubscriptions = nil
local customerBaseJson = redis.call("GET", customerCacheKey)
if customerBaseJson then
local customerBase = cjson.decode(customerBaseJson)
customerSubscriptions = customerBase.subscriptions
customerScheduledSubscriptions = customerBase.scheduled_subscriptions
end
-- Merge customer subscriptions into entity subscriptions (only add if not exists)
baseEntity.subscriptions = mergeCustomerSubscriptionsIntoEntity(entitySubscriptions, customerSubscriptions)
baseEntity.scheduled_subscriptions = mergeCustomerSubscriptionsIntoEntity(entityScheduledSubscriptions, customerScheduledSubscriptions)
else
-- No merging - just use entity's own subscriptions
baseEntity.subscriptions = entitySubscriptions
baseEntity.scheduled_subscriptions = entityScheduledSubscriptions
end
-- Build final entity object