wip: zen
This commit is contained in:
@@ -561,8 +561,9 @@ export async function handler(
|
|||||||
if (!authInfo) return
|
if (!authInfo) return
|
||||||
|
|
||||||
const cost = authInfo.isFree || authInfo.provider?.credentials ? 0 : centsToMicroCents(totalCostInCent)
|
const cost = authInfo.isFree || authInfo.provider?.credentials ? 0 : centsToMicroCents(totalCostInCent)
|
||||||
await Database.transaction(async (tx) => {
|
await Database.use((db) =>
|
||||||
await tx.insert(UsageTable).values({
|
Promise.all([
|
||||||
|
db.insert(UsageTable).values({
|
||||||
workspaceID: authInfo.workspaceID,
|
workspaceID: authInfo.workspaceID,
|
||||||
id: Identifier.create("usage"),
|
id: Identifier.create("usage"),
|
||||||
model: modelInfo.id,
|
model: modelInfo.id,
|
||||||
@@ -575,8 +576,8 @@ export async function handler(
|
|||||||
cacheWrite1hTokens,
|
cacheWrite1hTokens,
|
||||||
cost,
|
cost,
|
||||||
keyID: authInfo.apiKeyId,
|
keyID: authInfo.apiKeyId,
|
||||||
})
|
}),
|
||||||
await tx
|
db
|
||||||
.update(BillingTable)
|
.update(BillingTable)
|
||||||
.set({
|
.set({
|
||||||
balance: sql`${BillingTable.balance} - ${cost}`,
|
balance: sql`${BillingTable.balance} - ${cost}`,
|
||||||
@@ -588,8 +589,8 @@ export async function handler(
|
|||||||
`,
|
`,
|
||||||
timeMonthlyUsageUpdated: sql`now()`,
|
timeMonthlyUsageUpdated: sql`now()`,
|
||||||
})
|
})
|
||||||
.where(eq(BillingTable.workspaceID, authInfo.workspaceID))
|
.where(eq(BillingTable.workspaceID, authInfo.workspaceID)),
|
||||||
await tx
|
db
|
||||||
.update(UserTable)
|
.update(UserTable)
|
||||||
.set({
|
.set({
|
||||||
monthlyUsage: sql`
|
monthlyUsage: sql`
|
||||||
@@ -600,14 +601,12 @@ export async function handler(
|
|||||||
`,
|
`,
|
||||||
timeMonthlyUsageUpdated: sql`now()`,
|
timeMonthlyUsageUpdated: sql`now()`,
|
||||||
})
|
})
|
||||||
.where(and(eq(UserTable.workspaceID, authInfo.workspaceID), eq(UserTable.id, authInfo.user.id)))
|
.where(and(eq(UserTable.workspaceID, authInfo.workspaceID), eq(UserTable.id, authInfo.user.id))),
|
||||||
})
|
db
|
||||||
|
|
||||||
await Database.use((tx) =>
|
|
||||||
tx
|
|
||||||
.update(KeyTable)
|
.update(KeyTable)
|
||||||
.set({ timeUsed: sql`now()` })
|
.set({ timeUsed: sql`now()` })
|
||||||
.where(and(eq(KeyTable.workspaceID, authInfo.workspaceID), eq(KeyTable.id, authInfo.apiKeyId))),
|
.where(and(eq(KeyTable.workspaceID, authInfo.workspaceID), eq(KeyTable.id, authInfo.apiKeyId))),
|
||||||
|
]),
|
||||||
)
|
)
|
||||||
|
|
||||||
return { costInMicroCents: cost }
|
return { costInMicroCents: cost }
|
||||||
|
|||||||
Reference in New Issue
Block a user