From 6e836e9eb5b0bf8a674947c647e4ef654b9b0602 Mon Sep 17 00:00:00 2001 From: deyvisonwanderley Date: Mon, 1 Dec 2025 19:54:33 -0300 Subject: [PATCH] feat: implement template retrieval by ID and enhance error handling in template display for improved user experience --- .../ti/notificacoes/templates/+page.svelte | 5 +- .../notificacoes/templates/[id]/+page.svelte | 73 ++++++++++++++----- packages/backend/convex/templatesMensagens.ts | 13 ++++ 3 files changed, 72 insertions(+), 19 deletions(-) diff --git a/apps/web/src/routes/(dashboard)/ti/notificacoes/templates/+page.svelte b/apps/web/src/routes/(dashboard)/ti/notificacoes/templates/+page.svelte index 2869d8a..ed24f76 100644 --- a/apps/web/src/routes/(dashboard)/ti/notificacoes/templates/+page.svelte +++ b/apps/web/src/routes/(dashboard)/ti/notificacoes/templates/+page.svelte @@ -3,6 +3,7 @@ import { api } from '@sgse-app/backend/convex/_generated/api'; import type { FunctionReference } from 'convex/server'; import type { Id, Doc } from '@sgse-app/backend/convex/_generated/dataModel'; + import { resolve } from '$app/paths'; const client = useConvexClient(); const currentUser = useQuery(api.auth.getCurrentUser as FunctionReference<'query'>); @@ -191,7 +192,7 @@

Templates ({templatesFiltrados.length})

- +
diff --git a/apps/web/src/routes/(dashboard)/ti/notificacoes/templates/[id]/+page.svelte b/apps/web/src/routes/(dashboard)/ti/notificacoes/templates/[id]/+page.svelte index 2533506..071b4ce 100644 --- a/apps/web/src/routes/(dashboard)/ti/notificacoes/templates/[id]/+page.svelte +++ b/apps/web/src/routes/(dashboard)/ti/notificacoes/templates/[id]/+page.svelte @@ -3,29 +3,51 @@ import { useConvexClient, useQuery } from 'convex-svelte'; import { api } from '@sgse-app/backend/convex/_generated/api'; import type { FunctionReference } from 'convex/server'; - import type { Doc } from '@sgse-app/backend/convex/_generated/dataModel'; + import type { Doc, Id } from '@sgse-app/backend/convex/_generated/dataModel'; import { goto } from '$app/navigation'; import { resolve } from '$app/paths'; const client = useConvexClient(); const currentUser = useQuery(api.auth.getCurrentUser as FunctionReference<'query'>); - const templatesQuery = useQuery(api.templatesMensagens.listarTemplates, {}); - - const params = $derived($page.params); - const templateIdParam = $derived(params.id ?? ''); - - const templates = $derived.by(() => { - if (templatesQuery === undefined || templatesQuery === null) return []; - if ('data' in templatesQuery && templatesQuery.data !== undefined) { - return Array.isArray(templatesQuery.data) ? templatesQuery.data : []; + + const templateIdParam = $derived($page.params.id ?? ''); + + // Query específica para buscar o template por ID + const templateQuery = useQuery( + api.templatesMensagens.obterTemplatePorId, + () => { + if (!templateIdParam) return 'skip'; + // Validar se o ID tem o formato correto do Convex + if (typeof templateIdParam === 'string' && templateIdParam.length > 0) { + return { templateId: templateIdParam as Id<'templatesMensagens'> }; + } + return 'skip'; } - if (Array.isArray(templatesQuery)) return templatesQuery; - return []; + ); + + // Extrair template da query + const template = $derived.by(() => { + if (templateQuery === undefined || templateQuery === null) return null; + // useQuery retorna os dados diretamente + if (templateQuery && typeof templateQuery === 'object') { + // Se tem propriedade data, usar ela + if ('data' in templateQuery && templateQuery.data !== undefined && templateQuery.data !== null) { + return templateQuery.data as Doc<'templatesMensagens'> | null; + } + // Caso contrário, assumir que é o próprio template + if (!('data' in templateQuery)) { + return templateQuery as Doc<'templatesMensagens'> | null; + } + } + return null; }); - const template = $derived.by(() => { - const lista = templates as Doc<'templatesMensagens'>[]; - return lista.find((t) => String(t._id) === templateIdParam) ?? null; + const carregandoTemplate = $derived(templateQuery === undefined || templateQuery === null); + const erroTemplate = $derived.by(() => { + if (templateQuery && typeof templateQuery === 'object' && 'error' in templateQuery) { + return templateQuery.error as Error | string | null; + } + return null; }); let nome = $state(''); @@ -143,9 +165,25 @@ Voltar
- {#if !template} + {#if carregandoTemplate} +
+ +

Carregando template...

+
+ {:else if erroTemplate}
- Template não encontrado. + Erro ao carregar template: {typeof erroTemplate === 'string' ? erroTemplate : erroTemplate?.message || 'Erro desconhecido'} + + Voltar para Templates + +
+ {:else if !template} +
+ Template não encontrado. Verifique se o ID está correto. +
ID: {templateIdParam}
+ + Voltar para Templates +
{:else} {#if mensagem} @@ -279,6 +317,7 @@ {/if}
+
{/if} diff --git a/packages/backend/convex/templatesMensagens.ts b/packages/backend/convex/templatesMensagens.ts index d6a0387..78c24de 100644 --- a/packages/backend/convex/templatesMensagens.ts +++ b/packages/backend/convex/templatesMensagens.ts @@ -32,6 +32,19 @@ export const obterTemplatePorCodigo = query({ }, }); +/** + * Obter template por ID + */ +export const obterTemplatePorId = query({ + args: { + templateId: v.id("templatesMensagens"), + }, + handler: async (ctx, args) => { + const template = await ctx.db.get(args.templateId); + return template; + }, +}); + /** * Criar template customizado (apenas TI_MASTER) */