Skip to content

Commit a74c4a7

Browse files
committed
refactor: replace any types with unknown and proper types in billing
- Change error: any to error: unknown in catch blocks with type guards - Update isUniqueConstraintError helper to handle unknown type safely - Change logContext/logData from any to Record<string, unknown> - Improve org-monitoring.ts to avoid delete operator with destructuring Files improved: - grant-credits.ts: 4 error catch blocks - stripe-metering.ts: 2 retry callbacks - org-billing.ts: 1 error catch block + 2 log contexts - auto-topup.ts: 1 log context - org-monitoring.ts: 1 log object
1 parent 519d91c commit a74c4a7

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

packages/billing/src/auto-topup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ export async function checkAndTriggerOrgAutoTopup(params: {
524524
logger: Logger
525525
}): Promise<void> {
526526
const { organizationId, userId, logger } = params
527-
const logContext: any = { organizationId, userId }
527+
const logContext: Record<string, unknown> = { organizationId, userId }
528528

529529
try {
530530
const org = await getOrganizationSettings(organizationId)

packages/billing/src/grant-credits.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,12 @@ export async function grantCreditOperation(params: {
140140

141141
// If the grant already exists, we can safely ignore this error since
142142
// the operation is idempotent - the grant was already created successfully
143-
const isUniqueConstraintError = (error: any): boolean => {
143+
const isUniqueConstraintError = (error: unknown): boolean => {
144+
if (typeof error !== 'object' || error === null) return false
145+
const err = error as { code?: string; message?: string }
144146
return (
145-
error.code === '23505' ||
146-
(error.message && error.message.includes('already exists'))
147+
err.code === '23505' ||
148+
(err.message !== undefined && err.message.includes('already exists'))
147149
)
148150
}
149151

@@ -190,7 +192,7 @@ export async function grantCreditOperation(params: {
190192
expires_at: expiresAt,
191193
created_at: now,
192194
})
193-
} catch (error: any) {
195+
} catch (error: unknown) {
194196
if (isUniqueConstraintError(error)) {
195197
logger.info(
196198
{ userId, operationId, type, amount },
@@ -215,7 +217,7 @@ export async function grantCreditOperation(params: {
215217
expires_at: expiresAt,
216218
created_at: now,
217219
})
218-
} catch (error: any) {
220+
} catch (error: unknown) {
219221
if (isUniqueConstraintError(error)) {
220222
logger.info(
221223
{ userId, operationId, type, amount },
@@ -272,10 +274,11 @@ export async function processAndGrantCredit(params: {
272274
)
273275
},
274276
})
275-
} catch (error: any) {
277+
} catch (error: unknown) {
278+
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
276279
await logSyncFailure({
277280
id: operationId,
278-
errorMessage: error.message,
281+
errorMessage,
279282
provider: 'internal',
280283
logger,
281284
})

packages/billing/src/org-billing.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,10 @@ export async function grantOrganizationCredits(
387387
{ organizationId, userId, operationId, amount, expiresAt },
388388
'Created new organization credit grant',
389389
)
390-
} catch (error: any) {
390+
} catch (error: unknown) {
391391
// Check if this is a unique constraint violation on operation_id
392-
if (error.code === '23505' && error.constraint === 'credit_ledger_pkey') {
392+
const dbError = error as { code?: string; constraint?: string } | null
393+
if (dbError?.code === '23505' && dbError?.constraint === 'credit_ledger_pkey') {
393394
logger.info(
394395
{ organizationId, userId, operationId, amount },
395396
'Skipping duplicate organization credit grant due to idempotency check',
@@ -559,7 +560,7 @@ export async function updateStripeSubscriptionQuantity(params: {
559560
proration_date: Math.floor(Date.now() / 1000),
560561
})
561562

562-
const logData: any = {
563+
const logData: Record<string, unknown> = {
563564
orgId,
564565
actualQuantity,
565566
previousQuantity: teamFeeItem.quantity,
@@ -572,7 +573,7 @@ export async function updateStripeSubscriptionQuantity(params: {
572573
logger.info(logData, `Updated Stripe subscription quantity: ${context}`)
573574
}
574575
} catch (stripeError) {
575-
const logData: any = {
576+
const logData: Record<string, unknown> = {
576577
orgId,
577578
actualQuantity,
578579
context,

packages/billing/src/org-monitoring.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,12 @@ export async function trackOrganizationUsageMetrics(params: {
369369
// TODO: Generate usage reports
370370
// TODO: Identify usage patterns and optimization opportunities
371371
} catch (error) {
372-
const obj: any = {
373-
...params,
372+
const { logger: _logger, ...paramsWithoutLogger } = params
373+
const logData: Record<string, unknown> = {
374+
...paramsWithoutLogger,
374375
error: getErrorObject(error),
375376
}
376-
delete obj.logger
377-
logger.error(obj, 'Failed to track organization usage metrics')
377+
logger.error(logData, 'Failed to track organization usage metrics')
378378
}
379379
}
380380

packages/billing/src/stripe-metering.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@ export async function reportPurchasedCreditsToStripe(params: {
9696
),
9797
{
9898
maxRetries: 3,
99-
retryIf: (error: any) =>
100-
error?.type === 'StripeConnectionError' ||
101-
error?.type === 'StripeAPIError' ||
102-
error?.type === 'StripeRateLimitError',
103-
onRetry: (error: any, attempt: number) => {
99+
retryIf: (error: unknown) => {
100+
const stripeError = error as { type?: string } | null
101+
return (
102+
stripeError?.type === 'StripeConnectionError' ||
103+
stripeError?.type === 'StripeAPIError' ||
104+
stripeError?.type === 'StripeRateLimitError'
105+
)
106+
},
107+
onRetry: (error: unknown, attempt: number) => {
104108
logger.warn(
105109
{ ...logContext, attempt, error },
106110
'Retrying Stripe metering call',

0 commit comments

Comments
 (0)