Skip to content

Commit 3bff4e0

Browse files
authored
Merge pull request #1 from shajandevz/fix/engine-changes
fix(engine): resolve lint-staged and json path typing
2 parents cd800f2 + 713a963 commit 3bff4e0

File tree

7 files changed

+119
-52
lines changed

7 files changed

+119
-52
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"start:prod": "node dist/main",
1111
"dev:server": "tsx watch ./src/main.ts",
1212
"test": "tsx watch ./test/all.test.ts",
13-
"lint": "eslint --fix --ext .ts src",
14-
"lint:check": "eslint --ext .ts src",
13+
"lint": "ESLINT_USE_FLAT_CONFIG=false eslint --fix --ext .ts src",
14+
"lint:check": "ESLINT_USE_FLAT_CONFIG=false eslint --ext .ts src",
1515
"commit": "cz",
1616
"commitlint": "commitlint --edit",
1717
"db:generate": "node runWithProvider.js \"npx prisma generate --schema ./prisma/DATABASE_PROVIDER-schema.prisma\"",
@@ -53,7 +53,7 @@
5353
"homepage": "https://github.com/EvolutionAPI/evolution-api#readme",
5454
"lint-staged": {
5555
"src/**/*.{ts,js}": [
56-
"eslint --fix"
56+
"sh -c 'ESLINT_USE_FLAT_CONFIG=false eslint --fix'"
5757
],
5858
"src/**/*.ts": [
5959
"sh -c 'tsc --noEmit'"

project.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "engine",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "apps/engine/src",
5+
"projectType": "application",
6+
"prefix": "engine",
7+
"targets": {
8+
"serve": {
9+
"executor": "nx:run-commands",
10+
"options": {
11+
"command": "npm run dev:server",
12+
"cwd": "apps/engine"
13+
}
14+
},
15+
"build": {
16+
"executor": "nx:run-commands",
17+
"options": {
18+
"command": "npm run build",
19+
"cwd": "apps/engine"
20+
}
21+
},
22+
"db:migrate": {
23+
"executor": "nx:run-commands",
24+
"options": {
25+
"command": "npm run db:deploy",
26+
"cwd": "apps/engine"
27+
}
28+
},
29+
"db:seed": {
30+
"executor": "nx:run-commands",
31+
"options": {
32+
"command": "npm run db:seed",
33+
"cwd": "apps/engine"
34+
}
35+
}
36+
},
37+
"tags": ["type:app", "scope:engine"]
38+
}

src/api/integrations/channel/meta/whatsapp.business.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ export class BusinessStartupService extends ChannelStartupService {
758758
where: {
759759
instanceId: this.instanceId,
760760
key: {
761-
path: ['id'],
761+
path: '$.id',
762762
equals: key.id,
763763
},
764764
},

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

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,7 @@ export class BaileysStartupService extends ChannelStartupService {
18191819
const savedLabel = labelsRepository.find((l) => l.labelId === label.id);
18201820
if (label.deleted && savedLabel) {
18211821
await this.prismaRepository.label.delete({
1822-
where: { labelId_instanceId: { instanceId: this.instanceId, labelId: label.id } },
1822+
where: { id: savedLabel.id },
18231823
});
18241824
this.sendDataWebhook(Events.LABELS_EDIT, { ...label, instance: this.instance.name });
18251825
return;
@@ -1835,11 +1835,16 @@ export class BaileysStartupService extends ChannelStartupService {
18351835
predefinedId: label.predefinedId,
18361836
instanceId: this.instanceId,
18371837
};
1838-
await this.prismaRepository.label.upsert({
1839-
where: { labelId_instanceId: { instanceId: labelData.instanceId, labelId: labelData.labelId } },
1840-
update: labelData,
1841-
create: labelData,
1842-
});
1838+
if (savedLabel) {
1839+
await this.prismaRepository.label.update({
1840+
where: { id: savedLabel.id },
1841+
data: labelData,
1842+
});
1843+
} else {
1844+
await this.prismaRepository.label.create({
1845+
data: labelData,
1846+
});
1847+
}
18431848
}
18441849
}
18451850
},
@@ -3776,7 +3781,7 @@ export class BaileysStartupService extends ChannelStartupService {
37763781
if (messageId) {
37773782
const isLogicalDeleted = configService.get<Database>('DATABASE').DELETE_DATA.LOGICAL_MESSAGE_DELETE;
37783783
let message = await this.prismaRepository.message.findFirst({
3779-
where: { key: { path: ['id'], equals: messageId } },
3784+
where: { key: { path: '$.id', equals: messageId } },
37803785
});
37813786
if (isLogicalDeleted) {
37823787
if (!message) return response;
@@ -4196,7 +4201,7 @@ export class BaileysStartupService extends ChannelStartupService {
41964201
const messageId = messageSent.message?.protocolMessage?.key?.id;
41974202
if (messageId && this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
41984203
let message = await this.prismaRepository.message.findFirst({
4199-
where: { key: { path: ['id'], equals: messageId } },
4204+
where: { key: { path: '$.id', equals: messageId } },
42004205
});
42014206
if (!message) throw new NotFoundException('Message not found');
42024207

@@ -4734,6 +4739,26 @@ export class BaileysStartupService extends ChannelStartupService {
47344739
private async updateMessagesReadedByTimestamp(remoteJid: string, timestamp?: number): Promise<number> {
47354740
if (timestamp === undefined || timestamp === null) return 0;
47364741

4742+
const dbProvider = String(this.configService.get<Database>('DATABASE')?.PROVIDER || '').toLowerCase();
4743+
4744+
if (dbProvider === 'mysql') {
4745+
const result = await this.prismaRepository.$executeRaw`
4746+
UPDATE \`Message\`
4747+
SET \`status\` = ${status[4]}
4748+
WHERE \`instanceId\` = ${this.instanceId}
4749+
AND JSON_UNQUOTE(JSON_EXTRACT(\`key\`, '$.remoteJid')) = ${remoteJid}
4750+
AND JSON_EXTRACT(\`key\`, '$.fromMe') = false
4751+
AND \`messageTimestamp\` <= ${timestamp}
4752+
AND (\`status\` IS NULL OR \`status\` = ${status[3]})
4753+
`;
4754+
4755+
if (result && result > 0) {
4756+
this.updateChatUnreadMessages(remoteJid);
4757+
}
4758+
4759+
return result || 0;
4760+
}
4761+
47374762
// Use raw SQL to avoid JSON path issues
47384763
const result = await this.prismaRepository.$executeRaw`
47394764
UPDATE "Message"
@@ -4757,16 +4782,25 @@ export class BaileysStartupService extends ChannelStartupService {
47574782
}
47584783

47594784
private async updateChatUnreadMessages(remoteJid: string): Promise<number> {
4785+
const dbProvider = String(this.configService.get<Database>('DATABASE')?.PROVIDER || '').toLowerCase();
4786+
47604787
const [chat, unreadMessages] = await Promise.all([
47614788
this.prismaRepository.chat.findFirst({ where: { remoteJid } }),
4762-
// Use raw SQL to avoid JSON path issues
4763-
this.prismaRepository.$queryRaw`
4764-
SELECT COUNT(*)::int as count FROM "Message"
4765-
WHERE "instanceId" = ${this.instanceId}
4766-
AND "key"->>'remoteJid' = ${remoteJid}
4767-
AND ("key"->>'fromMe')::boolean = false
4768-
AND "status" = ${status[3]}
4769-
`.then((result: any[]) => result[0]?.count || 0),
4789+
dbProvider === 'mysql'
4790+
? this.prismaRepository.$queryRaw`
4791+
SELECT COUNT(*) as count FROM \`Message\`
4792+
WHERE \`instanceId\` = ${this.instanceId}
4793+
AND JSON_UNQUOTE(JSON_EXTRACT(\`key\`, '$.remoteJid')) = ${remoteJid}
4794+
AND JSON_EXTRACT(\`key\`, '$.fromMe') = false
4795+
AND \`status\` = ${status[3]}
4796+
`.then((result: any[]) => Number(result?.[0]?.count || 0))
4797+
: this.prismaRepository.$queryRaw`
4798+
SELECT COUNT(*)::int as count FROM "Message"
4799+
WHERE "instanceId" = ${this.instanceId}
4800+
AND "key"->>'remoteJid' = ${remoteJid}
4801+
AND ("key"->>'fromMe')::boolean = false
4802+
AND "status" = ${status[3]}
4803+
`.then((result: any[]) => result[0]?.count || 0),
47704804
]);
47714805

47724806
if (chat && chat.unreadMessages !== unreadMessages) {
@@ -5013,7 +5047,7 @@ export class BaileysStartupService extends ChannelStartupService {
50135047
}
50145048
}
50155049

5016-
public async fetchMessages(query: Query<Message>) {
5050+
public async fetchMessages(query: Query<Message>): Promise<any> {
50175051
const keyFilters = query?.where?.key as ExtendedIMessageKey;
50185052

50195053
const timestampFilter = {};
@@ -5034,13 +5068,13 @@ export class BaileysStartupService extends ChannelStartupService {
50345068
messageType: query?.where?.messageType,
50355069
...timestampFilter,
50365070
AND: [
5037-
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
5038-
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
5039-
keyFilters?.participant ? { key: { path: ['participant'], equals: keyFilters?.participant } } : {},
5071+
keyFilters?.id ? { key: { path: '$.id', equals: keyFilters?.id } } : {},
5072+
keyFilters?.fromMe ? { key: { path: '$.fromMe', equals: keyFilters?.fromMe } } : {},
5073+
keyFilters?.participant ? { key: { path: '$.participant', equals: keyFilters?.participant } } : {},
50405074
{
50415075
OR: [
5042-
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
5043-
keyFilters?.remoteJidAlt ? { key: { path: ['remoteJidAlt'], equals: keyFilters?.remoteJidAlt } } : {},
5076+
keyFilters?.remoteJid ? { key: { path: '$.remoteJid', equals: keyFilters?.remoteJid } } : {},
5077+
keyFilters?.remoteJidAlt ? { key: { path: '$.remoteJidAlt', equals: keyFilters?.remoteJidAlt } } : {},
50445078
],
50455079
},
50465080
],
@@ -5063,13 +5097,13 @@ export class BaileysStartupService extends ChannelStartupService {
50635097
messageType: query?.where?.messageType,
50645098
...timestampFilter,
50655099
AND: [
5066-
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
5067-
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
5068-
keyFilters?.participant ? { key: { path: ['participant'], equals: keyFilters?.participant } } : {},
5100+
keyFilters?.id ? { key: { path: '$.id', equals: keyFilters?.id } } : {},
5101+
keyFilters?.fromMe ? { key: { path: '$.fromMe', equals: keyFilters?.fromMe } } : {},
5102+
keyFilters?.participant ? { key: { path: '$.participant', equals: keyFilters?.participant } } : {},
50695103
{
50705104
OR: [
5071-
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
5072-
keyFilters?.remoteJidAlt ? { key: { path: ['remoteJidAlt'], equals: keyFilters?.remoteJidAlt } } : {},
5105+
keyFilters?.remoteJid ? { key: { path: '$.remoteJid', equals: keyFilters?.remoteJid } } : {},
5106+
keyFilters?.remoteJidAlt ? { key: { path: '$.remoteJidAlt', equals: keyFilters?.remoteJidAlt } } : {},
50735107
],
50745108
},
50755109
],

src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ export class ChatwootService {
15461546
const lastMessage = await this.prismaRepository.message.findFirst({
15471547
where: {
15481548
key: {
1549-
path: ['fromMe'],
1549+
path: '$.fromMe',
15501550
equals: false,
15511551
},
15521552
instanceId: instance.instanceId,
@@ -1576,7 +1576,7 @@ export class ChatwootService {
15761576
where: {
15771577
instanceId: instance.instanceId,
15781578
key: {
1579-
path: ['id'],
1579+
path: '$.id',
15801580
equals: key.id,
15811581
},
15821582
},
@@ -2025,7 +2025,7 @@ export class ChatwootService {
20252025
quotedMsg = await this.prismaRepository.message.findFirst({
20262026
where: {
20272027
key: {
2028-
path: ['id'],
2028+
path: '$.id',
20292029
equals: quotedId,
20302030
},
20312031
chatwootMessageId: {
@@ -2337,7 +2337,7 @@ export class ChatwootService {
23372337
await this.prismaRepository.message.deleteMany({
23382338
where: {
23392339
key: {
2340-
path: ['id'],
2340+
path: '$.id',
23412341
equals: body.key.id,
23422342
},
23432343
instanceId: instance.instanceId,

src/api/services/channel.service.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -623,10 +623,10 @@ export class ChannelStartupService {
623623
messageType: query?.where?.messageType,
624624
...timestampFilter,
625625
AND: [
626-
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
627-
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
628-
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
629-
keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {},
626+
keyFilters?.id ? { key: { path: '$.id', equals: keyFilters?.id } } : {},
627+
keyFilters?.fromMe ? { key: { path: '$.fromMe', equals: keyFilters?.fromMe } } : {},
628+
keyFilters?.remoteJid ? { key: { path: '$.remoteJid', equals: keyFilters?.remoteJid } } : {},
629+
keyFilters?.participants ? { key: { path: '$.participants', equals: keyFilters?.participants } } : {},
630630
],
631631
},
632632
});
@@ -647,10 +647,10 @@ export class ChannelStartupService {
647647
messageType: query?.where?.messageType,
648648
...timestampFilter,
649649
AND: [
650-
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
651-
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
652-
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
653-
keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {},
650+
keyFilters?.id ? { key: { path: '$.id', equals: keyFilters?.id } } : {},
651+
keyFilters?.fromMe ? { key: { path: '$.fromMe', equals: keyFilters?.fromMe } } : {},
652+
keyFilters?.remoteJid ? { key: { path: '$.remoteJid', equals: keyFilters?.remoteJid } } : {},
653+
keyFilters?.participants ? { key: { path: '$.participants', equals: keyFilters?.participants } } : {},
654654
],
655655
},
656656
orderBy: {

src/utils/onWhatsappCache.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,9 @@ export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
126126
// Ordena os JIDs para garantir consistência na string final
127127
const sortedJidOptions = [...finalJidOptions].sort();
128128
const newJidOptionsString = sortedJidOptions.join(',');
129-
const newLid = item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null;
130-
131129
const dataPayload = {
132130
remoteJid: remoteJid,
133131
jidOptions: newJidOptionsString,
134-
lid: newLid,
135132
};
136133

137134
// 4. Decide entre Criar ou Atualizar
@@ -142,9 +139,7 @@ export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
142139
: '';
143140

144141
const isDataSame =
145-
existingRecord.remoteJid === dataPayload.remoteJid &&
146-
existingJidOptionsString === dataPayload.jidOptions &&
147-
existingRecord.lid === dataPayload.lid;
142+
existingRecord.remoteJid === dataPayload.remoteJid && existingJidOptionsString === dataPayload.jidOptions;
148143

149144
if (isDataSame) {
150145
logger.verbose(`[saveOnWhatsappCache] Data for ${remoteJid} is already up-to-date. Skipping update.`);
@@ -153,7 +148,7 @@ export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
153148

154149
// Os dados são diferentes, então atualiza
155150
logger.verbose(
156-
`[saveOnWhatsappCache] Register exists, updating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}, lid=${dataPayload.lid}`,
151+
`[saveOnWhatsappCache] Register exists, updating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}`,
157152
);
158153
await prismaRepository.isOnWhatsapp.update({
159154
where: { id: existingRecord.id },
@@ -162,7 +157,7 @@ export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
162157
} else {
163158
// Cria nova entrada
164159
logger.verbose(
165-
`[saveOnWhatsappCache] Register does not exist, creating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}, lid=${dataPayload.lid}`,
160+
`[saveOnWhatsappCache] Register does not exist, creating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}`,
166161
);
167162
await prismaRepository.isOnWhatsapp.create({
168163
data: dataPayload,
@@ -203,7 +198,7 @@ export async function getOnWhatsappCache(remoteJids: string[]) {
203198
remoteJid: item.remoteJid,
204199
number: item.remoteJid.split('@')[0],
205200
jidOptions: item.jidOptions.split(','),
206-
lid: item.lid,
201+
lid: (item as any).lid || (item.remoteJid.includes('@lid') ? 'lid' : undefined),
207202
}));
208203
}
209204

0 commit comments

Comments
 (0)