Skip to content

Commit ff4019d

Browse files
rootclaude
authored andcommitted
fix: restore createJid import and fix chat upsert logic in messages.upsert
- Add missing `import { createJid }` from @utils/createJid and replace all `this.createJid()` calls with the imported function (was causing "this.createJid is not a function" runtime error) - Fix inverted chat existence check: update existing chat name instead of trying to create a duplicate (was causing Prisma P2002 unique constraint errors that blocked webhook delivery to n8n) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9e1e7be commit ff4019d

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import { BadRequestException, InternalServerErrorException, NotFoundException }
7777
import ffmpegPath from '@ffmpeg-installer/ffmpeg';
7878
import { Boom } from '@hapi/boom';
7979
import { Instance } from '@prisma/client';
80+
import { createJid } from '@utils/createJid';
8081
import { makeProxyAgent } from '@utils/makeProxyAgent';
8182
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
8283
import { status } from '@utils/renderStatus';
@@ -1148,21 +1149,27 @@ export class BaileysStartupService extends ChannelStartupService {
11481149
}
11491150
const existingChat = await this.prismaRepository.chat.findFirst({
11501151
where: { instanceId: this.instanceId, remoteJid: received.key.remoteJid },
1152+
select: { id: true, name: true },
11511153
});
11521154

1153-
if (existingChat) {
1154-
const chatToInsert = {
1155-
remoteJid: received.key.remoteJid,
1156-
instanceId: this.instanceId,
1157-
name: received.pushName || '',
1158-
unreadMessages: 0,
1159-
};
1160-
1161-
this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]);
1155+
if (
1156+
existingChat &&
1157+
received.pushName &&
1158+
existingChat.name !== received.pushName &&
1159+
received.pushName.trim().length > 0 &&
1160+
!received.key.fromMe &&
1161+
!received.key.remoteJid.includes('@g.us')
1162+
) {
1163+
this.sendDataWebhook(Events.CHATS_UPSERT, [{ ...existingChat, name: received.pushName }]);
11621164
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {
1163-
await this.prismaRepository.chat.create({
1164-
data: chatToInsert,
1165-
});
1165+
try {
1166+
await this.prismaRepository.chat.update({
1167+
where: { id: existingChat.id },
1168+
data: { name: received.pushName },
1169+
});
1170+
} catch {
1171+
console.log(`Chat insert record ignored: ${received.key.remoteJid} - ${this.instanceId}`);
1172+
}
11661173
}
11671174
}
11681175

@@ -1783,7 +1790,7 @@ export class BaileysStartupService extends ChannelStartupService {
17831790
}
17841791

17851792
public async profilePicture(number: string) {
1786-
const jid = this.createJid(number);
1793+
const jid = createJid(number);
17871794

17881795
try {
17891796
const profilePictureUrl = await this.client.profilePictureUrl(jid, 'image');
@@ -1801,7 +1808,7 @@ export class BaileysStartupService extends ChannelStartupService {
18011808
}
18021809

18031810
public async getStatus(number: string) {
1804-
const jid = this.createJid(number);
1811+
const jid = createJid(number);
18051812

18061813
try {
18071814
return {
@@ -1817,7 +1824,7 @@ export class BaileysStartupService extends ChannelStartupService {
18171824
}
18181825

18191826
public async fetchProfile(instanceName: string, number?: string) {
1820-
const jid = number ? this.createJid(number) : this.client?.user?.id;
1827+
const jid = number ? createJid(number) : this.client?.user?.id;
18211828

18221829
const onWhatsapp = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
18231830

@@ -1873,7 +1880,7 @@ export class BaileysStartupService extends ChannelStartupService {
18731880
}
18741881

18751882
public async offerCall({ number, isVideo, callDuration }: OfferCallDto) {
1876-
const jid = this.createJid(number);
1883+
const jid = createJid(number);
18771884

18781885
try {
18791886
const call = await this.client.offerCall(jid, isVideo);
@@ -2144,7 +2151,7 @@ export class BaileysStartupService extends ChannelStartupService {
21442151
mentions = group.participants.map((participant) => participant.id);
21452152
} else if (options.mentioned?.length) {
21462153
mentions = options.mentioned.map((mention) => {
2147-
const jid = this.createJid(mention);
2154+
const jid = createJid(mention);
21482155
if (isJidGroup(jid)) {
21492156
return null;
21502157
}
@@ -3214,7 +3221,7 @@ export class BaileysStartupService extends ChannelStartupService {
32143221
}
32153222

32163223
if (!contact.wuid) {
3217-
contact.wuid = this.createJid(contact.phoneNumber);
3224+
contact.wuid = createJid(contact.phoneNumber);
32183225
}
32193226

32203227
result += `item1.TEL;waid=${contact.wuid}:${contact.phoneNumber}\n` + 'item1.X-ABLabel:Celular\n' + 'END:VCARD';
@@ -3264,7 +3271,7 @@ export class BaileysStartupService extends ChannelStartupService {
32643271
};
32653272

32663273
data.numbers.forEach((number) => {
3267-
const jid = this.createJid(number);
3274+
const jid = createJid(number);
32683275

32693276
if (isJidGroup(jid)) {
32703277
jids.groups.push({ number, jid });
@@ -3457,7 +3464,7 @@ export class BaileysStartupService extends ChannelStartupService {
34573464
archive: data.archive,
34583465
lastMessages: [last_message],
34593466
},
3460-
this.createJid(number),
3467+
createJid(number),
34613468
);
34623469

34633470
return {
@@ -3494,7 +3501,7 @@ export class BaileysStartupService extends ChannelStartupService {
34943501
markRead: false,
34953502
lastMessages: [last_message],
34963503
},
3497-
this.createJid(number),
3504+
createJid(number),
34983505
);
34993506

35003507
return {
@@ -3699,7 +3706,7 @@ export class BaileysStartupService extends ChannelStartupService {
36993706

37003707
public async fetchBusinessProfile(number: string): Promise<NumberBusiness> {
37013708
try {
3702-
const jid = number ? this.createJid(number) : this.instance.wuid;
3709+
const jid = number ? createJid(number) : this.instance.wuid;
37033710

37043711
const profile = await this.client.getBusinessProfile(jid);
37053712

@@ -3847,7 +3854,7 @@ export class BaileysStartupService extends ChannelStartupService {
38473854
}
38483855

38493856
public async updateMessage(data: UpdateMessageDto) {
3850-
const jid = this.createJid(data.number);
3857+
const jid = createJid(data.number);
38513858

38523859
const options = await this.formatUpdateMessage(data);
38533860

@@ -4135,7 +4142,7 @@ export class BaileysStartupService extends ChannelStartupService {
41354142

41364143
const inviteUrl = inviteCode.inviteUrl;
41374144

4138-
const numbers = id.numbers.map((number) => this.createJid(number));
4145+
const numbers = id.numbers.map((number) => createJid(number));
41394146
const description = id.description ?? '';
41404147

41414148
const msg = `${description}\n\n${inviteUrl}`;
@@ -4206,7 +4213,7 @@ export class BaileysStartupService extends ChannelStartupService {
42064213

42074214
public async updateGParticipant(update: GroupUpdateParticipantDto) {
42084215
try {
4209-
const participants = update.participants.map((p) => this.createJid(p));
4216+
const participants = update.participants.map((p) => createJid(p));
42104217
const updateParticipants = await this.client.groupParticipantsUpdate(
42114218
update.groupJid,
42124219
participants,

0 commit comments

Comments
 (0)