sistema-de-chamados/web/convex/seed.ts

175 lines
6.9 KiB
TypeScript

import { mutation } from "./_generated/server";
export const seedDemo = mutation({
args: {},
handler: async (ctx) => {
const tenantId = "tenant-atlas";
const desiredQueues = [
{ name: "Chamados", slug: "chamados" },
{ name: "Laboratório", slug: "laboratorio" },
{ name: "Visitas", slug: "visitas" },
];
// Ensure queues
const existingQueues = await ctx.db
.query("queues")
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
.collect();
const normalizedQueues = await Promise.all(
existingQueues.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;
})
);
const presentQueues = normalizedQueues.filter(
(queue): queue is NonNullable<(typeof normalizedQueues)[number]> => Boolean(queue)
);
const queuesBySlug = new Map(presentQueues.map((queue) => [queue.slug, queue]));
const queuesByName = new Map(presentQueues.map((queue) => [queue.name, queue]));
const queues = [] as typeof presentQueues;
for (const def of desiredQueues) {
let queue = queuesBySlug.get(def.slug) ?? queuesByName.get(def.name);
if (!queue) {
const newId = await ctx.db.insert("queues", { tenantId, name: def.name, slug: def.slug, teamId: undefined });
queue = (await ctx.db.get(newId))!;
queuesBySlug.set(queue.slug, queue);
queuesByName.set(queue.name, queue);
}
queues.push(queue);
}
const queueChamados = queuesBySlug.get("chamados");
const queueLaboratorio = queuesBySlug.get("laboratorio");
if (!queueChamados || !queueLaboratorio) {
throw new Error("Filas padrão não foram inicializadas");
}
// 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) {
const updates: Record<string, unknown> = {};
if (found.name !== name) updates.name = name;
if ((found.role ?? "AGENT") !== role) updates.role = role;
const desiredAvatar = role === "CUSTOMER" ? found.avatarUrl ?? undefined : `https://avatar.vercel.sh/${name.split(" ")[0]}`;
if (found.avatarUrl !== desiredAvatar) updates.avatarUrl = desiredAvatar;
if (Object.keys(updates).length > 0) {
await ctx.db.patch(found._id, updates);
}
return found._id;
}
return await ctx.db.insert("users", { tenantId, name, email, role, avatarUrl: role === "CUSTOMER" ? undefined : `https://avatar.vercel.sh/${name.split(" ")[0]}` });
}
const adminId = await ensureUser("Administrador", "admin@sistema.dev", "ADMIN");
const staffRoster = [
{ name: "Gabriel Oliveira", email: "gabriel.oliveira@rever.com.br" },
{ name: "George Araujo", email: "george.araujo@rever.com.br" },
{ name: "Hugo Soares", email: "hugo.soares@rever.com.br" },
{ name: "Julio Cesar", email: "julio@rever.com.br" },
{ name: "Lorena Magalhães", email: "lorena@rever.com.br" },
{ name: "Rever", email: "renan.pac@paulicon.com.br" },
{ name: "Thiago Medeiros", email: "thiago.medeiros@rever.com.br" },
{ name: "Weslei Magalhães", email: "weslei@rever.com.br" },
];
const staffIds = await Promise.all(staffRoster.map((staff) => ensureUser(staff.name, staff.email)));
const defaultAssigneeId = staffIds[0] ?? adminId;
const eduardaId = await ensureUser("Eduarda Rocha", "eduarda.rocha@example.com", "CUSTOMER");
const clienteDemoId = await ensureUser("Cliente Demo", "cliente.demo@sistema.dev", "CUSTOMER");
const templateDefinitions = [
{
title: "A Rever agradece seu contato",
body: "<p>A Rever agradece seu contato. Recebemos sua solicitação e nossa equipe já está analisando os detalhes. Retornaremos com atualizações em breve.</p>",
},
{
title: "Atualização do chamado",
body: "<p>Seu chamado foi atualizado. Caso tenha novas informações ou dúvidas, basta responder a esta mensagem.</p>",
},
{
title: "Chamado resolvido",
body: "<p>Concluímos o atendimento deste chamado. A Rever agradece a parceria e permanecemos à disposição para novos suportes.</p>",
},
];
const existingTemplates = await ctx.db
.query("commentTemplates")
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
.collect();
for (const definition of templateDefinitions) {
const already = existingTemplates.find((template) => template?.title === definition.title);
if (already) continue;
const timestamp = Date.now();
await ctx.db.insert("commentTemplates", {
tenantId,
title: definition.title,
body: definition.body,
createdBy: adminId,
updatedBy: adminId,
createdAt: timestamp,
updatedAt: timestamp,
});
}
// 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 = queueChamados._id;
const queue2 = queueLaboratorio._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: defaultAssigneeId,
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: defaultAssigneeId,
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: {} });
},
});