Files
chronus/src/lib/validation.ts
Atridad Lahiji 815c08dd50
All checks were successful
Docker Deploy / build-and-push (push) Successful in 3m59s
Schema fixes
2026-01-20 12:08:06 -07:00

63 lines
1.3 KiB
TypeScript

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 };
}