feat: expand admin companies and users modules
This commit is contained in:
parent
a043b1203c
commit
2e3b46a7b5
31 changed files with 5626 additions and 2003 deletions
98
src/app/admin/users/page.tsx
Normal file
98
src/app/admin/users/page.tsx
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
import { AppShell } from "@/components/app-shell"
|
||||
import { SiteHeader } from "@/components/site-header"
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import { requireStaffSession } from "@/lib/auth-server"
|
||||
import { AdminUsersWorkspace, type AdminAccount } from "@/components/admin/users/admin-users-workspace"
|
||||
import { normalizeCompany } from "@/server/company-service"
|
||||
|
||||
export const runtime = "nodejs"
|
||||
export const dynamic = "force-dynamic"
|
||||
|
||||
export default async function AdminUsersPage() {
|
||||
const session = await requireStaffSession()
|
||||
const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID
|
||||
|
||||
const users = await prisma.user.findMany({
|
||||
where: {
|
||||
tenantId,
|
||||
role: { in: ["MANAGER", "COLLABORATOR"] },
|
||||
},
|
||||
include: {
|
||||
company: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
})
|
||||
|
||||
const emails = users.map((user) => user.email)
|
||||
const authUsers = await prisma.authUser.findMany({
|
||||
where: { email: { in: emails } },
|
||||
select: { id: true, email: true, updatedAt: true, createdAt: true },
|
||||
})
|
||||
|
||||
const sessions = await prisma.authSession.findMany({
|
||||
where: { userId: { in: authUsers.map((auth) => auth.id) } },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
select: { userId: true, updatedAt: true },
|
||||
})
|
||||
|
||||
const sessionByUserId = new Map<string, Date>()
|
||||
for (const sessionRow of sessions) {
|
||||
if (!sessionByUserId.has(sessionRow.userId)) {
|
||||
sessionByUserId.set(sessionRow.userId, sessionRow.updatedAt)
|
||||
}
|
||||
}
|
||||
|
||||
const authByEmail = new Map<string, { id: string; updatedAt: Date; createdAt: Date }>()
|
||||
for (const authUser of authUsers) {
|
||||
authByEmail.set(authUser.email.toLowerCase(), {
|
||||
id: authUser.id,
|
||||
updatedAt: authUser.updatedAt,
|
||||
createdAt: authUser.createdAt,
|
||||
})
|
||||
}
|
||||
|
||||
const accounts: AdminAccount[] = users.map((user) => {
|
||||
const auth = authByEmail.get(user.email.toLowerCase())
|
||||
const lastSeenAt = auth ? sessionByUserId.get(auth.id) ?? auth.updatedAt : null
|
||||
return {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name ?? user.email,
|
||||
role: user.role === "MANAGER" ? "MANAGER" : "COLLABORATOR",
|
||||
companyId: user.companyId ?? null,
|
||||
companyName: user.company?.name ?? null,
|
||||
tenantId: user.tenantId,
|
||||
createdAt: user.createdAt.toISOString(),
|
||||
updatedAt: user.updatedAt.toISOString(),
|
||||
authUserId: auth?.id ?? null,
|
||||
lastSeenAt: lastSeenAt ? lastSeenAt.toISOString() : null,
|
||||
}
|
||||
})
|
||||
|
||||
const companiesRaw = await prisma.company.findMany({
|
||||
where: { tenantId },
|
||||
orderBy: { name: "asc" },
|
||||
})
|
||||
const companies = companiesRaw.map(normalizeCompany)
|
||||
|
||||
return (
|
||||
<AppShell
|
||||
header={
|
||||
<SiteHeader
|
||||
title="Usuários"
|
||||
lead="Gerencie acessos de gestores/colaboradores e mantenha contatos, unidades e contratos atualizados."
|
||||
/>
|
||||
}
|
||||
>
|
||||
<div className="mx-auto w-full max-w-7xl px-4 pb-12 lg:px-8">
|
||||
<AdminUsersWorkspace initialAccounts={accounts} companies={companies} />
|
||||
</div>
|
||||
</AppShell>
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue