From 12262e24c825d6e1e12509b39e23f73e7999501b Mon Sep 17 00:00:00 2001 From: rever-tecnologia Date: Mon, 6 Oct 2025 09:05:11 -0300 Subject: [PATCH] Fix auth sync and set ticket channel chart default Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> --- web/scripts/debug-convex.mjs | 46 +++++++++++++++++++ web/scripts/seed-auth.mjs | 3 +- web/src/app/login/page.tsx | 11 ++++- web/src/components/chart-area-interactive.tsx | 6 +-- web/src/lib/auth-client.tsx | 4 +- 5 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 web/scripts/debug-convex.mjs diff --git a/web/scripts/debug-convex.mjs b/web/scripts/debug-convex.mjs new file mode 100644 index 0000000..258df55 --- /dev/null +++ b/web/scripts/debug-convex.mjs @@ -0,0 +1,46 @@ +import { ConvexHttpClient } from "convex/browser"; + +const url = process.env.NEXT_PUBLIC_CONVEX_URL; + +if (!url) { + console.error("Missing NEXT_PUBLIC_CONVEX_URL"); + process.exit(1); +} + +const client = new ConvexHttpClient(url); + +const tenantId = process.argv[2] ?? "tenant-atlas"; + +const ensureAdmin = await client.mutation("users:ensureUser", { + tenantId, + email: "admin@sistema.dev", + name: "Administrador", + role: "ADMIN", +}); + +console.log("Ensured admin user:", ensureAdmin); + +const agents = await client.query("users:listAgents", { tenantId }); +console.log("Agents:", agents); + +const viewerId = ensureAdmin?._id ?? agents[0]?._id; + +if (!viewerId) { + console.error("Unable to determine viewer id"); + process.exit(1); +} + +const tickets = await client.query("tickets:list", { + tenantId, + viewerId, + limit: 10, +}); + +console.log("Tickets:", tickets); + +const dashboard = await client.query("reports:dashboardOverview", { + tenantId, + viewerId, +}); + +console.log("Dashboard:", dashboard); diff --git a/web/scripts/seed-auth.mjs b/web/scripts/seed-auth.mjs index 18c0fbe..7e76634 100644 --- a/web/scripts/seed-auth.mjs +++ b/web/scripts/seed-auth.mjs @@ -1,6 +1,7 @@ -import { PrismaClient } from "@prisma/client" +import pkg from "@prisma/client" import { hashPassword } from "better-auth/crypto" +const { PrismaClient } = pkg const prisma = new PrismaClient() const email = process.env.SEED_USER_EMAIL ?? "admin@sistema.dev" diff --git a/web/src/app/login/page.tsx b/web/src/app/login/page.tsx index a86f4d6..3dbb6c8 100644 --- a/web/src/app/login/page.tsx +++ b/web/src/app/login/page.tsx @@ -1,6 +1,6 @@ "use client" -import { useEffect } from "react" +import { useEffect, useState } from "react" import Link from "next/link" import { useRouter, useSearchParams } from "next/navigation" import { GalleryVerticalEnd } from "lucide-react" @@ -19,6 +19,7 @@ export default function LoginPage() { const searchParams = useSearchParams() const { data: session, isPending } = useSession() const callbackUrl = searchParams?.get("callbackUrl") ?? undefined + const [isHydrated, setIsHydrated] = useState(false) useEffect(() => { if (!session?.user) return @@ -26,6 +27,12 @@ export default function LoginPage() { router.replace(destination) }, [callbackUrl, router, session?.user]) + useEffect(() => { + setIsHydrated(true) + }, []) + + const shouldDisable = !isHydrated || isPending + return (
@@ -39,7 +46,7 @@ export default function LoginPage() {
- +
diff --git a/web/src/components/chart-area-interactive.tsx b/web/src/components/chart-area-interactive.tsx index d58d06b..fbe2c06 100644 --- a/web/src/components/chart-area-interactive.tsx +++ b/web/src/components/chart-area-interactive.tsx @@ -41,7 +41,7 @@ export const description = "Distribuição semanal de tickets por canal" export function ChartAreaInteractive() { const isMobile = useIsMobile() - const [timeRange, setTimeRange] = React.useState("90d") + const [timeRange, setTimeRange] = React.useState("7d") const { session, convexUserId } = useAuth() const tenantId = session?.user.tenantId ?? DEFAULT_TENANT_ID @@ -118,13 +118,13 @@ export function ChartAreaInteractive() { 30 dias 7 dias - - + diff --git a/web/src/lib/auth-client.tsx b/web/src/lib/auth-client.tsx index 2443305..a1cfb5e 100644 --- a/web/src/lib/auth-client.tsx +++ b/web/src/lib/auth-client.tsx @@ -70,7 +70,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { }, [session?.user]) useEffect(() => { - if (!session?.user || !process.env.NEXT_PUBLIC_CONVEX_URL || convexUserId) return + if (!session?.user || convexUserId) return const controller = new AbortController() @@ -97,7 +97,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { controller.abort() } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [ensureUser, session?.user?.email, convexUserId]) + }, [ensureUser, session?.user?.email, session?.user?.tenantId, session?.user?.role, convexUserId]) const normalizedRole = session?.user?.role ? session.user.role.toLowerCase() : null