feat(convex): add internal url and remote access fixes
This commit is contained in:
parent
feb31d48c1
commit
da46fa448b
17 changed files with 73 additions and 92 deletions
|
|
@ -7,6 +7,7 @@ import { prisma } from "@/lib/prisma"
|
|||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import { assertStaffSession } from "@/lib/auth-server"
|
||||
import { ROLE_OPTIONS, type RoleOption, isAdmin } from "@/lib/authz"
|
||||
import { requireConvexUrl } from "@/server/convex-client"
|
||||
|
||||
function normalizeRole(input: string | null | undefined): RoleOption {
|
||||
const candidate = (input ?? "agent").toLowerCase() as RoleOption
|
||||
|
|
@ -256,10 +257,8 @@ export async function PATCH(request: Request, { params }: { params: Promise<{ id
|
|||
})
|
||||
}
|
||||
|
||||
const convexUrl = process.env.NEXT_PUBLIC_CONVEX_URL
|
||||
if (convexUrl) {
|
||||
try {
|
||||
const convex = new ConvexHttpClient(convexUrl)
|
||||
try {
|
||||
const convex = new ConvexHttpClient(requireConvexUrl())
|
||||
let managerConvexId: Id<"users"> | undefined
|
||||
if (hasManagerField && managerRecord?.email) {
|
||||
try {
|
||||
|
|
@ -299,10 +298,9 @@ export async function PATCH(request: Request, { params }: { params: Promise<{ id
|
|||
if (hasManagerField) {
|
||||
ensurePayload.managerId = managerConvexId
|
||||
}
|
||||
await convex.mutation(api.users.ensureUser, ensurePayload)
|
||||
} catch (error) {
|
||||
console.warn("Falha ao sincronizar usuário no Convex", error)
|
||||
}
|
||||
await convex.mutation(api.users.ensureUser, ensurePayload)
|
||||
} catch (error) {
|
||||
console.warn("Falha ao sincronizar usuário no Convex", error)
|
||||
}
|
||||
|
||||
const updatedDomain = await prisma.user.findUnique({
|
||||
|
|
@ -363,12 +361,10 @@ export async function DELETE(_: Request, { params }: { params: Promise<{ id: str
|
|||
return NextResponse.json({ error: "Você não pode remover o usuário atualmente autenticado." }, { status: 400 })
|
||||
}
|
||||
|
||||
const convexUrl = process.env.NEXT_PUBLIC_CONVEX_URL
|
||||
const tenantId = target.tenantId ?? session.user.tenantId ?? DEFAULT_TENANT_ID
|
||||
|
||||
if (convexUrl) {
|
||||
try {
|
||||
const convex = new ConvexHttpClient(convexUrl)
|
||||
try {
|
||||
const convex = new ConvexHttpClient(requireConvexUrl())
|
||||
const ensured = await convex.mutation(api.users.ensureUser, {
|
||||
tenantId,
|
||||
email: session.user.email,
|
||||
|
|
@ -393,10 +389,9 @@ export async function DELETE(_: Request, { params }: { params: Promise<{ id: str
|
|||
actorId,
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Falha ao remover usuário na base de dados"
|
||||
return NextResponse.json({ error: message }, { status: 400 })
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Falha ao remover usuário na base de dados"
|
||||
return NextResponse.json({ error: message }, { status: 400 })
|
||||
}
|
||||
|
||||
await prisma.authUser.delete({ where: { id: target.id } })
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import { prisma } from "@/lib/prisma"
|
|||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import type { Id } from "@/convex/_generated/dataModel"
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import { requireConvexUrl } from "@/server/convex-client"
|
||||
|
||||
export const runtime = "nodejs"
|
||||
|
||||
|
|
@ -21,9 +22,12 @@ export async function POST(request: Request) {
|
|||
return NextResponse.json({ error: "Informe e-mail e empresa" }, { status: 400 })
|
||||
}
|
||||
|
||||
const convexUrl = process.env.NEXT_PUBLIC_CONVEX_URL
|
||||
if (!convexUrl) return NextResponse.json({ error: "Convex não configurado" }, { status: 500 })
|
||||
const client = new ConvexHttpClient(convexUrl)
|
||||
let client: ConvexHttpClient
|
||||
try {
|
||||
client = new ConvexHttpClient(requireConvexUrl())
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Convex não configurado" }, { status: 500 })
|
||||
}
|
||||
|
||||
try {
|
||||
const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { ConvexHttpClient } from "convex/browser"
|
|||
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import { env } from "@/lib/env"
|
||||
import { requireConvexUrl } from "@/server/convex-client"
|
||||
|
||||
const fleetHostSchema = z.object({
|
||||
host: z
|
||||
|
|
@ -92,11 +93,6 @@ export async function POST(request: Request) {
|
|||
return NextResponse.json({ error: "Não autorizado" }, { status: 401 })
|
||||
}
|
||||
|
||||
const convexUrl = env.NEXT_PUBLIC_CONVEX_URL
|
||||
if (!convexUrl) {
|
||||
return NextResponse.json({ error: "Convex não configurado" }, { status: 500 })
|
||||
}
|
||||
|
||||
let parsed
|
||||
try {
|
||||
const raw = await request.json()
|
||||
|
|
@ -159,7 +155,7 @@ export async function POST(request: Request) {
|
|||
cpuLogicalCores: host.cpu_logical_cores,
|
||||
}
|
||||
|
||||
const client = new ConvexHttpClient(convexUrl)
|
||||
const client = new ConvexHttpClient(requireConvexUrl())
|
||||
|
||||
try {
|
||||
const result = await client.mutation(api.devices.upsertInventory, {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import { ConvexHttpClient } from "convex/browser"
|
|||
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import { env } from "@/lib/env"
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import {
|
||||
computeInviteStatus,
|
||||
|
|
@ -14,6 +13,7 @@ import {
|
|||
normalizeRoleOption,
|
||||
type NormalizedInvite,
|
||||
} from "@/server/invite-utils"
|
||||
import { requireConvexUrl } from "@/server/convex-client"
|
||||
|
||||
type AcceptInvitePayload = {
|
||||
name?: string
|
||||
|
|
@ -25,9 +25,8 @@ function validatePassword(password: string) {
|
|||
}
|
||||
|
||||
async function syncInvite(invite: NormalizedInvite) {
|
||||
const convexUrl = env.NEXT_PUBLIC_CONVEX_URL
|
||||
if (!convexUrl) return
|
||||
const client = new ConvexHttpClient(convexUrl)
|
||||
const url = requireConvexUrl()
|
||||
const client = new ConvexHttpClient(url)
|
||||
await client.mutation(api.invites.sync, {
|
||||
tenantId: invite.tenantId,
|
||||
inviteId: invite.id,
|
||||
|
|
@ -178,20 +177,17 @@ export async function POST(request: Request, context: { params: Promise<{ token:
|
|||
const normalized = normalizeInvite({ ...updatedInvite, events: [...invite.events, event] }, now)
|
||||
await syncInvite(normalized)
|
||||
|
||||
const convexUrl = env.NEXT_PUBLIC_CONVEX_URL
|
||||
if (convexUrl) {
|
||||
try {
|
||||
const convex = new ConvexHttpClient(convexUrl)
|
||||
await convex.mutation(api.users.ensureUser, {
|
||||
tenantId,
|
||||
email: invite.email,
|
||||
name,
|
||||
avatarUrl: undefined,
|
||||
role: role.toUpperCase(),
|
||||
})
|
||||
} catch (error) {
|
||||
console.warn("Falha ao sincronizar usuário no Convex", error)
|
||||
}
|
||||
try {
|
||||
const convex = new ConvexHttpClient(requireConvexUrl())
|
||||
await convex.mutation(api.users.ensureUser, {
|
||||
tenantId,
|
||||
email: invite.email,
|
||||
name,
|
||||
avatarUrl: undefined,
|
||||
role: role.toUpperCase(),
|
||||
})
|
||||
} catch (error) {
|
||||
console.warn("Falha ao sincronizar usuário no Convex", error)
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true })
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ import { ConvexHttpClient } from "convex/browser"
|
|||
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import { requireAuthenticatedSession } from "@/lib/auth-server"
|
||||
import { env } from "@/lib/env"
|
||||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import { ensureCollaboratorAccount } from "@/server/machines-auth"
|
||||
import { requireConvexUrl } from "@/server/convex-client"
|
||||
|
||||
const updateSchema = z.object({
|
||||
email: z.string().email().optional(),
|
||||
|
|
@ -167,18 +167,16 @@ export async function PATCH(request: Request) {
|
|||
role: effectiveRole,
|
||||
})
|
||||
|
||||
if (env.NEXT_PUBLIC_CONVEX_URL) {
|
||||
try {
|
||||
const client = new ConvexHttpClient(env.NEXT_PUBLIC_CONVEX_URL)
|
||||
await client.mutation(api.users.ensureUser, {
|
||||
tenantId,
|
||||
email: effectiveEmail,
|
||||
name,
|
||||
role: effectiveRole,
|
||||
})
|
||||
} catch (error) {
|
||||
console.warn("[portal.profile] Falha ao sincronizar usuário no Convex", error)
|
||||
}
|
||||
try {
|
||||
const client = new ConvexHttpClient(requireConvexUrl())
|
||||
await client.mutation(api.users.ensureUser, {
|
||||
tenantId,
|
||||
email: effectiveEmail,
|
||||
name,
|
||||
role: effectiveRole,
|
||||
})
|
||||
} catch (error) {
|
||||
console.warn("[portal.profile] Falha ao sincronizar usuário no Convex", error)
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, email: effectiveEmail })
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@ import { NextResponse } from "next/server"
|
|||
import { ConvexHttpClient } from "convex/browser"
|
||||
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import { env } from "@/lib/env"
|
||||
import { assertAuthenticatedSession } from "@/lib/auth-server"
|
||||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import { buildMachinesInventoryWorkbook, type MachineInventoryRecord } from "@/server/machines/inventory-export"
|
||||
import type { DeviceInventoryColumnConfig } from "@/lib/device-inventory-columns"
|
||||
import { requireConvexUrl } from "@/server/convex-client"
|
||||
|
||||
export const runtime = "nodejs"
|
||||
|
||||
|
|
@ -14,11 +14,6 @@ export async function GET(request: Request) {
|
|||
const session = await assertAuthenticatedSession()
|
||||
if (!session) return NextResponse.json({ error: "Não autorizado" }, { status: 401 })
|
||||
|
||||
const convexUrl = env.NEXT_PUBLIC_CONVEX_URL
|
||||
if (!convexUrl) {
|
||||
return NextResponse.json({ error: "Convex não configurado" }, { status: 500 })
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const companyId = searchParams.get("companyId") ?? undefined
|
||||
const machineIdParams = searchParams.getAll("machineId").filter(Boolean)
|
||||
|
|
@ -49,7 +44,7 @@ export async function GET(request: Request) {
|
|||
}
|
||||
}
|
||||
|
||||
const client = new ConvexHttpClient(convexUrl)
|
||||
const client = new ConvexHttpClient(requireConvexUrl())
|
||||
const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID
|
||||
|
||||
let viewerId: string | null = null
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ import { ConvexHttpClient } from "convex/browser"
|
|||
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import type { Id } from "@/convex/_generated/dataModel"
|
||||
import { env } from "@/lib/env"
|
||||
import { assertAuthenticatedSession } from "@/lib/auth-server"
|
||||
import { mapTicketWithDetailsFromServer } from "@/lib/mappers/ticket"
|
||||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import { renderTicketPdfBuffer } from "@/server/pdf/ticket-pdf-template"
|
||||
import { requireConvexUrl } from "@/server/convex-client"
|
||||
|
||||
export const runtime = "nodejs"
|
||||
|
||||
|
|
@ -32,12 +32,7 @@ export async function GET(_request: Request, context: { params: Promise<{ id: st
|
|||
return NextResponse.json({ error: "Não autorizado" }, { status: 401 })
|
||||
}
|
||||
|
||||
const convexUrl = env.NEXT_PUBLIC_CONVEX_URL
|
||||
if (!convexUrl) {
|
||||
return NextResponse.json({ error: "Convex não configurado" }, { status: 500 })
|
||||
}
|
||||
|
||||
const client = new ConvexHttpClient(convexUrl)
|
||||
const client = new ConvexHttpClient(requireConvexUrl())
|
||||
const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID
|
||||
|
||||
let viewerId: string | null = null
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@ import { NextResponse } from "next/server"
|
|||
import { ConvexHttpClient } from "convex/browser"
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import type { Id } from "@/convex/_generated/dataModel"
|
||||
import { env } from "@/lib/env"
|
||||
import { assertAuthenticatedSession } from "@/lib/auth-server"
|
||||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import { requireConvexUrl } from "@/server/convex-client"
|
||||
|
||||
export const runtime = "nodejs"
|
||||
|
||||
|
|
@ -42,11 +42,6 @@ export async function GET(request: Request) {
|
|||
return NextResponse.json({ items: [] }, { status: 401 })
|
||||
}
|
||||
|
||||
const convexUrl = env.NEXT_PUBLIC_CONVEX_URL
|
||||
if (!convexUrl) {
|
||||
return NextResponse.json({ items: [] }, { status: 500 })
|
||||
}
|
||||
|
||||
const normalizedRole = normalizeRole(session.user.role)
|
||||
const isAgentOrAdmin = normalizedRole === "admin" || normalizedRole === "agent"
|
||||
const canLinkOwnTickets = normalizedRole === "collaborator"
|
||||
|
|
@ -59,7 +54,7 @@ export async function GET(request: Request) {
|
|||
const rawQuery = url.searchParams.get("q") ?? ""
|
||||
const query = rawQuery.trim()
|
||||
|
||||
const client = new ConvexHttpClient(convexUrl)
|
||||
const client = new ConvexHttpClient(requireConvexUrl())
|
||||
|
||||
// Garantir que o usuário exista no Convex para obter viewerId
|
||||
let viewerId: string | null = null
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue