Skip to content

Commit a90f0f2

Browse files
committed
Fixes in mongodb and chatwoot
1 parent 4c69b05 commit a90f0f2

File tree

7 files changed

+186
-168
lines changed

7 files changed

+186
-168
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Feature
44
* Added AWS SQS Integration
55
* Added compatibility with typebot v2
6+
* Added endpoint sendPresence
67

78
### Fixed
89

@@ -16,6 +17,8 @@
1617
* Removed await from webhook when sending a message
1718
* Update typebot.service.ts - element.underline change ~ for *
1819
* Adjusts in proxy
20+
* Removed api restart on receiving an error
21+
* Fixes in mongodb and chatwoot
1922

2023
# 1.5.4 (2023-10-09 20:43)
2124

src/config/error.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function onUnexpectedError() {
88
stderr: process.stderr.fd,
99
error,
1010
});
11-
process.exit(1);
11+
// process.exit(1);
1212
});
1313

1414
process.on('unhandledRejection', (error, origin) => {
@@ -18,6 +18,6 @@ export function onUnexpectedError() {
1818
stderr: process.stderr.fd,
1919
error,
2020
});
21-
process.exit(1);
21+
// process.exit(1);
2222
});
2323
}

src/utils/use-multi-file-auth-state-db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export async function useMultiFileAuthStateDb(
101101
},
102102
},
103103
saveCreds: async () => {
104-
return writeData(creds, 'creds');
104+
return await writeData(creds, 'creds');
105105
},
106106
};
107107
}

src/whatsapp/services/chatwoot.service.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,10 @@ export class ChatwootService {
987987
const state = waInstance?.connectionStatus?.state;
988988

989989
if (state !== 'open') {
990+
if (state === 'close') {
991+
this.logger.verbose('request cleaning up instance: ' + instance.instanceName);
992+
await this.waMonitor.cleaningUp(instance.instanceName);
993+
}
990994
this.logger.verbose('connect to whatsapp');
991995
const number = command.split(':')[1];
992996
await waInstance.connectToWhatsapp(number);
@@ -1272,17 +1276,17 @@ export class ChatwootService {
12721276
public async eventWhatsapp(event: string, instance: InstanceDto, body: any) {
12731277
this.logger.verbose('event whatsapp to instance: ' + instance.instanceName);
12741278
try {
1275-
const client = await this.clientCw(instance);
1279+
const waInstance = this.waMonitor.waInstances[instance.instanceName];
12761280

1277-
if (!client) {
1278-
this.logger.warn('client not found');
1281+
if (!waInstance) {
1282+
this.logger.warn('wa instance not found');
12791283
return null;
12801284
}
12811285

1282-
const waInstance = this.waMonitor.waInstances[instance.instanceName];
1286+
const client = await this.clientCw(instance);
12831287

1284-
if (!waInstance) {
1285-
this.logger.warn('wa instance not found');
1288+
if (!client) {
1289+
this.logger.warn('client not found');
12861290
return null;
12871291
}
12881292

@@ -1535,16 +1539,18 @@ export class ChatwootService {
15351539
await this.createBotMessage(instance, msgStatus, 'incoming');
15361540
}
15371541

1538-
// if (event === 'connection.update') {
1539-
// this.logger.verbose('event connection.update');
1542+
if (event === 'connection.update') {
1543+
this.logger.verbose('event connection.update');
15401544

1541-
// if (body.status === 'open') {
1542-
// const msgConnection = `🚀 Connection successfully established!`;
1543-
1544-
// this.logger.verbose('send message to chatwoot');
1545-
// await this.createBotMessage(instance, msgConnection, 'incoming');
1546-
// }
1547-
// }
1545+
if (body.status === 'open') {
1546+
// if we have qrcode count then we understand that a new connection was established
1547+
if (this.waMonitor.waInstances[instance.instanceName].qrCode.count > 0) {
1548+
const msgConnection = `🚀 Connection successfully established!`;
1549+
this.logger.verbose('send message to chatwoot');
1550+
await this.createBotMessage(instance, msgConnection, 'incoming');
1551+
}
1552+
}
1553+
}
15481554

15491555
if (event === 'qrcode.updated') {
15501556
this.logger.verbose('event qrcode.updated');

src/whatsapp/services/monitor.service.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import { RedisCache } from '../../libs/redis.client';
1313
import {
1414
AuthModel,
1515
ChamaaiModel,
16-
ChatModel,
16+
// ChatModel,
1717
ChatwootModel,
18-
ContactModel,
19-
MessageModel,
20-
MessageUpModel,
18+
// ContactModel,
19+
// MessageModel,
20+
// MessageUpModel,
2121
ProxyModel,
2222
RabbitmqModel,
2323
SettingsModel,
@@ -39,7 +39,7 @@ export class WAMonitoringService {
3939

4040
this.removeInstance();
4141
this.noConnection();
42-
this.delInstanceFiles();
42+
// this.delInstanceFiles();
4343

4444
Object.assign(this.db, configService.get<Database>('DATABASE'));
4545
Object.assign(this.redis, configService.get<Redis>('REDIS'));
@@ -193,6 +193,13 @@ export class WAMonitoringService {
193193

194194
public async cleaningUp(instanceName: string) {
195195
this.logger.verbose('cleaning up instance: ' + instanceName);
196+
if (this.redis.ENABLED) {
197+
this.logger.verbose('cleaning up instance in redis: ' + instanceName);
198+
this.cache.reference = instanceName;
199+
await this.cache.delAll();
200+
return;
201+
}
202+
196203
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
197204
this.logger.verbose('cleaning up instance in database: ' + instanceName);
198205
await this.repository.dbServer.connect();
@@ -203,13 +210,6 @@ export class WAMonitoringService {
203210
return;
204211
}
205212

206-
if (this.redis.ENABLED) {
207-
this.logger.verbose('cleaning up instance in redis: ' + instanceName);
208-
this.cache.reference = instanceName;
209-
await this.cache.delAll();
210-
return;
211-
}
212-
213213
this.logger.verbose('cleaning up instance in files: ' + instanceName);
214214
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
215215
}
@@ -239,10 +239,10 @@ export class WAMonitoringService {
239239

240240
this.logger.verbose('cleaning store database instance: ' + instanceName);
241241

242-
await ChatModel.deleteMany({ owner: instanceName });
243-
await ContactModel.deleteMany({ owner: instanceName });
244-
await MessageUpModel.deleteMany({ owner: instanceName });
245-
await MessageModel.deleteMany({ owner: instanceName });
242+
// await ChatModel.deleteMany({ owner: instanceName });
243+
// await ContactModel.deleteMany({ owner: instanceName });
244+
// await MessageUpModel.deleteMany({ owner: instanceName });
245+
// await MessageModel.deleteMany({ owner: instanceName });
246246

247247
await AuthModel.deleteMany({ _id: instanceName });
248248
await WebhookModel.deleteMany({ _id: instanceName });
@@ -357,8 +357,8 @@ export class WAMonitoringService {
357357
this.eventEmitter.on('logout.instance', async (instanceName: string) => {
358358
this.logger.verbose('logout instance: ' + instanceName);
359359
try {
360-
this.logger.verbose('request cleaning up instance: ' + instanceName);
361-
this.cleaningUp(instanceName);
360+
// this.logger.verbose('request cleaning up instance: ' + instanceName);
361+
// this.cleaningUp(instanceName);
362362
} finally {
363363
this.logger.warn(`Instance "${instanceName}" - LOGOUT`);
364364
}

src/whatsapp/services/typebot.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export class TypebotService {
246246
prefilledVariables: {
247247
...data.prefilledVariables,
248248
remoteJid: data.remoteJid,
249-
pushName: data.pushName || '',
249+
pushName: data.pushName || data.prefilledVariables?.pushName || '',
250250
instanceName: instance.instanceName,
251251
},
252252
},

0 commit comments

Comments
 (0)