feat: integración cloud real — JWT auto-login, upload pedidos y sync

- tryAutoLogin: restaura sesión cloud y local desde SharedPreferences
- uploadPedido: sube pedidos al gateway con toCloudEncabezado/Detalle
- _uploadPendingPedidos: sube PENDIENTE en silencio al iniciar sesión
- _syncCloudData: actualiza contadores itemsSynced/clientesSynced
- loginLocal(): auth explícita sin cloud para el toggle "Cuenta local"
- DB v2: migración onUpgrade agrega columna sync_estado, SHA-256 en passwords
- Pedido.syncEstado: campo mutable PENDIENTE|SINCRONIZADO|ERROR
- ItemPedido.toCloudDetalle() + Pedido.toCloudEncabezado(usuario)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Pablinux
2026-06-17 09:26:59 -05:00
co-authored by Claude Sonnet 4.6
parent abe310699d
commit eb081f3ea4
6 changed files with 324 additions and 35 deletions
+28
View File
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import '../models/pedido.dart';
// URL idéntica a sigmac_app/gateway_cloud_api.dart
const _kBase = 'https://api-gateway-cloud.telcotronics.net';
@@ -159,6 +160,33 @@ class CloudSyncService {
.toList();
}
// ── Upload Pedidos ─────────────────────────────────────────────────────────
/// Sube un pedido al cloud.
/// Endpoint: POST /api/documentos/pedidos?db={db}
/// Body: { encabezado: {...}, detalles: [...] }
static Future<void> uploadPedido(Pedido pedido, String usuario) async {
final db = await _requireDb();
final headers = await _headers();
headers['Content-Type'] = 'application/json';
final uri = Uri.parse('$_kBase/api/documentos/pedidos')
.replace(queryParameters: {'db': db});
final res = await http
.post(
uri,
headers: headers,
body: jsonEncode({
'encabezado': pedido.toCloudEncabezado(usuario),
'detalles': pedido.items.map((i) => i.toCloudDetalle()).toList(),
}),
)
.timeout(const Duration(seconds: 20));
_checkStatus(res);
}
// ── Helper ─────────────────────────────────────────────────────────────────
static void _checkStatus(http.Response res) {