Update available count of shows when inserting videos

This commit is contained in:
Zoe Roux 2025-05-03 16:07:06 +02:00
parent e26bc931f5
commit 205dda652a
No known key found for this signature in database
3 changed files with 18 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import { and, count, eq, exists, ne, sql } from "drizzle-orm";
import { type SQLWrapper, and, count, eq, exists, ne, sql } from "drizzle-orm";
import { type Transaction, db } from "~/db";
import { entries, entryVideoJoin, showTranslations, shows } from "~/db/schema";
import { conflictUpdateAllExcept, sqlarr } from "~/db/utils";
@ -138,9 +138,10 @@ async function insertBaseShow(tx: Transaction, show: Show) {
export async function updateAvailableCount(
tx: Transaction,
showPks: number[],
updateEntryCount = true,
showPks: number[] | SQLWrapper,
updateEntryCount = false,
) {
const showPkQ = Array.isArray(showPks) ? sqlarr(showPks) : showPks;
return await tx
.update(shows)
.set({
@ -168,5 +169,5 @@ export async function updateAvailableCount(
)}`,
}),
})
.where(eq(shows.pk, sql`any(${sqlarr(showPks)})`));
.where(eq(shows.pk, sql`any(${showPkQ})`));
}

View File

@ -8,6 +8,7 @@ import {
isUniqueConstraint,
jsonbBuildObject,
jsonbObjectAgg,
sqlarr,
values,
} from "~/db/utils";
import { KError } from "~/models/error";
@ -343,6 +344,17 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] })
},
{} as Record<number, { slug: string }[]>,
);
await updateAvailableCount(
tx,
tx
.selectDistinct({ pk: entries.showPk })
.from(entries)
.where(
eq(entries.pk, sql`any(${sqlarr(ret.map((x) => x.entryPk))})`),
),
);
return error(
201,
vids.map((x) => ({

View File

@ -4,7 +4,7 @@ import type { SelectResultField } from "drizzle-orm/query-builders/select.types"
export const buildRelations = <
R extends string,
P extends object,
Rel extends Record<R, (languages: P) => Subquery>,
Rel extends Record<R, (params: P) => Subquery>,
>(
enabled: R[],
relations: Rel,