-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcredits.ts
More file actions
217 lines (189 loc) · 7.66 KB
/
Copy pathcredits.ts
File metadata and controls
217 lines (189 loc) · 7.66 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
import { PurchasedCredit, User, UserRole, UserWithOrganization } from '@auto-drive/models'
import {
AdminCreditBatchRow,
AdminUserCreditBatchRow,
purchasedCreditsRepository,
} from '../../infrastructure/repositories/users/purchasedCredits.js'
import { AccountsUseCases } from './accounts.js'
import { config } from '../../config.js'
import { ForbiddenError } from '../../errors/index.js'
import { err, ok, Result } from 'neverthrow'
import { hasGoogleAuth } from '../featureFlags/index.js'
import { createLogger } from '../../infrastructure/drivers/logger.js'
const logger = createLogger('CreditsUseCases')
// Number of days ahead to consider a batch "expiring soon" for the
// /credits/batches/expiring endpoint and the economics admin view.
const EXPIRING_WITHIN_DAYS = 30
// ---------------------------------------------------------------------------
// CreditSummary
// Returned by GET /credits/summary.
// ---------------------------------------------------------------------------
export type CreditSummary = {
uploadBytesRemaining: bigint
downloadBytesRemaining: bigint
/** Soonest expires_at across active rows, or null when no active credits. */
nextExpiryDate: Date | null
/** Number of active (non-expired) purchase rows. */
batchCount: number
/**
* True when the user can still make a purchase without exceeding the cap.
* Cap is enforced on upload bytes only — download credits are not allocated
* on purchase right now, so only uploadBytesRemaining is checked.
*/
canPurchase: boolean
/** Maximum bytes the user could purchase right now without hitting the cap. */
maxPurchasableBytes: bigint
/** True when the user is authenticated via Google OAuth. */
googleVerified: boolean
/**
* Number of days after purchase before credits expire.
* Driven by the CREDIT_EXPIRY_DAYS environment variable so the frontend
* can display the correct duration without a separate API call.
*/
expiryDays: number
}
const getSummary = async (
user: UserWithOrganization,
): Promise<CreditSummary> => {
const account = await AccountsUseCases.getOrCreateAccount(user)
const summary = await purchasedCreditsRepository.getRemainingCredits(account.id)
const cap = config.credits.maxBytesPerUser
// Cap is enforced on upload bytes only. Download credits are not allocated
// on purchase right now so there is no download cap to check.
const maxPurchasableBytes =
cap > summary.uploadBytesRemaining ? cap - summary.uploadBytesRemaining : 0n
const canPurchase = maxPurchasableBytes > 0n
return {
uploadBytesRemaining: summary.uploadBytesRemaining,
downloadBytesRemaining: summary.downloadBytesRemaining,
nextExpiryDate: summary.nextExpiryDate,
batchCount: summary.activeRowCount,
canPurchase,
maxPurchasableBytes,
googleVerified: hasGoogleAuth(user),
expiryDays: config.credits.expiryDays,
}
}
// ---------------------------------------------------------------------------
// getBatches
// Full purchase history (including expired rows) for the authenticated user.
// Returned by GET /credits/batches.
// ---------------------------------------------------------------------------
const getBatches = async (
user: UserWithOrganization,
): Promise<PurchasedCredit[]> => {
const account = await AccountsUseCases.getOrCreateAccount(user)
return purchasedCreditsRepository.getByAccountId(account.id)
}
// ---------------------------------------------------------------------------
// getExpiringBatches
// Active rows expiring within EXPIRING_WITHIN_DAYS for the authenticated user.
// Returned by GET /credits/batches/expiring.
// ---------------------------------------------------------------------------
const getExpiringBatches = async (
user: UserWithOrganization,
): Promise<PurchasedCredit[]> => {
const account = await AccountsUseCases.getOrCreateAccount(user)
return purchasedCreditsRepository.getExpiringCreditsByAccountId(
account.id,
EXPIRING_WITHIN_DAYS,
)
}
// ---------------------------------------------------------------------------
// CreditEconomics / getEconomics
// System-wide stats for admin users only.
// Returned by GET /credits/economics.
// ---------------------------------------------------------------------------
export type CreditEconomics = {
/** Count of active rows expiring within 30 days, system-wide. */
totalExpiringWithin30Days: number
/** Sum of upload bytes remaining across those rows. */
totalExpiringUploadBytes: bigint
/** Sum of download bytes remaining across those rows. */
totalExpiringDownloadBytes: bigint
}
const getEconomics = async (
executor: User,
): Promise<Result<CreditEconomics, ForbiddenError>> => {
if (executor.role !== UserRole.Admin) {
logger.warn('Non-admin user attempted to access credit economics', {
publicId: executor.publicId,
})
return err(new ForbiddenError('Admin access required'))
}
const aggregate =
await purchasedCreditsRepository.getExpiringCreditsAggregate(
EXPIRING_WITHIN_DAYS,
)
return ok({
totalExpiringWithin30Days: aggregate.count,
totalExpiringUploadBytes: aggregate.totalUploadBytesRemaining,
totalExpiringDownloadBytes: aggregate.totalDownloadBytesRemaining,
})
}
// ---------------------------------------------------------------------------
// getAllBatches
// Admin-only: full purchase history across all users with their publicId.
// Returns 403 for non-admin callers.
// ---------------------------------------------------------------------------
const getAllBatches = async (
executor: User,
): Promise<Result<AdminCreditBatchRow[], ForbiddenError>> => {
if (executor.role !== UserRole.Admin) {
logger.warn('Non-admin user attempted to access all credit batches', {
publicId: executor.publicId,
})
return err(new ForbiddenError('Admin access required'))
}
const rows = await purchasedCreditsRepository.getAllWithUserPublicId()
return ok(rows)
}
// ---------------------------------------------------------------------------
// getUserBatches
// Admin-only: full purchase history for a specific user identified by their
// userPublicId. Includes intent data (price, wallet address) for refund UX.
// Returns 403 for non-admin callers.
// ---------------------------------------------------------------------------
const getUserBatches = async (
executor: User,
userPublicId: string,
): Promise<Result<AdminUserCreditBatchRow[], ForbiddenError>> => {
if (executor.role !== UserRole.Admin) {
return err(new ForbiddenError('Admin access required'))
}
const rows = await purchasedCreditsRepository.getByUserPublicId(userPublicId)
return ok(rows)
}
// ---------------------------------------------------------------------------
// refundBatch
// Admin-only: zeros the remaining bytes for a specific credit batch and marks
// it as refunded. Idempotent — calling it on an already-refunded row is a
// no-op that still returns ok() so the UI can safely retry on network errors.
// Returns 403 for non-admin callers, 404 if the batch does not exist.
// ---------------------------------------------------------------------------
const refundBatch = async (
executor: User,
batchId: string,
): Promise<Result<void, ForbiddenError | Error>> => {
if (executor.role !== UserRole.Admin) {
return err(new ForbiddenError('Admin access required'))
}
const updated = await purchasedCreditsRepository.markAsRefunded(batchId)
if (!updated) {
return err(new Error('Credit batch not found'))
}
logger.info('Admin marked credit batch as refunded', {
batchId,
adminPublicId: executor.publicId,
})
return ok(undefined)
}
export const CreditsUseCases = {
getSummary,
getBatches,
getExpiringBatches,
getEconomics,
getAllBatches,
getUserBatches,
refundBatch,
}