feat: harden ticket creation ux and seeding
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
parent
be27dcfd15
commit
a51783ce29
11 changed files with 338 additions and 537 deletions
87
web/scripts/reassign-legacy-assignees.mjs
Normal file
87
web/scripts/reassign-legacy-assignees.mjs
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
import { ConvexHttpClient } from "convex/browser"
|
||||
|
||||
const CONVEX_URL = process.env.NEXT_PUBLIC_CONVEX_URL
|
||||
const TENANT_ID = process.env.SEED_TENANT_ID ?? "tenant-atlas"
|
||||
|
||||
if (!CONVEX_URL) {
|
||||
console.error("NEXT_PUBLIC_CONVEX_URL não definido. Configure o endpoint do Convex e execute novamente.")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const TARGET_NAMES = new Set(["Ana Souza", "Bruno Lima"])
|
||||
const REPLACEMENT = {
|
||||
name: "Rever",
|
||||
email: "renan.pac@paulicon.com.br",
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const client = new ConvexHttpClient(CONVEX_URL)
|
||||
|
||||
const admin = await client.mutation("users:ensureUser", {
|
||||
tenantId: TENANT_ID,
|
||||
email: "admin@sistema.dev",
|
||||
name: "Administrador",
|
||||
role: "ADMIN",
|
||||
})
|
||||
|
||||
if (!admin?._id) {
|
||||
throw new Error("Não foi possível garantir o usuário administrador")
|
||||
}
|
||||
|
||||
const replacementUser = await client.mutation("users:ensureUser", {
|
||||
tenantId: TENANT_ID,
|
||||
email: REPLACEMENT.email,
|
||||
name: REPLACEMENT.name,
|
||||
role: "AGENT",
|
||||
})
|
||||
|
||||
if (!replacementUser?._id) {
|
||||
throw new Error("Não foi possível garantir o usuário Rever")
|
||||
}
|
||||
|
||||
const agents = await client.query("users:listAgents", { tenantId: TENANT_ID })
|
||||
const targets = agents.filter((agent) => TARGET_NAMES.has(agent.name))
|
||||
|
||||
if (targets.length === 0) {
|
||||
console.log("Nenhum responsável legado encontrado. Nada a atualizar.")
|
||||
}
|
||||
|
||||
const targetIds = new Set(targets.map((agent) => agent._id))
|
||||
|
||||
const tickets = await client.query("tickets:list", {
|
||||
tenantId: TENANT_ID,
|
||||
viewerId: admin._id,
|
||||
})
|
||||
|
||||
let reassignedCount = 0
|
||||
for (const ticket of tickets) {
|
||||
if (ticket.assignee && targetIds.has(ticket.assignee.id)) {
|
||||
await client.mutation("tickets:changeAssignee", {
|
||||
ticketId: ticket.id,
|
||||
assigneeId: replacementUser._id,
|
||||
actorId: admin._id,
|
||||
})
|
||||
reassignedCount += 1
|
||||
console.log(`Ticket ${ticket.reference} reatribuído para ${replacementUser.name}`)
|
||||
}
|
||||
}
|
||||
|
||||
for (const agent of targets) {
|
||||
try {
|
||||
await client.mutation("users:deleteUser", {
|
||||
userId: agent._id,
|
||||
actorId: admin._id,
|
||||
})
|
||||
console.log(`Usuário removido: ${agent.name}`)
|
||||
} catch (error) {
|
||||
console.error(`Falha ao remover ${agent.name}:`, error)
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Total de tickets reatribuídos: ${reassignedCount}`)
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error("Erro ao reatribuir responsáveis legacy:", error)
|
||||
process.exitCode = 1
|
||||
})
|
||||
|
|
@ -4,13 +4,43 @@ import { hashPassword } from "better-auth/crypto"
|
|||
const { PrismaClient } = pkg
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
const email = process.env.SEED_USER_EMAIL ?? "admin@sistema.dev"
|
||||
const password = process.env.SEED_USER_PASSWORD ?? "admin123"
|
||||
const name = process.env.SEED_USER_NAME ?? "Administrador"
|
||||
const role = process.env.SEED_USER_ROLE ?? "admin"
|
||||
const tenantId = process.env.SEED_USER_TENANT ?? "tenant-atlas"
|
||||
|
||||
async function main() {
|
||||
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({
|
||||
|
|
@ -18,13 +48,13 @@ async function main() {
|
|||
update: {
|
||||
name,
|
||||
role,
|
||||
tenantId,
|
||||
tenantId: userTenant,
|
||||
},
|
||||
create: {
|
||||
email,
|
||||
name,
|
||||
role,
|
||||
tenantId,
|
||||
tenantId: userTenant,
|
||||
accounts: {
|
||||
create: {
|
||||
providerId: "credential",
|
||||
|
|
@ -79,7 +109,13 @@ async function main() {
|
|||
console.log(` Role: ${user.role}`)
|
||||
console.log(` Tenant: ${user.tenantId ?? "(nenhum)"}`)
|
||||
console.log(` Provider: ${account?.providerId ?? "-"}`)
|
||||
console.log(`Senha provisoria: ${password}`)
|
||||
console.log(` Senha provisoria: ${password}`)
|
||||
}
|
||||
|
||||
async function main() {
|
||||
for (const user of defaultUsers) {
|
||||
await upsertAuthUser(user)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue