Hours report: add company filter (select) and CSV support via companyId; include filter metadata in file name

This commit is contained in:
Esdras Renan 2025-10-07 15:42:35 -03:00
parent 08cc8037d5
commit 9c8a43a6b1
2 changed files with 26 additions and 6 deletions

View file

@ -22,6 +22,7 @@ export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const range = searchParams.get("range") ?? undefined
const q = searchParams.get("q")?.toLowerCase().trim() ?? ""
const companyId = searchParams.get("companyId") ?? ""
const client = new ConvexHttpClient(convexUrl)
const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID
@ -54,7 +55,10 @@ export async function GET(request: Request) {
if (q) rows.push(["Filtro", q])
rows.push([])
rows.push(["Cliente", "Avulso", "Horas internas", "Horas externas", "Horas totais", "Horas contratadas/mês", "% uso"])
const items = q ? report.items.filter((i: any) => String(i.name).toLowerCase().includes(q)) : report.items
type Item = { companyId: string; name: string; isAvulso: boolean; internalMs: number; externalMs: number; totalMs: number; contractedHoursPerMonth: number | null }
let items = (report.items as Item[])
if (companyId) items = items.filter((i) => String(i.companyId) === companyId)
if (q) items = items.filter((i) => i.name.toLowerCase().includes(q))
for (const item of items) {
const internalH = msToHours(item.internalMs)
const externalH = msToHours(item.externalMs)
@ -67,7 +71,7 @@ export async function GET(request: Request) {
return new NextResponse(csv, {
headers: {
"Content-Type": "text/csv; charset=UTF-8",
"Content-Disposition": `attachment; filename="hours-by-client-${tenantId}-${report.rangeDays ?? '90'}d${q ? `-${encodeURIComponent(q)}` : ''}.csv"`,
"Content-Disposition": `attachment; filename="hours-by-client-${tenantId}-${report.rangeDays ?? '90'}d${companyId ? `-${companyId}` : ''}${q ? `-${encodeURIComponent(q)}` : ''}.csv"`,
"Cache-Control": "no-store",
},
})