Skip to content

Commit 9b5e8f4

Browse files
committed
refactor(billing): drop the unused executor from the deleted-identity email check
isDeletedAccountIdentity reads on db as it did before; only userHasDeletedAccountIdentity takes a caller's transaction, so its savepoint wrapper moves inline. Hoist the headroom descriptors to a module constant.
1 parent 0799434 commit 9b5e8f4

2 files changed

Lines changed: 16 additions & 23 deletions

File tree

‎apps/sim/lib/core/admission/transient-failure.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,18 @@ function hasAdmissionCode(failure: AdmissionFailureLike, code: string): boolean
8383
return failure.code === code || failure.cause?.code === code
8484
}
8585

86+
const RESERVATION_HEADROOM_DESCRIPTORS = [
87+
ADMISSION_ERROR_DESCRIPTOR.RESERVATION_PAYER_HEADROOM,
88+
ADMISSION_ERROR_DESCRIPTOR.RESERVATION_MEMBER_HEADROOM,
89+
] as const
90+
8691
/**
8792
* Whether a reservation was denied because the payer's or member's usage headroom is spent. The
8893
* run is refused for good, though a later one can be admitted as in-flight runs settle, which is
8994
* why deterministic admission rejections leave these out.
9095
*/
9196
export function isReservationHeadroomDenial(failure: AdmissionFailureLike): boolean {
92-
return [
93-
ADMISSION_ERROR_DESCRIPTOR.RESERVATION_PAYER_HEADROOM,
94-
ADMISSION_ERROR_DESCRIPTOR.RESERVATION_MEMBER_HEADROOM,
95-
].some(
97+
return RESERVATION_HEADROOM_DESCRIPTORS.some(
9698
(descriptor) =>
9799
failure.statusCode === descriptor.statusCode && hasAdmissionCode(failure, descriptor.code)
98100
)

‎apps/sim/lib/users/deleted-identity.ts‎

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -233,26 +233,15 @@ async function findDeletedIdentityMatch(
233233
return Boolean(match)
234234
}
235235

236-
/**
237-
* Runs a lookup on `executor`. Inside a caller's transaction it runs in a savepoint, so a failed
238-
* lookup rolls back only itself instead of aborting the caller's transaction.
239-
*/
240-
function readIdentity<T>(executor: DbOrTx, read: (reader: DbOrTx) => Promise<T>): Promise<T> {
241-
return executor === db ? read(db) : executor.transaction(read)
242-
}
243-
244236
/**
245237
* Whether any of these identities belonged to an account deleted within the
246238
* retention window. Fails open: a lookup error returns `false` and is logged,
247239
* because refusing a legitimate person's allowance is worse than one missed match.
248240
*/
249-
export async function isDeletedAccountIdentity(
250-
identity: SignInIdentity,
251-
executor: DbOrTx = db
252-
): Promise<boolean> {
241+
export async function isDeletedAccountIdentity(identity: SignInIdentity): Promise<boolean> {
253242
if (!isBillingEnabled) return false
254243
try {
255-
return await readIdentity(executor, (reader) => findDeletedIdentityMatch(reader, identity))
244+
return await findDeletedIdentityMatch(db, identity)
256245
} catch (error) {
257246
logger.error('Deleted-identity lookup failed; granting the free allowance', { error })
258247
return false
@@ -264,20 +253,22 @@ export async function isDeletedAccountIdentity(
264253
* the retention window: always its email, and its linked social sign-ins only
265254
* when `includeSignIns` is set, which callers do only for a user created in the
266255
* current signup request (a sign-in linked later never withholds anything).
267-
* Pass the caller's transaction as `executor` to read on it. Fails open like
268-
* {@link isDeletedAccountIdentity}.
256+
* Pass the caller's transaction as `executor` to read on it; the read then runs
257+
* in a savepoint, so a failed lookup never aborts that transaction. Fails open
258+
* like {@link isDeletedAccountIdentity}.
269259
*/
270260
export async function userHasDeletedAccountIdentity(
271261
userId: string,
272262
{ includeSignIns }: { includeSignIns: boolean },
273263
executor: DbOrTx = db
274264
): Promise<boolean> {
275265
if (!isBillingEnabled) return false
266+
const read = async (reader: DbOrTx) => {
267+
const identity = await loadSignInIdentity(reader, userId)
268+
return findDeletedIdentityMatch(reader, includeSignIns ? identity : { email: identity.email })
269+
}
276270
try {
277-
return await readIdentity(executor, async (reader) => {
278-
const identity = await loadSignInIdentity(reader, userId)
279-
return findDeletedIdentityMatch(reader, includeSignIns ? identity : { email: identity.email })
280-
})
271+
return await (executor === db ? read(db) : executor.transaction(read))
281272
} catch (error) {
282273
logger.error('Deleted-identity lookup failed; granting the free allowance', { userId, error })
283274
return false

0 commit comments

Comments
 (0)