|
|
|
@ -6,6 +6,7 @@ import { normalizeWhatsAppId, isGroupId } from '../utils/whatsapp';
|
|
|
|
import { Metrics } from './metrics';
|
|
|
|
import { Metrics } from './metrics';
|
|
|
|
import { TaskService } from '../tasks/service';
|
|
|
|
import { TaskService } from '../tasks/service';
|
|
|
|
import { codeId, formatDDMM } from '../utils/formatting';
|
|
|
|
import { codeId, formatDDMM } from '../utils/formatting';
|
|
|
|
|
|
|
|
import { getDb } from '../db/locator';
|
|
|
|
|
|
|
|
|
|
|
|
type AdminContext = {
|
|
|
|
type AdminContext = {
|
|
|
|
sender: string; // normalized user id (digits only)
|
|
|
|
sender: string; // normalized user id (digits only)
|
|
|
|
@ -60,8 +61,9 @@ export class AdminService {
|
|
|
|
return [{ recipient: sender, message: '🚫 No estás autorizado para usar /admin.' }];
|
|
|
|
return [{ recipient: sender, message: '🚫 No estás autorizado para usar /admin.' }];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const instanceDb = ((this as any).dbInstance ?? getDb()) as Database;
|
|
|
|
// Asegurar acceso a la misma DB para AllowedGroups
|
|
|
|
// Asegurar acceso a la misma DB para AllowedGroups
|
|
|
|
try { AllowedGroups.dbInstance = this.dbInstance; } catch {}
|
|
|
|
try { AllowedGroups.dbInstance = instanceDb; } catch {}
|
|
|
|
|
|
|
|
|
|
|
|
const raw = String(ctx.message || '').trim();
|
|
|
|
const raw = String(ctx.message || '').trim();
|
|
|
|
const lower = raw.toLowerCase();
|
|
|
|
const lower = raw.toLowerCase();
|
|
|
|
@ -109,18 +111,18 @@ export class AdminService {
|
|
|
|
if (!isGroupId(ctx.groupId)) {
|
|
|
|
if (!isGroupId(ctx.groupId)) {
|
|
|
|
return [{ recipient: sender, message: 'ℹ️ Este comando se debe usar dentro de un grupo.' }];
|
|
|
|
return [{ recipient: sender, message: 'ℹ️ Este comando se debe usar dentro de un grupo.' }];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.dbInstance.transaction(() => {
|
|
|
|
instanceDb.transaction(() => {
|
|
|
|
this.dbInstance.prepare(`
|
|
|
|
instanceDb.prepare(`
|
|
|
|
UPDATE groups
|
|
|
|
UPDATE groups
|
|
|
|
SET active = 0, archived = 1, last_verified = strftime('%Y-%m-%d %H:%M:%f','now')
|
|
|
|
SET active = 0, archived = 1, last_verified = strftime('%Y-%m-%d %H:%M:%f','now')
|
|
|
|
WHERE id = ?
|
|
|
|
WHERE id = ?
|
|
|
|
`).run(ctx.groupId);
|
|
|
|
`).run(ctx.groupId);
|
|
|
|
this.dbInstance.prepare(`
|
|
|
|
instanceDb.prepare(`
|
|
|
|
UPDATE calendar_tokens
|
|
|
|
UPDATE calendar_tokens
|
|
|
|
SET revoked_at = strftime('%Y-%m-%d %H:%M:%f','now')
|
|
|
|
SET revoked_at = strftime('%Y-%m-%d %H:%M:%f','now')
|
|
|
|
WHERE group_id = ? AND revoked_at IS NULL
|
|
|
|
WHERE group_id = ? AND revoked_at IS NULL
|
|
|
|
`).run(ctx.groupId);
|
|
|
|
`).run(ctx.groupId);
|
|
|
|
this.dbInstance.prepare(`
|
|
|
|
instanceDb.prepare(`
|
|
|
|
UPDATE group_members
|
|
|
|
UPDATE group_members
|
|
|
|
SET is_active = 0
|
|
|
|
SET is_active = 0
|
|
|
|
WHERE group_id = ? AND is_active = 1
|
|
|
|
WHERE group_id = ? AND is_active = 1
|
|
|
|
@ -136,18 +138,18 @@ export class AdminService {
|
|
|
|
if (!isGroupId(arg)) {
|
|
|
|
if (!isGroupId(arg)) {
|
|
|
|
return [{ recipient: sender, message: '⚠️ Debes indicar un group_id válido terminado en @g.us' }];
|
|
|
|
return [{ recipient: sender, message: '⚠️ Debes indicar un group_id válido terminado en @g.us' }];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.dbInstance.transaction(() => {
|
|
|
|
instanceDb.transaction(() => {
|
|
|
|
this.dbInstance.prepare(`
|
|
|
|
instanceDb.prepare(`
|
|
|
|
UPDATE groups
|
|
|
|
UPDATE groups
|
|
|
|
SET active = 0, archived = 1, last_verified = strftime('%Y-%m-%d %H:%M:%f','now')
|
|
|
|
SET active = 0, archived = 1, last_verified = strftime('%Y-%m-%d %H:%M:%f','now')
|
|
|
|
WHERE id = ?
|
|
|
|
WHERE id = ?
|
|
|
|
`).run(arg);
|
|
|
|
`).run(arg);
|
|
|
|
this.dbInstance.prepare(`
|
|
|
|
instanceDb.prepare(`
|
|
|
|
UPDATE calendar_tokens
|
|
|
|
UPDATE calendar_tokens
|
|
|
|
SET revoked_at = strftime('%Y-%m-%d %H:%M:%f','now')
|
|
|
|
SET revoked_at = strftime('%Y-%m-%d %H:%M:%f','now')
|
|
|
|
WHERE group_id = ? AND revoked_at IS NULL
|
|
|
|
WHERE group_id = ? AND revoked_at IS NULL
|
|
|
|
`).run(arg);
|
|
|
|
`).run(arg);
|
|
|
|
this.dbInstance.prepare(`
|
|
|
|
instanceDb.prepare(`
|
|
|
|
UPDATE group_members
|
|
|
|
UPDATE group_members
|
|
|
|
SET is_active = 0
|
|
|
|
SET is_active = 0
|
|
|
|
WHERE group_id = ? AND is_active = 1
|
|
|
|
WHERE group_id = ? AND is_active = 1
|
|
|
|
@ -162,10 +164,10 @@ export class AdminService {
|
|
|
|
if (!isGroupId(ctx.groupId)) {
|
|
|
|
if (!isGroupId(ctx.groupId)) {
|
|
|
|
return [{ recipient: sender, message: 'ℹ️ Este comando se debe usar dentro de un grupo.' }];
|
|
|
|
return [{ recipient: sender, message: 'ℹ️ Este comando se debe usar dentro de un grupo.' }];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.dbInstance.transaction(() => {
|
|
|
|
instanceDb.transaction(() => {
|
|
|
|
this.dbInstance.prepare(`DELETE FROM tasks WHERE group_id = ?`).run(ctx.groupId);
|
|
|
|
instanceDb.prepare(`DELETE FROM tasks WHERE group_id = ?`).run(ctx.groupId);
|
|
|
|
this.dbInstance.prepare(`DELETE FROM groups WHERE id = ?`).run(ctx.groupId);
|
|
|
|
instanceDb.prepare(`DELETE FROM groups WHERE id = ?`).run(ctx.groupId);
|
|
|
|
try { this.dbInstance.prepare(`DELETE FROM allowed_groups WHERE group_id = ?`).run(ctx.groupId); } catch {}
|
|
|
|
try { instanceDb.prepare(`DELETE FROM allowed_groups WHERE group_id = ?`).run(ctx.groupId); } catch {}
|
|
|
|
})();
|
|
|
|
})();
|
|
|
|
return [{ recipient: sender, message: `🗑️ Grupo borrado y datos asociados eliminados: ${ctx.groupId}` }];
|
|
|
|
return [{ recipient: sender, message: `🗑️ Grupo borrado y datos asociados eliminados: ${ctx.groupId}` }];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -176,10 +178,10 @@ export class AdminService {
|
|
|
|
if (!isGroupId(arg)) {
|
|
|
|
if (!isGroupId(arg)) {
|
|
|
|
return [{ recipient: sender, message: '⚠️ Debes indicar un group_id válido terminado en @g.us' }];
|
|
|
|
return [{ recipient: sender, message: '⚠️ Debes indicar un group_id válido terminado en @g.us' }];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.dbInstance.transaction(() => {
|
|
|
|
instanceDb.transaction(() => {
|
|
|
|
this.dbInstance.prepare(`DELETE FROM tasks WHERE group_id = ?`).run(arg);
|
|
|
|
instanceDb.prepare(`DELETE FROM tasks WHERE group_id = ?`).run(arg);
|
|
|
|
this.dbInstance.prepare(`DELETE FROM groups WHERE id = ?`).run(arg);
|
|
|
|
instanceDb.prepare(`DELETE FROM groups WHERE id = ?`).run(arg);
|
|
|
|
try { this.dbInstance.prepare(`DELETE FROM allowed_groups WHERE group_id = ?`).run(arg); } catch {}
|
|
|
|
try { instanceDb.prepare(`DELETE FROM allowed_groups WHERE group_id = ?`).run(arg); } catch {}
|
|
|
|
})();
|
|
|
|
})();
|
|
|
|
return [{ recipient: sender, message: `🗑️ Grupo borrado y datos asociados eliminados: ${arg}` }];
|
|
|
|
return [{ recipient: sender, message: `🗑️ Grupo borrado y datos asociados eliminados: ${arg}` }];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -229,7 +231,7 @@ export class AdminService {
|
|
|
|
|
|
|
|
|
|
|
|
// /admin sync-grupos
|
|
|
|
// /admin sync-grupos
|
|
|
|
if (rest === 'sync-grupos' || rest === 'group-sync' || rest === 'syncgroups') {
|
|
|
|
if (rest === 'sync-grupos' || rest === 'group-sync' || rest === 'syncgroups') {
|
|
|
|
try { (GroupSyncService as any).dbInstance = this.dbInstance; } catch {}
|
|
|
|
try { (GroupSyncService as any).dbInstance = instanceDb; } catch {}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const r = await GroupSyncService.syncGroups(true);
|
|
|
|
const r = await GroupSyncService.syncGroups(true);
|
|
|
|
return [{ recipient: sender, message: `✅ Sync de grupos ejecutado: ${r.added} añadidos, ${r.updated} actualizados.` }];
|
|
|
|
return [{ recipient: sender, message: `✅ Sync de grupos ejecutado: ${r.added} añadidos, ${r.updated} actualizados.` }];
|
|
|
|
@ -251,7 +253,7 @@ export class AdminService {
|
|
|
|
rest.startsWith('list-all ')
|
|
|
|
rest.startsWith('list-all ')
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
// Asegurar acceso a la misma DB para TaskService
|
|
|
|
// Asegurar acceso a la misma DB para TaskService
|
|
|
|
try { TaskService.dbInstance = this.dbInstance; } catch {}
|
|
|
|
try { TaskService.dbInstance = instanceDb; } catch {}
|
|
|
|
|
|
|
|
|
|
|
|
const DEFAULT_LIMIT = 50;
|
|
|
|
const DEFAULT_LIMIT = 50;
|
|
|
|
let limit = DEFAULT_LIMIT;
|
|
|
|
let limit = DEFAULT_LIMIT;
|
|
|
|
|