import pkg from "@prisma/client" import { hashPassword } from "better-auth/crypto" const { PrismaClient } = pkg const prisma = new PrismaClient() const tenantId = process.env.SEED_USER_TENANT ?? "tenant-atlas" const singleUserFromEnv = process.env.SEED_USER_EMAIL ? [{ email: process.env.SEED_USER_EMAIL, password: process.env.SEED_USER_PASSWORD ?? "admin123", name: process.env.SEED_USER_NAME ?? "Administrador", role: process.env.SEED_USER_ROLE ?? "admin", tenantId, }] : null const defaultUsers = singleUserFromEnv ?? [ { email: "admin@sistema.dev", password: "admin123", name: "Administrador", role: "admin", tenantId, }, { email: "cliente.demo@sistema.dev", password: "cliente123", name: "Cliente Demo", role: "customer", tenantId, }, { email: "gabriel.oliveira@rever.com.br", password: "agent123", name: "Gabriel Oliveira", role: "agent", tenantId, }, { email: "george.araujo@rever.com.br", password: "agent123", name: "George Araujo", role: "agent", tenantId, }, { email: "hugo.soares@rever.com.br", password: "agent123", name: "Hugo Soares", role: "agent", tenantId, }, { email: "julio@rever.com.br", password: "agent123", name: "Julio Cesar", role: "agent", tenantId, }, { email: "lorena@rever.com.br", password: "agent123", name: "Lorena Magalhães", role: "agent", tenantId, }, { email: "renan.pac@paulicon.com.br", password: "agent123", name: "Rever", role: "agent", tenantId, }, { email: "thiago.medeiros@rever.com.br", password: "agent123", name: "Thiago Medeiros", role: "agent", tenantId, }, { email: "weslei@rever.com.br", password: "agent123", name: "Weslei Magalhães", role: "agent", tenantId, }, ] async function upsertAuthUser({ email, password, name, role, tenantId: userTenant }: (typeof defaultUsers)[number]) { const hashedPassword = await hashPassword(password) const user = await prisma.authUser.upsert({ where: { email }, update: { name, role, tenantId: userTenant, }, create: { email, name, role, tenantId: userTenant, accounts: { create: { providerId: "credential", accountId: email, password: hashedPassword, }, }, }, include: { accounts: true, }, }) await prisma.authAccount.updateMany({ where: { userId: user.id, accountId: email, }, data: { providerId: "credential", }, }) let account = await prisma.authAccount.findFirst({ where: { userId: user.id, providerId: "credential", accountId: email, }, }) if (account) { account = await prisma.authAccount.update({ where: { id: account.id }, data: { password: hashedPassword, }, }) } else { account = await prisma.authAccount.create({ data: { userId: user.id, providerId: "credential", accountId: email, password: hashedPassword, }, }) } console.log(`✅ Usuario seed criado/atualizado: ${user.email}`) console.log(` ID: ${user.id}`) console.log(` Role: ${user.role}`) console.log(` Tenant: ${user.tenantId ?? "(nenhum)"}`) console.log(` Provider: ${account?.providerId ?? "-"}`) console.log(` Senha provisoria: ${password}`) } async function main() { for (const user of defaultUsers) { await upsertAuthUser(user) } } main() .catch((error) => { console.error("Erro ao criar usuario seed", error) process.exitCode = 1 }) .finally(async () => { await prisma.$disconnect() })