This commit is contained in:
Lizandro Guarnizo
2026-05-16 21:27:30 -05:00
parent fcc3ff7ca7
commit 4fa506c7d6
11 changed files with 586 additions and 13 deletions
+15 -2
View File
@@ -28,8 +28,10 @@ type PortalUser struct {
Telefono string `json:"telefono" gorm:"column:telefono"`
Indicativo string `json:"indicativo" gorm:"column:indicativo"` // ej: +57, +1
Pais string `json:"pais" gorm:"column:pais"`
Documento string `json:"documento" gorm:"column:documento"` // RUT, NIT, CC, etc.
Empresa string `json:"empresa" gorm:"column:empresa"` // empresa que representa
Documento string `json:"documento" gorm:"column:documento"` // RUT, NIT, CC, etc.
Empresa string `json:"empresa" gorm:"column:empresa"` // empresa que representa
DocumentoRutFile string `json:"documento_rut_file" gorm:"column:documento_rut_file"` // ruta del archivo RUT
DocumentoRutNombre string `json:"documento_rut_nombre" gorm:"column:documento_rut_nombre"` // nombre original
PortalAccesos []PortalAcceso `json:"portal_accesos" gorm:"foreignKey:PortalUserID"`
}
@@ -99,6 +101,17 @@ func UpdatePortalUserPassword(id uint, hashedPassword string) error {
return app.Http.Database.DB.Model(&PortalUser{}).Where("id = ?", id).Update("password", hashedPassword).Error
}
func UpdatePortalUserTelegramChatID(id uint, chatID string) error {
return app.Http.Database.DB.Model(&PortalUser{}).Where("id = ?", id).Update("telegram_chat_id", chatID).Error
}
func UpdatePortalUserRutFile(id uint, path, nombre string) error {
return app.Http.Database.DB.Model(&PortalUser{}).Where("id = ?", id).Updates(map[string]interface{}{
"documento_rut_file": path,
"documento_rut_nombre": nombre,
}).Error
}
func DeletePortalUser(id uint) error {
// Eliminar también sus accesos de partner
if err := app.Http.Database.DB.Where("portal_user_id = ?", id).Delete(&PortalAcceso{}).Error; err != nil {
+50
View File
@@ -0,0 +1,50 @@
package models
import (
"crypto/rand"
"fmt"
"github.com/sujit-baniya/fiber-boilerplate/app"
"gorm.io/gorm"
)
// TelegramPortalToken almacena un código de verificación temporal para vincular
// el Telegram de un usuario del portal con el bot de notificaciones.
type TelegramPortalToken struct {
gorm.Model
PortalUserID uint `json:"portal_user_id" gorm:"column:portal_user_id;uniqueIndex"`
Token string `json:"token" gorm:"column:token;uniqueIndex;size:8"`
}
func (TelegramPortalToken) TableName() string { return "telegram_portal_tokens" }
// GenerateTelegramPortalToken genera (o renueva) el código de vinculación para el usuario.
// El código tiene 6 caracteres hexadecimales en mayúsculas (e.g. "A3F9B2").
func GenerateTelegramPortalToken(portalUserID uint) (*TelegramPortalToken, error) {
// Eliminar tokens previos del mismo usuario
app.Http.Database.DB.Where("portal_user_id = ?", portalUserID).Delete(&TelegramPortalToken{})
b := make([]byte, 3)
if _, err := rand.Read(b); err != nil {
return nil, fmt.Errorf("no se pudo generar token: %w", err)
}
token := fmt.Sprintf("%06X", b)
t := &TelegramPortalToken{PortalUserID: portalUserID, Token: token}
if err := app.Http.Database.DB.Create(t).Error; err != nil {
return nil, err
}
return t, nil
}
// FindTelegramPortalToken busca un token de vinculación activo.
func FindTelegramPortalToken(token string) (*TelegramPortalToken, error) {
var t TelegramPortalToken
err := app.Http.Database.DB.Where("token = ?", token).First(&t).Error
return &t, err
}
// DeleteTelegramPortalToken elimina el token de un usuario (tras vincular correctamente).
func DeleteTelegramPortalToken(portalUserID uint) {
app.Http.Database.DB.Where("portal_user_id = ?", portalUserID).Delete(&TelegramPortalToken{})
}
+22
View File
@@ -57,6 +57,28 @@ func (ts *TelegramService) SendMessage(chatID interface{}, message string) error
return nil
}
// GetBotUsername obtiene el username (@nombre) del bot a partir de su token.
func GetBotUsername(botToken string) string {
if botToken == "" {
return ""
}
resp, err := http.Get(fmt.Sprintf("https://api.telegram.org/bot%s/getMe", botToken))
if err != nil {
return ""
}
defer resp.Body.Close()
var result struct {
OK bool `json:"ok"`
Result struct {
Username string `json:"username"`
} `json:"result"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return ""
}
return result.Result.Username
}
// SendMessageWithToken envía un mensaje usando un bot token explícito (útil para notificar a usuarios con su propio chat_id).
func (ts *TelegramService) SendMessageWithToken(chatID interface{}, message, botToken string) error {
svc := &TelegramService{BotToken: botToken}