Implementa las 4 fases de la especificación de automatización: módulo de plantillas/tarifas editable por el equipo, generación de PDF (HTML+JS vía Chrome headless) para cotizaciones/contratos/arquitecturas/cuentas de cobro, chat propio en el dashboard reutilizando el mismo motor y tools del bot de Telegram, y nuevas tools del agente para crear estos documentos end-to-end. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
|
)
|
|
|
|
// GetDocumentosGenerados lista el historial de documentos producidos por la
|
|
// automatización con IA (cotizaciones, contratos, actas, cuentas de cobro),
|
|
// sin importar el canal que los generó.
|
|
func GetDocumentosGenerados(c *fiber.Ctx) error {
|
|
page, _ := strconv.Atoi(c.Query("page", "1"))
|
|
limit, _ := strconv.Atoi(c.Query("limit", "20"))
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
offset := (page - 1) * limit
|
|
clienteID, _ := strconv.ParseUint(c.Query("cliente_id", "0"), 10, 32)
|
|
records, total, err := models.GetAllDocumentosGenerados(limit, offset, c.Query("tipo"), uint(clienteID))
|
|
if err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(fiber.Map{
|
|
"registros": records,
|
|
"total": total,
|
|
"totalPages": int(math.Ceil(float64(total) / float64(limit))),
|
|
"page": page,
|
|
})
|
|
}
|
|
|
|
// DownloadDocumentoGenerado sirve el PDF ya generado.
|
|
func DownloadDocumentoGenerado(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 32)
|
|
if err != nil {
|
|
return c.Status(400).JSON(fiber.Map{"error": "ID inválido"})
|
|
}
|
|
doc, err := models.GetDocumentoGeneradoByID(uint(id))
|
|
if err != nil || doc.Archivo == "" {
|
|
return c.Status(404).JSON(fiber.Map{"error": "Documento no encontrado"})
|
|
}
|
|
clean := filepath.Clean(doc.Archivo)
|
|
if !strings.HasPrefix(clean, "uploads/") {
|
|
return c.Status(403).JSON(fiber.Map{"error": "Acceso denegado"})
|
|
}
|
|
c.Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, doc.Nombre))
|
|
return c.SendFile(clean)
|
|
}
|