Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
128 lines
2.8 KiB
JavaScript
128 lines
2.8 KiB
JavaScript
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: "agente.demo@sistema.dev",
|
|
password: "agent123",
|
|
name: "Agente Demo",
|
|
role: "agent",
|
|
tenantId,
|
|
},
|
|
{
|
|
email: "cliente.demo@sistema.dev",
|
|
password: "cliente123",
|
|
name: "Cliente Demo",
|
|
role: "customer",
|
|
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()
|
|
})
|