Update availableSince of entries on POST /videos

This commit is contained in:
Zoe Roux 2025-05-03 16:36:31 +02:00
parent 205dda652a
commit 46d98e038d
No known key found for this signature in database
3 changed files with 43 additions and 27 deletions

View File

@ -10,7 +10,7 @@ import { conflictUpdateAllExcept, sqlarr, values } from "~/db/utils";
import type { SeedEntry as SEntry, SeedExtra as SExtra } from "~/models/entry"; import type { SeedEntry as SEntry, SeedExtra as SExtra } from "~/models/entry";
import { enqueueOptImage } from "../images"; import { enqueueOptImage } from "../images";
import { guessNextRefresh } from "../refresh"; import { guessNextRefresh } from "../refresh";
import { updateAvailableCount } from "./shows"; import { updateAvailableCount, updateAvailableSince } from "./shows";
type SeedEntry = SEntry & { type SeedEntry = SEntry & {
video?: undefined; video?: undefined;
@ -192,16 +192,7 @@ export const insertEntries = async (
if (!onlyExtras) if (!onlyExtras)
await updateAvailableCount(tx, [show.pk], show.kind === "serie"); await updateAvailableCount(tx, [show.pk], show.kind === "serie");
const entriesPk = [...new Set(vids.map((x) => x.entryPk))]; await updateAvailableSince(tx,[...new Set(vids.map((x) => x.entryPk))]);
await tx
.update(entries)
.set({ availableSince: sql`now()` })
.where(
and(
eq(entries.pk, sql`any(${sqlarr(entriesPk)})`),
isNull(entries.availableSince),
),
);
return ret; return ret;
}); });

View File

@ -1,4 +1,13 @@
import { type SQLWrapper, and, count, eq, exists, ne, sql } from "drizzle-orm"; import {
type SQLWrapper,
and,
count,
eq,
exists,
isNull,
ne,
sql,
} from "drizzle-orm";
import { type Transaction, db } from "~/db"; import { type Transaction, db } from "~/db";
import { entries, entryVideoJoin, showTranslations, shows } from "~/db/schema"; import { entries, entryVideoJoin, showTranslations, shows } from "~/db/schema";
import { conflictUpdateAllExcept, sqlarr } from "~/db/utils"; import { conflictUpdateAllExcept, sqlarr } from "~/db/utils";
@ -171,3 +180,18 @@ export async function updateAvailableCount(
}) })
.where(eq(shows.pk, sql`any(${showPkQ})`)); .where(eq(shows.pk, sql`any(${showPkQ})`));
} }
export async function updateAvailableSince(
tx: Transaction,
entriesPk: number[],
) {
return await tx
.update(entries)
.set({ availableSince: sql`now()` })
.where(
and(
eq(entries.pk, sql`any(${sqlarr(entriesPk)})`),
isNull(entries.availableSince),
),
);
}

View File

@ -26,7 +26,10 @@ import { desc as description } from "~/models/utils/descriptions";
import { Guesses, SeedVideo, Video } from "~/models/video"; import { Guesses, SeedVideo, Video } from "~/models/video";
import { comment } from "~/utils"; import { comment } from "~/utils";
import { computeVideoSlug } from "./seed/insert/entries"; import { computeVideoSlug } from "./seed/insert/entries";
import { updateAvailableCount } from "./seed/insert/shows"; import {
updateAvailableCount,
updateAvailableSince,
} from "./seed/insert/shows";
const CreatedVideo = t.Object({ const CreatedVideo = t.Object({
id: t.String({ format: "uuid" }), id: t.String({ format: "uuid" }),
@ -345,15 +348,15 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] })
{} as Record<number, { slug: string }[]>, {} as Record<number, { slug: string }[]>,
); );
const entriesPk = [...new Set(ret.map((x) => x.entryPk))];
await updateAvailableCount( await updateAvailableCount(
tx, tx,
tx tx
.selectDistinct({ pk: entries.showPk }) .selectDistinct({ pk: entries.showPk })
.from(entries) .from(entries)
.where( .where(eq(entries.pk, sql`any(${sqlarr(entriesPk)})`)),
eq(entries.pk, sql`any(${sqlarr(ret.map((x) => x.entryPk))})`),
),
); );
await updateAvailableSince(tx, entriesPk);
return error( return error(
201, 201,
@ -405,18 +408,16 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] })
.where( .where(
and( and(
inArray(entryVideoJoin.videoPk, tx.select().from(vids)), inArray(entryVideoJoin.videoPk, tx.select().from(vids)),
not( notExists(
exists( tx
tx .select()
.select() .from(evj)
.from(evj) .where(
.where( and(
and( eq(evj.entryPk, entryVideoJoin.entryPk),
eq(evj.entryPk, entryVideoJoin.entryPk), not(inArray(evj.videoPk, db.select().from(vids))),
not(inArray(evj.videoPk, db.select().from(vids))),
),
), ),
), ),
), ),
), ),
), ),