Some checks failed
Docker Deploy / build-and-push (push) Has been cancelled
63 lines
1.4 KiB
TypeScript
63 lines
1.4 KiB
TypeScript
import { db } from "../db";
|
|
import { clients, tags as tagsTable } from "../db/schema";
|
|
import { eq, and, inArray } from "drizzle-orm";
|
|
|
|
export async function validateTimeEntryResources({
|
|
organizationId,
|
|
clientId,
|
|
tagIds,
|
|
}: {
|
|
organizationId: string;
|
|
clientId: string;
|
|
tagIds?: string[];
|
|
}) {
|
|
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 (tagIds && tagIds.length > 0) {
|
|
const validTags = await db
|
|
.select()
|
|
.from(tagsTable)
|
|
.where(
|
|
and(
|
|
inArray(tagsTable.id, tagIds),
|
|
eq(tagsTable.organizationId, organizationId),
|
|
),
|
|
)
|
|
.all();
|
|
|
|
if (validTags.length !== tagIds.length) {
|
|
return { valid: false, error: "Invalid tags" };
|
|
}
|
|
}
|
|
|
|
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 };
|
|
}
|