-
Notifications
You must be signed in to change notification settings - Fork 10
feat: persist catalog valuations + GET /api/catalogs/{catalogId}/valuations #795
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
sweetmantech
wants to merge
1
commit into
main
Choose a base branch
from
feat/catalog-valuations-api
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
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,32 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; | ||
| import { getCatalogValuationsHandler } from "@/lib/catalog/getCatalogValuationsHandler"; | ||
|
|
||
| /** | ||
| * OPTIONS /api/catalogs/{catalogId}/valuations — CORS preflight. | ||
| * | ||
| * @returns A NextResponse with CORS headers. | ||
| */ | ||
| export async function OPTIONS() { | ||
| return new NextResponse(null, { | ||
| status: 200, | ||
| headers: getCorsHeaders(), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * GET /api/catalogs/{catalogId}/valuations — the catalog's persisted | ||
| * valuation history, latest-first (limit=1 is the current value). | ||
| * | ||
| * @param request - The request object. | ||
| * @param options - Route options containing params. | ||
| * @param options.params - Route params containing the catalogId. | ||
| * @returns A NextResponse with the valuation series. | ||
| */ | ||
| export async function GET( | ||
| request: NextRequest, | ||
| options: { params: Promise<{ catalogId: string }> }, | ||
| ) { | ||
| const { catalogId } = await options.params; | ||
| return getCatalogValuationsHandler(request, catalogId); | ||
| } |
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
115 changes: 115 additions & 0 deletions
115
lib/catalog/__tests__/getCatalogValuationsHandler.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,115 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { getCatalogValuationsHandler } from "../getCatalogValuationsHandler"; | ||
| import { validateGetCatalogValuationsQuery } from "../validateGetCatalogValuationsQuery"; | ||
| import { selectAccountCatalog } from "@/lib/supabase/account_catalogs/selectAccountCatalog"; | ||
| import { selectCatalogValuations } from "@/lib/supabase/catalog_valuations/selectCatalogValuations"; | ||
|
|
||
| vi.mock("@/lib/networking/getCorsHeaders", () => ({ | ||
| getCorsHeaders: vi.fn(() => ({ "Access-Control-Allow-Origin": "*" })), | ||
| })); | ||
| vi.mock("../validateGetCatalogValuationsQuery", () => ({ | ||
| validateGetCatalogValuationsQuery: vi.fn(), | ||
| })); | ||
| vi.mock("@/lib/supabase/account_catalogs/selectAccountCatalog", () => ({ | ||
| selectAccountCatalog: vi.fn(), | ||
| })); | ||
| vi.mock("@/lib/supabase/catalog_valuations/selectCatalogValuations", () => ({ | ||
| selectCatalogValuations: vi.fn(), | ||
| })); | ||
|
|
||
| const accountId = "550e8400-e29b-41d4-a716-446655440000"; | ||
| const catalogId = "740d5050-40ec-4892-a040-b78bb50fef2f"; | ||
|
|
||
| const makeRequest = () => new NextRequest(`http://localhost/api/catalogs/${catalogId}/valuations`); | ||
|
|
||
| const okQuery = () => | ||
| vi | ||
| .mocked(validateGetCatalogValuationsQuery) | ||
| .mockResolvedValue({ accountId, catalogId, limit: 30 }); | ||
|
|
||
| const okCatalog = () => | ||
| vi | ||
| .mocked(selectAccountCatalog) | ||
| .mockResolvedValue({ account: accountId, catalog: catalogId } as never); | ||
|
|
||
| describe("getCatalogValuationsHandler", () => { | ||
| beforeEach(() => vi.clearAllMocks()); | ||
|
|
||
| it("short-circuits with the validator error (auth + params live in the validator)", async () => { | ||
| const denied = NextResponse.json({ status: "error" }, { status: 401 }); | ||
| vi.mocked(validateGetCatalogValuationsQuery).mockResolvedValue(denied); | ||
|
|
||
| const response = await getCatalogValuationsHandler(makeRequest(), catalogId); | ||
|
|
||
| expect(response).toBe(denied); | ||
| expect(selectAccountCatalog).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns 404 when the catalog is not owned by the caller (or missing)", async () => { | ||
| okQuery(); | ||
| vi.mocked(selectAccountCatalog).mockResolvedValue(null); | ||
|
|
||
| const response = await getCatalogValuationsHandler(makeRequest(), catalogId); | ||
|
|
||
| expect(response.status).toBe(404); | ||
| expect(selectCatalogValuations).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns the valuation series shaped to the documented contract", async () => { | ||
| okQuery(); | ||
| okCatalog(); | ||
| const rows = [ | ||
| { | ||
| id: "v2", | ||
| catalog_id: catalogId, | ||
| low: "1000", | ||
| mid: "2000", | ||
| high: "3000", | ||
| measured_song_count: 12, | ||
| total_streams: 456789, | ||
| measured_at: "2026-07-29T00:00:00Z", | ||
| created_at: "2026-07-29T00:00:00Z", | ||
| }, | ||
| ]; | ||
| vi.mocked(selectCatalogValuations).mockResolvedValue(rows as never); | ||
|
|
||
| const response = await getCatalogValuationsHandler(makeRequest(), catalogId); | ||
| const body = await response.json(); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(selectCatalogValuations).toHaveBeenCalledWith({ catalogId, limit: 30 }); | ||
| expect(body.valuations).toEqual([ | ||
| { | ||
| low: 1000, | ||
| mid: 2000, | ||
| high: 3000, | ||
| measured_song_count: 12, | ||
| total_streams: 456789, | ||
| measured_at: "2026-07-29T00:00:00Z", | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("returns an empty series when the catalog has no valuations yet", async () => { | ||
| okQuery(); | ||
| okCatalog(); | ||
| vi.mocked(selectCatalogValuations).mockResolvedValue([]); | ||
|
|
||
| const response = await getCatalogValuationsHandler(makeRequest(), catalogId); | ||
| const body = await response.json(); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(body.valuations).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns 500 when the select fails", async () => { | ||
| okQuery(); | ||
| okCatalog(); | ||
| vi.mocked(selectCatalogValuations).mockResolvedValue(null); | ||
|
|
||
| const response = await getCatalogValuationsHandler(makeRequest(), catalogId); | ||
|
|
||
| expect(response.status).toBe(500); | ||
| }); | ||
| }); | ||
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,94 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { persistCatalogValuation } from "../persistCatalogValuation"; | ||
| import { insertCatalogValuation } from "@/lib/supabase/catalog_valuations/insertCatalogValuation"; | ||
| import { selectCatalogValuations } from "@/lib/supabase/catalog_valuations/selectCatalogValuations"; | ||
|
|
||
| vi.mock("@/lib/supabase/catalog_valuations/insertCatalogValuation", () => ({ | ||
| insertCatalogValuation: vi.fn(), | ||
| })); | ||
| vi.mock("@/lib/supabase/catalog_valuations/selectCatalogValuations", () => ({ | ||
| selectCatalogValuations: vi.fn(), | ||
| })); | ||
|
|
||
| const catalogId = "740d5050-40ec-4892-a040-b78bb50fef2f"; | ||
| const input = { | ||
| catalogId, | ||
| valuation: { low: 1000, mid: 2000, high: 3000 }, | ||
| measuredSongCount: 12, | ||
| totalStreams: 456789, | ||
| }; | ||
|
|
||
| describe("persistCatalogValuation", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("inserts a snake_case row from the camelCase inputs", async () => { | ||
| vi.mocked(insertCatalogValuation).mockResolvedValue({ id: "v1" } as never); | ||
|
|
||
| await persistCatalogValuation(input); | ||
|
|
||
| expect(insertCatalogValuation).toHaveBeenCalledWith({ | ||
| catalog_id: catalogId, | ||
| low: 1000, | ||
| mid: 2000, | ||
| high: 3000, | ||
| measured_song_count: 12, | ||
| total_streams: 456789, | ||
| }); | ||
| expect(selectCatalogValuations).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("never throws when the insert fails (best-effort)", async () => { | ||
| vi.mocked(insertCatalogValuation).mockRejectedValue(new Error("down")); | ||
|
|
||
| await expect(persistCatalogValuation(input)).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| describe("dedupeDaily", () => { | ||
| it("skips the insert when a row already exists for today (UTC)", async () => { | ||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date("2026-07-29T18:00:00Z")); | ||
| vi.mocked(selectCatalogValuations).mockResolvedValue([ | ||
| { measured_at: "2026-07-29T02:00:00Z" } as never, | ||
| ]); | ||
|
|
||
| await persistCatalogValuation({ ...input, dedupeDaily: true }); | ||
|
|
||
| expect(selectCatalogValuations).toHaveBeenCalledWith({ catalogId, limit: 1 }); | ||
| expect(insertCatalogValuation).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("inserts when the newest row is from an earlier day", async () => { | ||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date("2026-07-29T18:00:00Z")); | ||
| vi.mocked(selectCatalogValuations).mockResolvedValue([ | ||
| { measured_at: "2026-07-28T23:59:00Z" } as never, | ||
| ]); | ||
| vi.mocked(insertCatalogValuation).mockResolvedValue({ id: "v2" } as never); | ||
|
|
||
| await persistCatalogValuation({ ...input, dedupeDaily: true }); | ||
|
|
||
| expect(insertCatalogValuation).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("inserts when the catalog has no valuations yet", async () => { | ||
| vi.mocked(selectCatalogValuations).mockResolvedValue([]); | ||
| vi.mocked(insertCatalogValuation).mockResolvedValue({ id: "v1" } as never); | ||
|
|
||
| await persistCatalogValuation({ ...input, dedupeDaily: true }); | ||
|
|
||
| expect(insertCatalogValuation).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("still inserts when the dedupe lookup errors (null)", async () => { | ||
| vi.mocked(selectCatalogValuations).mockResolvedValue(null); | ||
| vi.mocked(insertCatalogValuation).mockResolvedValue({ id: "v1" } as never); | ||
|
|
||
| await persistCatalogValuation({ ...input, dedupeDaily: true }); | ||
|
|
||
| expect(insertCatalogValuation).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| }); |
74 changes: 74 additions & 0 deletions
74
lib/catalog/__tests__/validateGetCatalogValuationsQuery.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,74 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { validateGetCatalogValuationsQuery } from "../validateGetCatalogValuationsQuery"; | ||
| import { validateAuthContext } from "@/lib/auth/validateAuthContext"; | ||
|
|
||
| vi.mock("@/lib/networking/getCorsHeaders", () => ({ | ||
| getCorsHeaders: vi.fn(() => ({ "Access-Control-Allow-Origin": "*" })), | ||
| })); | ||
| vi.mock("@/lib/auth/validateAuthContext", () => ({ validateAuthContext: vi.fn() })); | ||
|
|
||
| const accountId = "550e8400-e29b-41d4-a716-446655440000"; | ||
| const catalogId = "740d5050-40ec-4892-a040-b78bb50fef2f"; | ||
|
|
||
| const makeRequest = (query = "") => | ||
| new NextRequest(`http://localhost/api/catalogs/${catalogId}/valuations${query}`); | ||
|
|
||
| const okAuth = () => | ||
| vi.mocked(validateAuthContext).mockResolvedValue({ accountId, orgId: null } as never); | ||
|
|
||
| describe("validateGetCatalogValuationsQuery", () => { | ||
| beforeEach(() => vi.clearAllMocks()); | ||
|
|
||
| it("short-circuits with the auth error before touching params", async () => { | ||
| const denied = NextResponse.json({ status: "error" }, { status: 401 }); | ||
| vi.mocked(validateAuthContext).mockResolvedValue(denied as never); | ||
|
|
||
| const result = await validateGetCatalogValuationsQuery(makeRequest(), catalogId); | ||
|
|
||
| expect(result).toBe(denied); | ||
| }); | ||
|
|
||
| it("returns the accountId, catalogId and default limit of 30", async () => { | ||
| okAuth(); | ||
|
|
||
| const result = await validateGetCatalogValuationsQuery(makeRequest(), catalogId); | ||
|
|
||
| expect(result).toEqual({ accountId, catalogId, limit: 30 }); | ||
| }); | ||
|
|
||
| it("accepts an explicit limit", async () => { | ||
| okAuth(); | ||
|
|
||
| const result = await validateGetCatalogValuationsQuery(makeRequest("?limit=1"), catalogId); | ||
|
|
||
| expect(result).toEqual({ accountId, catalogId, limit: 1 }); | ||
| }); | ||
|
|
||
| it("rejects a non-UUID catalogId with 400", async () => { | ||
| okAuth(); | ||
|
|
||
| const result = await validateGetCatalogValuationsQuery(makeRequest(), "not-a-uuid"); | ||
|
|
||
| expect(result).toBeInstanceOf(NextResponse); | ||
| expect((result as NextResponse).status).toBe(400); | ||
| }); | ||
|
|
||
| it("rejects limit above 100 with 400", async () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: Missing test coverage for the lower limit boundary. The schema rejects limit=0 and limit=-1 with 400, but only the upper boundary (limit=101) and non-numeric input are tested. Add a test for limit=0 or limit=-1 to match the boundary coverage on the lower end. Prompt for AI agents |
||
| okAuth(); | ||
|
|
||
| const result = await validateGetCatalogValuationsQuery(makeRequest("?limit=101"), catalogId); | ||
|
|
||
| expect(result).toBeInstanceOf(NextResponse); | ||
| expect((result as NextResponse).status).toBe(400); | ||
| }); | ||
|
|
||
| it("rejects a non-numeric limit with 400", async () => { | ||
| okAuth(); | ||
|
|
||
| const result = await validateGetCatalogValuationsQuery(makeRequest("?limit=abc"), catalogId); | ||
|
|
||
| expect(result).toBeInstanceOf(NextResponse); | ||
| expect((result as NextResponse).status).toBe(400); | ||
| }); | ||
| }); | ||
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.
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.
P2: The 500-error test only checks
response.status === 500and never verifies the response body. Per the team's established convention, tests for 500 responses should assert that raw exception text does not leak into the body (e.g., assertbody.error === "Internal server error"or that stack traces/DB errors are absent). Without this check, a future change that accidentally returns a dynamic error message would not be caught.Prompt for AI agents