completed redis lock release functions
This commit is contained in:
@@ -12,12 +12,13 @@
|
||||
|
||||
params:
|
||||
cache_key: string
|
||||
sorted_entitlements: array of entitlement objects
|
||||
customer_entitlement_ids: array of customer_entitlement IDs
|
||||
full_customer: decoded FullCustomer object
|
||||
|
||||
Returns: context table with:
|
||||
customer_entitlements: { [cus_ent_id]: { base_path, balance, adjustment, entities } }
|
||||
rollovers: { [rollover_id]: { base_path, balance, usage, entities } }
|
||||
rollovers: { [rollover_id]: { base_path, cus_ent_id, balance, usage, entities } }
|
||||
mutation_logs: {} (ordered mutation items for receipts and replay)
|
||||
pending_writes: {} (empty array to queue writes)
|
||||
logs: {} (debug logs)
|
||||
logger: { log(fmt, ...): function } (logger that appends to logs)
|
||||
@@ -28,6 +29,7 @@ local function init_context(params)
|
||||
local context = {
|
||||
customer_entitlements = {},
|
||||
rollovers = {},
|
||||
mutation_logs = {},
|
||||
pending_writes = {},
|
||||
logs = logs,
|
||||
logger = {
|
||||
@@ -37,8 +39,7 @@ local function init_context(params)
|
||||
},
|
||||
}
|
||||
|
||||
for _, ent_obj in ipairs(params.sorted_entitlements) do
|
||||
local ent_id = ent_obj.customer_entitlement_id
|
||||
for _, ent_id in ipairs(params.customer_entitlement_ids or {}) do
|
||||
local cus_ent, cus_product, ce_idx, cp_idx = find_entitlement(params.full_customer, ent_id)
|
||||
|
||||
if cus_ent then
|
||||
@@ -56,7 +57,9 @@ local function init_context(params)
|
||||
base_path = '$.customer_products[' .. cp_idx_0 .. '].customer_entitlements[' .. ce_idx_0 .. ']'
|
||||
end
|
||||
|
||||
local has_entity_scope = ent_obj.entity_feature_id ~= nil and ent_obj.entity_feature_id ~= cjson.null
|
||||
local entitlement = cus_ent.entitlement
|
||||
local has_entity_scope = not is_nil(entitlement)
|
||||
and not is_nil(entitlement.entity_feature_id)
|
||||
|
||||
local ent_data = {
|
||||
base_path = base_path,
|
||||
@@ -90,6 +93,7 @@ local function init_context(params)
|
||||
if rollover_data then
|
||||
context.rollovers[rollover.id] = {
|
||||
base_path = rollover_path,
|
||||
cus_ent_id = ent_id,
|
||||
balance = rollover_data.balance,
|
||||
usage = rollover_data.usage,
|
||||
entities = rollover_data.entities,
|
||||
@@ -105,111 +109,191 @@ local function init_context(params)
|
||||
end
|
||||
|
||||
--[[
|
||||
update_in_memory_customer_entitlement(params)
|
||||
|
||||
Updates balance (and optionally adjustment) in-memory on the context object.
|
||||
|
||||
params:
|
||||
target: table (ent_data or entities table)
|
||||
entity_id: string or nil (if entity-scoped)
|
||||
delta: number (the change amount, negative = deduction)
|
||||
alter_granted_balance: boolean (if true, also update adjustment)
|
||||
append_mutation_log(params)
|
||||
|
||||
Appends one ordered mutation log entry for later receipt persistence and replay.
|
||||
]]
|
||||
local function update_in_memory_customer_entitlement(params)
|
||||
local function append_mutation_log(params)
|
||||
local context = params.context
|
||||
table.insert(context.mutation_logs, {
|
||||
target_type = params.target_type,
|
||||
customer_entitlement_id = params.customer_entitlement_id or cjson.null,
|
||||
rollover_id = params.rollover_id or cjson.null,
|
||||
entity_id = params.entity_id or cjson.null,
|
||||
credit_cost = params.credit_cost or 1,
|
||||
balance_delta = params.balance_delta or 0,
|
||||
adjustment_delta = params.adjustment_delta or 0,
|
||||
usage_delta = params.usage_delta or 0,
|
||||
value_delta = params.value_delta or 0,
|
||||
})
|
||||
end
|
||||
|
||||
--[[
|
||||
update_in_memory_customer_entitlement_mutation(params)
|
||||
|
||||
Applies an arbitrary balance/adjustment mutation to an in-memory
|
||||
customer_entitlement target.
|
||||
]]
|
||||
local function update_in_memory_customer_entitlement_mutation(params)
|
||||
local target = params.target
|
||||
local entity_id = params.entity_id
|
||||
local delta = params.delta
|
||||
local alter_granted_balance = params.alter_granted_balance
|
||||
local balance_delta = params.balance_delta
|
||||
local adjustment_delta = params.adjustment_delta
|
||||
|
||||
if balance_delta == nil then
|
||||
balance_delta = params.delta or 0
|
||||
end
|
||||
|
||||
if adjustment_delta == nil then
|
||||
adjustment_delta = params.alter_granted_balance and balance_delta or 0
|
||||
end
|
||||
|
||||
if entity_id then
|
||||
if not target[entity_id] then
|
||||
target[entity_id] = { balance = 0, adjustment = 0 }
|
||||
end
|
||||
target[entity_id].balance = (target[entity_id].balance or 0) + delta
|
||||
if alter_granted_balance then
|
||||
target[entity_id].adjustment = (target[entity_id].adjustment or 0) + delta
|
||||
end
|
||||
else
|
||||
target.balance = (target.balance or 0) + delta
|
||||
if alter_granted_balance then
|
||||
target.adjustment = (target.adjustment or 0) + delta
|
||||
end
|
||||
target[entity_id].balance = (target[entity_id].balance or 0) + balance_delta
|
||||
target[entity_id].adjustment = (target[entity_id].adjustment or 0) + adjustment_delta
|
||||
return
|
||||
end
|
||||
|
||||
target.balance = (target.balance or 0) + balance_delta
|
||||
target.adjustment = (target.adjustment or 0) + adjustment_delta
|
||||
end
|
||||
|
||||
--[[
|
||||
queue_balance_update(params)
|
||||
|
||||
Queues a balance update to pending_writes and logs the action.
|
||||
|
||||
params:
|
||||
context: table (context object)
|
||||
path: string (JSON path to the balance field, WITHOUT .balance suffix)
|
||||
delta: number (the change amount, negative = subtract from balance)
|
||||
alter_granted_balance: boolean (if true, also queue adjustment update)
|
||||
]]
|
||||
local function queue_balance_update(params)
|
||||
local context = params.context
|
||||
local path = params.path
|
||||
local delta = params.delta
|
||||
|
||||
-- Queue balance write
|
||||
table.insert(context.pending_writes, { path = path .. '.balance', delta = delta })
|
||||
|
||||
-- Queue adjustment write if needed
|
||||
if params.alter_granted_balance then
|
||||
table.insert(context.pending_writes, { path = path .. '.adjustment', delta = delta })
|
||||
end
|
||||
end
|
||||
update_in_memory_rollover_mutation(params)
|
||||
|
||||
--[[
|
||||
queue_rollover_update(params)
|
||||
|
||||
Queues a rollover balance/usage update to pending_writes.
|
||||
Rollovers track both balance (decrements) and usage (increments).
|
||||
|
||||
params:
|
||||
context: table (context object)
|
||||
path: string (JSON path to the rollover or entity, WITHOUT .balance/.usage suffix)
|
||||
deduct_amount: number (positive amount to deduct from balance and add to usage)
|
||||
Applies an arbitrary balance/usage mutation to an in-memory rollover target.
|
||||
]]
|
||||
local function queue_rollover_update(params)
|
||||
local context = params.context
|
||||
local path = params.path
|
||||
local deduct_amount = params.deduct_amount
|
||||
|
||||
-- Queue balance decrement
|
||||
table.insert(context.pending_writes, { path = path .. '.balance', delta = -deduct_amount })
|
||||
|
||||
-- Queue usage increment
|
||||
table.insert(context.pending_writes, { path = path .. '.usage', delta = deduct_amount })
|
||||
end
|
||||
|
||||
--[[
|
||||
update_in_memory_rollover(params)
|
||||
|
||||
Updates balance and usage in-memory on a rollover object.
|
||||
|
||||
params:
|
||||
target: table (rollover_data or entities table)
|
||||
entity_id: string or nil (if entity-scoped)
|
||||
deduct_amount: number (positive amount to deduct from balance and add to usage)
|
||||
]]
|
||||
local function update_in_memory_rollover(params)
|
||||
local function update_in_memory_rollover_mutation(params)
|
||||
local target = params.target
|
||||
local entity_id = params.entity_id
|
||||
local deduct_amount = params.deduct_amount
|
||||
local balance_delta = params.balance_delta or 0
|
||||
local usage_delta = params.usage_delta or 0
|
||||
|
||||
if entity_id then
|
||||
if not target[entity_id] then
|
||||
target[entity_id] = { balance = 0, usage = 0 }
|
||||
end
|
||||
target[entity_id].balance = (target[entity_id].balance or 0) - deduct_amount
|
||||
target[entity_id].usage = (target[entity_id].usage or 0) + deduct_amount
|
||||
else
|
||||
target.balance = (target.balance or 0) - deduct_amount
|
||||
target.usage = (target.usage or 0) + deduct_amount
|
||||
target[entity_id].balance = (target[entity_id].balance or 0) + balance_delta
|
||||
target[entity_id].usage = (target[entity_id].usage or 0) + usage_delta
|
||||
return
|
||||
end
|
||||
|
||||
target.balance = (target.balance or 0) + balance_delta
|
||||
target.usage = (target.usage or 0) + usage_delta
|
||||
end
|
||||
|
||||
--[[
|
||||
queue_customer_entitlement_mutation(params)
|
||||
|
||||
Queues a generic customer_entitlement mutation into pending_writes and
|
||||
mutation_logs.
|
||||
]]
|
||||
local function queue_customer_entitlement_mutation(params)
|
||||
local context = params.context
|
||||
local path = params.path
|
||||
local balance_delta = params.balance_delta
|
||||
local adjustment_delta = params.adjustment_delta
|
||||
|
||||
if balance_delta == nil then
|
||||
balance_delta = params.delta or 0
|
||||
end
|
||||
|
||||
if adjustment_delta == nil then
|
||||
adjustment_delta = params.alter_granted_balance and balance_delta or 0
|
||||
end
|
||||
|
||||
if balance_delta ~= 0 then
|
||||
table.insert(context.pending_writes, { path = path .. '.balance', delta = balance_delta })
|
||||
end
|
||||
|
||||
if adjustment_delta ~= 0 then
|
||||
table.insert(context.pending_writes, { path = path .. '.adjustment', delta = adjustment_delta })
|
||||
end
|
||||
|
||||
append_mutation_log({
|
||||
context = context,
|
||||
target_type = 'customer_entitlement',
|
||||
customer_entitlement_id = params.customer_entitlement_id,
|
||||
rollover_id = nil,
|
||||
entity_id = params.entity_id,
|
||||
credit_cost = params.credit_cost or 1,
|
||||
balance_delta = balance_delta,
|
||||
adjustment_delta = adjustment_delta,
|
||||
usage_delta = 0,
|
||||
value_delta = params.value_delta or 0,
|
||||
})
|
||||
end
|
||||
|
||||
--[[
|
||||
queue_rollover_mutation(params)
|
||||
|
||||
Queues a generic rollover mutation into pending_writes and mutation_logs.
|
||||
]]
|
||||
local function queue_rollover_mutation(params)
|
||||
local context = params.context
|
||||
local path = params.path
|
||||
local balance_delta = params.balance_delta or 0
|
||||
local usage_delta = params.usage_delta or 0
|
||||
local rollover_id = params.rollover_id
|
||||
|
||||
if balance_delta ~= 0 then
|
||||
table.insert(context.pending_writes, { path = path .. '.balance', delta = balance_delta })
|
||||
end
|
||||
|
||||
if usage_delta ~= 0 then
|
||||
table.insert(context.pending_writes, { path = path .. '.usage', delta = usage_delta })
|
||||
end
|
||||
|
||||
local rollover_data = context.rollovers[rollover_id]
|
||||
append_mutation_log({
|
||||
context = context,
|
||||
target_type = 'rollover',
|
||||
customer_entitlement_id = rollover_data and rollover_data.cus_ent_id or nil,
|
||||
rollover_id = rollover_id,
|
||||
entity_id = params.entity_id,
|
||||
credit_cost = params.credit_cost or 1,
|
||||
balance_delta = balance_delta,
|
||||
adjustment_delta = 0,
|
||||
usage_delta = usage_delta,
|
||||
value_delta = params.value_delta or 0,
|
||||
})
|
||||
end
|
||||
|
||||
--[[
|
||||
queue_rollover_update(params)
|
||||
|
||||
Queues a rollover balance/usage update to pending_writes.
|
||||
Rollovers track both balance (decrements) and usage (increments).
|
||||
]]
|
||||
local function queue_rollover_update(params)
|
||||
local deduct_amount = params.deduct_amount
|
||||
|
||||
queue_rollover_mutation({
|
||||
context = params.context,
|
||||
path = params.path,
|
||||
rollover_id = params.rollover_id,
|
||||
entity_id = params.entity_id,
|
||||
balance_delta = -deduct_amount,
|
||||
usage_delta = deduct_amount,
|
||||
value_delta = params.value_delta or 0,
|
||||
})
|
||||
end
|
||||
|
||||
--[[
|
||||
update_in_memory_rollover(params)
|
||||
|
||||
Backwards-compatible wrapper for main deduction paths.
|
||||
]]
|
||||
local function update_in_memory_rollover(params)
|
||||
update_in_memory_rollover_mutation({
|
||||
target = params.target,
|
||||
entity_id = params.entity_id,
|
||||
balance_delta = -(params.deduct_amount or 0),
|
||||
usage_delta = params.deduct_amount or 0,
|
||||
})
|
||||
end
|
||||
|
||||
--[[
|
||||
|
||||
Reference in New Issue
Block a user