-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathluaScriptsV2.ts
More file actions
187 lines (156 loc) · 6.21 KB
/
luaScriptsV2.ts
File metadata and controls
187 lines (156 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Path to script folders
const DEDUCT_DIR = join(__dirname, "deductFromCustomerEntitlements");
const DELETE_CACHE_DIR = join(__dirname, "deleteFullCustomerCache");
const RESET_DIR = join(__dirname, "resetCustomerEntitlements");
const UPDATE_DIR = join(__dirname, "updateCustomerEntitlements");
// ============================================================================
// HELPER MODULES
// ============================================================================
const LUA_UTILS = readFileSync(join(DEDUCT_DIR, "luaUtils.lua"), "utf-8");
const READ_BALANCES = readFileSync(
join(DEDUCT_DIR, "readBalances.lua"),
"utf-8",
);
const CONTEXT_UTILS = readFileSync(
join(DEDUCT_DIR, "contextUtils.lua"),
"utf-8",
);
const GET_TOTAL_BALANCE = readFileSync(
join(DEDUCT_DIR, "getTotalBalance.lua"),
"utf-8",
);
const DEDUCT_FROM_ROLLOVERS = readFileSync(
join(DEDUCT_DIR, "deductFromRollovers.lua"),
"utf-8",
);
const DEDUCT_FROM_MAIN_BALANCE = readFileSync(
join(DEDUCT_DIR, "deductFromMainBalance.lua"),
"utf-8",
);
// ============================================================================
// MAIN SCRIPT
// ============================================================================
const mainScript = readFileSync(
join(DEDUCT_DIR, "deductFromCustomerEntitlements.lua"),
"utf-8",
);
/**
* Lua script for deducting from customer entitlements in Redis.
* Uses JSON.NUMINCRBY for atomic incremental updates to prevent race conditions.
* Composed from helper modules via string interpolation.
* Supports both positive deductions and negative refunds.
*/
export const DEDUCT_FROM_CUSTOMER_ENTITLEMENTS_SCRIPT = `${LUA_UTILS}
${READ_BALANCES}
${CONTEXT_UTILS}
${GET_TOTAL_BALANCE}
${DEDUCT_FROM_ROLLOVERS}
${DEDUCT_FROM_MAIN_BALANCE}
${mainScript}`;
// ============================================================================
// DELETE FULL CUSTOMER CACHE SCRIPTS
// ============================================================================
/**
* Lua script for deleting a single FullCustomer cache from Redis.
* Checks test guard, sets stale-write guard, and deletes cache atomically.
*/
export const DELETE_FULL_CUSTOMER_CACHE_SCRIPT = readFileSync(
join(DELETE_CACHE_DIR, "deleteFullCustomerCache.lua"),
"utf-8",
);
/**
* Lua script for setting a FullCustomer cache in Redis.
* Checks stale-write guard, checks if cache exists, and sets cache atomically.
*/
export const SET_FULL_CUSTOMER_CACHE_SCRIPT = readFileSync(
join(DELETE_CACHE_DIR, "setFullCustomerCache.lua"),
"utf-8",
);
/**
* Lua script for batch deleting multiple FullCustomer caches from Redis.
* For each customer: checks test guard, sets stale-write guard, deletes cache.
*/
export const BATCH_DELETE_FULL_CUSTOMER_CACHE_SCRIPT = readFileSync(
join(DELETE_CACHE_DIR, "batchDeleteFullCustomerCache.lua"),
"utf-8",
);
// ============================================================================
// RESET CUSTOMER ENTITLEMENTS SCRIPT (deprecated — kept for backward compat)
// ============================================================================
const resetMainScript = readFileSync(
join(RESET_DIR, "resetCustomerEntitlements.lua"),
"utf-8",
);
/**
* @deprecated Use UPDATE_CUSTOMER_ENTITLEMENTS_SCRIPT instead.
*/
export const RESET_CUSTOMER_ENTITLEMENTS_SCRIPT = `${LUA_UTILS}
${resetMainScript}`;
// ============================================================================
// UPDATE CUSTOMER ENTITLEMENTS SCRIPT (unified reset + deduction cache update)
// ============================================================================
const updateMainScript = readFileSync(
join(UPDATE_DIR, "updateCustomerEntitlements.lua"),
"utf-8",
);
/**
* Unified Lua script for atomically updating cusEnt fields in the cached
* FullCustomer. Handles both reset and deduction cache updates — both are
* "apply absolute values to customer entitlements in the Redis cache."
*/
export const UPDATE_CUSTOMER_ENTITLEMENTS_SCRIPT = `${LUA_UTILS}
${updateMainScript}`;
// ============================================================================
// INCREMENT CUSTOMER ENTITLEMENT BALANCE SCRIPT
// ============================================================================
const incrementMainScript = readFileSync(
join(__dirname, "incrementCusEntBalance.lua"),
"utf-8",
);
/**
* Lua script for atomically incrementing a cusEnt balance in the cached
* FullCustomer via JSON.NUMINCRBY. Safe with concurrent deductions.
*/
export const INCREMENT_CUS_ENT_BALANCE_SCRIPT = `${LUA_UTILS}
${incrementMainScript}`;
// ============================================================================
// UPDATE CUSTOMER PRODUCT OPTIONS SCRIPT
// ============================================================================
/**
* Lua script for atomically incrementing a cusProduct's options[].quantity
* in the cached FullCustomer via JSON.NUMINCRBY.
*/
export const UPDATE_CUS_PRODUCT_OPTIONS_SCRIPT = readFileSync(
join(__dirname, "updateCusProductOptions.lua"),
"utf-8",
);
// ============================================================================
// UPDATE CUSTOMER DATA SCRIPT (top-level customer fields)
// ============================================================================
/**
* Lua script for atomically updating top-level customer fields in the cached
* FullCustomer (name, email, metadata, send_email_receipts, etc.).
*/
export const UPDATE_CUSTOMER_DATA_SCRIPT = readFileSync(
join(__dirname, "updateCustomerData.lua"),
"utf-8",
);
// ============================================================================
// APPEND ENTITY TO CUSTOMER SCRIPT
// ============================================================================
/**
* Lua script for atomically appending an entity to the customer's entities
* array in the cached FullCustomer. Checks for duplicates before appending.
*
* CRDT-safe: JSON.ARRAPPEND uses merge conflict resolution in Active-Active,
* so concurrent appends from different regions will both succeed.
*/
export const APPEND_ENTITY_TO_CUSTOMER_SCRIPT = readFileSync(
join(__dirname, "appendEntityToCustomer.lua"),
"utf-8",
);