-
Notifications
You must be signed in to change notification settings - Fork 9
feat(organizations): GET + POST + DELETE /api/organizations/domains #749
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0b437fe
feat(organizations): organization domain management endpoints
sweetmantech 746ebfc
Merge branch 'main' into feat/org-domain-endpoints
sweetmantech 2b017dc
refactor(organizations): align domain endpoints with #748 review outc…
sweetmantech b10879e
refactor(organizations): converge members+domains errors on shared er…
sweetmantech 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; | ||
| import { getOrgDomainsHandler } from "@/lib/organizations/getOrgDomainsHandler"; | ||
| import { addOrgDomainHandler } from "@/lib/organizations/addOrgDomainHandler"; | ||
| import { removeOrgDomainHandler } from "@/lib/organizations/removeOrgDomainHandler"; | ||
|
|
||
| /** | ||
| * OPTIONS handler for CORS preflight requests. | ||
| * | ||
| * @returns A NextResponse with CORS headers. | ||
| */ | ||
| export async function OPTIONS() { | ||
| return new NextResponse(null, { | ||
| status: 200, | ||
| headers: getCorsHeaders(), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * GET /api/organizations/domains | ||
| * | ||
| * Lists the email domains mapped to an organization. Accounts that sign up | ||
| * with an email at a mapped domain automatically join the organization. | ||
| * The caller must be a member of the organization or a Recoup admin. | ||
| * | ||
| * Query parameters: | ||
| * - organization_id (required): The organization's account ID (UUID) | ||
| * | ||
| * @param request - The request object | ||
| * @returns A NextResponse with the organization's domain mappings | ||
| */ | ||
| export async function GET(request: NextRequest) { | ||
| return getOrgDomainsHandler(request); | ||
| } | ||
|
|
||
| /** | ||
| * POST /api/organizations/domains | ||
| * | ||
| * Maps an email domain to an organization for automatic membership. | ||
| * Idempotent for the same organization; returns 409 when the domain is | ||
| * already mapped to a different organization. | ||
| * | ||
| * Body parameters: | ||
| * - organizationId (required): The organization's account ID (UUID) | ||
| * - domain (required): The email domain to map (e.g. "seekermusic.com") | ||
| * | ||
| * @param request - The request object containing the body | ||
| * @returns A NextResponse with the domain mapping | ||
| */ | ||
| export async function POST(request: NextRequest) { | ||
| return addOrgDomainHandler(request); | ||
| } | ||
|
|
||
| /** | ||
| * DELETE /api/organizations/domains | ||
| * | ||
| * Removes an email domain mapping from an organization. Idempotent. | ||
| * | ||
| * Query parameters: | ||
| * - organization_id (required): The organization's account ID (UUID) | ||
| * - domain (required): The email domain to unmap (e.g. "seekermusic.com") | ||
| * | ||
| * @param request - The request object | ||
| * @returns A NextResponse indicating success | ||
| */ | ||
| export async function DELETE(request: NextRequest) { | ||
| return removeOrgDomainHandler(request); | ||
| } |
202 changes: 202 additions & 0 deletions
202
lib/organizations/__tests__/addOrgDomainHandler.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,202 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { addOrgDomainHandler } from "../addOrgDomainHandler"; | ||
| import { validateAuthContext } from "@/lib/auth/validateAuthContext"; | ||
| import { canManageOrganization } from "@/lib/organizations/canManageOrganization"; | ||
| import { selectOrganizationDomain } from "@/lib/supabase/organization_domains/selectOrganizationDomain"; | ||
| import { insertOrganizationDomain } from "@/lib/supabase/organization_domains/insertOrganizationDomain"; | ||
|
|
||
| vi.mock("@/lib/auth/validateAuthContext", () => ({ | ||
| validateAuthContext: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/organizations/canManageOrganization", () => ({ | ||
| canManageOrganization: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/organization_domains/selectOrganizationDomain", () => ({ | ||
| selectOrganizationDomain: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/organization_domains/insertOrganizationDomain", () => ({ | ||
| insertOrganizationDomain: vi.fn(), | ||
| })); | ||
|
|
||
| const ORG_ID = "9f0b5f61-6f8d-4b64-92f5-0d1a5f0a1c2e"; | ||
| const OTHER_ORG_ID = "1b2c3d4e-5f60-4a71-8b92-a3b4c5d6e7f8"; | ||
|
|
||
| function makeRequest(body: unknown) { | ||
| return new NextRequest("http://x/api/organizations/domains", { | ||
| method: "POST", | ||
| body: JSON.stringify(body), | ||
| }); | ||
| } | ||
|
|
||
| describe("addOrgDomainHandler", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(validateAuthContext).mockResolvedValue({ | ||
| accountId: "acc-1", | ||
| orgId: null, | ||
| authToken: "token", | ||
| }); | ||
| vi.mocked(canManageOrganization).mockResolvedValue(true); | ||
| vi.mocked(selectOrganizationDomain).mockResolvedValue(null); | ||
| }); | ||
|
|
||
| describe("successful cases", () => { | ||
| it("inserts a new mapping with a normalized domain", async () => { | ||
| vi.mocked(insertOrganizationDomain).mockResolvedValue({ | ||
| id: "dom-1", | ||
| domain: "seekermusic.com", | ||
| organization_id: ORG_ID, | ||
| created_at: "2026-01-01", | ||
| }); | ||
|
|
||
| const response = await addOrgDomainHandler( | ||
| makeRequest({ organizationId: ORG_ID, domain: " @SeekerMusic.COM " }), | ||
| ); | ||
| const body = await response.json(); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(body).toEqual({ | ||
| status: "success", | ||
| id: "dom-1", | ||
| domain: "seekermusic.com", | ||
| organization_id: ORG_ID, | ||
| }); | ||
| expect(selectOrganizationDomain).toHaveBeenCalledWith("seekermusic.com"); | ||
| expect(insertOrganizationDomain).toHaveBeenCalledWith({ | ||
| domain: "seekermusic.com", | ||
| organizationId: ORG_ID, | ||
| }); | ||
| }); | ||
|
|
||
| it("is idempotent when the domain is already mapped to the same org", async () => { | ||
| vi.mocked(selectOrganizationDomain).mockResolvedValue({ | ||
| id: "dom-1", | ||
| domain: "seekermusic.com", | ||
| organization_id: ORG_ID, | ||
| created_at: "2026-01-01", | ||
| }); | ||
|
|
||
| const response = await addOrgDomainHandler( | ||
| makeRequest({ organizationId: ORG_ID, domain: "seekermusic.com" }), | ||
| ); | ||
| const body = await response.json(); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(body).toEqual({ | ||
| status: "success", | ||
| id: "dom-1", | ||
| domain: "seekermusic.com", | ||
| organization_id: ORG_ID, | ||
| }); | ||
| expect(insertOrganizationDomain).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("error cases", () => { | ||
| it("returns the auth error response when unauthenticated", async () => { | ||
| const authError = NextResponse.json( | ||
| { status: "error", message: "unauthorized" }, | ||
| { status: 401 }, | ||
| ); | ||
| vi.mocked(validateAuthContext).mockResolvedValue(authError); | ||
|
|
||
| const response = await addOrgDomainHandler( | ||
| makeRequest({ organizationId: ORG_ID, domain: "seekermusic.com" }), | ||
| ); | ||
|
|
||
| expect(response.status).toBe(401); | ||
| expect(insertOrganizationDomain).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns 400 for an invalid body", async () => { | ||
| const response = await addOrgDomainHandler( | ||
| makeRequest({ organizationId: ORG_ID, domain: "not a domain" }), | ||
| ); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| const body = await response.json(); | ||
| expect(body.status).toBe("error"); | ||
| expect(typeof body.error).toBe("string"); | ||
| }); | ||
|
|
||
| it("returns 403 when the caller cannot manage the organization", async () => { | ||
| vi.mocked(canManageOrganization).mockResolvedValue(false); | ||
|
|
||
| const response = await addOrgDomainHandler( | ||
| makeRequest({ organizationId: ORG_ID, domain: "seekermusic.com" }), | ||
| ); | ||
| const body = await response.json(); | ||
|
|
||
| expect(response.status).toBe(403); | ||
| expect(body).toEqual({ | ||
| status: "error", | ||
| error: "Access denied to specified organization_id", | ||
| }); | ||
| expect(insertOrganizationDomain).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns 409 when the domain is mapped to a different org", async () => { | ||
| vi.mocked(selectOrganizationDomain).mockResolvedValue({ | ||
| id: "dom-1", | ||
| domain: "seekermusic.com", | ||
| organization_id: OTHER_ORG_ID, | ||
| created_at: "2026-01-01", | ||
| }); | ||
|
|
||
| const response = await addOrgDomainHandler( | ||
| makeRequest({ organizationId: ORG_ID, domain: "seekermusic.com" }), | ||
| ); | ||
| const body = await response.json(); | ||
|
|
||
| expect(response.status).toBe(409); | ||
| expect(body.status).toBe("error"); | ||
| expect(body.error).toContain("already mapped"); | ||
| expect(insertOrganizationDomain).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns 500 when the insert fails", async () => { | ||
| vi.mocked(insertOrganizationDomain).mockResolvedValue(null); | ||
|
|
||
| const response = await addOrgDomainHandler( | ||
| makeRequest({ organizationId: ORG_ID, domain: "seekermusic.com" }), | ||
| ); | ||
| const body = await response.json(); | ||
|
|
||
| expect(response.status).toBe(500); | ||
| expect(body.status).toBe("error"); | ||
| expect(typeof body.error).toBe("string"); | ||
| }); | ||
|
|
||
| it("returns 500 and does not insert when the existing-mapping lookup fails", async () => { | ||
| vi.mocked(selectOrganizationDomain).mockRejectedValue( | ||
| new Error("Failed to fetch organization_domain: boom"), | ||
| ); | ||
|
|
||
| const response = await addOrgDomainHandler( | ||
| makeRequest({ organizationId: ORG_ID, domain: "seekermusic.com" }), | ||
| ); | ||
| const body = await response.json(); | ||
|
|
||
| expect(response.status).toBe(500); | ||
| expect(body).toEqual({ status: "error", error: "Internal server error" }); | ||
| expect(insertOrganizationDomain).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns a generic 500 without leaking exception details when a dependency throws", async () => { | ||
| vi.mocked(insertOrganizationDomain).mockRejectedValue(new Error("SECRET_DB_DETAIL")); | ||
|
|
||
| const response = await addOrgDomainHandler( | ||
| makeRequest({ organizationId: ORG_ID, domain: "seekermusic.com" }), | ||
| ); | ||
| const body = await response.json(); | ||
|
|
||
| expect(response.status).toBe(500); | ||
| expect(body).toEqual({ status: "error", error: "Internal server error" }); | ||
| expect(JSON.stringify(body)).not.toContain("SECRET_DB_DETAIL"); | ||
| }); | ||
| }); | ||
| }); | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.