|
|
|
@ -33,6 +33,9 @@ type EvolutionGroup = {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export class GroupSyncService {
|
|
|
|
export class GroupSyncService {
|
|
|
|
|
|
|
|
// Static property for DB instance injection (defaults to global db)
|
|
|
|
|
|
|
|
static dbInstance: Database = db;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Gets the sync interval duration in milliseconds.
|
|
|
|
* Gets the sync interval duration in milliseconds.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
@ -92,12 +95,12 @@ export class GroupSyncService {
|
|
|
|
parent: g.linkedParent
|
|
|
|
parent: g.linkedParent
|
|
|
|
})));
|
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
|
|
const dbGroupsBefore = db.prepare('SELECT id, active FROM groups').all();
|
|
|
|
const dbGroupsBefore = this.dbInstance.prepare('SELECT id, active FROM groups').all();
|
|
|
|
console.log('ℹ️ Grupos en DB antes de upsert:', dbGroupsBefore);
|
|
|
|
console.log('ℹ️ Grupos en DB antes de upsert:', dbGroupsBefore);
|
|
|
|
|
|
|
|
|
|
|
|
const result = await this.upsertGroups(communityGroups);
|
|
|
|
const result = await this.upsertGroups(communityGroups);
|
|
|
|
|
|
|
|
|
|
|
|
const dbGroupsAfter = db.prepare('SELECT id, active FROM groups').all();
|
|
|
|
const dbGroupsAfter = this.dbInstance.prepare('SELECT id, active FROM groups').all();
|
|
|
|
console.log('ℹ️ Grupos en DB después de upsert:', dbGroupsAfter);
|
|
|
|
console.log('ℹ️ Grupos en DB después de upsert:', dbGroupsAfter);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
@ -187,7 +190,7 @@ export class GroupSyncService {
|
|
|
|
return groups;
|
|
|
|
return groups;
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
|
console.error('❌ Failed to fetch groups:', {
|
|
|
|
console.error('❌ Failed to fetch groups:', {
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
error: error instanceof Error ? error.message : String(e),
|
|
|
|
stack: error instanceof Error ? error.stack : undefined
|
|
|
|
stack: error instanceof Error ? error.stack : undefined
|
|
|
|
});
|
|
|
|
});
|
|
|
|
throw error;
|
|
|
|
throw error;
|
|
|
|
@ -195,7 +198,7 @@ export class GroupSyncService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static cacheActiveGroups(): void {
|
|
|
|
private static cacheActiveGroups(): void {
|
|
|
|
const groups = db.prepare('SELECT id, name FROM groups WHERE active = TRUE').all();
|
|
|
|
const groups = this.dbInstance.prepare('SELECT id, name FROM groups WHERE active = TRUE').all();
|
|
|
|
activeGroupsCache.clear();
|
|
|
|
activeGroupsCache.clear();
|
|
|
|
for (const group of groups) {
|
|
|
|
for (const group of groups) {
|
|
|
|
activeGroupsCache.set(group.id, group.name);
|
|
|
|
activeGroupsCache.set(group.id, group.name);
|
|
|
|
@ -204,7 +207,7 @@ export class GroupSyncService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static getActiveGroupsCount(): number {
|
|
|
|
private static getActiveGroupsCount(): number {
|
|
|
|
const result = db.prepare('SELECT COUNT(*) as count FROM groups WHERE active = TRUE').get();
|
|
|
|
const result = this.dbInstance.prepare('SELECT COUNT(*) as count FROM groups WHERE active = TRUE').get();
|
|
|
|
return result?.count || 0;
|
|
|
|
return result?.count || 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -275,9 +278,9 @@ export class GroupSyncService {
|
|
|
|
let added = 0;
|
|
|
|
let added = 0;
|
|
|
|
let updated = 0;
|
|
|
|
let updated = 0;
|
|
|
|
|
|
|
|
|
|
|
|
const transactionResult = db.transaction(() => {
|
|
|
|
const transactionResult = this.dbInstance.transaction(() => {
|
|
|
|
// First mark all groups as inactive and update verification timestamp
|
|
|
|
// First mark all groups as inactive and update verification timestamp
|
|
|
|
const inactiveResult = db.prepare(`
|
|
|
|
const inactiveResult = this.dbInstance.prepare(`
|
|
|
|
UPDATE groups
|
|
|
|
UPDATE groups
|
|
|
|
SET active = FALSE,
|
|
|
|
SET active = FALSE,
|
|
|
|
last_verified = CURRENT_TIMESTAMP
|
|
|
|
last_verified = CURRENT_TIMESTAMP
|
|
|
|
@ -289,17 +292,17 @@ export class GroupSyncService {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
for (const group of groups) {
|
|
|
|
for (const group of groups) {
|
|
|
|
const existing = db.prepare('SELECT 1 FROM groups WHERE id = ?').get(group.id);
|
|
|
|
const existing = this.dbInstance.prepare('SELECT 1 FROM groups WHERE id = ?').get(group.id);
|
|
|
|
console.log('Checking group:', group.id, 'exists:', !!existing);
|
|
|
|
console.log('Checking group:', group.id, 'exists:', !!existing);
|
|
|
|
|
|
|
|
|
|
|
|
if (existing) {
|
|
|
|
if (existing) {
|
|
|
|
const updateResult = db.prepare(
|
|
|
|
const updateResult = this.dbInstance.prepare(
|
|
|
|
'UPDATE groups SET name = ?, active = TRUE, last_verified = CURRENT_TIMESTAMP WHERE id = ?'
|
|
|
|
'UPDATE groups SET name = ?, active = TRUE, last_verified = CURRENT_TIMESTAMP WHERE id = ?'
|
|
|
|
).run(group.subject, group.id);
|
|
|
|
).run(group.subject, group.id);
|
|
|
|
console.log('Updated group:', group.id, 'result:', updateResult);
|
|
|
|
console.log('Updated group:', group.id, 'result:', updateResult);
|
|
|
|
updated++;
|
|
|
|
updated++;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
const insertResult = db.prepare(
|
|
|
|
const insertResult = this.dbInstance.prepare(
|
|
|
|
'INSERT INTO groups (id, community_id, name, active) VALUES (?, ?, ?, TRUE)'
|
|
|
|
'INSERT INTO groups (id, community_id, name, active) VALUES (?, ?, ?, TRUE)'
|
|
|
|
).run(group.id, env.WHATSAPP_COMMUNITY_ID, group.subject);
|
|
|
|
).run(group.id, env.WHATSAPP_COMMUNITY_ID, group.subject);
|
|
|
|
console.log('Added group:', group.id, 'result:', insertResult);
|
|
|
|
console.log('Added group:', group.id, 'result:', insertResult);
|
|
|
|
|