@@ -13,14 +13,23 @@ class StudentLookupViewModel extends ChangeNotifier {
1313
1414 // 1. VARIABLE PARA GUARDAR EL USUARIO ENCONTRADO
1515 UserModel ? _foundUser;
16+ bool _isBoletaVerified = false ;
17+
1618
17- // Getters para que la vista consuma los datos
1819 bool get isLoading => _isLoading;
1920 String ? get errorMessage => _errorMessage;
20-
21- // 2. GETTER PÚBLICO
2221 UserModel ? get foundUser => _foundUser;
22+ bool get isBoletaVerified => _isBoletaVerified;
23+
24+ final RegExp _curpRegex = RegExp (
25+ r'^[A-Z][AEIOUX][A-Z]{2}\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])[HM](?:AS|BC|BS|CC|CL|CM|CS|CH|DF|DG|GT|GR|HG|JC|MC|MN|MS|NT|NL|OC|PL|QT|QR|SP|SL|SR|TC|TS|TL|VZ|YN|ZS|NE)[B-DF-HJ-NP-TV-Z]{3}[0-9A-Z]\d$' ,
26+ );
27+
28+ bool isCurpValid (String curp) {
29+ return _curpRegex.hasMatch (curp.toUpperCase ());
30+ }
2331
32+ // PASO 1: Buscar Boleta
2433 Future <bool > searchStudent (String boleta) async {
2534 if (boleta.isEmpty) {
2635 _errorMessage = "Escribe una boleta" ;
@@ -31,6 +40,7 @@ class StudentLookupViewModel extends ChangeNotifier {
3140 _isLoading = true ;
3241 _errorMessage = null ;
3342 _foundUser = null ; // Reseteamos búsqueda anterior
43+ _isBoletaVerified = false ;
3444 notifyListeners ();
3545
3646 try {
@@ -48,6 +58,7 @@ class StudentLookupViewModel extends ChangeNotifier {
4858 } else {
4959 // 3. ¡ÉXITO! GUARDAMOS EL USUARIO
5060 _foundUser = user;
61+ _isBoletaVerified = true ;
5162 notifyListeners ();
5263 return true ;
5364 }
@@ -58,4 +69,45 @@ class StudentLookupViewModel extends ChangeNotifier {
5869 return false ;
5970 }
6071 }
72+
73+ Future <bool > validateCurp (String curp) async {
74+ _errorMessage = null ;
75+
76+ // 1. Validar Regex
77+ if (curp.isEmpty || ! _curpRegex.hasMatch (curp.toUpperCase ())) {
78+ _errorMessage = "El formato del CURP es inválido." ;
79+ notifyListeners ();
80+ return false ;
81+ }
82+
83+ _isLoading = true ;
84+ notifyListeners ();
85+
86+ try {
87+ // 2. Validar duplicado en BD
88+ final bool exists = await _authRepo.checkCurpExists (curp.toUpperCase ());
89+ _isLoading = false ;
90+
91+ if (exists) {
92+ _errorMessage = "Este CURP ya está registrado en otra cuenta." ;
93+ notifyListeners ();
94+ return false ;
95+ }
96+
97+ return true ;
98+ } catch (e) {
99+ _isLoading = false ;
100+ _errorMessage = "Error al validar CURP: $e " ;
101+ notifyListeners ();
102+ return false ;
103+ }
104+ }
105+
106+ // Método para "resetear" si el usuario se equivocó de boleta
107+ void resetSearch () {
108+ _foundUser = null ;
109+ _isBoletaVerified = false ;
110+ _errorMessage = null ;
111+ notifyListeners ();
112+ }
61113}
0 commit comments