From 15afa970458bd29a0421e05819772bbda1cd196f Mon Sep 17 00:00:00 2001 From: amianthus <49116958+SirTenzin@users.noreply.github.com> Date: Mon, 12 Jan 2026 12:07:40 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20lua=20for=20loose=20ents?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../contextUtils.lua | 18 ++++++++++-- .../luaUtils.lua | 29 ++++++++++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/server/src/_luaScriptsV2/deductFromCustomerEntitlements/contextUtils.lua b/server/src/_luaScriptsV2/deductFromCustomerEntitlements/contextUtils.lua index bc532a292..5d83d347c 100644 --- a/server/src/_luaScriptsV2/deductFromCustomerEntitlements/contextUtils.lua +++ b/server/src/_luaScriptsV2/deductFromCustomerEntitlements/contextUtils.lua @@ -42,9 +42,20 @@ local function init_context(params) local cus_ent, cus_product, ce_idx, cp_idx = find_entitlement(params.full_customer, ent_id) if cus_ent then - local cp_idx_0 = cp_idx - 1 - local ce_idx_0 = ce_idx - 1 - local base_path = '$.customer_products[' .. cp_idx_0 .. '].customer_entitlements[' .. ce_idx_0 .. ']' + local base_path + local is_loose = (cp_idx == nil) -- Loose entitlement if no customer_product index + + if is_loose then + -- Loose entitlement: path is $.extra_customer_entitlements[idx] + local ece_idx_0 = ce_idx - 1 + base_path = '$.extra_customer_entitlements[' .. ece_idx_0 .. ']' + else + -- Product entitlement: path is $.customer_products[cp_idx].customer_entitlements[ce_idx] + local cp_idx_0 = cp_idx - 1 + local ce_idx_0 = ce_idx - 1 + 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 ent_data = { @@ -52,6 +63,7 @@ local function init_context(params) has_entity_scope = has_entity_scope, adjustment = cus_ent.adjustment or 0, unlimited = cus_ent.unlimited, + is_loose = is_loose, } if has_entity_scope then diff --git a/server/src/_luaScriptsV2/deductFromCustomerEntitlements/luaUtils.lua b/server/src/_luaScriptsV2/deductFromCustomerEntitlements/luaUtils.lua index 765d85983..780cc2ca9 100644 --- a/server/src/_luaScriptsV2/deductFromCustomerEntitlements/luaUtils.lua +++ b/server/src/_luaScriptsV2/deductFromCustomerEntitlements/luaUtils.lua @@ -32,20 +32,33 @@ end -- ============================================================================ -- HELPER: Find entitlement in FullCustomer by ID --- Returns: cus_ent table, cus_product table, cus_ent_index, cus_product_index +-- Returns: cus_ent table, cus_product table (or nil for loose), cus_ent_index, cus_product_index (or nil for loose) +-- For loose entitlements: cus_product=nil and cus_product_index=nil -- ============================================================================ local function find_entitlement(full_customer, ent_id) - if not full_customer.customer_products then return nil, nil, nil, nil end - - for cp_idx, cus_product in ipairs(full_customer.customer_products) do - if cus_product.customer_entitlements then - for ce_idx, cus_ent in ipairs(cus_product.customer_entitlements) do - if cus_ent.id == ent_id then - return cus_ent, cus_product, ce_idx, cp_idx + -- Search in customer_products first + if full_customer.customer_products then + for cp_idx, cus_product in ipairs(full_customer.customer_products) do + if cus_product.customer_entitlements then + for ce_idx, cus_ent in ipairs(cus_product.customer_entitlements) do + if cus_ent.id == ent_id then + return cus_ent, cus_product, ce_idx, cp_idx + end end end end end + + -- Search in extra_customer_entitlements (loose entitlements) + if full_customer.extra_customer_entitlements then + for ece_idx, cus_ent in ipairs(full_customer.extra_customer_entitlements) do + if cus_ent.id == ent_id then + -- Return nil for cus_product and cus_product_index to indicate loose entitlement + return cus_ent, nil, ece_idx, nil + end + end + end + return nil, nil, nil, nil end