mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 01:41:24 -06:00
Adjust search for chats, messages and contacts
This commit is contained in:
parent
620cd8b06b
commit
585bf84af4
@ -146,7 +146,6 @@ export class ChatRouter extends RouterBroker {
|
||||
|
||||
return res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
// TODO: realizar filtro pelo postgres corretamente
|
||||
.post(this.routerPath('findContacts'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<Query<Contact>>({
|
||||
request: req,
|
||||
@ -168,7 +167,6 @@ export class ChatRouter extends RouterBroker {
|
||||
|
||||
return res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
// TODO: realizar filtro pelo postgres corretamente
|
||||
.post(this.routerPath('findStatusMessage'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<Query<MessageUpdate>>({
|
||||
request: req,
|
||||
@ -179,8 +177,7 @@ export class ChatRouter extends RouterBroker {
|
||||
|
||||
return res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
// TODO: realizar filtro pelo postgres corretamente
|
||||
.get(this.routerPath('findChats'), ...guards, async (req, res) => {
|
||||
.post(this.routerPath('findChats'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<InstanceDto>({
|
||||
request: req,
|
||||
schema: null,
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { Contact, Message } from '@prisma/client';
|
||||
import { WASocket } from '@whiskeysockets/baileys';
|
||||
import axios from 'axios';
|
||||
import { execSync } from 'child_process';
|
||||
@ -35,7 +36,7 @@ import { getSQS, removeQueues as removeQueuesSQS } from '../integrations/sqs/lib
|
||||
import { TypebotService } from '../integrations/typebot/services/typebot.service';
|
||||
import { WebsocketDto } from '../integrations/websocket/dto/websocket.dto';
|
||||
import { getIO } from '../integrations/websocket/libs/socket.server';
|
||||
import { PrismaRepository } from '../repository/repository.service';
|
||||
import { PrismaRepository, Query } from '../repository/repository.service';
|
||||
import { waMonitor } from '../server.module';
|
||||
import { Events, wa } from '../types/wa.types';
|
||||
import { CacheService } from './cache.service';
|
||||
@ -1075,59 +1076,106 @@ export class ChannelStartupService {
|
||||
return `${number}@s.whatsapp.net`;
|
||||
}
|
||||
|
||||
public async fetchContacts(query: any) {
|
||||
if (query?.where) {
|
||||
query.where.remoteJid = this.instance.name;
|
||||
if (query.where?.remoteJid) {
|
||||
query.where.remoteJid = this.createJid(query.where.remoteJid);
|
||||
}
|
||||
} else {
|
||||
query = {
|
||||
where: {
|
||||
instanceId: this.instanceId,
|
||||
},
|
||||
};
|
||||
}
|
||||
public async fetchContacts(query: Query<Contact>) {
|
||||
return await this.prismaRepository.contact.findMany({
|
||||
where: query.where,
|
||||
where: {
|
||||
instanceId: this.instanceId,
|
||||
remoteJid: query.where?.remoteJid,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
public async fetchMessages(query: any) {
|
||||
if (query?.where) {
|
||||
if (query.where?.key?.remoteJid) {
|
||||
query.where.key.remoteJid = this.createJid(query.where.key.remoteJid);
|
||||
}
|
||||
query.where.instanceId = this.instanceId;
|
||||
} else {
|
||||
query = {
|
||||
where: {
|
||||
instanceId: this.instanceId,
|
||||
},
|
||||
limit: query?.limit,
|
||||
};
|
||||
public async fetchMessages(query: Query<Message>) {
|
||||
const keyFilters = query?.where?.key as {
|
||||
id?: string;
|
||||
fromMe?: boolean;
|
||||
remoteJid?: string;
|
||||
participants?: string;
|
||||
};
|
||||
|
||||
const count = await this.prismaRepository.message.count({
|
||||
where: {
|
||||
instanceId: this.instanceId,
|
||||
id: query?.where?.id,
|
||||
source: query?.where?.source,
|
||||
messageType: query?.where?.messageType,
|
||||
AND: [
|
||||
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
|
||||
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
|
||||
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
|
||||
keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
if (!query?.offset) {
|
||||
query.offset = 50;
|
||||
}
|
||||
return await this.prismaRepository.message.findMany(query);
|
||||
|
||||
if (!query?.page) {
|
||||
query.page = 1;
|
||||
}
|
||||
|
||||
const messages = await this.prismaRepository.message.findMany({
|
||||
where: {
|
||||
instanceId: this.instanceId,
|
||||
id: query?.where?.id,
|
||||
source: query?.where?.source,
|
||||
messageType: query?.where?.messageType,
|
||||
AND: [
|
||||
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
|
||||
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
|
||||
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
|
||||
keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {},
|
||||
],
|
||||
},
|
||||
orderBy: {
|
||||
messageTimestamp: 'desc',
|
||||
},
|
||||
skip: query.offset * (query?.page === 1 ? 0 : (query?.page as number) - 1),
|
||||
take: query.offset,
|
||||
select: {
|
||||
id: true,
|
||||
key: true,
|
||||
pushName: true,
|
||||
messageType: true,
|
||||
message: true,
|
||||
messageTimestamp: true,
|
||||
instanceId: true,
|
||||
source: true,
|
||||
MessageUpdate: {
|
||||
select: {
|
||||
status: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
messages: {
|
||||
total: count,
|
||||
pages: Math.ceil(count / query.offset),
|
||||
currentPage: query.page,
|
||||
records: messages,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public async fetchStatusMessage(query: any) {
|
||||
if (query?.where) {
|
||||
if (query.where?.remoteJid) {
|
||||
query.where.remoteJid = this.createJid(query.where.remoteJid);
|
||||
}
|
||||
query.where.instanceId = this.instanceId;
|
||||
} else {
|
||||
query = {
|
||||
where: {
|
||||
instanceId: this.instanceId,
|
||||
},
|
||||
limit: query?.limit,
|
||||
};
|
||||
}
|
||||
return await this.prismaRepository.messageUpdate.findMany(query);
|
||||
return await this.prismaRepository.messageUpdate.findMany({
|
||||
where: {
|
||||
instanceId: this.instanceId,
|
||||
remoteJid: query.where?.remoteJid,
|
||||
messageId: query.where?.id,
|
||||
},
|
||||
skip: query.offset * (query?.page === 1 ? 0 : (query?.page as number) - 1),
|
||||
take: query.offset,
|
||||
});
|
||||
}
|
||||
|
||||
public async fetchChats() {
|
||||
return await this.prismaRepository.chat.findMany({ where: { instanceId: this.instanceId } });
|
||||
return await this.prismaRepository.chat.findMany({
|
||||
where: { instanceId: this.instanceId },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -2621,15 +2621,25 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
}
|
||||
|
||||
public async getLastMessage(number: string) {
|
||||
const messages = await this.fetchMessages({
|
||||
where: {
|
||||
key: {
|
||||
remoteJid: number,
|
||||
},
|
||||
owner: this.instance.name,
|
||||
const where: any = {
|
||||
key: {
|
||||
remoteJid: number,
|
||||
},
|
||||
instanceId: this.instance.id,
|
||||
};
|
||||
|
||||
const messages = await this.prismaRepository.message.findMany({
|
||||
where,
|
||||
orderBy: {
|
||||
messageTimestamp: 'desc',
|
||||
},
|
||||
take: 1,
|
||||
});
|
||||
|
||||
if (messages.length === 0) {
|
||||
throw new NotFoundException('Messages not found');
|
||||
}
|
||||
|
||||
let lastMessage = messages.pop();
|
||||
|
||||
for (const message of messages) {
|
||||
|
Loading…
Reference in New Issue
Block a user