Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add developer management API routes

Richie McIlroye24876d4bf5a
diff --git a/apps/web/app/api/developer/v1/[...route]/route.ts b/apps/web/app/api/developer/v1/[...route]/route.ts
new file mode 100644
index 000000000..590c20609
--- /dev/null
+++ b/apps/web/app/api/developer/v1/[...route]/route.ts
@@ -0,0 +1,14 @@
+import { Hono } from "hono";
+import { handle } from "hono/vercel";
+
+import * as usage from "./usage";
+import * as videos from "./videos";
+
+const app = new Hono()
+ .basePath("/api/developer/v1")
+ .route("/videos", videos.app)
+ .route("/usage", usage.app);
+
+export const GET = handle(app);
+export const POST = handle(app);
+export const DELETE = handle(app);
diff --git a/apps/web/app/api/developer/v1/[...route]/usage.ts b/apps/web/app/api/developer/v1/[...route]/usage.ts
new file mode 100644
index 000000000..d47858698
--- /dev/null
+++ b/apps/web/app/api/developer/v1/[...route]/usage.ts
@@ -0,0 +1,47 @@
+import { db } from "@cap/database";
+import { developerCreditAccounts, developerVideos } from "@cap/database/schema";
+import { and, count, eq, isNull, sql } from "drizzle-orm";
+import { Hono } from "hono";
+import { withDeveloperSecretAuth } from "../../../utils";
+
+export const app = new Hono<{
+ Variables: {
+ developerAppId: string;
+ developerKeyType: "secret";
+ };
+}>().use(withDeveloperSecretAuth);
+
+app.get("/", async (c) => {
+ const appId = c.get("developerAppId");
+
+ const [[account], [videoStats]] = await Promise.all([
+ db()
+ .select()
+ .from(developerCreditAccounts)
+ .where(eq(developerCreditAccounts.appId, appId))
+ .limit(1),
+ db()
+ .select({
+ totalVideos: count(),
+ totalDurationMinutes: sql<number>`COALESCE(SUM(${developerVideos.duration}) / 60, 0)`,
+ })
+ .from(developerVideos)
+ .where(
+ and(
+ eq(developerVideos.appId, appId),
+ isNull(developerVideos.deletedAt),
+ ),
+ ),
+ ]);
+
+ return c.json({
+ data: {
+ balanceMicroCredits: account?.balanceMicroCredits ?? 0,
+ balanceDollars: ((account?.balanceMicroCredits ?? 0) / 100_000).toFixed(
+ 2,
+ ),
+ totalVideos: videoStats?.totalVideos ?? 0,
+ totalDurationMinutes: videoStats?.totalDurationMinutes ?? 0,
+ },
+ });
+});
diff --git a/apps/web/app/api/developer/v1/[...route]/videos.ts b/apps/web/app/api/developer/v1/[...route]/videos.ts
new file mode 100644
index 000000000..bf0a1f08a
--- /dev/null
+++ b/apps/web/app/api/developer/v1/[...route]/videos.ts
@@ -0,0 +1,125 @@
+import { db } from "@cap/database";
+import { developerVideos } from "@cap/database/schema";
+import { and, desc, eq, isNull } from "drizzle-orm";
+import { Hono } from "hono";
+import { withDeveloperSecretAuth } from "../../../utils";
+
+export const app = new Hono<{
+ Variables: {
+ developerAppId: string;
+ developerKeyType: "secret";
+ };
+}>().use(withDeveloperSecretAuth);
+
+app.get("/", async (c) => {
+ const appId = c.get("developerAppId");
+ const userId = c.req.query("userId");
+ const limit = Math.min(Number(c.req.query("limit") ?? 50), 100);
+ const offset = Number(c.req.query("offset") ?? 0);
+
+ const conditions = [
+ eq(developerVideos.appId, appId),
+ isNull(developerVideos.deletedAt),
+ ];
+
+ if (userId) {
+ conditions.push(eq(developerVideos.externalUserId, userId));
+ }
+
+ const videos = await db()
+ .select()
+ .from(developerVideos)
+ .where(and(...conditions))
+ .orderBy(desc(developerVideos.createdAt))
+ .limit(limit)
+ .offset(offset);
+
+ return c.json({ data: videos });
+});
+
+app.get("/:id", async (c) => {
+ const appId = c.get("developerAppId");
+ const videoId = c.req.param("id");
+
+ const [video] = await db()
+ .select()
+ .from(developerVideos)
+ .where(
+ and(
+ eq(developerVideos.id, videoId),
+ eq(developerVideos.appId, appId),
+ isNull(developerVideos.deletedAt),
+ ),
+ )
+ .limit(1);
+
+ if (!video) {
+ return c.json({ error: "Video not found" }, 404);
+ }
+
+ return c.json({ data: video });
+});
+
+app.delete("/:id", async (c) => {
+ const appId = c.get("developerAppId");
+ const videoId = c.req.param("id");
+
+ const [video] = await db()
+ .select()
+ .from(developerVideos)
+ .where(
+ and(
+ eq(developerVideos.id, videoId),
+ eq(developerVideos.appId, appId),
+ isNull(developerVideos.deletedAt),
+ ),
+ )
+ .limit(1);
+
+ if (!video) {
+ return c.json({ error: "Video not found" }, 404);
+ }
+
+ await db()
+ .update(developerVideos)
+ .set({ deletedAt: new Date() })
+ .where(eq(developerVideos.id, videoId));
+
+ return c.json({ success: true });
+});
+
+app.get("/:id/status", async (c) => {
+ const appId = c.get("developerAppId");
+ const videoId = c.req.param("id");
+
+ const [video] = await db()
+ .select({
+ id: developerVideos.id,
+ duration: developerVideos.duration,
+ width: developerVideos.width,
+ height: developerVideos.height,
+ transcriptionStatus: developerVideos.transcriptionStatus,
+ })
+ .from(developerVideos)
+ .where(
+ and(
+ eq(developerVideos.id, videoId),
+ eq(developerVideos.appId, appId),
+ isNull(developerVideos.deletedAt),
+ ),
+ )
+ .limit(1);
+
+ if (!video) {
+ return c.json({ error: "Video not found" }, 404);
+ }
+
+ const ready = video.duration !== null && video.width !== null;
+
+ return c.json({
+ data: {
+ ...video,
+ ready,
+ },
+ });
+});