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

@@ -1,7 +1,7 @@
import { and, eq, lte } from 'drizzle-orm';
import { db } from '../db/client';
import { devices, pushNotifications } from '../db/schema';
import { devices, notificationDeliveries } from '../db/schema';
import { hasRequiredTables } from '../utils/db-schema';
const MAX_ATTEMPTS = Number(process.env.PUSH_MAX_ATTEMPTS ?? 5);
@@ -12,7 +12,7 @@ export const enqueuePushNotification = async (input: {
type: string;
payload?: Record<string, unknown>;
}): Promise<void> => {
await db.insert(pushNotifications).values({
await db.insert(notificationDeliveries).values({
ownerUserId: input.ownerUserId,
recipientDeviceId: input.recipientDeviceId,
type: input.type,
@@ -25,7 +25,9 @@ export const enqueuePushNotification = async (input: {
};
const deliverPush = async (notificationId: string): Promise<void> => {
const notification = await db.query.pushNotifications.findFirst({ where: eq(pushNotifications.id, notificationId) });
const notification = await db.query.notificationDeliveries.findFirst({
where: eq(notificationDeliveries.id, notificationId),
});
if (!notification || notification.status === 'delivered' || notification.status === 'failed') {
return;
@@ -41,7 +43,7 @@ const deliverPush = async (notificationId: string): Promise<void> => {
const shouldFail = attempts >= MAX_ATTEMPTS;
await db
.update(pushNotifications)
.update(notificationDeliveries)
.set({
attempts,
status: shouldFail ? 'failed' : 'queued',
@@ -49,14 +51,13 @@ const deliverPush = async (notificationId: string): Promise<void> => {
nextAttemptAt: new Date(now.getTime() + nextDelaySeconds * 1000),
updatedAt: now,
})
.where(eq(pushNotifications.id, notification.id));
.where(eq(notificationDeliveries.id, notification.id));
return;
}
// Mock push provider: consider "delivered" when token exists.
await db
.update(pushNotifications)
.update(notificationDeliveries)
.set({
attempts,
status: 'delivered',
@@ -64,14 +65,14 @@ const deliverPush = async (notificationId: string): Promise<void> => {
lastError: null,
updatedAt: now,
})
.where(eq(pushNotifications.id, notification.id));
.where(eq(notificationDeliveries.id, notification.id));
};
export const dispatchPushQueueOnce = async (): Promise<number> => {
const now = new Date();
const queued = await db.query.pushNotifications.findMany({
where: and(eq(pushNotifications.status, 'queued'), lte(pushNotifications.nextAttemptAt, now)),
const queued = await db.query.notificationDeliveries.findMany({
where: and(eq(notificationDeliveries.status, 'queued'), lte(notificationDeliveries.nextAttemptAt, now)),
limit: 100,
});
@@ -84,7 +85,7 @@ export const dispatchPushQueueOnce = async (): Promise<number> => {
export const startPushWorker = (): void => {
const intervalMs = Number(process.env.PUSH_WORKER_INTERVAL_MS ?? 10_000);
const requiredTables = ['push_notifications', 'devices'];
const requiredTables = ['notification_deliveries', 'devices'];
void (async () => {
const ready = await hasRequiredTables(requiredTables);