refactor(backend): simplify media schema and recording metadata

This commit is contained in:
2026-03-11 17:15:00 +00:00
parent 662d8d7b90
commit c6919d8174
18 changed files with 223 additions and 113 deletions

View File

@@ -3,7 +3,7 @@ import { Router } from 'express';
import { z } from 'zod';
import { db } from '../db/client';
import { deviceCommands, deviceLinks, devices } from '../db/schema';
import { commands, deviceLinks, devices } from '../db/schema';
import { simpleStreamingEnabled } from '../media/config';
import { requireAuth } from '../middleware/auth';
import { requireDeviceAuth } from '../middleware/device-auth';
@@ -98,7 +98,7 @@ router.post('/', requireAuth, async (req, res) => {
const now = new Date();
const [command] = await db
.insert(deviceCommands)
.insert(commands)
.values({
ownerUserId: authSession.user.id,
sourceDeviceId: sourceDevice.id,
@@ -118,7 +118,7 @@ router.post('/', requireAuth, async (req, res) => {
await dispatchCommandById(command.id);
const refreshed = await db.query.deviceCommands.findFirst({ where: eq(deviceCommands.id, command.id) });
const refreshed = await db.query.commands.findFirst({ where: eq(commands.id, command.id) });
res.status(201).json({
message: 'Command queued',
@@ -141,13 +141,13 @@ router.get('/', requireAuth, async (req, res) => {
return;
}
const commands = await db.query.deviceCommands.findMany({
where: eq(deviceCommands.ownerUserId, authSession.user.id),
orderBy: [desc(deviceCommands.createdAt)],
const commandResults = await db.query.commands.findMany({
where: eq(commands.ownerUserId, authSession.user.id),
orderBy: [desc(commands.createdAt)],
limit: parsed.data.limit,
});
const filtered = commands.filter((command) => {
const filtered = commandResults.filter((command) => {
if (parsed.data.sourceDeviceId && command.sourceDeviceId !== parsed.data.sourceDeviceId) {
return false;
}
@@ -187,8 +187,8 @@ router.post('/:commandId/ack', requireDeviceAuth, async (req, res) => {
return;
}
const command = await db.query.deviceCommands.findFirst({
where: eq(deviceCommands.id, parsedParams.data.commandId),
const command = await db.query.commands.findFirst({
where: eq(commands.id, parsedParams.data.commandId),
});
if (!command) {
@@ -204,14 +204,14 @@ router.post('/:commandId/ack', requireDeviceAuth, async (req, res) => {
const now = new Date();
const [updated] = await db
.update(deviceCommands)
.update(commands)
.set({
status: parsed.data.status,
acknowledgedAt: now,
updatedAt: now,
error: parsed.data.status === 'rejected' ? parsed.data.error ?? 'rejected' : null,
})
.where(eq(deviceCommands.id, command.id))
.where(eq(commands.id, command.id))
.returning();
res.json({ message: 'Command acknowledged', command: updated });