GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(console): add quota reset support action (#47313) · krumpstead/opencode@5cf9f51

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/console
    • app/src/routes/api/support/actions
      • reset-quota.ts
    • core/src
      • quota.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/console/app/src/routes/api/support/actions/reset-quota.ts‎

Copy file name to clipboard
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { APIEvent } from "@solidjs/start/server"
2+
import { Quota } from "@opencode-ai/console-core/quota.js"
3+
import { safeEqual } from "@opencode-ai/console-core/util/crypto.js"
4+
import { Resource } from "@opencode-ai/console-resource"
5+
import z from "zod"
6+
7+
const Body = z.object({
8+
workspaceID: z.string().startsWith("wrk_"),
9+
plan: z.enum(["lite", "subscription"]),
10+
})
11+
12+
export async function POST(event: APIEvent) {
13+
if (!safeEqual(event.request.headers.get("authorization") ?? "", `Bearer ${Resource.SUPPORT_API_KEY.value}`)) {
14+
return Response.json({ error: "Unauthorized" }, { status: 401 })
15+
}
16+
17+
const body = Body.safeParse(await event.request.json().catch(() => undefined))
18+
if (!body.success) {
19+
return Response.json({ error: "Invalid request", issues: body.error.issues }, { status: 400 })
20+
}
21+
return Quota.reset(body.data)
22+
.then((result) => Response.json({ success: true, message: "Usage counters reset to zero", result }))
23+
.catch((error) => Response.json({ error: error instanceof Error ? error.message : String(error) }, { status: 400 }))
24+
}
Collapse file

‎packages/console/core/src/quota.ts‎

Copy file name to clipboard
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { z } from "zod"
2+
import { and, Database, eq, isNull } from "./drizzle"
3+
import { LiteTable, SubscriptionTable } from "./schema/billing.sql"
4+
import { Identifier } from "./identifier"
5+
import { fn } from "./util/fn"
6+
7+
export namespace Quota {
8+
// Zero all usage counters of one plan for a workspace, using the same
9+
// workspace-scoped addressing as unsubscribeLite/unsubscribeBlack and
10+
// Billing.subtractLiteUsage. Timestamps are left untouched on purpose: a
11+
// zeroed counter with a current-window stamp reads as empty and the next
12+
// write accumulates on top of zero, so the active windows stay undisturbed.
13+
export const reset = fn(
14+
z.object({
15+
workspaceID: Identifier.schema("workspace"),
16+
plan: z.enum(["lite", "subscription"]),
17+
}),
18+
async (input) => {
19+
if (input.plan === "lite") {
20+
return Database.transaction(async (db) => {
21+
const where = and(eq(LiteTable.workspaceID, input.workspaceID), isNull(LiteTable.timeDeleted))
22+
const rows = await db
23+
.select({
24+
rollingUsage: LiteTable.rollingUsage,
25+
weeklyUsage: LiteTable.weeklyUsage,
26+
monthlyUsage: LiteTable.monthlyUsage,
27+
})
28+
.from(LiteTable)
29+
.where(where)
30+
if (rows.length === 0) throw new Error("No lite usage counters found for workspace")
31+
32+
await db.update(LiteTable).set({ rollingUsage: 0, weeklyUsage: 0, monthlyUsage: 0 }).where(where)
33+
return {
34+
plan: "lite" as const,
35+
before: {
36+
rollingUsage: rows.reduce((sum, row) => sum + (row.rollingUsage ?? 0), 0),
37+
weeklyUsage: rows.reduce((sum, row) => sum + (row.weeklyUsage ?? 0), 0),
38+
monthlyUsage: rows.reduce((sum, row) => sum + (row.monthlyUsage ?? 0), 0),
39+
},
40+
}
41+
})
42+
}
43+
if (input.plan === "subscription") {
44+
return Database.transaction(async (db) => {
45+
const where = and(eq(SubscriptionTable.workspaceID, input.workspaceID), isNull(SubscriptionTable.timeDeleted))
46+
const rows = await db
47+
.select({ rollingUsage: SubscriptionTable.rollingUsage, fixedUsage: SubscriptionTable.fixedUsage })
48+
.from(SubscriptionTable)
49+
.where(where)
50+
if (rows.length === 0) throw new Error("No subscription usage counters found for workspace")
51+
52+
await db.update(SubscriptionTable).set({ rollingUsage: 0, fixedUsage: 0 }).where(where)
53+
return {
54+
plan: "subscription" as const,
55+
before: {
56+
rollingUsage: rows.reduce((sum, row) => sum + (row.rollingUsage ?? 0), 0),
57+
fixedUsage: rows.reduce((sum, row) => sum + (row.fixedUsage ?? 0), 0),
58+
},
59+
}
60+
})
61+
}
62+
throw new Error(`Unknown plan: ${input.plan}`)
63+
},
64+
)
65+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.