import { mutation } from "./_generated/server"; export const seedDemo = mutation({ args: {}, handler: async (ctx) => { const tenantId = "tenant-atlas"; // Ensure queues const existingQueues = await ctx.db .query("queues") .withIndex("by_tenant", (q) => q.eq("tenantId", tenantId)) .collect(); let queues = existingQueues.length ? existingQueues : await Promise.all( [ { name: "Chamados", slug: "chamados" }, { name: "Laboratório", slug: "laboratorio" }, ].map((q) => ctx.db.insert("queues", { tenantId, name: q.name, slug: q.slug, teamId: undefined })) ).then((ids) => Promise.all(ids.map((id) => ctx.db.get(id)))) ; queues = await Promise.all( queues.map(async (queue) => { if (!queue) return queue; if (queue.name === "Suporte N1" || queue.slug === "suporte-n1") { await ctx.db.patch(queue._id, { name: "Chamados", slug: "chamados" }); return (await ctx.db.get(queue._id)) ?? queue; } if (queue.name === "Suporte N2" || queue.slug === "suporte-n2") { await ctx.db.patch(queue._id, { name: "Laboratório", slug: "laboratorio" }); return (await ctx.db.get(queue._id)) ?? queue; } return queue; }) ); // Ensure users async function ensureUser(name: string, email: string, role = "AGENT") { const found = await ctx.db .query("users") .withIndex("by_tenant_email", (q) => q.eq("tenantId", tenantId).eq("email", email)) .first(); if (found) return found._id; return await ctx.db.insert("users", { tenantId, name, email, role, avatarUrl: `https://avatar.vercel.sh/${name.split(" ")[0]}` }); } const reverId = await ensureUser("Rever", "renan.pac@paulicon.com.br"); const agenteDemoId = await ensureUser("Agente Demo", "agente.demo@sistema.dev"); const eduardaId = await ensureUser("Eduarda Rocha", "eduarda.rocha@example.com", "CUSTOMER"); const clienteDemoId = await ensureUser("Cliente Demo", "cliente.demo@sistema.dev", "CUSTOMER"); // Seed a couple of tickets const now = Date.now(); const newestRef = await ctx.db .query("tickets") .withIndex("by_tenant_reference", (q) => q.eq("tenantId", tenantId)) .order("desc") .take(1); let ref = newestRef[0]?.reference ?? 41000; const queue1 = queues[0]!._id; const queue2 = queues[1]!._id; const t1 = await ctx.db.insert("tickets", { tenantId, reference: ++ref, subject: "Erro 500 ao acessar portal do cliente", summary: "Clientes relatam erro intermitente no portal web", status: "OPEN", priority: "URGENT", channel: "EMAIL", queueId: queue1, requesterId: eduardaId, assigneeId: reverId, createdAt: now - 1000 * 60 * 60 * 5, updatedAt: now - 1000 * 60 * 10, tags: ["portal", "cliente"], }); await ctx.db.insert("ticketEvents", { ticketId: t1, type: "CREATED", createdAt: now - 1000 * 60 * 60 * 5, payload: {} }); const t2 = await ctx.db.insert("tickets", { tenantId, reference: ++ref, subject: "Integração ERP parada", summary: "Webhook do ERP retornando timeout", status: "PENDING", priority: "HIGH", channel: "WHATSAPP", queueId: queue2, requesterId: clienteDemoId, assigneeId: agenteDemoId, createdAt: now - 1000 * 60 * 60 * 8, updatedAt: now - 1000 * 60 * 30, tags: ["Integração", "erp"], }); await ctx.db.insert("ticketEvents", { ticketId: t2, type: "CREATED", createdAt: now - 1000 * 60 * 60 * 8, payload: {} }); }, });