-
Notifications
You must be signed in to change notification settings - Fork 46
feat(api): add usage email filters and members endpoint #4211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
evanjacobson
wants to merge
9
commits into
main
Choose a base branch
from
feat/public-api-endpoints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e1d336b
feat(api): add public organization members endpoint
evanjacobson d4343ec
feat(usage): support email user filters
evanjacobson 62c590f
fix(usage): hide unmapped user ids in email display
evanjacobson ce4959d
fix(usage): aggregate email breakdown buckets
evanjacobson 52c4530
fix(usage): preserve limits for email breakdowns
evanjacobson e2e18a2
fix(usage): keep raw breakdown keys
evanjacobson 4db69ed
fix(usage): aggregate scoped email breakdowns
evanjacobson e022ad4
fix(usage): bound email breakdown aggregation
evanjacobson c9c884a
docs(api): document organization members endpoint
evanjacobson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
apps/web/src/app/api/v1/organizations/[id]/members/route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { beforeAll, describe, expect, it, jest } from '@jest/globals'; | ||
| import { NextRequest } from 'next/server'; | ||
| import type { handleTRPCRequest } from '@/lib/trpc-route-handler'; | ||
| import type { GET as routeGET } from './route'; | ||
|
|
||
| jest.mock('@/lib/trpc-route-handler', () => ({ | ||
| handleTRPCRequest: jest.fn(), | ||
| })); | ||
|
|
||
| const { handleTRPCRequest: mockedHandleTRPCRequest } = jest.requireMock( | ||
| '@/lib/trpc-route-handler' | ||
| ) as { | ||
| handleTRPCRequest: jest.MockedFunction<typeof handleTRPCRequest>; | ||
| }; | ||
|
|
||
| let GET: typeof routeGET; | ||
|
|
||
| beforeAll(async () => { | ||
| ({ GET } = await import('./route')); | ||
| }); | ||
|
|
||
| describe('GET /api/v1/organizations/[id]/members', () => { | ||
| it('returns public organization members without invited-member invite fields', async () => { | ||
| const invitedMember = { | ||
| email: 'invited@example.com', | ||
| role: 'member' as const, | ||
| inviteDate: '2026-06-22T00:00:00.000Z', | ||
| inviteToken: 'secret-token', | ||
| inviteId: 'invite-id', | ||
| status: 'invited' as const, | ||
| inviteUrl: 'https://example.com/users/accept-invite/secret-token', | ||
| dailyUsageLimitUsd: null, | ||
| currentDailyUsageUsd: null, | ||
| }; | ||
| const withMembers = jest.fn(async (_input: { organizationId: string }) => ({ | ||
| id: 'org-id', | ||
| name: 'Test Org', | ||
| members: [ | ||
| { | ||
| id: 'user-id', | ||
| name: 'Active User', | ||
| email: 'active@example.com', | ||
| role: 'owner' as const, | ||
| status: 'active' as const, | ||
| inviteDate: null, | ||
| dailyUsageLimitUsd: null, | ||
| currentDailyUsageUsd: null, | ||
| }, | ||
| invitedMember, | ||
| ], | ||
| })); | ||
| const caller = { | ||
| organizations: { | ||
| withMembers, | ||
| }, | ||
| }; | ||
|
|
||
| mockedHandleTRPCRequest.mockImplementationOnce(async (_request, handler) => { | ||
| const result = await handler(caller as never); | ||
| return Response.json(result) as never; | ||
| }); | ||
|
|
||
| const response = await GET(new NextRequest('http://localhost:3000'), { | ||
| params: Promise.resolve({ id: 'org-id' }), | ||
| }); | ||
|
|
||
| expect(withMembers).toHaveBeenCalledWith({ organizationId: 'org-id' }); | ||
| expect(invitedMember).toHaveProperty('inviteToken', 'secret-token'); | ||
| expect(response.status).toBe(200); | ||
| await expect(response.json()).resolves.toEqual([ | ||
| { | ||
| id: 'user-id', | ||
| name: 'Active User', | ||
| email: 'active@example.com', | ||
| role: 'owner', | ||
| status: 'active', | ||
| inviteDate: null, | ||
| dailyUsageLimitUsd: null, | ||
| currentDailyUsageUsd: null, | ||
| }, | ||
| { | ||
| email: 'invited@example.com', | ||
| role: 'member', | ||
| inviteDate: '2026-06-22T00:00:00.000Z', | ||
| status: 'invited', | ||
| dailyUsageLimitUsd: null, | ||
| currentDailyUsageUsd: null, | ||
| }, | ||
| ]); | ||
| }); | ||
| }); |
12 changes: 12 additions & 0 deletions
12
apps/web/src/app/api/v1/organizations/[id]/members/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import type { NextRequest } from 'next/server'; | ||
| import { PublicOrganizationMembersSchema } from '@/lib/organizations/organization-types'; | ||
| import { handleTRPCRequest } from '@/lib/trpc-route-handler'; | ||
|
|
||
| export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { | ||
| const organizationId = (await params).id; | ||
|
|
||
| return handleTRPCRequest(request, async caller => { | ||
| const org = await caller.organizations.withMembers({ organizationId }); | ||
| return PublicOrganizationMembersSchema.parse(org.members); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Low, Documentation: The shared REST operation documents the common error responses, but this members endpoint can also return HTTP 404.
organizations.withMembersraisesNOT_FOUNDwhen the organization does not exist, andhandleTRPCRequestpreserves that status. Without a documented 404, generated clients and API consumers may treat a valid endpoint response as unexpected. Could the REST route metadata support route specific responses and add a 404 usingRestErrorResponseSchemafor this endpoint? An OpenAPI assertion for the members operation would keep the contract in sync.