Add isAvailable filter for both entries & shows

This commit is contained in:
Zoe Roux 2025-06-05 00:21:40 +02:00
parent 107e581801
commit be4b4f016b
No known key found for this signature in database
5 changed files with 19 additions and 4 deletions

View File

@ -72,6 +72,7 @@ export const entryFilters: FilterDef = {
runtime: { column: entries.runtime, type: "float" }, runtime: { column: entries.runtime, type: "float" },
airDate: { column: entries.airDate, type: "date" }, airDate: { column: entries.airDate, type: "date" },
playedDate: { column: entryProgressQ.playedDate, type: "date" }, playedDate: { column: entryProgressQ.playedDate, type: "date" },
isAvailable: { column: isNotNull(entries.availableSince), type: "bool" },
}; };
const extraFilters: FilterDef = { const extraFilters: FilterDef = {

View File

@ -1,4 +1,4 @@
import { type SQL, and, eq, exists, ne, sql } from "drizzle-orm"; import { type SQL, and, eq, exists, gt, ne, sql } from "drizzle-orm";
import { db } from "~/db"; import { db } from "~/db";
import { import {
entries, entries,
@ -60,7 +60,7 @@ export const showFilters: FilterDef = {
runtime: { column: shows.runtime, type: "float" }, runtime: { column: shows.runtime, type: "float" },
airDate: { column: shows.startAir, type: "date" }, airDate: { column: shows.startAir, type: "date" },
startAir: { column: shows.startAir, type: "date" }, startAir: { column: shows.startAir, type: "date" },
endAir: { column: shows.startAir, type: "date" }, endAir: { column: shows.endAir, type: "date" },
originalLanguage: { originalLanguage: {
column: sql`${shows.original}->'language'`, column: sql`${shows.original}->'language'`,
type: "string", type: "string",
@ -76,6 +76,7 @@ export const showFilters: FilterDef = {
values: WatchlistStatus.enum, values: WatchlistStatus.enum,
}, },
score: { column: watchStatusQ.score, type: "int" }, score: { column: watchStatusQ.score, type: "int" },
isAvailable: { column: sql`(${shows.availableCount} > 0)`, type: "bool" },
}; };
export const showSort = Sort( export const showSort = Sort(
{ {

View File

@ -9,7 +9,7 @@ export type FilterDef = {
[key: string]: [key: string]:
| { | {
column: Column | SQLWrapper; column: Column | SQLWrapper;
type: "int" | "float" | "date" | "string"; type: "int" | "float" | "date" | "string" | "bool";
isArray?: boolean; isArray?: boolean;
} }
| { | {

View File

@ -29,7 +29,8 @@ export type Value =
| { type: "float"; value: number } | { type: "float"; value: number }
| { type: "date"; value: string } | { type: "date"; value: string }
| { type: "string"; value: string } | { type: "string"; value: string }
| { type: "enum"; value: string }; | { type: "enum"; value: string }
| { type: "bool"; value: boolean };
const operators = ["eq", "ne", "gt", "ge", "lt", "le", "has"] as const; const operators = ["eq", "ne", "gt", "ge", "lt", "le", "has"] as const;
export type Operator = (typeof operators)[number]; export type Operator = (typeof operators)[number];
export type Expression = export type Expression =

View File

@ -48,6 +48,18 @@ export const toDrizzle = (expr: Expression, config: FilterDef): SQL => {
// but parser doesn't know if an enum should be a string // but parser doesn't know if an enum should be a string
expr.value = { type: "string", value: expr.value.value }; expr.value = { type: "string", value: expr.value.value };
} }
if (prop.type === "bool" && expr.value.type === "enum") {
if (expr.value.value !== "false" && expr.value.value !== "true") {
throw new KErrorT(
comment`
Invalid value for property ${expr.property}.
Get ${expr.value.value} but expected true or false.
`,
{ in: where },
);
}
expr.value = { type: "bool", value: expr.value.value === "true" }
}
if (prop.type !== expr.value.type) { if (prop.type !== expr.value.type) {
throw new KErrorT( throw new KErrorT(
comment` comment`