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);
|
return res.status(HttpStatus.CREATED).json(response);
|
||||||
})
|
})
|
||||||
// TODO: realizar filtro pelo postgres corretamente
|
|
||||||
.post(this.routerPath('findContacts'), ...guards, async (req, res) => {
|
.post(this.routerPath('findContacts'), ...guards, async (req, res) => {
|
||||||
const response = await this.dataValidate<Query<Contact>>({
|
const response = await this.dataValidate<Query<Contact>>({
|
||||||
request: req,
|
request: req,
|
||||||
@ -168,7 +167,6 @@ export class ChatRouter extends RouterBroker {
|
|||||||
|
|
||||||
return res.status(HttpStatus.OK).json(response);
|
return res.status(HttpStatus.OK).json(response);
|
||||||
})
|
})
|
||||||
// TODO: realizar filtro pelo postgres corretamente
|
|
||||||
.post(this.routerPath('findStatusMessage'), ...guards, async (req, res) => {
|
.post(this.routerPath('findStatusMessage'), ...guards, async (req, res) => {
|
||||||
const response = await this.dataValidate<Query<MessageUpdate>>({
|
const response = await this.dataValidate<Query<MessageUpdate>>({
|
||||||
request: req,
|
request: req,
|
||||||
@ -179,8 +177,7 @@ export class ChatRouter extends RouterBroker {
|
|||||||
|
|
||||||
return res.status(HttpStatus.OK).json(response);
|
return res.status(HttpStatus.OK).json(response);
|
||||||
})
|
})
|
||||||
// TODO: realizar filtro pelo postgres corretamente
|
.post(this.routerPath('findChats'), ...guards, async (req, res) => {
|
||||||
.get(this.routerPath('findChats'), ...guards, async (req, res) => {
|
|
||||||
const response = await this.dataValidate<InstanceDto>({
|
const response = await this.dataValidate<InstanceDto>({
|
||||||
request: req,
|
request: req,
|
||||||
schema: null,
|
schema: null,
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { Contact, Message } from '@prisma/client';
|
||||||
import { WASocket } from '@whiskeysockets/baileys';
|
import { WASocket } from '@whiskeysockets/baileys';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { execSync } from 'child_process';
|
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 { TypebotService } from '../integrations/typebot/services/typebot.service';
|
||||||
import { WebsocketDto } from '../integrations/websocket/dto/websocket.dto';
|
import { WebsocketDto } from '../integrations/websocket/dto/websocket.dto';
|
||||||
import { getIO } from '../integrations/websocket/libs/socket.server';
|
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 { waMonitor } from '../server.module';
|
||||||
import { Events, wa } from '../types/wa.types';
|
import { Events, wa } from '../types/wa.types';
|
||||||
import { CacheService } from './cache.service';
|
import { CacheService } from './cache.service';
|
||||||
@ -1075,59 +1076,106 @@ export class ChannelStartupService {
|
|||||||
return `${number}@s.whatsapp.net`;
|
return `${number}@s.whatsapp.net`;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchContacts(query: any) {
|
public async fetchContacts(query: Query<Contact>) {
|
||||||
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,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return await this.prismaRepository.contact.findMany({
|
return await this.prismaRepository.contact.findMany({
|
||||||
where: query.where,
|
where: {
|
||||||
|
instanceId: this.instanceId,
|
||||||
|
remoteJid: query.where?.remoteJid,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchMessages(query: any) {
|
public async fetchMessages(query: Query<Message>) {
|
||||||
if (query?.where) {
|
const keyFilters = query?.where?.key as {
|
||||||
if (query.where?.key?.remoteJid) {
|
id?: string;
|
||||||
query.where.key.remoteJid = this.createJid(query.where.key.remoteJid);
|
fromMe?: boolean;
|
||||||
}
|
remoteJid?: string;
|
||||||
query.where.instanceId = this.instanceId;
|
participants?: string;
|
||||||
} else {
|
};
|
||||||
query = {
|
|
||||||
where: {
|
const count = await this.prismaRepository.message.count({
|
||||||
instanceId: this.instanceId,
|
where: {
|
||||||
},
|
instanceId: this.instanceId,
|
||||||
limit: query?.limit,
|
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) {
|
public async fetchStatusMessage(query: any) {
|
||||||
if (query?.where) {
|
return await this.prismaRepository.messageUpdate.findMany({
|
||||||
if (query.where?.remoteJid) {
|
where: {
|
||||||
query.where.remoteJid = this.createJid(query.where.remoteJid);
|
instanceId: this.instanceId,
|
||||||
}
|
remoteJid: query.where?.remoteJid,
|
||||||
query.where.instanceId = this.instanceId;
|
messageId: query.where?.id,
|
||||||
} else {
|
},
|
||||||
query = {
|
skip: query.offset * (query?.page === 1 ? 0 : (query?.page as number) - 1),
|
||||||
where: {
|
take: query.offset,
|
||||||
instanceId: this.instanceId,
|
});
|
||||||
},
|
|
||||||
limit: query?.limit,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return await this.prismaRepository.messageUpdate.findMany(query);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchChats() {
|
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) {
|
public async getLastMessage(number: string) {
|
||||||
const messages = await this.fetchMessages({
|
const where: any = {
|
||||||
where: {
|
key: {
|
||||||
key: {
|
remoteJid: number,
|
||||||
remoteJid: number,
|
|
||||||
},
|
|
||||||
owner: this.instance.name,
|
|
||||||
},
|
},
|
||||||
|
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();
|
let lastMessage = messages.pop();
|
||||||
|
|
||||||
for (const message of messages) {
|
for (const message of messages) {
|
||||||
|
Loading…
Reference in New Issue
Block a user