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 { model Chat {
id String @id @default(cuid()) id String @id @default(cuid())
remoteJid String @db.VarChar(100) remoteJid String @db.VarChar(100)
name String? @db.VarChar(100)
labels Json? @db.Json labels Json? @db.Json
createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp
updatedAt DateTime? @updatedAt @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 { model Chat {
id String @id @default(cuid()) id String @id @default(cuid())
remoteJid String @db.VarChar(100) remoteJid String @db.VarChar(100)
name String? @db.VarChar(100)
labels Json? @db.JsonB labels Json? @db.JsonB
createdAt DateTime? @default(now()) @db.Timestamp createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime? @updatedAt @db.Timestamp updatedAt DateTime? @updatedAt @db.Timestamp

View File

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

View File

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

View File

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