-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathgrant-credits.ts
More file actions
439 lines (399 loc) · 12.2 KB
/
grant-credits.ts
File metadata and controls
439 lines (399 loc) · 12.2 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import { trackEvent } from '@codebuff/common/analytics'
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
import { GRANT_PRIORITIES } from '@codebuff/common/constants/grant-priorities'
import { DEFAULT_FREE_CREDITS_GRANT } from '@codebuff/common/old-constants'
import { getNextQuotaReset } from '@codebuff/common/util/dates'
import { withRetry } from '@codebuff/common/util/promise'
import db from '@codebuff/internal/db'
import * as schema from '@codebuff/internal/db/schema'
import { logSyncFailure } from '@codebuff/internal/util/sync-failure'
import { and, desc, eq, gt, isNull, lte, or, sql } from 'drizzle-orm'
import { generateOperationIdTimestamp } from './utils'
import type { Logger } from '@codebuff/common/types/contracts/logger'
import type { GrantType } from '@codebuff/internal/db/schema'
type CreditGrantSelect = typeof schema.creditLedger.$inferSelect
type DbTransaction = Parameters<typeof db.transaction>[0] extends (
tx: infer T,
) => any
? T
: never
/**
* Finds the amount of the most recent expired 'free' grant for a user.
* Finds the amount of the most recent expired 'free' grant for a user,
* excluding migration grants (operation_id starting with 'migration-').
* If there is a previous grant, caps the amount at 2000 credits.
* If no expired 'free' grant is found, returns the default free limit.
* @param userId The ID of the user.
* @returns The amount of the last expired free grant (capped at 2000) or the default.
*/
export async function getPreviousFreeGrantAmount(params: {
userId: string
logger: Logger
}): Promise<number> {
const { userId, logger } = params
const now = new Date()
const lastExpiredFreeGrant = await db
.select({
principal: schema.creditLedger.principal,
})
.from(schema.creditLedger)
.where(
and(
eq(schema.creditLedger.user_id, userId),
eq(schema.creditLedger.type, 'free'),
lte(schema.creditLedger.expires_at, now), // Grant has expired
),
)
.orderBy(desc(schema.creditLedger.expires_at)) // Most recent expiry first
.limit(1)
if (lastExpiredFreeGrant.length > 0) {
// TODO: remove this once it's past May 22nd, after all users have been migrated over
const cappedAmount = Math.min(lastExpiredFreeGrant[0].principal, 2000)
logger.debug(
{ userId, amount: lastExpiredFreeGrant[0].principal },
'Found previous expired free grant amount.',
)
return cappedAmount
} else {
logger.debug(
{ userId, defaultAmount: DEFAULT_FREE_CREDITS_GRANT },
'No previous expired free grant found. Using default.',
)
return DEFAULT_FREE_CREDITS_GRANT // Default if no previous grant found
}
}
/**
* Calculates the total referral bonus credits a user should receive based on
* their referral history (both as referrer and referred).
* @param userId The ID of the user.
* @returns The total referral bonus credits earned.
*/
export async function calculateTotalReferralBonus(params: {
userId: string
logger: Logger
}): Promise<number> {
const { userId, logger } = params
try {
const result = await db
.select({
totalCredits: sql<string>`COALESCE(SUM(${schema.referral.credits}), 0)`,
})
.from(schema.referral)
.where(
or(
eq(schema.referral.referrer_id, userId),
eq(schema.referral.referred_id, userId),
),
)
const totalBonus = parseInt(result[0]?.totalCredits ?? '0')
logger.debug({ userId, totalBonus }, 'Calculated total referral bonus.')
return totalBonus
} catch (error) {
logger.error(
{ userId, error },
'Error calculating total referral bonus. Returning 0.',
)
return 0
}
}
/**
* Core grant operation that can be part of a larger transaction.
*/
export async function grantCreditOperation(params: {
userId: string
amount: number
type: GrantType
description: string
expiresAt: Date | null
operationId: string
tx?: DbTransaction
logger: Logger
}) {
const {
userId,
amount,
type,
description,
expiresAt,
operationId,
tx,
logger,
} = params
const dbClient = tx || db
const now = new Date()
// If the grant already exists, we can safely ignore this error since
// the operation is idempotent - the grant was already created successfully
const isUniqueConstraintError = (error: any): boolean => {
return (
error.code === '23505' ||
(error.message && error.message.includes('already exists'))
)
}
// First check for any negative balances
const negativeGrants = await dbClient
.select()
.from(schema.creditLedger)
.where(
and(
eq(schema.creditLedger.user_id, userId),
or(
isNull(schema.creditLedger.expires_at),
gt(schema.creditLedger.expires_at, now),
),
),
)
.then((grants) => grants.filter((g) => g.balance < 0))
if (negativeGrants.length > 0) {
const totalDebt = negativeGrants.reduce(
(sum, g) => sum + Math.abs(g.balance),
0,
)
for (const grant of negativeGrants) {
await dbClient
.update(schema.creditLedger)
.set({ balance: 0 })
.where(eq(schema.creditLedger.operation_id, grant.operation_id))
}
const remainingAmount = Math.max(0, amount - totalDebt)
if (remainingAmount > 0) {
try {
await dbClient.insert(schema.creditLedger).values({
operation_id: operationId,
user_id: userId,
principal: amount,
balance: remainingAmount,
type,
description:
totalDebt > 0
? `${description} (${totalDebt} credits used to clear existing debt)`
: description,
priority: GRANT_PRIORITIES[type],
expires_at: expiresAt,
created_at: now,
})
} catch (error: any) {
if (isUniqueConstraintError(error)) {
logger.info(
{ userId, operationId, type, amount },
'Skipping duplicate credit grant due to idempotency check',
)
return
}
throw error
}
}
} else {
// No debt - create grant normally
try {
await dbClient.insert(schema.creditLedger).values({
operation_id: operationId,
user_id: userId,
principal: amount,
balance: amount,
type,
description,
priority: GRANT_PRIORITIES[type],
expires_at: expiresAt,
created_at: now,
})
} catch (error: any) {
if (isUniqueConstraintError(error)) {
logger.info(
{ userId, operationId, type, amount },
'Skipping duplicate credit grant due to idempotency check',
)
return
}
throw error
}
}
trackEvent({
event: AnalyticsEvent.CREDIT_GRANT,
userId,
properties: {
operationId,
type,
description,
amount,
expiresAt,
},
logger,
})
logger.info(
{ userId, operationId, type, amount, expiresAt },
'Created new credit grant',
)
}
/**
* Processes a credit grant request with retries and failure logging.
* Used for standalone credit grants that need retry logic and failure tracking.
*/
export async function processAndGrantCredit(params: {
userId: string
amount: number
type: GrantType
description: string
expiresAt: Date | null
operationId: string
logger: Logger
}): Promise<void> {
const { operationId, logger } = params
try {
await withRetry(() => grantCreditOperation(params), {
maxRetries: 3,
retryIf: () => true,
onRetry: (error, attempt) => {
logger.warn(
{ operationId, attempt, error },
`processAndGrantCredit retry ${attempt}`,
)
},
})
} catch (error: any) {
await logSyncFailure({
id: operationId,
errorMessage: error.message,
provider: 'internal',
logger,
})
logger.error(
{ operationId, error },
'processAndGrantCredit failed after retries, logged to sync_failure',
)
throw error
}
}
/**
* Revokes credits from a specific grant by operation ID.
* This sets the balance to 0 and updates the description to indicate a refund.
*
* @param operationId The operation ID of the grant to revoke
* @param reason The reason for revoking the credits (e.g. refund)
* @returns true if the grant was found and revoked, false otherwise
*/
export async function revokeGrantByOperationId(params: {
operationId: string
reason: string
logger: Logger
}): Promise<boolean> {
const { operationId, reason, logger } = params
return await db.transaction(async (tx) => {
const grant = await tx.query.creditLedger.findFirst({
where: eq(schema.creditLedger.operation_id, operationId),
})
if (!grant) {
logger.warn({ operationId }, 'Attempted to revoke non-existent grant')
return false
}
if (grant.balance < 0) {
logger.warn(
{ operationId, currentBalance: grant.balance },
'Cannot revoke grant with negative balance - user has already spent these credits',
)
return false
}
await tx
.update(schema.creditLedger)
.set({
principal: 0,
balance: 0,
description: `${grant.description} (Revoked: ${reason})`,
})
.where(eq(schema.creditLedger.operation_id, operationId))
logger.info(
{
operationId,
userId: grant.user_id,
revokedAmount: grant.balance,
reason,
},
'Revoked credit grant',
)
return true
})
}
/**
* Checks if a user's quota needs to be reset, and if so:
* 1. Calculates their new monthly grant amount
* 2. Issues the grant with the appropriate expiry
* 3. Updates their next_quota_reset date
* All of this is done in a single transaction to ensure consistency.
*
* @param userId The ID of the user
* @returns The effective quota reset date (either existing or new)
*/
export async function triggerMonthlyResetAndGrant(params: {
userId: string
logger: Logger
}): Promise<Date> {
const { userId, logger } = params
return await db.transaction(async (tx) => {
const now = new Date()
// Get user's current reset date
const user = await tx.query.user.findFirst({
where: eq(schema.user.id, userId),
columns: {
next_quota_reset: true,
},
})
if (!user) {
throw new Error(`User ${userId} not found`)
}
const currentResetDate = user.next_quota_reset
// If reset date is in the future, no action needed
if (currentResetDate && currentResetDate > now) {
return currentResetDate
}
// Calculate new reset date
const newResetDate = getNextQuotaReset(currentResetDate)
// Calculate grant amounts separately
const [freeGrantAmount, referralBonus] = await Promise.all([
getPreviousFreeGrantAmount(params),
calculateTotalReferralBonus(params),
])
// Generate a deterministic operation ID based on userId and reset date to minute precision
const timestamp = generateOperationIdTimestamp(newResetDate)
const freeOperationId = `free-${userId}-${timestamp}`
const referralOperationId = `referral-${userId}-${timestamp}`
// Update the user's next reset date
await tx
.update(schema.user)
.set({ next_quota_reset: newResetDate })
.where(eq(schema.user.id, userId))
// Always grant free credits - use grantCreditOperation with tx to keep everything in the same transaction
await grantCreditOperation({
...params,
amount: freeGrantAmount,
type: 'free',
description: 'Monthly free credits',
expiresAt: newResetDate, // Free credits expire at next reset
operationId: freeOperationId,
tx,
})
// Only grant referral credits if there are any
if (referralBonus > 0) {
await grantCreditOperation({
...params,
amount: referralBonus,
type: 'referral',
description: 'Monthly referral bonus',
expiresAt: newResetDate, // Referral credits expire at next reset
operationId: referralOperationId,
tx,
})
}
logger.info(
{
userId,
freeOperationId,
referralOperationId,
freeGrantAmount,
referralBonus,
newResetDate,
previousResetDate: currentResetDate,
},
'Processed monthly credit grants and reset',
)
return newResetDate
})
}