-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathgroup-security-advisor.js
More file actions
860 lines (746 loc) · 31.7 KB
/
Copy pathgroup-security-advisor.js
File metadata and controls
860 lines (746 loc) · 31.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/**
* La Tanda - Sistema de Advertencias de Seguridad para Grupos
* Funcionalidad: Advertencias y recomendaciones cuando usuarios se unen a grupos
* Versión: 1.0.0
*
* PROPÓSITO: Aconsejar a usuarios y coordinadores sobre mejores prácticas de seguridad
* antes de unirse o aceptar miembros en grupos de tandas.
*/
class GroupSecurityAdvisor {
constructor() {
this.API_BASE = 'https://latanda.online';
this.currentUser = null;
// Tipos de advertencias
this.WARNING_TYPES = {
NEW_MEMBER_JOINING: 'new_member_joining',
COORDINATOR_ACCEPTING: 'coordinator_accepting',
HIGH_AMOUNT_GROUP: 'high_amount_group',
NEW_COORDINATOR: 'new_coordinator',
LARGE_GROUP: 'large_group'
};
// Niveles de riesgo
this.RISK_LEVELS = {
LOW: 'low',
MEDIUM: 'medium',
HIGH: 'high',
CRITICAL: 'critical',
FROZEN: 'frozen',
BLACKLISTED: 'blacklisted'
};
// Estados de cuenta y grupos
this.ACCOUNT_STATUS = {
ACTIVE: 'active',
FROZEN: 'frozen',
SUSPENDED: 'suspended',
BLACKLISTED: 'blacklisted',
UNDER_REVIEW: 'under_review'
};
this.init();
}
async init() {
this.loadUserData();
this.setupEventListeners();
}
loadUserData() {
const authData = localStorage.getItem('laTandaWeb3Auth');
if (authData) {
this.currentUser = JSON.parse(authData).user;
}
}
setupEventListeners() {
// Interceptar acciones de unirse a grupos
document.addEventListener('click', (e) => {
if (e.target.classList.contains('join-group-btn')) {
this.handleJoinGroupAttempt(e.target);
}
if (e.target.classList.contains('accept-member-btn')) {
this.handleAcceptMemberAttempt(e.target);
}
});
}
/**
* CORE: Advertencia cuando usuario quiere unirse a un grupo
*/
async handleJoinGroupAttempt(button) {
const groupId = button.dataset.groupId;
try {
// Obtener información del grupo
const groupInfo = await this.getGroupInfo(groupId);
// Evaluar riesgo
const riskAssessment = this.evaluateJoinRisk(groupInfo);
// VERIFICAR SI ESTÁ BLOQUEADO POR CONGELAMIENTO
const hasBlockingRisk = riskAssessment.risks.some(risk => risk.blockAction);
if (hasBlockingRisk) {
// Mostrar advertencia de cuenta congelada/bloqueada
this.showFrozenAccountWarning(groupInfo);
return;
}
// Mostrar advertencia si es necesario
if (riskAssessment.showWarning) {
const userDecision = await this.showJoinWarning(groupInfo, riskAssessment);
if (userDecision.proceed) {
await this.proceedWithJoin(groupId, userDecision.acknowledgments);
} else {
this.showNotification('👍 Buena decisión. Te recomendamos conocer mejor al grupo antes de unirte.', 'info');
}
} else {
// Proceder normalmente
await this.proceedWithJoin(groupId, []);
}
} catch (error) {
this.showNotification('Error al evaluar el grupo: ' + error.message, 'error');
}
}
/**
* CORE: Advertencia cuando coordinador va a aceptar un miembro
*/
async handleAcceptMemberAttempt(button) {
const memberId = button.dataset.memberId;
const groupId = button.dataset.groupId;
try {
// Obtener información del miembro candidato
const memberInfo = await this.getMemberInfo(memberId);
const groupInfo = await this.getGroupInfo(groupId);
// Evaluar riesgo
const riskAssessment = this.evaluateAcceptanceRisk(memberInfo, groupInfo);
// Mostrar advertencia si es necesario
if (riskAssessment.showWarning) {
const coordinatorDecision = await this.showAcceptanceWarning(memberInfo, groupInfo, riskAssessment);
if (coordinatorDecision.proceed) {
await this.proceedWithAcceptance(memberId, groupId, coordinatorDecision.acknowledgments);
} else {
this.showNotification('👍 Es prudente conocer mejor a los miembros antes de aceptarlos.', 'info');
}
} else {
// Proceder normalmente
await this.proceedWithAcceptance(memberId, groupId, []);
}
} catch (error) {
this.showNotification('Error al evaluar el miembro: ' + error.message, 'error');
}
}
/**
* Evaluar riesgo de unirse a un grupo
*/
evaluateJoinRisk(groupInfo) {
const risks = [];
let riskLevel = this.RISK_LEVELS.LOW;
let showWarning = false;
// VERIFICAR ESTADO DEL COORDINADOR
if (groupInfo.coordinator_status === this.ACCOUNT_STATUS.FROZEN) {
risks.push({
type: 'coordinator_frozen',
message: 'El coordinador de este grupo tiene su cuenta CONGELADA por incumplimientos',
level: this.RISK_LEVELS.FROZEN,
severity: 'critical',
blockAction: true
});
riskLevel = this.RISK_LEVELS.FROZEN;
showWarning = true;
}
if (groupInfo.coordinator_status === this.ACCOUNT_STATUS.BLACKLISTED) {
risks.push({
type: 'coordinator_blacklisted',
message: 'El coordinador está en LISTA NEGRA por fraude confirmado',
level: this.RISK_LEVELS.BLACKLISTED,
severity: 'critical',
blockAction: true
});
riskLevel = this.RISK_LEVELS.BLACKLISTED;
showWarning = true;
}
// VERIFICAR HISTORIAL DE INCUMPLIMIENTOS DEL COORDINADOR
if (groupInfo.coordinator_failed_payments && groupInfo.coordinator_failed_payments > 2) {
risks.push({
type: 'coordinator_payment_issues',
message: `El coordinador ha faltado ${groupInfo.coordinator_failed_payments} pagos en grupos anteriores`,
level: this.RISK_LEVELS.HIGH,
severity: 'high'
});
riskLevel = this.RISK_LEVELS.HIGH;
showWarning = true;
}
// VERIFICAR ADVERTENCIAS PÚBLICAS
if (groupInfo.coordinator_public_warnings && groupInfo.coordinator_public_warnings > 5) {
risks.push({
type: 'coordinator_multiple_warnings',
message: `El coordinador tiene ${groupInfo.coordinator_public_warnings} advertencias públicas de otros usuarios`,
level: this.RISK_LEVELS.HIGH,
severity: 'high'
});
riskLevel = this.RISK_LEVELS.HIGH;
showWarning = true;
}
// Verificar si el coordinador es nuevo
if (groupInfo.coordinator_days_active < 30) {
risks.push({
type: 'new_coordinator',
message: 'El coordinador del grupo es relativamente nuevo en la plataforma',
level: this.RISK_LEVELS.MEDIUM
});
riskLevel = this.RISK_LEVELS.MEDIUM;
showWarning = true;
}
// Verificar cantidad alta de dinero
if (groupInfo.contribution_amount > 1000) {
risks.push({
type: 'high_amount',
message: 'Este grupo maneja cantidades altas de dinero',
level: this.RISK_LEVELS.HIGH
});
riskLevel = this.RISK_LEVELS.HIGH;
showWarning = true;
}
// Verificar grupo muy grande
if (groupInfo.member_count > 20) {
risks.push({
type: 'large_group',
message: 'Este es un grupo muy grande, puede ser más difícil conocer a todos',
level: this.RISK_LEVELS.MEDIUM
});
if (riskLevel === this.RISK_LEVELS.LOW) riskLevel = this.RISK_LEVELS.MEDIUM;
showWarning = true;
}
// Verificar si no hay miembros verificados
if (groupInfo.verified_members_count === 0) {
risks.push({
type: 'no_verified_members',
message: 'Ningún miembro del grupo tiene verificación completa',
level: this.RISK_LEVELS.HIGH
});
riskLevel = this.RISK_LEVELS.HIGH;
showWarning = true;
}
return {
risks,
riskLevel,
showWarning,
recommendedActions: this.getRecommendedActions(risks)
};
}
/**
* Evaluar riesgo de aceptar un miembro
*/
evaluateAcceptanceRisk(memberInfo, groupInfo) {
const risks = [];
let riskLevel = this.RISK_LEVELS.LOW;
let showWarning = false;
// Verificar si el usuario es muy nuevo
if (memberInfo.days_since_registration < 7) {
risks.push({
type: 'new_user',
message: 'Este usuario se registró hace menos de una semana',
level: this.RISK_LEVELS.HIGH
});
riskLevel = this.RISK_LEVELS.HIGH;
showWarning = true;
}
// Verificar perfil incompleto
if (!memberInfo.phone_verified || !memberInfo.email_verified) {
risks.push({
type: 'unverified_profile',
message: 'El usuario no ha verificado completamente su perfil',
level: this.RISK_LEVELS.MEDIUM
});
if (riskLevel === this.RISK_LEVELS.LOW) riskLevel = this.RISK_LEVELS.MEDIUM;
showWarning = true;
}
// Verificar historial de grupos
if (memberInfo.groups_left_count > 3) {
risks.push({
type: 'frequent_leaver',
message: 'Este usuario ha salido de varios grupos anteriormente',
level: this.RISK_LEVELS.MEDIUM
});
if (riskLevel === this.RISK_LEVELS.LOW) riskLevel = this.RISK_LEVELS.MEDIUM;
showWarning = true;
}
// Verificar si no tiene foto de perfil
if (!memberInfo.avatar_url || memberInfo.avatar_url.includes('default')) {
risks.push({
type: 'no_profile_photo',
message: 'El usuario no tiene una foto de perfil personalizada',
level: this.RISK_LEVELS.LOW
});
showWarning = true;
}
return {
risks,
riskLevel,
showWarning,
recommendedActions: this.getRecommendedActionsForMember(risks)
};
}
/**
* Mostrar advertencia para unirse a grupo
*/
async showJoinWarning(groupInfo, riskAssessment) {
return new Promise((resolve) => {
const modal = this.createWarningModal({
title: '⚠️ Consejos de Seguridad - Únete al Grupo',
subtitle: `Antes de unirte a "${groupInfo.name}"`,
risks: riskAssessment.risks,
recommendations: riskAssessment.recommendedActions,
riskLevel: riskAssessment.riskLevel,
proceedText: 'Entiendo y quiero unirme',
cancelText: 'Mejor me espero'
});
const proceedBtn = modal.querySelector('#proceedBtn');
const cancelBtn = modal.querySelector('#cancelBtn');
const acknowledgmentCheckboxes = modal.querySelectorAll('.acknowledgment-checkbox');
proceedBtn.onclick = () => {
const acknowledgments = Array.from(acknowledgmentCheckboxes)
.filter(cb => cb.checked)
.map(cb => cb.dataset.acknowledgment);
if (acknowledgments.length < acknowledgmentCheckboxes.length) {
this.showNotification('Debes confirmar que has leído todas las recomendaciones', 'warning');
return;
}
modal.remove();
resolve({ proceed: true, acknowledgments });
};
cancelBtn.onclick = () => {
modal.remove();
resolve({ proceed: false, acknowledgments: [] });
};
document.body.appendChild(modal);
});
}
/**
* Mostrar advertencia para aceptar miembro
*/
async showAcceptanceWarning(memberInfo, groupInfo, riskAssessment) {
return new Promise((resolve) => {
const modal = this.createWarningModal({
title: '⚠️ Consejos de Seguridad - Aceptar Miembro',
subtitle: `Antes de aceptar a "${memberInfo.name}" en tu grupo`,
risks: riskAssessment.risks,
recommendations: riskAssessment.recommendedActions,
riskLevel: riskAssessment.riskLevel,
proceedText: 'Entiendo y quiero aceptar',
cancelText: 'Mejor reviso más'
});
const proceedBtn = modal.querySelector('#proceedBtn');
const cancelBtn = modal.querySelector('#cancelBtn');
const acknowledgmentCheckboxes = modal.querySelectorAll('.acknowledgment-checkbox');
proceedBtn.onclick = () => {
const acknowledgments = Array.from(acknowledgmentCheckboxes)
.filter(cb => cb.checked)
.map(cb => cb.dataset.acknowledgment);
if (acknowledgments.length < acknowledgmentCheckboxes.length) {
this.showNotification('Debes confirmar que has leído todas las recomendaciones', 'warning');
return;
}
modal.remove();
resolve({ proceed: true, acknowledgments });
};
cancelBtn.onclick = () => {
modal.remove();
resolve({ proceed: false, acknowledgments: [] });
};
document.body.appendChild(modal);
});
}
/**
* Crear modal de advertencia
*/
createWarningModal(config) {
const modal = document.createElement('div');
modal.className = `security-warning-modal risk-${config.riskLevel}`;
const risksHTML = config.risks.map(risk => `
<div class="risk-item risk-${risk.level}">
<div class="risk-icon">
${this.getRiskIcon(risk.level)}
</div>
<div class="risk-content">
<p>${risk.message}</p>
</div>
</div>
`).join('');
const recommendationsHTML = config.recommendations.map((rec, index) => `
<div class="recommendation-item">
<label class="recommendation-checkbox">
<input type="checkbox" class="acknowledgment-checkbox" data-acknowledgment="${rec.id}">
<span class="checkmark"></span>
<span class="recommendation-text">${rec.text}</span>
</label>
</div>
`).join('');
modal.innerHTML = `
<div class="modal-overlay"></div>
<div class="modal-content">
<div class="warning-header">
<h2>${config.title}</h2>
<p class="warning-subtitle">${config.subtitle}</p>
</div>
<div class="warning-body">
<div class="risks-section">
<h3>🚨 Factores a considerar:</h3>
<div class="risks-list">
${risksHTML}
</div>
</div>
<div class="recommendations-section">
<h3>💡 Nuestras recomendaciones:</h3>
<div class="recommendations-list">
${recommendationsHTML}
</div>
</div>
<div class="security-tips">
<div class="tip-box">
<h4>🛡️ Consejos de Seguridad</h4>
<ul>
<li>Conoce personalmente a los miembros del grupo cuando sea posible</li>
<li>Comienza con cantidades pequeñas hasta generar confianza</li>
<li>Verifica que el coordinador tenga experiencia previa</li>
<li>Pregunta por referencias de otros miembros</li>
<li>Mantén registros de todas las transacciones</li>
</ul>
</div>
</div>
</div>
<div class="warning-actions">
<button id="cancelBtn" class="btn-secondary">
${config.cancelText}
</button>
<button id="proceedBtn" class="btn-primary" disabled>
${config.proceedText}
</button>
</div>
</div>
`;
// Habilitar botón solo cuando se marquen todas las confirmaciones
const checkboxes = modal.querySelectorAll('.acknowledgment-checkbox');
const proceedBtn = modal.querySelector('#proceedBtn');
checkboxes.forEach(checkbox => {
checkbox.addEventListener('change', () => {
const allChecked = Array.from(checkboxes).every(cb => cb.checked);
proceedBtn.disabled = !allChecked;
proceedBtn.classList.toggle('enabled', allChecked);
});
});
return modal;
}
/**
* Obtener recomendaciones basadas en riesgos
*/
getRecommendedActions(risks) {
const actions = [];
if (risks.some(r => r.type === 'new_coordinator')) {
actions.push({
id: 'verify_coordinator',
text: 'He verificado el perfil del coordinador y me siento cómodo con su experiencia'
});
}
if (risks.some(r => r.type === 'high_amount')) {
actions.push({
id: 'understand_amount',
text: 'Entiendo que este grupo maneja cantidades altas y estoy preparado financieramente'
});
}
if (risks.some(r => r.type === 'large_group')) {
actions.push({
id: 'accept_large_group',
text: 'Comprendo que en grupos grandes es más difícil conocer a todos los miembros'
});
}
if (risks.some(r => r.type === 'no_verified_members')) {
actions.push({
id: 'accept_unverified',
text: 'Acepto que los miembros del grupo pueden no estar completamente verificados'
});
}
// Acción general siempre presente
actions.push({
id: 'general_understanding',
text: 'He leído y entiendo todos los consejos de seguridad de La Tanda'
});
return actions;
}
/**
* Obtener recomendaciones para aceptar miembros
*/
getRecommendedActionsForMember(risks) {
const actions = [];
if (risks.some(r => r.type === 'new_user')) {
actions.push({
id: 'verify_new_user',
text: 'He considerado que este usuario es nuevo y tomaré precauciones adicionales'
});
}
if (risks.some(r => r.type === 'unverified_profile')) {
actions.push({
id: 'accept_unverified_profile',
text: 'Acepto que el perfil del usuario no está completamente verificado'
});
}
if (risks.some(r => r.type === 'frequent_leaver')) {
actions.push({
id: 'understand_history',
text: 'He revisado el historial del usuario y entiendo los riesgos'
});
}
// Acción general siempre presente
actions.push({
id: 'coordinator_responsibility',
text: 'Entiendo mi responsabilidad como coordinador de velar por la seguridad del grupo'
});
return actions;
}
/**
* SISTEMA DE CONGELAMIENTO Y ADVERTENCIAS
*/
async freezeCoordinatorAccount(coordinatorId, reason, evidence) {
try {
const response = await fetch(`${this.API_BASE}/api/security/freeze-account`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.getAuthToken()}`
},
body: JSON.stringify({
coordinator_id: coordinatorId,
reason: reason,
evidence: evidence,
freeze_type: 'AUTOMATIC_SYSTEM',
initiated_by: 'SECURITY_SYSTEM',
timestamp: new Date().toISOString()
})
});
const result = await response.json();
if (result.success) {
await this.createPublicWarning(coordinatorId, reason, 'ACCOUNT_FROZEN');
return true;
}
throw new Error(result.message);
} catch (error) {
console.error('Error freezing coordinator account:', error);
return false;
}
}
async createPublicWarning(userId, reason, severity) {
try {
const response = await fetch(`${this.API_BASE}/api/security/public-warning`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.getAuthToken()}`
},
body: JSON.stringify({
user_id: userId,
warning_type: severity,
reason: reason,
public_visibility: true,
auto_generated: true,
timestamp: new Date().toISOString()
})
});
return await response.json();
} catch (error) {
console.error('Error creating public warning:', error);
return { success: false };
}
}
async checkAccountFrozenStatus(userId) {
try {
const response = await fetch(`${this.API_BASE}/api/security/account-status/${userId}`, {
headers: {
'Authorization': `Bearer ${this.getAuthToken()}`
}
});
const result = await response.json();
return result.success ? result.data : { status: 'active' };
} catch (error) {
console.error('Error checking account status:', error);
return { status: 'active' };
}
}
showFrozenAccountWarning(groupInfo) {
const modal = document.createElement('div');
modal.className = 'security-warning-modal risk-frozen';
modal.innerHTML = `
<div class="modal-overlay"></div>
<div class="modal-content">
<div class="warning-header frozen-header">
<h2>🧊 CUENTA CONGELADA</h2>
<p class="warning-subtitle">No puedes unirte a este grupo</p>
</div>
<div class="warning-body">
<div class="frozen-alert">
<div class="frozen-icon">🚨</div>
<div class="frozen-message">
<h3>Coordinador Sancionado</h3>
<p>El coordinador de este grupo ha sido automáticamente congelado por el sistema debido a:</p>
<ul>
<li><strong>Faltas a pagos:</strong> ${groupInfo.coordinator_failed_payments || 0} incumplimientos</li>
<li><strong>Advertencias públicas:</strong> ${groupInfo.coordinator_public_warnings || 0} reportes</li>
<li><strong>Estado:</strong> Recursos congelados hasta resolver situación</li>
</ul>
</div>
</div>
<div class="security-advice">
<h4>🛡️ Nuestra Recomendación</h4>
<p>Por tu seguridad, busca grupos con coordinadores confiables y verificados. Los recursos de este coordinador están congelados hasta que resuelva los problemas pendientes.</p>
</div>
</div>
<div class="warning-actions">
<button id="closeBtn" class="btn-primary">
Entendido, buscaré otro grupo
</button>
</div>
</div>
`;
const closeBtn = modal.querySelector('#closeBtn');
closeBtn.onclick = () => modal.remove();
document.body.appendChild(modal);
return modal;
}
/**
* UTILITY METHODS
*/
getRiskIcon(level) {
const icons = {
[this.RISK_LEVELS.LOW]: '⚠️',
[this.RISK_LEVELS.MEDIUM]: '🔶',
[this.RISK_LEVELS.HIGH]: '🔴',
[this.RISK_LEVELS.CRITICAL]: '🚨'
};
return icons[level] || '⚠️';
}
async getGroupInfo(groupId) {
try {
const response = await fetch(`${this.API_BASE}/api/groups/${groupId}/security-info`, {
headers: {
'Authorization': `Bearer ${this.getAuthToken()}`
}
});
const result = await response.json();
return result.success ? result.data : null;
} catch (error) {
console.error('Error getting group info:', error);
throw error;
}
}
async getMemberInfo(memberId) {
try {
const response = await fetch(`${this.API_BASE}/api/users/${memberId}/security-info`, {
headers: {
'Authorization': `Bearer ${this.getAuthToken()}`
}
});
const result = await response.json();
return result.success ? result.data : null;
} catch (error) {
console.error('Error getting member info:', error);
throw error;
}
}
async proceedWithJoin(groupId, acknowledgments) {
try {
// Log de la acción con advertencias mostradas
await this.logSecurityAction('GROUP_JOIN_WITH_WARNINGS', {
group_id: groupId,
user_id: this.currentUser.id,
acknowledged_warnings: acknowledgments,
timestamp: new Date().toISOString()
});
// Proceder con la unión normal
const response = await fetch(`${this.API_BASE}/api/registration/groups/join/${groupId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.getAuthToken()}`
},
body: JSON.stringify({
user_id: this.currentUser.id,
security_acknowledgments: acknowledgments
})
});
const result = await response.json();
if (result.success) {
this.showNotification('¡Te has unido al grupo exitosamente! 🎉', 'success');
} else {
throw new Error(result.message);
}
} catch (error) {
this.showNotification('Error al unirse al grupo: ' + error.message, 'error');
}
}
async proceedWithAcceptance(memberId, groupId, acknowledgments) {
try {
// Log de la acción con advertencias mostradas
await this.logSecurityAction('MEMBER_ACCEPTANCE_WITH_WARNINGS', {
member_id: memberId,
group_id: groupId,
coordinator_id: this.currentUser.id,
acknowledged_warnings: acknowledgments,
timestamp: new Date().toISOString()
});
// Proceder con la aceptación normal
const response = await fetch(`${this.API_BASE}/api/registration/groups/accept-member`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.getAuthToken()}`
},
body: JSON.stringify({
member_id: memberId,
group_id: groupId,
coordinator_id: this.currentUser.id,
security_acknowledgments: acknowledgments
})
});
const result = await response.json();
if (result.success) {
this.showNotification('Miembro aceptado en el grupo exitosamente! 🎉', 'success');
} else {
throw new Error(result.message);
}
} catch (error) {
this.showNotification('Error al aceptar miembro: ' + error.message, 'error');
}
}
async logSecurityAction(action, data) {
try {
await fetch(`${this.API_BASE}/api/audit/security-log`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.getAuthToken()}`
},
body: JSON.stringify({
action: action,
data: data,
user_agent: navigator.userAgent,
ip_address: 'client_side' // Se detectará en el servidor
})
});
} catch (error) {
console.error('Error logging security action:', error);
}
}
getAuthToken() {
const authData = localStorage.getItem('laTandaWeb3Auth');
return authData ? JSON.parse(authData).auth_token : null;
}
showNotification(message, type = 'info') {
// Usar el sistema de notificaciones existente
if (window.auth && window.auth.showNotification) {
window.auth.showNotification(message, type);
} else {
console.log(`[${type.toUpperCase()}] ${message}`);
}
}
}
// Exportar el sistema
if (typeof module !== 'undefined' && module.exports) {
module.exports = GroupSecurityAdvisor;
}
// Auto-inicializar si se carga en el navegador
if (typeof window !== 'undefined') {
window.GroupSecurityAdvisor = GroupSecurityAdvisor;
// Inicializar cuando el DOM esté listo
document.addEventListener('DOMContentLoaded', () => {
window.groupSecurityAdvisor = new GroupSecurityAdvisor();
});
}