diff --git a/api/src/controllers/profiles/watchlist.ts b/api/src/controllers/profiles/watchlist.ts index ae3256e4..d579cba5 100644 --- a/api/src/controllers/profiles/watchlist.ts +++ b/api/src/controllers/profiles/watchlist.ts @@ -1,6 +1,12 @@ -import { type SQL, and, eq, isNotNull, isNull, sql } from "drizzle-orm"; +import { and, eq, isNotNull, isNull, sql } from "drizzle-orm"; import Elysia, { t } from "elysia"; import { auth, getUserInfo } from "~/auth"; +import { + getShows, + showFilters, + showSort, + watchStatusQ, +} from "~/controllers/shows/logic"; import { db } from "~/db"; import { profiles, shows } from "~/db/schema"; import { watchlist } from "~/db/schema/watchlist"; @@ -19,7 +25,6 @@ import { } from "~/models/utils"; import { desc } from "~/models/utils/descriptions"; import { MovieWatchStatus, SerieWatchStatus } from "~/models/watchlist"; -import { getShows, showFilters, showSort, watchStatusQ } from "./shows/logic"; async function setWatchStatus({ show, @@ -72,7 +77,7 @@ async function setWatchStatus({ }) .returning({ ...getColumns(watchlist), - percent: sql`${watchlist.seenCount}`.as("percent"), + percent: sql`${watchlist.seenCount}`.as("percent"), }); return ret; } @@ -82,7 +87,10 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) .guard( { query: t.Object({ - sort: showSort, + sort: { + ...showSort, + default: ["watchStatus", ...showSort.default], + }, filter: t.Optional(Filter({ def: showFilters })), query: t.Optional(t.String({ description: desc.query })), limit: t.Integer({ @@ -233,7 +241,7 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) }), body: SerieWatchStatus, response: { - 200: t.Union([SerieWatchStatus, DbMetadata]), + 200: t.Intersect([SerieWatchStatus, DbMetadata]), 404: KError, }, permissions: ["core.read"], @@ -280,7 +288,7 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) }), body: t.Omit(MovieWatchStatus, ["percent"]), response: { - 200: t.Union([MovieWatchStatus, DbMetadata]), + 200: t.Intersect([MovieWatchStatus, DbMetadata]), 404: KError, }, permissions: ["core.read"], diff --git a/api/tests/movies/watchstatus.test.ts b/api/tests/movies/watchstatus.test.ts index 609133d8..eb6cf011 100644 --- a/api/tests/movies/watchstatus.test.ts +++ b/api/tests/movies/watchstatus.test.ts @@ -67,6 +67,21 @@ describe("Set & get watch status", () => { }); }); + it("Can filter watchlist", async () => { + let [resp, body] = await getWatchlist("me", { + filter: "watchStatus eq rewatching", + }); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(1); + expect(body.items[0].slug).toBe(bubble.slug); + + [resp, body] = await getWatchlist("me", { + filter: "watchStatus eq completed", + }); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(0); + }); + it("Return watchstatus in /shows", async () => { const [resp, body] = await getShows({}); expectStatus(resp, body).toBe(200);