import { db } from "../db"; import { clients, tags as tagsTable } from "../db/schema"; import { eq, and } from "drizzle-orm"; export async function validateTimeEntryResources({ organizationId, clientId, tagId, }: { organizationId: string; clientId: string; tagId?: string | null; }) { const client = await db .select() .from(clients) .where( and(eq(clients.id, clientId), eq(clients.organizationId, organizationId)), ) .get(); if (!client) { return { valid: false, error: "Invalid client" }; } if (tagId) { const validTag = await db .select() .from(tagsTable) .where( and( eq(tagsTable.id, tagId), eq(tagsTable.organizationId, organizationId), ), ) .get(); if (!validTag) { return { valid: false, error: "Invalid tag" }; } } return { valid: true }; } export function validateTimeRange( start: string | number | Date, end: string | number | Date, ) { const startDate = new Date(start); const endDate = new Date(end); if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) { return { valid: false, error: "Invalid date format" }; } if (endDate <= startDate) { return { valid: false, error: "End time must be after start time" }; } return { valid: true, startDate, endDate }; }