mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 09:51:24 -06:00
sync history set
This commit is contained in:
parent
81a62373f8
commit
3342a22cc7
@ -52,7 +52,9 @@ export class ChatwootService {
|
|||||||
private async getProvider(instance: InstanceDto) {
|
private async getProvider(instance: InstanceDto) {
|
||||||
const cacheKey = `${instance.instanceName}:getProvider`;
|
const cacheKey = `${instance.instanceName}:getProvider`;
|
||||||
if (await this.cache.has(cacheKey)) {
|
if (await this.cache.has(cacheKey)) {
|
||||||
return (await this.cache.get(cacheKey)) as ChatwootModel;
|
const provider = (await this.cache.get(cacheKey)) as ChatwootModel;
|
||||||
|
|
||||||
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
const provider = await this.waMonitor.waInstances[instance.instanceName]?.findChatwoot();
|
const provider = await this.waMonitor.waInstances[instance.instanceName]?.findChatwoot();
|
||||||
@ -84,14 +86,14 @@ export class ChatwootService {
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getClientCwConfig(): ChatwootAPIConfig & { name_inbox: string; merge_brazil_contacts: boolean } {
|
public getClientCwConfig(): ChatwootAPIConfig & { nameInbox: string; mergeBrazilContacts: boolean } {
|
||||||
return {
|
return {
|
||||||
basePath: this.provider.url,
|
basePath: this.provider.url,
|
||||||
with_credentials: true,
|
with_credentials: true,
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
token: this.provider.token,
|
token: this.provider.token,
|
||||||
name_inbox: this.provider.name_inbox,
|
nameInbox: this.provider.nameInbox,
|
||||||
merge_brazil_contacts: this.provider.merge_brazil_contacts,
|
mergeBrazilContacts: this.provider.mergeBrazilContacts,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,7 +141,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const contact = await client.contact.getContactable({
|
const contact = await client.contact.getContactable({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
id,
|
id,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -166,7 +168,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const findInbox: any = await client.inboxes.list({
|
const findInbox: any = await client.inboxes.list({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
});
|
});
|
||||||
|
|
||||||
const checkDuplicate = findInbox.payload.map((inbox) => inbox.name).includes(inboxName);
|
const checkDuplicate = findInbox.payload.map((inbox) => inbox.name).includes(inboxName);
|
||||||
@ -180,7 +182,7 @@ export class ChatwootService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const inbox = await client.inboxes.create({
|
const inbox = await client.inboxes.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
data: {
|
data: {
|
||||||
name: inboxName,
|
name: inboxName,
|
||||||
channel: data as any,
|
channel: data as any,
|
||||||
@ -229,7 +231,7 @@ export class ChatwootService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const conversation = await client.conversations.create({
|
const conversation = await client.conversations.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -245,7 +247,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const message = await client.messages.create({
|
const message = await client.messages.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
conversationId: conversation.id,
|
conversationId: conversation.id,
|
||||||
data: {
|
data: {
|
||||||
content: contentMsg,
|
content: contentMsg,
|
||||||
@ -297,7 +299,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const contact = await client.contacts.create({
|
const contact = await client.contacts.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -324,7 +326,7 @@ export class ChatwootService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const contact = await client.contacts.update({
|
const contact = await client.contacts.update({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
id,
|
id,
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
@ -356,14 +358,13 @@ export class ChatwootService {
|
|||||||
|
|
||||||
if (isGroup) {
|
if (isGroup) {
|
||||||
contact = await client.contacts.search({
|
contact = await client.contacts.search({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
q: query,
|
q: query,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// hotfix for: https://github.com/EvolutionAPI/evolution-api/pull/382. waiting fix: https://github.com/figurolatam/chatwoot-sdk/pull/7
|
|
||||||
contact = await chatwootRequest(this.getClientCwConfig(), {
|
contact = await chatwootRequest(this.getClientCwConfig(), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: `/api/v1/accounts/${this.provider.account_id}/contacts/filter`,
|
url: `/api/v1/accounts/${this.provider.accountId}/contacts/filter`,
|
||||||
body: {
|
body: {
|
||||||
payload: this.getFilterPayload(query),
|
payload: this.getFilterPayload(query),
|
||||||
},
|
},
|
||||||
@ -386,7 +387,7 @@ export class ChatwootService {
|
|||||||
try {
|
try {
|
||||||
const contact = await chatwootRequest(this.getClientCwConfig(), {
|
const contact = await chatwootRequest(this.getClientCwConfig(), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: `/api/v1/accounts/${this.provider.account_id}/actions/contact_merge`,
|
url: `/api/v1/accounts/${this.provider.accountId}/actions/contact_merge`,
|
||||||
body: {
|
body: {
|
||||||
base_contact_id: contacts.find((contact) => contact.phone_number.length === 14)?.id,
|
base_contact_id: contacts.find((contact) => contact.phone_number.length === 14)?.id,
|
||||||
mergee_contact_id: contacts.find((contact) => contact.phone_number.length === 13)?.id,
|
mergee_contact_id: contacts.find((contact) => contact.phone_number.length === 13)?.id,
|
||||||
@ -405,7 +406,7 @@ export class ChatwootService {
|
|||||||
const searchableFields = this.getSearchableFields();
|
const searchableFields = this.getSearchableFields();
|
||||||
|
|
||||||
// eslint-disable-next-line prettier/prettier
|
// eslint-disable-next-line prettier/prettier
|
||||||
if (contacts.length === 2 && this.getClientCwConfig().merge_brazil_contacts && query.startsWith('+55')) {
|
if (contacts.length === 2 && this.getClientCwConfig().mergeBrazilContacts && query.startsWith('+55')) {
|
||||||
const contact = this.mergeBrazilianContacts(contacts);
|
const contact = this.mergeBrazilianContacts(contacts);
|
||||||
if (contact) {
|
if (contact) {
|
||||||
return contact;
|
return contact;
|
||||||
@ -488,7 +489,7 @@ export class ChatwootService {
|
|||||||
let conversationExists: conversation | boolean;
|
let conversationExists: conversation | boolean;
|
||||||
try {
|
try {
|
||||||
conversationExists = await client.conversations.get({
|
conversationExists = await client.conversations.get({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
conversationId: conversationId,
|
conversationId: conversationId,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -597,18 +598,18 @@ export class ChatwootService {
|
|||||||
const contactId = contact?.payload?.id || contact?.payload?.contact?.id || contact?.id;
|
const contactId = contact?.payload?.id || contact?.payload?.contact?.id || contact?.id;
|
||||||
|
|
||||||
const contactConversations = (await client.contacts.listConversations({
|
const contactConversations = (await client.contacts.listConversations({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
id: contactId,
|
id: contactId,
|
||||||
})) as any;
|
})) as any;
|
||||||
|
|
||||||
if (contactConversations?.payload?.length) {
|
if (contactConversations?.payload?.length) {
|
||||||
let conversation: any;
|
let conversation: any;
|
||||||
if (this.provider.reopen_conversation) {
|
if (this.provider.reopenConversation) {
|
||||||
conversation = contactConversations.payload.find((conversation) => conversation.inbox_id == filterInbox.id);
|
conversation = contactConversations.payload.find((conversation) => conversation.inbox_id == filterInbox.id);
|
||||||
|
|
||||||
if (this.provider.conversation_pending) {
|
if (this.provider.conversationPending) {
|
||||||
await client.conversations.toggleStatus({
|
await client.conversations.toggleStatus({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
conversationId: conversation.id,
|
conversationId: conversation.id,
|
||||||
data: {
|
data: {
|
||||||
status: 'pending',
|
status: 'pending',
|
||||||
@ -632,12 +633,12 @@ export class ChatwootService {
|
|||||||
inbox_id: filterInbox.id.toString(),
|
inbox_id: filterInbox.id.toString(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.provider.conversation_pending) {
|
if (this.provider.conversationPending) {
|
||||||
data['status'] = 'pending';
|
data['status'] = 'pending';
|
||||||
}
|
}
|
||||||
|
|
||||||
const conversation = await client.conversations.create({
|
const conversation = await client.conversations.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -667,7 +668,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const inbox = (await client.inboxes.list({
|
const inbox = (await client.inboxes.list({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
})) as any;
|
})) as any;
|
||||||
|
|
||||||
if (!inbox) {
|
if (!inbox) {
|
||||||
@ -675,7 +676,7 @@ export class ChatwootService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const findByName = inbox.payload.find((inbox) => inbox.name === this.getClientCwConfig().name_inbox);
|
const findByName = inbox.payload.find((inbox) => inbox.name === this.getClientCwConfig().nameInbox);
|
||||||
|
|
||||||
if (!findByName) {
|
if (!findByName) {
|
||||||
this.logger.warn('inbox not found');
|
this.logger.warn('inbox not found');
|
||||||
@ -710,7 +711,7 @@ export class ChatwootService {
|
|||||||
const replyToIds = await this.getReplyToIds(messageBody, instance);
|
const replyToIds = await this.getReplyToIds(messageBody, instance);
|
||||||
|
|
||||||
const message = await client.messages.create({
|
const message = await client.messages.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
conversationId: conversationId,
|
conversationId: conversationId,
|
||||||
data: {
|
data: {
|
||||||
content: content,
|
content: content,
|
||||||
@ -745,7 +746,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const conversations = (await client.contacts.listConversations({
|
const conversations = (await client.contacts.listConversations({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
id: contact.id,
|
id: contact.id,
|
||||||
})) as any;
|
})) as any;
|
||||||
|
|
||||||
@ -795,7 +796,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const message = await client.messages.create({
|
const message = await client.messages.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
conversationId: conversation.id,
|
conversationId: conversation.id,
|
||||||
data: {
|
data: {
|
||||||
content: content,
|
content: content,
|
||||||
@ -848,7 +849,7 @@ export class ChatwootService {
|
|||||||
const config = {
|
const config = {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
maxBodyLength: Infinity,
|
maxBodyLength: Infinity,
|
||||||
url: `${this.provider.url}/api/v1/accounts/${this.provider.account_id}/conversations/${conversationId}/messages`,
|
url: `${this.provider.url}/api/v1/accounts/${this.provider.accountId}/conversations/${conversationId}/messages`,
|
||||||
headers: {
|
headers: {
|
||||||
api_access_token: this.provider.token,
|
api_access_token: this.provider.token,
|
||||||
...data.getHeaders(),
|
...data.getHeaders(),
|
||||||
@ -917,7 +918,7 @@ export class ChatwootService {
|
|||||||
const config = {
|
const config = {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
maxBodyLength: Infinity,
|
maxBodyLength: Infinity,
|
||||||
url: `${this.provider.url}/api/v1/accounts/${this.provider.account_id}/conversations/${conversation.id}/messages`,
|
url: `${this.provider.url}/api/v1/accounts/${this.provider.accountId}/conversations/${conversation.id}/messages`,
|
||||||
headers: {
|
headers: {
|
||||||
api_access_token: this.provider.token,
|
api_access_token: this.provider.token,
|
||||||
...data.getHeaders(),
|
...data.getHeaders(),
|
||||||
@ -1015,7 +1016,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client.messages.create({
|
client.messages.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
conversationId: conversation,
|
conversationId: conversation,
|
||||||
data: {
|
data: {
|
||||||
content: i18next.t('cw.message.notsent', {
|
content: i18next.t('cw.message.notsent', {
|
||||||
@ -1038,9 +1039,9 @@ export class ChatwootService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// invalidate the conversation cache if reopen_conversation is false and the conversation was resolved
|
// invalidate the conversation cache if reopenConversation is false and the conversation was resolved
|
||||||
if (
|
if (
|
||||||
this.provider.reopen_conversation === false &&
|
this.provider.reopenConversation === false &&
|
||||||
body.event === 'conversation_status_changed' &&
|
body.event === 'conversation_status_changed' &&
|
||||||
body.status === 'resolved' &&
|
body.status === 'resolved' &&
|
||||||
body.meta?.sender?.identifier
|
body.meta?.sender?.identifier
|
||||||
@ -1174,10 +1175,10 @@ export class ChatwootService {
|
|||||||
if (senderName === null || senderName === undefined) {
|
if (senderName === null || senderName === undefined) {
|
||||||
formatText = messageReceived;
|
formatText = messageReceived;
|
||||||
} else {
|
} else {
|
||||||
const formattedDelimiter = this.provider.sign_delimiter
|
const formattedDelimiter = this.provider.signDelimiter
|
||||||
? this.provider.sign_delimiter.replaceAll('\\n', '\n')
|
? this.provider.signDelimiter.replaceAll('\\n', '\n')
|
||||||
: '\n';
|
: '\n';
|
||||||
const textToConcat = this.provider.sign_msg ? [`*${senderName}:*`] : [];
|
const textToConcat = this.provider.signMsg ? [`*${senderName}:*`] : [];
|
||||||
textToConcat.push(messageReceived);
|
textToConcat.push(messageReceived);
|
||||||
|
|
||||||
formatText = textToConcat.join(formattedDelimiter);
|
formatText = textToConcat.join(formattedDelimiter);
|
||||||
@ -1694,7 +1695,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client.messages.create({
|
client.messages.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
conversationId: getConversation,
|
conversationId: getConversation,
|
||||||
data: {
|
data: {
|
||||||
content: `🚨 ${i18next.t('numbernotinwhatsapp')}`,
|
content: `🚨 ${i18next.t('numbernotinwhatsapp')}`,
|
||||||
@ -1968,7 +1969,7 @@ export class ChatwootService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return await client.messages.delete({
|
return await client.messages.delete({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
conversationId: message.chatwootConversationId,
|
conversationId: message.chatwootConversationId,
|
||||||
messageId: message.chatwootMessageId,
|
messageId: message.chatwootMessageId,
|
||||||
});
|
});
|
||||||
@ -2029,7 +2030,7 @@ export class ChatwootService {
|
|||||||
|
|
||||||
if (!sourceId && inbox) {
|
if (!sourceId && inbox) {
|
||||||
const conversation = (await client.conversations.get({
|
const conversation = (await client.conversations.get({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
conversationId: conversationId,
|
conversationId: conversationId,
|
||||||
})) as conversation_show & {
|
})) as conversation_show & {
|
||||||
last_non_activity_message: { conversation: { contact_inbox: contact_inboxes } };
|
last_non_activity_message: { conversation: { contact_inbox: contact_inboxes } };
|
||||||
@ -2210,7 +2211,7 @@ export class ChatwootService {
|
|||||||
recentContacts.forEach(async (contact) => {
|
recentContacts.forEach(async (contact) => {
|
||||||
if (contactsWithProfilePicture.has(contact.identifier)) {
|
if (contactsWithProfilePicture.has(contact.identifier)) {
|
||||||
client.contacts.update({
|
client.contacts.update({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.accountId,
|
||||||
id: contact.id,
|
id: contact.id,
|
||||||
data: {
|
data: {
|
||||||
avatar_url: contactsWithProfilePicture.get(contact.identifier).profilePictureUrl || null,
|
avatar_url: contactsWithProfilePicture.get(contact.identifier).profilePictureUrl || null,
|
||||||
|
@ -32,13 +32,13 @@ export const chatwootSchema: JSONSchema7 = {
|
|||||||
signDelimiter: { type: ['string', 'null'] },
|
signDelimiter: { type: ['string', 'null'] },
|
||||||
nameInbox: { type: ['string', 'null'] },
|
nameInbox: { type: ['string', 'null'] },
|
||||||
reopenConversation: { type: 'boolean', enum: [true, false] },
|
reopenConversation: { type: 'boolean', enum: [true, false] },
|
||||||
conversation_pending: { type: 'boolean', enum: [true, false] },
|
conversationPending: { type: 'boolean', enum: [true, false] },
|
||||||
autoCreate: { type: 'boolean', enum: [true, false] },
|
autoCreate: { type: 'boolean', enum: [true, false] },
|
||||||
importContacts: { type: 'boolean', enum: [true, false] },
|
importContacts: { type: 'boolean', enum: [true, false] },
|
||||||
mergeBrazilContacts: { type: 'boolean', enum: [true, false] },
|
mergeBrazilContacts: { type: 'boolean', enum: [true, false] },
|
||||||
importMessages: { type: 'boolean', enum: [true, false] },
|
importMessages: { type: 'boolean', enum: [true, false] },
|
||||||
daysLimitImportMessages: { type: 'number' },
|
daysLimitImportMessages: { type: 'number' },
|
||||||
},
|
},
|
||||||
required: ['enabled', 'accountId', 'token', 'url', 'signMsg', 'reopenConversation', 'conversation_pending'],
|
required: ['enabled', 'accountId', 'token', 'url', 'signMsg', 'reopenConversation', 'conversationPending'],
|
||||||
...isNotEmpty('enabled', 'accountId', 'token', 'url', 'signMsg', 'reopenConversation', 'conversation_pending'),
|
...isNotEmpty('enabled', 'accountId', 'token', 'url', 'signMsg', 'reopenConversation', 'conversationPending'),
|
||||||
};
|
};
|
||||||
|
@ -290,6 +290,41 @@ export class ChannelStartupService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const chatwoot = await this.prismaRepository.chatwoot.findUnique({
|
||||||
|
where: {
|
||||||
|
instanceId: this.instanceId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (chatwoot) {
|
||||||
|
await this.prismaRepository.chatwoot.update({
|
||||||
|
where: {
|
||||||
|
instanceId: this.instanceId,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
enabled: data.enabled,
|
||||||
|
accountId: data.accountId,
|
||||||
|
token: data.token,
|
||||||
|
url: data.url,
|
||||||
|
nameInbox: data.nameInbox,
|
||||||
|
signMsg: data.signMsg,
|
||||||
|
signDelimiter: data.signMsg ? data.signDelimiter : null,
|
||||||
|
number: data.number,
|
||||||
|
reopenConversation: data.reopenConversation,
|
||||||
|
conversationPending: data.conversationPending,
|
||||||
|
mergeBrazilContacts: data.mergeBrazilContacts,
|
||||||
|
importContacts: data.importContacts,
|
||||||
|
importMessages: data.importMessages,
|
||||||
|
daysLimitImportMessages: data.daysLimitImportMessages,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.assign(this.localChatwoot, { ...data, signDelimiter: data.signMsg ? data.signDelimiter : null });
|
||||||
|
|
||||||
|
this.clearCacheChatwoot();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await this.prismaRepository.chatwoot.create({
|
await this.prismaRepository.chatwoot.create({
|
||||||
data: {
|
data: {
|
||||||
enabled: data.enabled,
|
enabled: data.enabled,
|
||||||
|
@ -750,7 +750,6 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
private readonly chatHandle = {
|
private readonly chatHandle = {
|
||||||
'chats.upsert': async (chats: Chat[]) => {
|
'chats.upsert': async (chats: Chat[]) => {
|
||||||
console.log('chats.upsert', chats);
|
|
||||||
const existingChatIds = await this.prismaRepository.chat.findMany({
|
const existingChatIds = await this.prismaRepository.chat.findMany({
|
||||||
where: { instanceId: this.instanceId },
|
where: { instanceId: this.instanceId },
|
||||||
select: { remoteJid: true },
|
select: { remoteJid: true },
|
||||||
@ -902,13 +901,13 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
try {
|
try {
|
||||||
const instance: InstanceDto = { instanceName: this.instance.name };
|
const instance: InstanceDto = { instanceName: this.instance.name };
|
||||||
|
|
||||||
const daysLimitToImport =
|
let timestampLimitToImport = null;
|
||||||
this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled
|
|
||||||
? this.localChatwoot.daysLimitImportMessages
|
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) {
|
||||||
: 1000;
|
const daysLimitToImport = this.localChatwoot.enabled ? this.localChatwoot.daysLimitImportMessages : 1000;
|
||||||
|
|
||||||
const date = new Date();
|
const date = new Date();
|
||||||
const timestampLimitToImport = new Date(date.setDate(date.getDate() - daysLimitToImport)).getTime() / 1000;
|
timestampLimitToImport = new Date(date.setDate(date.getDate() - daysLimitToImport)).getTime() / 1000;
|
||||||
|
|
||||||
const maxBatchTimestamp = Math.max(...messages.map((message) => message.messageTimestamp as number));
|
const maxBatchTimestamp = Math.max(...messages.map((message) => message.messageTimestamp as number));
|
||||||
|
|
||||||
@ -917,6 +916,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
if (!processBatch) {
|
if (!processBatch) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const chatsRaw: any[] = [];
|
const chatsRaw: any[] = [];
|
||||||
const chatsRepository = new Set(
|
const chatsRepository = new Set(
|
||||||
@ -935,17 +935,20 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
chatsRaw.push({
|
chatsRaw.push({
|
||||||
remoteJid: chat.id,
|
remoteJid: chat.id,
|
||||||
instanceId: this.instanceId,
|
instanceId: this.instanceId,
|
||||||
lastMsgTimestamp: parseInt(chat.lastMessageRecvTimestamp?.toString()).toString(),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.sendDataWebhook(Events.CHATS_SET, chatsRaw);
|
this.sendDataWebhook(Events.CHATS_SET, chatsRaw);
|
||||||
|
|
||||||
this.prismaRepository.chat.createMany({
|
console.log('chatsRaw', chatsRaw);
|
||||||
|
|
||||||
|
const chatsSaved = await this.prismaRepository.chat.createMany({
|
||||||
data: chatsRaw,
|
data: chatsRaw,
|
||||||
skipDuplicates: true,
|
skipDuplicates: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('chatsSaved', chatsSaved);
|
||||||
|
|
||||||
const messagesRaw: any[] = [];
|
const messagesRaw: any[] = [];
|
||||||
|
|
||||||
const messagesRepository = new Set(
|
const messagesRepository = new Set(
|
||||||
@ -964,10 +967,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (
|
if (chatwootImport.getRepositoryMessagesCache(instance) === null) {
|
||||||
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
|
|
||||||
chatwootImport.getRepositoryMessagesCache(instance) === null
|
|
||||||
) {
|
|
||||||
chatwootImport.setRepositoryMessagesCache(instance, messagesRepository);
|
chatwootImport.setRepositoryMessagesCache(instance, messagesRepository);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -980,23 +980,16 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
m.messageTimestamp = m.messageTimestamp?.toNumber();
|
m.messageTimestamp = m.messageTimestamp?.toNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) {
|
||||||
if (m.messageTimestamp <= timestampLimitToImport) {
|
if (m.messageTimestamp <= timestampLimitToImport) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (messagesRepository.has(m.key.id)) {
|
if (messagesRepository.has(m.key.id)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const status: Record<number, wa.StatusMessage> = {
|
|
||||||
0: 'ERROR',
|
|
||||||
1: 'PENDING',
|
|
||||||
2: 'SERVER_ACK',
|
|
||||||
3: 'DELIVERY_ACK',
|
|
||||||
4: 'READ',
|
|
||||||
5: 'PLAYED',
|
|
||||||
};
|
|
||||||
|
|
||||||
messagesRaw.push({
|
messagesRaw.push({
|
||||||
key: m.key,
|
key: m.key,
|
||||||
pushName: m.pushName || m.key.remoteJid.split('@')[0],
|
pushName: m.pushName || m.key.remoteJid.split('@')[0],
|
||||||
@ -1004,18 +997,22 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
message: { ...m.message },
|
message: { ...m.message },
|
||||||
messageType: getContentType(m.message),
|
messageType: getContentType(m.message),
|
||||||
messageTimestamp: m.messageTimestamp as number,
|
messageTimestamp: m.messageTimestamp as number,
|
||||||
status: m.status ? status[m.status] : null,
|
|
||||||
instanceId: this.instanceId,
|
instanceId: this.instanceId,
|
||||||
|
source: getDevice(m.key.id),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('messagesRaw', messagesRaw);
|
||||||
|
|
||||||
this.sendDataWebhook(Events.MESSAGES_SET, [...messagesRaw]);
|
this.sendDataWebhook(Events.MESSAGES_SET, [...messagesRaw]);
|
||||||
|
|
||||||
this.prismaRepository.message.createMany({
|
const messagesSaved = await this.prismaRepository.message.createMany({
|
||||||
data: messagesRaw,
|
data: messagesRaw,
|
||||||
skipDuplicates: true,
|
skipDuplicates: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('messagesSaved', messagesSaved);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
|
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
|
||||||
this.localChatwoot.enabled &&
|
this.localChatwoot.enabled &&
|
||||||
|
Loading…
Reference in New Issue
Block a user