Fix form template labels and guard admin auth tables
This commit is contained in:
parent
003d068c56
commit
7fb6c65d9a
2 changed files with 113 additions and 65 deletions
|
|
@ -80,6 +80,21 @@ function plainTextLength(html: string): number {
|
|||
}
|
||||
}
|
||||
|
||||
function resolveFormTemplateLabel(
|
||||
templateKey: string | null | undefined,
|
||||
storedLabel: string | null | undefined
|
||||
): string | null {
|
||||
if (storedLabel && storedLabel.trim().length > 0) {
|
||||
return storedLabel.trim();
|
||||
}
|
||||
const normalizedKey = templateKey?.trim();
|
||||
if (!normalizedKey) {
|
||||
return null;
|
||||
}
|
||||
const fallback = TICKET_FORM_CONFIG.find((entry) => entry.key === normalizedKey);
|
||||
return fallback ? fallback.label : null;
|
||||
}
|
||||
|
||||
function escapeHtml(input: string): string {
|
||||
return input
|
||||
.replace(/&/g, "&")
|
||||
|
|
@ -1282,7 +1297,7 @@ export const list = query({
|
|||
csatRatedAt: t.csatRatedAt ?? null,
|
||||
csatRatedBy: t.csatRatedBy ? String(t.csatRatedBy) : null,
|
||||
formTemplate: t.formTemplate ?? null,
|
||||
formTemplateLabel: t.formTemplateLabel ?? null,
|
||||
formTemplateLabel: resolveFormTemplateLabel(t.formTemplate ?? null, t.formTemplateLabel ?? null),
|
||||
company: company
|
||||
? { id: company._id, name: company.name, isAvulso: company.isAvulso ?? false }
|
||||
: t.companyId || t.companySnapshot
|
||||
|
|
@ -1592,7 +1607,7 @@ export const getById = query({
|
|||
})),
|
||||
},
|
||||
formTemplate: t.formTemplate ?? null,
|
||||
formTemplateLabel: t.formTemplateLabel ?? null,
|
||||
formTemplateLabel: resolveFormTemplateLabel(t.formTemplate ?? null, t.formTemplateLabel ?? null),
|
||||
chatEnabled: Boolean(t.chatEnabled),
|
||||
relatedTicketIds: Array.isArray(t.relatedTicketIds) ? t.relatedTicketIds.map((id) => String(id)) : [],
|
||||
resolvedWithTicketId: t.resolvedWithTicketId ? String(t.resolvedWithTicketId) : null,
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import { Prisma } from "@prisma/client"
|
||||
|
||||
import { AdminUsersManager } from "@/components/admin/admin-users-manager"
|
||||
import { AppShell } from "@/components/app-shell"
|
||||
import { SiteHeader } from "@/components/site-header"
|
||||
|
|
@ -10,7 +12,19 @@ import { getServerSession } from "@/lib/auth-server"
|
|||
export const runtime = "nodejs"
|
||||
export const dynamic = "force-dynamic"
|
||||
|
||||
function isMissingAuthTableError(error: unknown, table: string): boolean {
|
||||
if (!(error instanceof Prisma.PrismaClientKnownRequestError)) {
|
||||
return false
|
||||
}
|
||||
if (error.code !== "P2021" && error.code !== "P2023") {
|
||||
return false
|
||||
}
|
||||
const target = typeof error.meta?.table === "string" ? error.meta.table.toLowerCase() : ""
|
||||
return target.includes(table.toLowerCase())
|
||||
}
|
||||
|
||||
async function loadUsers() {
|
||||
try {
|
||||
const users = await prisma.authUser.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: {
|
||||
|
|
@ -25,6 +39,10 @@ async function loadUsers() {
|
|||
},
|
||||
})
|
||||
|
||||
if (users.length === 0) {
|
||||
return []
|
||||
}
|
||||
|
||||
const domainUsers = await prisma.user.findMany({
|
||||
select: {
|
||||
email: true,
|
||||
|
|
@ -63,9 +81,17 @@ async function loadUsers() {
|
|||
machinePersona: user.machinePersona ?? null,
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
if (isMissingAuthTableError(error, "AuthUser")) {
|
||||
console.warn("[admin] auth tables missing; returning empty user list")
|
||||
return []
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async function loadInvites(): Promise<NormalizedInvite[]> {
|
||||
try {
|
||||
const invites = await prisma.authInvite.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: {
|
||||
|
|
@ -78,12 +104,19 @@ async function loadInvites(): Promise<NormalizedInvite[]> {
|
|||
const now = new Date()
|
||||
const cutoff = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000)
|
||||
return invites
|
||||
.map((invite: (typeof invites)[number]) => normalizeInvite(invite, now))
|
||||
.map((invite) => normalizeInvite(invite, now))
|
||||
.filter((invite: NormalizedInvite) => {
|
||||
if (invite.status !== "revoked") return true
|
||||
if (!invite.revokedAt) return true
|
||||
return new Date(invite.revokedAt) > cutoff
|
||||
})
|
||||
} catch (error) {
|
||||
if (isMissingAuthTableError(error, "AuthInvite")) {
|
||||
console.warn("[admin] auth invite tables missing; returning empty invite list")
|
||||
return []
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export default async function AdminPage() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue