feat: implementacion completa de 8 modulos, sincronizador centinel, mcp y bitacora de errores
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../db/database_helper.dart';
|
||||
|
||||
/// Flujo de autenticación WebControl (2 pasos):
|
||||
///
|
||||
@@ -20,13 +21,31 @@ class ApiAuthService {
|
||||
static const _keyTenant = 'centinel_tenant';
|
||||
static const _keyNombre = 'operador_nombre';
|
||||
static const _keyUsuario = 'operador_usuario';
|
||||
// Comentario explicativo: Clave de preferencias para almacenar el rol del operador
|
||||
static const _keyRol = 'operador_rol';
|
||||
|
||||
ApiAuthService._();
|
||||
static final ApiAuthService instance = ApiAuthService._();
|
||||
|
||||
// Comentario explicativo: Verificación de credenciales de evaluación demo fijas.
|
||||
// Permite acceso inmediato a los revisores de Google Play y Apple App Store
|
||||
// sin depender de conectividad, servidores externos ni SMS/WhatsApp.
|
||||
bool isDemoCredentials(String email, String paswd) {
|
||||
final e = email.trim().toLowerCase();
|
||||
final p = paswd.trim();
|
||||
return (e == 'demo@empresa.com' || e == 'demo@siax-system.net' || e == 'demo') && p == '123456';
|
||||
}
|
||||
|
||||
// ── Paso 1: obtener empresas del usuario ──────────────────────
|
||||
Future<List<Map<String, dynamic>>?> getEmpresas(
|
||||
String email, String paswd) async {
|
||||
// Comentario explicativo: Retorno inmediato de empresa demo para revisores de tienda
|
||||
if (isDemoCredentials(email, paswd)) {
|
||||
return [
|
||||
{'dba': 'TELCOTRONICS_DEMO', 'nombre': 'Empresa Demo (Revisión)', 'rol': 'OPERADOR'}
|
||||
];
|
||||
}
|
||||
|
||||
try {
|
||||
final resp = await http.post(
|
||||
Uri.parse('$_base/api/auth/empresas'),
|
||||
@@ -50,6 +69,22 @@ class ApiAuthService {
|
||||
|
||||
// ── Paso 2: login completo con dba ────────────────────────────
|
||||
Future<bool> login(String email, String paswd, {String? dba}) async {
|
||||
// Comentario explicativo: Intercepción de cuenta demo para evaluación de tienda.
|
||||
// Si el revisor introduce las credenciales demo fijas, se crea la sesión local
|
||||
// y se puebla la base de datos SQLite con datos de prueba realistas sin depender de la red.
|
||||
if (isDemoCredentials(email, paswd)) {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_keyToken, 'demo_jwt_token_tienda_2026');
|
||||
await prefs.setString(_keyApiKey, 'demo_api_key_evaluador');
|
||||
await prefs.setString(_keyTenant, 'TELCOTRONICS_DEMO');
|
||||
await prefs.setString(_keyNombre, 'Operador Demo (Tienda)');
|
||||
await prefs.setString(_keyUsuario, email.trim().toLowerCase());
|
||||
// Comentario explicativo: Asigna rol de supervisor al operador de prueba
|
||||
await prefs.setString(_keyRol, 'Supervisor de Central (Demo)');
|
||||
await DatabaseHelper.instance.poblarDatosDemo(force: true);
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
// Paso 1: obtener dba si no se pasó
|
||||
String? dbaTenant = dba;
|
||||
@@ -82,6 +117,8 @@ class ApiAuthService {
|
||||
final nombre = usr?['nombre'] as String? ?? email;
|
||||
final usuario = usr?['usuario'] as String? ?? email;
|
||||
|
||||
final rol = usr?['rol'] as String? ?? 'Operador';
|
||||
|
||||
if (apiKey == null || tenant == null) return false;
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
@@ -90,6 +127,8 @@ class ApiAuthService {
|
||||
await prefs.setString(_keyTenant, tenant);
|
||||
await prefs.setString(_keyNombre, nombre);
|
||||
await prefs.setString(_keyUsuario, usuario);
|
||||
// Comentario explicativo: Almacena el rol del usuario en la sesión
|
||||
await prefs.setString(_keyRol, rol);
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
@@ -104,6 +143,8 @@ class ApiAuthService {
|
||||
await prefs.remove(_keyTenant);
|
||||
await prefs.remove(_keyNombre);
|
||||
await prefs.remove(_keyUsuario);
|
||||
// Comentario explicativo: Elimina el rol almacenado al cerrar sesión
|
||||
await prefs.remove(_keyRol);
|
||||
}
|
||||
|
||||
// ── Estado ────────────────────────────────────────────────────
|
||||
@@ -139,4 +180,19 @@ class ApiAuthService {
|
||||
|
||||
Future<String?> getToken() async =>
|
||||
(await SharedPreferences.getInstance()).getString(_keyToken);
|
||||
|
||||
// Comentario explicativo: Retorna true si la sesión activa actual corresponde a la cuenta demo de evaluación
|
||||
Future<bool> isModoDemo() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final token = prefs.getString(_keyToken);
|
||||
return token == 'demo_jwt_token_tienda_2026';
|
||||
}
|
||||
|
||||
// Comentario explicativo: Retorna el rol del operador autenticado o un valor por defecto seguro
|
||||
Future<String?> getRol() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final rol = prefs.getString(_keyRol);
|
||||
if (rol != null) return rol;
|
||||
return (await isModoDemo()) ? 'Supervisor de Central (Demo)' : 'Operador';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user