Update hostinger_controller.go
This commit is contained in:
@@ -401,16 +401,16 @@ func syncVencimientoFromOrders(orders []services.HostingerSubscription) {
|
|||||||
db := app.Http.Database.DB
|
db := app.Http.Database.DB
|
||||||
|
|
||||||
// Indexar suscripciones por ID para búsqueda O(1)
|
// Indexar suscripciones por ID para búsqueda O(1)
|
||||||
subMap := make(map[string]services.HostingerSubscription, len(orders))
|
subByID := make(map[string]services.HostingerSubscription, len(orders))
|
||||||
for _, sub := range orders {
|
for _, sub := range orders {
|
||||||
subMap[sub.ID] = sub
|
subByID[sub.ID] = sub
|
||||||
}
|
}
|
||||||
|
|
||||||
var servidores []models.Servidor
|
var servidores []models.Servidor
|
||||||
db.Where("hostinger_subscription_id != '' AND hostinger_subscription_id IS NOT NULL").Find(&servidores)
|
db.Where("hostinger_subscription_id != '' AND hostinger_subscription_id IS NOT NULL").Find(&servidores)
|
||||||
|
|
||||||
for _, srv := range servidores {
|
for _, srv := range servidores {
|
||||||
sub, ok := subMap[srv.HostingerSubscriptionID]
|
sub, ok := subByID[srv.HostingerSubscriptionID]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -427,30 +427,48 @@ func syncVencimientoFromOrders(orders []services.HostingerSubscription) {
|
|||||||
|
|
||||||
// syncServidoresFromVPSList actualiza specs y vencimiento de todos los servidores
|
// syncServidoresFromVPSList actualiza specs y vencimiento de todos los servidores
|
||||||
// que tienen hostinger_vps_id vinculado, usando la lista de VPS ya descargada.
|
// que tienen hostinger_vps_id vinculado, usando la lista de VPS ya descargada.
|
||||||
// El vencimiento se obtiene con una llamada al billing de cada VPS (por subscription_id).
|
// También auto-descubre el vínculo comparando la IP del VPS con ip_servidor.
|
||||||
func syncServidoresFromVPSList(client *services.HostingerClient, vpsList []services.HostingerVPS) {
|
func syncServidoresFromVPSList(client *services.HostingerClient, vpsList []services.HostingerVPS) {
|
||||||
db := app.Http.Database.DB
|
db := app.Http.Database.DB
|
||||||
|
|
||||||
// Indexar VPS por ID
|
// Cargar todos los servidores
|
||||||
vpsMap := make(map[int]services.HostingerVPS, len(vpsList))
|
var todos []models.Servidor
|
||||||
for _, vps := range vpsList {
|
db.Find(&todos)
|
||||||
vpsMap[vps.ID] = vps
|
|
||||||
|
// Índice por hostinger_vps_id (ya vinculados)
|
||||||
|
byVpsID := make(map[int]*models.Servidor, len(todos))
|
||||||
|
// Índice por IP (para auto-descubrimiento)
|
||||||
|
byIP := make(map[string]*models.Servidor, len(todos))
|
||||||
|
for i := range todos {
|
||||||
|
if todos[i].HostingerVpsID != nil {
|
||||||
|
byVpsID[*todos[i].HostingerVpsID] = &todos[i]
|
||||||
|
}
|
||||||
|
if todos[i].IpServidor != "" {
|
||||||
|
byIP[todos[i].IpServidor] = &todos[i]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var servidores []models.Servidor
|
for _, vps := range vpsList {
|
||||||
db.Where("hostinger_vps_id IS NOT NULL").Find(&servidores)
|
// 1. Buscar por hostinger_vps_id ya guardado
|
||||||
|
srv := byVpsID[vps.ID]
|
||||||
|
|
||||||
for _, srv := range servidores {
|
// 2. Auto-descubrir por IP si no está vinculado
|
||||||
if srv.HostingerVpsID == nil {
|
if srv == nil {
|
||||||
continue
|
for _, ip4 := range vps.IPV4 {
|
||||||
|
if s, ok := byIP[ip4.Address]; ok {
|
||||||
|
srv = s
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
vps, ok := vpsMap[*srv.HostingerVpsID]
|
|
||||||
if !ok {
|
if srv == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
updates := map[string]interface{}{
|
updates := map[string]interface{}{
|
||||||
"hostinger_state": vps.State,
|
"hostinger_vps_id": vps.ID,
|
||||||
|
"hostinger_state": vps.State,
|
||||||
}
|
}
|
||||||
if len(vps.IPV4) > 0 && vps.IPV4[0].Address != "" {
|
if len(vps.IPV4) > 0 && vps.IPV4[0].Address != "" {
|
||||||
updates["ip_servidor"] = vps.IPV4[0].Address
|
updates["ip_servidor"] = vps.IPV4[0].Address
|
||||||
@@ -465,12 +483,14 @@ func syncServidoresFromVPSList(client *services.HostingerClient, vpsList []servi
|
|||||||
updates["disco"] = fmt.Sprintf("%d GB", vps.DiskBytes/1024)
|
updates["disco"] = fmt.Sprintf("%d GB", vps.DiskBytes/1024)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Vencimiento: usar subscription_id del VPS o el ya guardado
|
// Guardar subscription_id del VPS para el puente con facturación
|
||||||
subID := vps.SubscriptionID
|
subID := vps.SubscriptionID
|
||||||
if subID == "" {
|
if subID == "" {
|
||||||
subID = srv.HostingerSubscriptionID
|
subID = srv.HostingerSubscriptionID
|
||||||
}
|
}
|
||||||
if subID != "" {
|
if subID != "" {
|
||||||
|
updates["hostinger_subscription_id"] = subID
|
||||||
|
// Intentar traer fecha de vencimiento desde billing
|
||||||
if sub, err := client.GetSubscriptionByID(subID); err == nil {
|
if sub, err := client.GetSubscriptionByID(subID); err == nil {
|
||||||
venc := sub.ExpiresAt
|
venc := sub.ExpiresAt
|
||||||
if venc == "" {
|
if venc == "" {
|
||||||
@@ -479,7 +499,6 @@ func syncServidoresFromVPSList(client *services.HostingerClient, vpsList []servi
|
|||||||
if venc != "" && len(venc) >= 10 {
|
if venc != "" && len(venc) >= 10 {
|
||||||
updates["vencimiento"] = venc[:10]
|
updates["vencimiento"] = venc[:10]
|
||||||
}
|
}
|
||||||
updates["hostinger_subscription_id"] = subID
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user