- Removed SLA configuration selection from the TicketForm component to streamline the ticket creation process. - Updated the abrir-chamado route to eliminate unnecessary SLA loading logic and directly pass loading state to the TicketForm. - Enhanced the central-chamados route to support SLA configurations by priority, allowing for better management of SLA settings. - Introduced new mutations for SLA configuration management, including creation, updating, and deletion of SLA settings. - Improved email templates for ticket notifications, ensuring better communication with users regarding ticket status and updates.
422 lines
17 KiB
TypeScript
422 lines
17 KiB
TypeScript
import { v } from "convex/values";
|
|
import { mutation, query } from "./_generated/server";
|
|
import { registrarAtividade } from "./logsAtividades";
|
|
import { Doc } from "./_generated/dataModel";
|
|
|
|
/**
|
|
* Listar todos os templates
|
|
*/
|
|
export const listarTemplates = query({
|
|
args: {},
|
|
handler: async (ctx) => {
|
|
const templates = await ctx.db.query("templatesMensagens").collect();
|
|
return templates;
|
|
},
|
|
});
|
|
|
|
/**
|
|
* Obter template por código
|
|
*/
|
|
export const obterTemplatePorCodigo = query({
|
|
args: {
|
|
codigo: v.string(),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const template = await ctx.db
|
|
.query("templatesMensagens")
|
|
.withIndex("by_codigo", (q) => q.eq("codigo", args.codigo))
|
|
.first();
|
|
|
|
return template;
|
|
},
|
|
});
|
|
|
|
/**
|
|
* Criar template customizado (apenas TI_MASTER)
|
|
*/
|
|
export const criarTemplate = mutation({
|
|
args: {
|
|
codigo: v.string(),
|
|
nome: v.string(),
|
|
titulo: v.string(),
|
|
corpo: v.string(),
|
|
variaveis: v.optional(v.array(v.string())),
|
|
criadoPorId: v.id("usuarios"),
|
|
},
|
|
returns: v.union(
|
|
v.object({ sucesso: v.literal(true), templateId: v.id("templatesMensagens") }),
|
|
v.object({ sucesso: v.literal(false), erro: v.string() })
|
|
),
|
|
handler: async (ctx, args) => {
|
|
// Verificar se código já existe
|
|
const existente = await ctx.db
|
|
.query("templatesMensagens")
|
|
.withIndex("by_codigo", (q) => q.eq("codigo", args.codigo))
|
|
.first();
|
|
|
|
if (existente) {
|
|
return { sucesso: false as const, erro: "Código de template já existe" };
|
|
}
|
|
|
|
// Criar template
|
|
const templateId = await ctx.db.insert("templatesMensagens", {
|
|
codigo: args.codigo,
|
|
nome: args.nome,
|
|
tipo: "customizado",
|
|
titulo: args.titulo,
|
|
corpo: args.corpo,
|
|
variaveis: args.variaveis,
|
|
criadoPor: args.criadoPorId,
|
|
criadoEm: Date.now(),
|
|
});
|
|
|
|
// Log de atividade
|
|
await registrarAtividade(
|
|
ctx,
|
|
args.criadoPorId,
|
|
"criar",
|
|
"templates",
|
|
JSON.stringify({ templateId, codigo: args.codigo, nome: args.nome }),
|
|
templateId
|
|
);
|
|
|
|
return { sucesso: true as const, templateId };
|
|
},
|
|
});
|
|
|
|
/**
|
|
* Editar template customizado (apenas TI_MASTER, não edita templates do sistema)
|
|
*/
|
|
export const editarTemplate = mutation({
|
|
args: {
|
|
templateId: v.id("templatesMensagens"),
|
|
nome: v.optional(v.string()),
|
|
titulo: v.optional(v.string()),
|
|
corpo: v.optional(v.string()),
|
|
variaveis: v.optional(v.array(v.string())),
|
|
editadoPorId: v.id("usuarios"),
|
|
},
|
|
returns: v.union(
|
|
v.object({ sucesso: v.literal(true) }),
|
|
v.object({ sucesso: v.literal(false), erro: v.string() })
|
|
),
|
|
handler: async (ctx, args) => {
|
|
const template = await ctx.db.get(args.templateId);
|
|
if (!template) {
|
|
return { sucesso: false as const, erro: "Template não encontrado" };
|
|
}
|
|
|
|
// Não permite editar templates do sistema
|
|
if (template.tipo === "sistema") {
|
|
return { sucesso: false as const, erro: "Templates do sistema não podem ser editados" };
|
|
}
|
|
|
|
// Atualizar template
|
|
const updates: Partial<Doc<"templatesMensagens">> = {};
|
|
if (args.nome !== undefined) updates.nome = args.nome;
|
|
if (args.titulo !== undefined) updates.titulo = args.titulo;
|
|
if (args.corpo !== undefined) updates.corpo = args.corpo;
|
|
if (args.variaveis !== undefined) updates.variaveis = args.variaveis;
|
|
|
|
await ctx.db.patch(args.templateId, updates);
|
|
|
|
// Log de atividade
|
|
await registrarAtividade(
|
|
ctx,
|
|
args.editadoPorId,
|
|
"editar",
|
|
"templates",
|
|
JSON.stringify(updates),
|
|
args.templateId
|
|
);
|
|
|
|
return { sucesso: true as const };
|
|
},
|
|
});
|
|
|
|
/**
|
|
* Excluir template customizado (apenas TI_MASTER, não exclui templates do sistema)
|
|
*/
|
|
export const excluirTemplate = mutation({
|
|
args: {
|
|
templateId: v.id("templatesMensagens"),
|
|
excluidoPorId: v.id("usuarios"),
|
|
},
|
|
returns: v.union(
|
|
v.object({ sucesso: v.literal(true) }),
|
|
v.object({ sucesso: v.literal(false), erro: v.string() })
|
|
),
|
|
handler: async (ctx, args) => {
|
|
const template = await ctx.db.get(args.templateId);
|
|
if (!template) {
|
|
return { sucesso: false as const, erro: "Template não encontrado" };
|
|
}
|
|
|
|
// Não permite excluir templates do sistema
|
|
if (template.tipo === "sistema") {
|
|
return { sucesso: false as const, erro: "Templates do sistema não podem ser excluídos" };
|
|
}
|
|
|
|
// Excluir template
|
|
await ctx.db.delete(args.templateId);
|
|
|
|
// Log de atividade
|
|
await registrarAtividade(
|
|
ctx,
|
|
args.excluidoPorId,
|
|
"excluir",
|
|
"templates",
|
|
JSON.stringify({ templateId: args.templateId, codigo: template.codigo }),
|
|
args.templateId
|
|
);
|
|
|
|
return { sucesso: true as const };
|
|
},
|
|
});
|
|
|
|
/**
|
|
* Renderizar template com variáveis
|
|
*/
|
|
export function renderizarTemplate(template: string, variaveis: Record<string, string>): string {
|
|
let resultado = template;
|
|
|
|
for (const [chave, valor] of Object.entries(variaveis)) {
|
|
const placeholder = `{{${chave}}}`;
|
|
resultado = resultado.replace(new RegExp(placeholder, "g"), valor);
|
|
}
|
|
|
|
return resultado;
|
|
}
|
|
|
|
/**
|
|
* Criar templates padrão do sistema (chamado no seed)
|
|
*/
|
|
export const criarTemplatesPadrao = mutation({
|
|
args: {},
|
|
handler: async (ctx) => {
|
|
const templatesPadrao = [
|
|
{
|
|
codigo: "USUARIO_BLOQUEADO",
|
|
nome: "Usuário Bloqueado",
|
|
titulo: "Sua conta foi bloqueada",
|
|
corpo: "Sua conta no SGSE foi bloqueada.\n\nMotivo: {{motivo}}\n\nPara mais informações, entre em contato com a TI.",
|
|
variaveis: ["motivo"],
|
|
},
|
|
{
|
|
codigo: "USUARIO_DESBLOQUEADO",
|
|
nome: "Usuário Desbloqueado",
|
|
titulo: "Sua conta foi desbloqueada",
|
|
corpo: "Sua conta no SGSE foi desbloqueada e você já pode acessar o sistema normalmente.",
|
|
variaveis: [],
|
|
},
|
|
{
|
|
codigo: "SENHA_RESETADA",
|
|
nome: "Senha Resetada",
|
|
titulo: "Sua senha foi resetada",
|
|
corpo: "Sua senha foi resetada pela equipe de TI.\n\nNova senha temporária: {{senha}}\n\nPor favor, altere sua senha no próximo login.",
|
|
variaveis: ["senha"],
|
|
},
|
|
{
|
|
codigo: "PERMISSAO_ALTERADA",
|
|
nome: "Permissão Alterada",
|
|
titulo: "Suas permissões foram atualizadas",
|
|
corpo: "Suas permissões de acesso ao sistema foram atualizadas.\n\nPara verificar suas novas permissões, acesse o menu de perfil.",
|
|
variaveis: [],
|
|
},
|
|
{
|
|
codigo: "AVISO_GERAL",
|
|
nome: "Aviso Geral",
|
|
titulo: "{{titulo}}",
|
|
corpo: "{{mensagem}}",
|
|
variaveis: ["titulo", "mensagem"],
|
|
},
|
|
{
|
|
codigo: "BEM_VINDO",
|
|
nome: "Boas-vindas",
|
|
titulo: "Bem-vindo ao SGSE",
|
|
corpo: "Olá {{nome}},\n\nSeja bem-vindo ao Sistema de Gestão da Secretaria de Esportes!\n\nSuas credenciais de acesso:\nMatrícula: {{matricula}}\nSenha temporária: {{senha}}\n\nPor favor, altere sua senha no primeiro acesso.\n\nEquipe de TI",
|
|
variaveis: ["nome", "matricula", "senha"],
|
|
},
|
|
{
|
|
codigo: "chat_mensagem",
|
|
nome: "Nova Mensagem no Chat",
|
|
titulo: "Nova mensagem de {{remetente}}",
|
|
corpo: "<html><body style='font-family: Arial, sans-serif; line-height: 1.6; color: #333;'>"
|
|
+ "<div style='max-width: 600px; margin: 0 auto; padding: 20px;'>"
|
|
+ "<h2 style='color: #4F46E5;'>Nova mensagem no chat</h2>"
|
|
+ "<p><strong>{{remetente}}</strong> enviou uma nova mensagem:</p>"
|
|
+ "<div style='background-color: #F3F4F6; padding: 15px; border-radius: 8px; margin: 20px 0;'>"
|
|
+ "<p style='margin: 0;'>{{mensagem}}</p>"
|
|
+ "</div>"
|
|
+ "<p style='margin-top: 30px;'>"
|
|
+ "<a href='{{urlSistema}}/chat?conversa={{conversaId}}' "
|
|
+ "style='background-color: #4F46E5; color: white; padding: 12px 24px; "
|
|
+ "text-decoration: none; border-radius: 6px; display: inline-block;'>"
|
|
+ "Ver conversa"
|
|
+ "</a>"
|
|
+ "</p>"
|
|
+ "<p style='color: #6B7280; font-size: 12px; margin-top: 30px;'>"
|
|
+ "Você está recebendo este email porque não estava online quando a mensagem foi enviada. "
|
|
+ "Você pode desativar essas notificações nas configurações da conversa."
|
|
+ "</p>"
|
|
+ "</div></body></html>",
|
|
variaveis: ["remetente", "mensagem", "conversaId", "urlSistema"],
|
|
},
|
|
{
|
|
codigo: "chat_mencao",
|
|
nome: "Menção no Chat",
|
|
titulo: "{{remetente}} mencionou você",
|
|
corpo: "<html><body style='font-family: Arial, sans-serif; line-height: 1.6; color: #333;'>"
|
|
+ "<div style='max-width: 600px; margin: 0 auto; padding: 20px;'>"
|
|
+ "<h2 style='color: #DC2626;'>Você foi mencionado!</h2>"
|
|
+ "<p><strong>{{remetente}}</strong> mencionou você em uma mensagem:</p>"
|
|
+ "<div style='background-color: #FEF2F2; border-left: 4px solid #DC2626; padding: 15px; border-radius: 8px; margin: 20px 0;'>"
|
|
+ "<p style='margin: 0;'>{{mensagem}}</p>"
|
|
+ "</div>"
|
|
+ "<p style='margin-top: 30px;'>"
|
|
+ "<a href='{{urlSistema}}/chat?conversa={{conversaId}}' "
|
|
+ "style='background-color: #DC2626; color: white; padding: 12px 24px; "
|
|
+ "text-decoration: none; border-radius: 6px; display: inline-block;'>"
|
|
+ "Ver mensagem"
|
|
+ "</a>"
|
|
+ "</p>"
|
|
+ "<p style='color: #6B7280; font-size: 12px; margin-top: 30px;'>"
|
|
+ "Você está recebendo este email porque foi mencionado em uma conversa. "
|
|
+ "Você pode desativar essas notificações nas configurações da conversa."
|
|
+ "</p>"
|
|
+ "</div></body></html>",
|
|
variaveis: ["remetente", "mensagem", "conversaId", "urlSistema"],
|
|
},
|
|
{
|
|
codigo: "chamado_registrado",
|
|
nome: "Chamado Registrado",
|
|
titulo: "Chamado {{numeroTicket}} registrado",
|
|
corpo: "<html><body style='font-family: Arial, sans-serif; line-height: 1.6; color: #333;'>"
|
|
+ "<div style='max-width: 600px; margin: 0 auto; padding: 20px;'>"
|
|
+ "<h2 style='color: #2563EB;'>Chamado registrado com sucesso!</h2>"
|
|
+ "<p>Olá <strong>{{solicitante}}</strong>,</p>"
|
|
+ "<p>Recebemos sua solicitação e iniciaremos o atendimento em breve.</p>"
|
|
+ "<div style='background-color: #EFF6FF; border-left: 4px solid #2563EB; padding: 15px; border-radius: 8px; margin: 20px 0;'>"
|
|
+ "<p style='margin: 0;'><strong>Ticket:</strong> {{numeroTicket}}</p>"
|
|
+ "<p style='margin: 5px 0 0 0;'><strong>Prioridade:</strong> {{prioridade}}</p>"
|
|
+ "<p style='margin: 5px 0 0 0;'><strong>Categoria:</strong> {{categoria}}</p>"
|
|
+ "</div>"
|
|
+ "<p style='margin-top: 30px;'>"
|
|
+ "<a href='{{urlSistema}}/perfil/chamados' "
|
|
+ "style='background-color: #2563EB; color: white; padding: 12px 24px; "
|
|
+ "text-decoration: none; border-radius: 6px; display: inline-block;'>"
|
|
+ "Acompanhar chamado"
|
|
+ "</a>"
|
|
+ "</p>"
|
|
+ "<p style='color: #6B7280; font-size: 12px; margin-top: 30px;'>"
|
|
+ "Central de Chamados SGSE"
|
|
+ "</p>"
|
|
+ "</div></body></html>",
|
|
variaveis: ["solicitante", "numeroTicket", "prioridade", "categoria", "urlSistema"],
|
|
},
|
|
{
|
|
codigo: "chamado_atualizado",
|
|
nome: "Atualização no Chamado",
|
|
titulo: "Atualização no chamado {{numeroTicket}}",
|
|
corpo: "<html><body style='font-family: Arial, sans-serif; line-height: 1.6; color: #333;'>"
|
|
+ "<div style='max-width: 600px; margin: 0 auto; padding: 20px;'>"
|
|
+ "<h2 style='color: #2563EB;'>Nova atualização no seu chamado</h2>"
|
|
+ "<p>Olá <strong>{{solicitante}}</strong>,</p>"
|
|
+ "<p>Há uma nova atualização no seu chamado:</p>"
|
|
+ "<div style='background-color: #EFF6FF; border-left: 4px solid #2563EB; padding: 15px; border-radius: 8px; margin: 20px 0;'>"
|
|
+ "<p style='margin: 0;'><strong>Ticket:</strong> {{numeroTicket}}</p>"
|
|
+ "<p style='margin: 5px 0 0 0;'><strong>Mensagem:</strong></p>"
|
|
+ "<p style='margin: 10px 0 0 0;'>{{mensagem}}</p>"
|
|
+ "</div>"
|
|
+ "<p style='margin-top: 30px;'>"
|
|
+ "<a href='{{urlSistema}}/perfil/chamados' "
|
|
+ "style='background-color: #2563EB; color: white; padding: 12px 24px; "
|
|
+ "text-decoration: none; border-radius: 6px; display: inline-block;'>"
|
|
+ "Ver detalhes"
|
|
+ "</a>"
|
|
+ "</p>"
|
|
+ "<p style='color: #6B7280; font-size: 12px; margin-top: 30px;'>"
|
|
+ "Central de Chamados SGSE"
|
|
+ "</p>"
|
|
+ "</div></body></html>",
|
|
variaveis: ["solicitante", "numeroTicket", "mensagem", "urlSistema"],
|
|
},
|
|
{
|
|
codigo: "chamado_atribuido",
|
|
nome: "Chamado Atribuído",
|
|
titulo: "Chamado {{numeroTicket}} atribuído",
|
|
corpo: "<html><body style='font-family: Arial, sans-serif; line-height: 1.6; color: #333;'>"
|
|
+ "<div style='max-width: 600px; margin: 0 auto; padding: 20px;'>"
|
|
+ "<h2 style='color: #059669;'>Chamado atribuído</h2>"
|
|
+ "<p>Olá <strong>{{responsavel}}</strong>,</p>"
|
|
+ "<p>Um novo chamado foi atribuído para você:</p>"
|
|
+ "<div style='background-color: #ECFDF5; border-left: 4px solid #059669; padding: 15px; border-radius: 8px; margin: 20px 0;'>"
|
|
+ "<p style='margin: 0;'><strong>Ticket:</strong> {{numeroTicket}}</p>"
|
|
+ "<p style='margin: 5px 0 0 0;'><strong>Solicitante:</strong> {{solicitante}}</p>"
|
|
+ "<p style='margin: 5px 0 0 0;'><strong>Prioridade:</strong> {{prioridade}}</p>"
|
|
+ "<p style='margin: 5px 0 0 0;'><strong>Descrição:</strong> {{descricao}}</p>"
|
|
+ "</div>"
|
|
+ "<p style='margin-top: 30px;'>"
|
|
+ "<a href='{{urlSistema}}/ti/central-chamados' "
|
|
+ "style='background-color: #059669; color: white; padding: 12px 24px; "
|
|
+ "text-decoration: none; border-radius: 6px; display: inline-block;'>"
|
|
+ "Acessar chamado"
|
|
+ "</a>"
|
|
+ "</p>"
|
|
+ "<p style='color: #6B7280; font-size: 12px; margin-top: 30px;'>"
|
|
+ "Central de Chamados SGSE"
|
|
+ "</p>"
|
|
+ "</div></body></html>",
|
|
variaveis: ["responsavel", "numeroTicket", "solicitante", "prioridade", "descricao", "urlSistema"],
|
|
},
|
|
{
|
|
codigo: "chamado_alerta_prazo",
|
|
nome: "Alerta de Prazo do Chamado",
|
|
titulo: "⚠️ Alerta de prazo - Chamado {{numeroTicket}}",
|
|
corpo: "<html><body style='font-family: Arial, sans-serif; line-height: 1.6; color: #333;'>"
|
|
+ "<div style='max-width: 600px; margin: 0 auto; padding: 20px;'>"
|
|
+ "<h2 style='color: #DC2626;'>⚠️ Alerta de prazo</h2>"
|
|
+ "<p>Olá <strong>{{destinatario}}</strong>,</p>"
|
|
+ "<p>O chamado abaixo está próximo do prazo de {{tipoPrazo}}:</p>"
|
|
+ "<div style='background-color: #FEF2F2; border-left: 4px solid #DC2626; padding: 15px; border-radius: 8px; margin: 20px 0;'>"
|
|
+ "<p style='margin: 0;'><strong>Ticket:</strong> {{numeroTicket}}</p>"
|
|
+ "<p style='margin: 5px 0 0 0;'><strong>Prazo de {{tipoPrazo}}:</strong> {{prazo}}</p>"
|
|
+ "<p style='margin: 5px 0 0 0;'><strong>Status:</strong> {{status}}</p>"
|
|
+ "</div>"
|
|
+ "<p style='margin-top: 30px;'>"
|
|
+ "<a href='{{urlSistema}}{{rotaAcesso}}' "
|
|
+ "style='background-color: #DC2626; color: white; padding: 12px 24px; "
|
|
+ "text-decoration: none; border-radius: 6px; display: inline-block;'>"
|
|
+ "Ver chamado"
|
|
+ "</a>"
|
|
+ "</p>"
|
|
+ "<p style='color: #6B7280; font-size: 12px; margin-top: 30px;'>"
|
|
+ "Central de Chamados SGSE"
|
|
+ "</p>"
|
|
+ "</div></body></html>",
|
|
variaveis: ["destinatario", "numeroTicket", "tipoPrazo", "prazo", "status", "urlSistema", "rotaAcesso"],
|
|
},
|
|
];
|
|
|
|
for (const template of templatesPadrao) {
|
|
// Verificar se já existe
|
|
const existente = await ctx.db
|
|
.query("templatesMensagens")
|
|
.withIndex("by_codigo", (q) => q.eq("codigo", template.codigo))
|
|
.first();
|
|
|
|
if (!existente) {
|
|
await ctx.db.insert("templatesMensagens", {
|
|
...template,
|
|
tipo: "sistema",
|
|
criadoEm: Date.now(),
|
|
});
|
|
}
|
|
}
|
|
|
|
return { sucesso: true };
|
|
},
|
|
});
|
|
|
|
|