Merge pull request #746 from judsonjuniorr/v2-save-chat-name

V2 save chat name
This commit is contained in:
Davidson Gomes 2024-08-12 09:40:51 -03:00 committed by GitHub
commit 367d44a4d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 22 additions and 17 deletions

View File

@ -114,6 +114,7 @@ model Session {
model Chat {
id String @id @default(cuid())
remoteJid String @db.VarChar(100)
name String? @db.VarChar(100)
labels Json? @db.Json
createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp
updatedAt DateTime? @updatedAt @db.Timestamp

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Chat" ADD COLUMN "name" VARCHAR(100);

View File

@ -114,6 +114,7 @@ model Session {
model Chat {
id String @id @default(cuid())
remoteJid String @db.VarChar(100)
name String? @db.VarChar(100)
labels Json? @db.JsonB
createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime? @updatedAt @db.Timestamp

View File

@ -1267,6 +1267,7 @@ export class ChannelStartupService {
SELECT
"Chat"."id",
"Chat"."remoteJid",
"Chat"."name",
"Chat"."labels",
"Chat"."createdAt",
"Chat"."updatedAt",
@ -1283,6 +1284,7 @@ export class ChannelStartupService {
SELECT
"Chat"."id",
"Chat"."remoteJid",
"Chat"."name",
"Chat"."labels",
"Chat"."createdAt",
"Chat"."updatedAt",

View File

@ -227,11 +227,16 @@ export class BaileysStartupService extends ChannelStartupService {
this.client?.ws?.close();
await this.prismaRepository.session.delete({
where: {
sessionId: this.instanceId,
},
const sessionExists = await this.prismaRepository.session.findFirst({
where: { sessionId: this.instanceId },
});
if (sessionExists) {
await this.prismaRepository.session.delete({
where: {
sessionId: this.instanceId,
},
});
}
}
public async getProfileName() {
@ -717,7 +722,7 @@ export class BaileysStartupService extends ChannelStartupService {
const chatsToInsert = chats
.filter((chat) => !existingChatIdSet.has(chat.id))
.map((chat) => ({ remoteJid: chat.id, instanceId: this.instanceId }));
.map((chat) => ({ remoteJid: chat.id, instanceId: this.instanceId, name: chat.name }));
this.sendDataWebhook(Events.CHATS_UPSERT, chatsToInsert);
@ -749,10 +754,9 @@ export class BaileysStartupService extends ChannelStartupService {
where: {
instanceId: this.instanceId,
remoteJid: chat.id,
name: chat.name,
},
data: {
remoteJid: chat.id,
},
data: { remoteJid: chat.id },
});
}
},
@ -895,7 +899,7 @@ export class BaileysStartupService extends ChannelStartupService {
}
}
const chatsRaw: any[] = [];
const chatsRaw: { remoteJid: string; instanceId: string; name?: string }[] = [];
const chatsRepository = new Set(
(
await this.prismaRepository.chat.findMany({
@ -912,18 +916,17 @@ export class BaileysStartupService extends ChannelStartupService {
chatsRaw.push({
remoteJid: chat.id,
instanceId: this.instanceId,
name: chat.name,
});
}
this.sendDataWebhook(Events.CHATS_SET, chatsRaw);
if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) {
const chatsSaved = await this.prismaRepository.chat.createMany({
await this.prismaRepository.chat.createMany({
data: chatsRaw,
skipDuplicates: true,
});
console.log('chatsSaved', chatsSaved);
}
const messagesRaw: any[] = [];
@ -982,12 +985,10 @@ export class BaileysStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.MESSAGES_SET, [...messagesRaw]);
if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) {
const messagesSaved = await this.prismaRepository.message.createMany({
await this.prismaRepository.message.createMany({
data: messagesRaw,
skipDuplicates: true,
});
console.log('messagesSaved', messagesSaved);
}
if (

View File

@ -300,7 +300,6 @@ export class BusinessStartupService extends ChannelStartupService {
protected async messageHandle(received: any, database: Database, settings: any) {
try {
console.log(received);
let messageRaw: any;
let pushName: any;
@ -983,7 +982,6 @@ export class BusinessStartupService extends ChannelStartupService {
return messageRaw;
} catch (error) {
console.log(error.response.data);
this.logger.error(error);
throw new BadRequestException(error.toString());
}