fix: Improve database saving functionality

This commit fixes an issue with data being inconsistently saved in the database. It introduces a new configuration option to enable/disable saving of chat and contact data, as well as message updates and historic data.

The following files were modified:
- .env.example
- CHANGELOG.md
- src/api/services/channels/whatsapp.baileys.service.ts
- src/config/env.config.ts

The 'DATABASE_SAVE_DATA_NEW_MESSAGE', 'DATABASE_SAVE_DATA_MESSAGE_UPDATE', 'DATABASE_SAVE_DATA_CONTACTS', 'DATABASE_SAVE_DATA_CHATS', and 'DATABASE_SAVE_DATA_HISTORIC' options have been added to the 'SaveData' interface in 'env.config.ts'. These options control whether new messages, message updates, contacts, chats, and historic data are saved in the database, respectively. The 'DATABASE_SAVE_DATA_HISTORIC' option is new and allows for more granular control over what data is saved.

The 'WhatsappBaileysService' in 'whatsapp.baileys.service.ts' has been updated to check these configuration options before saving data in the database. This ensures that data is only saved when explicitly allowed.

The 'CHANGELOG.md' has been updated to reflect these changes.
This commit is contained in:
Davidson Gomes 2024-07-17 18:41:11 -03:00
parent 19e9ff9cc8
commit 1e96878e5f
4 changed files with 102 additions and 84 deletions

View File

@ -27,6 +27,8 @@ DATABASE_SAVE_DATA_NEW_MESSAGE=true
DATABASE_SAVE_MESSAGE_UPDATE=true
DATABASE_SAVE_DATA_CONTACTS=true
DATABASE_SAVE_DATA_CHATS=true
DATABASE_SAVE_DATA_LABELS=true
DATABASE_SAVE_DATA_HISTORIC=true
RABBITMQ_ENABLED=false
RABBITMQ_URI=amqp://localhost

View File

@ -3,6 +3,7 @@
### Fixed
* Resolved issue with Chatwoot not receiving messages sent by Typebot
* Fixed the function of saving or not saving data in the database
# 2.0.0-beta (2024-07-14 17:00)

View File

@ -695,9 +695,10 @@ export class BaileysStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.CHATS_UPSERT, chatsToInsert);
if (chatsToInsert.length > 0) {
await this.prismaRepository.chat.createMany({
data: chatsToInsert,
});
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS)
await this.prismaRepository.chat.createMany({
data: chatsToInsert,
});
}
},
@ -754,10 +755,11 @@ export class BaileysStartupService extends ChannelStartupService {
if (contactsRaw.length > 0) this.sendDataWebhook(Events.CONTACTS_UPSERT, contactsRaw);
if (contactsRaw.length > 0) {
await this.prismaRepository.contact.createMany({
data: contactsRaw,
skipDuplicates: true,
});
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CONTACTS)
await this.prismaRepository.contact.createMany({
data: contactsRaw,
skipDuplicates: true,
});
}
if (
@ -877,12 +879,14 @@ export class BaileysStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.CHATS_SET, chatsRaw);
const chatsSaved = await this.prismaRepository.chat.createMany({
data: chatsRaw,
skipDuplicates: true,
});
if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) {
const chatsSaved = await this.prismaRepository.chat.createMany({
data: chatsRaw,
skipDuplicates: true,
});
console.log('chatsSaved', chatsSaved);
console.log('chatsSaved', chatsSaved);
}
const messagesRaw: any[] = [];
@ -939,12 +943,14 @@ export class BaileysStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.MESSAGES_SET, [...messagesRaw]);
const messagesSaved = await this.prismaRepository.message.createMany({
data: messagesRaw,
skipDuplicates: true,
});
if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) {
const messagesSaved = await this.prismaRepository.message.createMany({
data: messagesRaw,
skipDuplicates: true,
});
console.log('messagesSaved', messagesSaved);
console.log('messagesSaved', messagesSaved);
}
if (
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
@ -1083,64 +1089,66 @@ export class BaileysStartupService extends ChannelStartupService {
}
}
const msg = await this.prismaRepository.message.create({
data: messageRaw,
});
if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
const msg = await this.prismaRepository.message.create({
data: messageRaw,
});
if (isMedia) {
if (this.configService.get<S3>('S3').ENABLE) {
try {
const message: any = received;
const media = await this.getBase64FromMediaMessage(
{
message,
},
true,
);
if (isMedia) {
if (this.configService.get<S3>('S3').ENABLE) {
try {
const message: any = received;
const media = await this.getBase64FromMediaMessage(
{
message,
},
true,
);
const { buffer, mediaType, fileName, size } = media;
const { buffer, mediaType, fileName, size } = media;
const mimetype = mime.lookup(fileName).toString();
const mimetype = mime.lookup(fileName).toString();
const fullName = join(`${this.instance.id}`, received.key.remoteJid, mediaType, fileName);
const fullName = join(`${this.instance.id}`, received.key.remoteJid, mediaType, fileName);
await s3Service.uploadFile(fullName, buffer, size.fileLength, {
'Content-Type': mimetype,
});
await s3Service.uploadFile(fullName, buffer, size.fileLength, {
'Content-Type': mimetype,
});
await this.prismaRepository.media.create({
data: {
messageId: msg.id,
instanceId: this.instanceId,
type: mediaType,
fileName: fullName,
mimetype,
},
});
await this.prismaRepository.media.create({
data: {
messageId: msg.id,
instanceId: this.instanceId,
type: mediaType,
fileName: fullName,
mimetype,
},
});
const mediaUrl = await s3Service.getObjectUrl(fullName);
const mediaUrl = await s3Service.getObjectUrl(fullName);
messageRaw.message.mediaUrl = mediaUrl;
} catch (error) {
this.logger.error(['Error on upload file to minio', error?.message, error?.stack]);
}
} else {
if (this.localWebhook.webhookBase64 === true) {
const buffer = await downloadMediaMessage(
{ key: received.key, message: received?.message },
'buffer',
{},
{
logger: P({ level: 'error' }) as any,
reuploadRequest: this.client.updateMediaMessage,
},
);
messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined;
messageRaw.message.mediaUrl = mediaUrl;
} catch (error) {
this.logger.error(['Error on upload file to minio', error?.message, error?.stack]);
}
}
}
}
if (isMedia && !this.configService.get<S3>('S3').ENABLE && this.localWebhook.webhookBase64 === true) {
const buffer = await downloadMediaMessage(
{ key: received.key, message: received?.message },
'buffer',
{},
{
logger: P({ level: 'error' }) as any,
reuploadRequest: this.client.updateMediaMessage,
},
);
messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined;
}
this.logger.log(messageRaw);
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
@ -1198,9 +1206,10 @@ export class BaileysStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
await this.prismaRepository.contact.create({
data: contactRaw,
});
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CONTACTS)
await this.prismaRepository.contact.create({
data: contactRaw,
});
}
} catch (error) {
this.logger.error(error);
@ -1273,9 +1282,10 @@ export class BaileysStartupService extends ChannelStartupService {
instanceId: this.instanceId,
};
await this.prismaRepository.messageUpdate.create({
data: message,
});
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE)
await this.prismaRepository.messageUpdate.create({
data: message,
});
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp(
@ -1301,9 +1311,10 @@ export class BaileysStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.MESSAGES_UPDATE, message);
await this.prismaRepository.messageUpdate.create({
data: message,
});
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE)
await this.prismaRepository.messageUpdate.create({
data: message,
});
}
}
},
@ -1352,15 +1363,16 @@ export class BaileysStartupService extends ChannelStartupService {
const labelName = label.name.replace(/[^\x20-\x7E]/g, '');
if (!savedLabel || savedLabel.color !== `${label.color}` || savedLabel.name !== labelName) {
await this.prismaRepository.label.create({
data: {
color: `${label.color}`,
name: labelName,
labelId: label.id,
predefinedId: label.predefinedId,
instanceId: this.instanceId,
},
});
if (this.configService.get<Database>('DATABASE').SAVE_DATA.LABELS)
await this.prismaRepository.label.create({
data: {
color: `${label.color}`,
name: labelName,
labelId: label.id,
predefinedId: label.predefinedId,
instanceId: this.instanceId,
},
});
this.sendDataWebhook(Events.LABELS_EDIT, { ...label, instance: this.instance.name });
}
},
@ -1925,9 +1937,10 @@ export class BaileysStartupService extends ChannelStartupService {
);
}
await this.prismaRepository.message.create({
data: messageRaw,
});
if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE)
await this.prismaRepository.message.create({
data: messageRaw,
});
return messageSent;
} catch (error) {

View File

@ -37,6 +37,7 @@ export type ProviderSession = {
export type SaveData = {
INSTANCE: boolean;
HISTORIC: boolean;
NEW_MESSAGE: boolean;
MESSAGE_UPDATE: boolean;
CONTACTS: boolean;
@ -288,6 +289,7 @@ export class ConfigService {
MESSAGE_UPDATE: process.env?.DATABASE_SAVE_MESSAGE_UPDATE === 'true',
CONTACTS: process.env?.DATABASE_SAVE_DATA_CONTACTS === 'true',
CHATS: process.env?.DATABASE_SAVE_DATA_CHATS === 'true',
HISTORIC: process.env?.DATABASE_SAVE_DATA_HISTORIC === 'true',
LABELS: process.env?.DATABASE_SAVE_DATA_LABELS === 'true',
},
},