MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / GET

Function GET

web/src/app/api/orgs/[orgId]/usage/export/route.ts:17–103  ·  view source on GitHub ↗
(
  request: NextRequest,
  { params }: RouteParams,
)

Source from the content-addressed store, hash-verified

15}
16
17export async function GET(
18 request: NextRequest,
19 { params }: RouteParams,
20): Promise<NextResponse> {
21 try {
22 const session = await getServerSession(authOptions)
23 if (!session?.user?.id) {
24 return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
25 }
26
27 const { orgId } = await params
28
29 // Check if user is a member of this organization
30 const membership = await db
31 .select({ role: schema.orgMember.role })
32 .from(schema.orgMember)
33 .where(
34 and(
35 eq(schema.orgMember.org_id, orgId),
36 eq(schema.orgMember.user_id, session.user.id),
37 ),
38 )
39 .limit(1)
40
41 if (membership.length === 0) {
42 return NextResponse.json(
43 { error: 'Organization not found' },
44 { status: 404 },
45 )
46 }
47
48 // Sync organization billing cycle with Stripe and get current cycle start
49 const quotaResetDate = await syncOrganizationBillingCycle({
50 organizationId: orgId,
51 logger,
52 })
53
54 // Get all usage data for this cycle
55 const usageData = await db
56 .select({
57 date: schema.message.finished_at,
58 user_name: schema.user.name,
59 repository_url: schema.message.repo_url,
60 credits_used: schema.message.credits,
61 message_id: schema.message.id,
62 })
63 .from(schema.message)
64 .innerJoin(schema.user, eq(schema.message.user_id, schema.user.id))
65 .where(
66 and(
67 eq(schema.message.org_id, orgId),
68 gte(schema.message.finished_at, quotaResetDate),
69 ),
70 )
71 .orderBy(desc(schema.message.finished_at))
72
73 // Convert to CSV
74 const csvHeaders = 'Date,User,Repository,Credits Used,Message ID\n'

Callers

nothing calls this directly

Calls 2

fromMethod · 0.80

Tested by

no test coverage detected