feat: create route to be able to mark episode as watched

This commit is contained in:
2024-10-10 12:52:22 +02:00
parent 223c2f1e4c
commit 91dd250823
6 changed files with 197 additions and 18 deletions

View File

@@ -1,4 +1,5 @@
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import type { HonoRequest } from "hono";
import { env } from "hono/adapter";
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
@@ -65,6 +66,26 @@ const route = createRoute({
},
});
export async function updateWatchStatus(
env: Env,
req: HonoRequest,
deviceId: string,
titleId: number,
watchStatus: WatchStatus | null,
) {
const { wasAdded, wasDeleted } = await setWatchStatus(
env,
deviceId,
Number(titleId),
watchStatus,
);
if (wasAdded) {
await maybeScheduleNextAiringEpisode(env, req, titleId);
} else if (wasDeleted) {
await removeTask(env, "new-episode", buildNewEpisodeTaskId(titleId));
}
}
app.openapi(route, async (c) => {
const {
deviceId,
@@ -76,25 +97,13 @@ app.openapi(route, async (c) => {
if (!isRetrying) {
try {
const { wasAdded, wasDeleted } = await setWatchStatus(
env<Env, typeof c>(c, "workerd"),
await updateWatchStatus(
env(c, "workerd"),
c.req,
deviceId,
Number(titleId),
titleId,
watchStatus,
);
if (wasAdded) {
await maybeScheduleNextAiringEpisode(
env<Env, typeof c>(c, "workerd"),
c.req,
titleId,
);
} else if (wasDeleted) {
await removeTask(
env<Env, typeof c>(c, "workerd"),
"new-episode",
buildNewEpisodeTaskId(titleId),
);
}
} catch (error) {
console.error(new Error("Error setting watch status", { cause: error }));
console.error(error);