-
Notifications
You must be signed in to change notification settings - Fork 10
feat: optional catalog_id on POST /api/emails leads the email with the value delta #799
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
2
commits into
main
Choose a base branch
from
feat/email-valuation-delta
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
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,85 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { getCatalogValuationDelta } from "../getCatalogValuationDelta"; | ||
| import { selectCatalogValuations } from "@/lib/supabase/catalog_valuations/selectCatalogValuations"; | ||
| import { selectCatalogMeasurementsAggregate } from "@/lib/supabase/song_measurements/selectCatalogMeasurementsAggregate"; | ||
| import { getCatalogEarliestReleaseDate } from "../getCatalogEarliestReleaseDate"; | ||
|
|
||
| vi.mock("@/lib/supabase/catalog_valuations/selectCatalogValuations", () => ({ | ||
| selectCatalogValuations: vi.fn(), | ||
| })); | ||
| vi.mock("@/lib/supabase/song_measurements/selectCatalogMeasurementsAggregate", () => ({ | ||
| selectCatalogMeasurementsAggregate: vi.fn(), | ||
| })); | ||
| vi.mock("../getCatalogEarliestReleaseDate", () => ({ getCatalogEarliestReleaseDate: vi.fn() })); | ||
|
|
||
| const catalogId = "740d5050-40ec-4892-a040-b78bb50fef2f"; | ||
| const row = (mid: number, measured_at: string) => ({ | ||
| low: mid * 0.8, | ||
| mid, | ||
| high: mid * 1.2, | ||
| measured_at, | ||
| }); | ||
|
|
||
| describe("getCatalogValuationDelta", () => { | ||
| beforeEach(() => vi.clearAllMocks()); | ||
|
|
||
| it("returns current + previous from the two most recent history rows", async () => { | ||
| vi.mocked(selectCatalogValuations).mockResolvedValue([ | ||
| row(1_100_000, "2026-07-29T00:00:00Z"), | ||
| row(1_000_000, "2026-07-22T00:00:00Z"), | ||
| ] as never); | ||
|
|
||
| const delta = await getCatalogValuationDelta({ catalogId }); | ||
|
|
||
| expect(selectCatalogValuations).toHaveBeenCalledWith({ catalogId, limit: 2 }); | ||
| expect(delta).toMatchObject({ | ||
| current: { mid: 1_100_000, measured_at: "2026-07-29T00:00:00Z" }, | ||
| previous: { mid: 1_000_000 }, | ||
| }); | ||
| expect(selectCatalogMeasurementsAggregate).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns a baseline (previous null) when only one history row exists", async () => { | ||
| vi.mocked(selectCatalogValuations).mockResolvedValue([ | ||
| row(1_100_000, "2026-07-29T00:00:00Z"), | ||
| ] as never); | ||
|
|
||
| const delta = await getCatalogValuationDelta({ catalogId }); | ||
|
|
||
| expect(delta?.previous).toBeNull(); | ||
| expect(delta?.current.mid).toBe(1_100_000); | ||
| }); | ||
|
|
||
| it("falls back to the read-time band when the history is empty but songs are measured", async () => { | ||
| vi.mocked(selectCatalogValuations).mockResolvedValue([]); | ||
| vi.mocked(selectCatalogMeasurementsAggregate).mockResolvedValue({ | ||
| measuredSongCount: 10, | ||
| totalStreams: 1_000_000, | ||
| }); | ||
| vi.mocked(getCatalogEarliestReleaseDate).mockResolvedValue("2016-07-01"); | ||
|
|
||
| const delta = await getCatalogValuationDelta({ catalogId }); | ||
|
|
||
| expect(delta).not.toBeNull(); | ||
| expect(delta?.previous).toBeNull(); | ||
| expect(delta?.current.mid).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it("returns null when the catalog has no measured songs at all", async () => { | ||
| vi.mocked(selectCatalogValuations).mockResolvedValue([]); | ||
| vi.mocked(selectCatalogMeasurementsAggregate).mockResolvedValue({ | ||
| measuredSongCount: 0, | ||
| totalStreams: 0, | ||
| }); | ||
|
|
||
| const delta = await getCatalogValuationDelta({ catalogId }); | ||
|
|
||
| expect(delta).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null and never throws when the history read errors", async () => { | ||
| vi.mocked(selectCatalogValuations).mockRejectedValue(new Error("down")); | ||
|
|
||
| await expect(getCatalogValuationDelta({ catalogId })).resolves.toBeNull(); | ||
| }); | ||
| }); | ||
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); | ||
|
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: The 500-error test only checks Prompt for AI agents |
||
| }); | ||
| }); | ||
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); | ||
| }); | ||
| }); | ||
| }); |
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: Fallback test sets up aggregate/earliest-release mocks but never asserts they were called, so the test can pass even if the internal fallback wiring is broken (e.g., the function starts using a different data source instead of
selectCatalogMeasurementsAggregate).Prompt for AI agents