Skip to content

Commit dd21a29

Browse files
committed
fix(baileys): salvar corretamente buffer no db
1 parent d58d0b8 commit dd21a29

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4444,24 +4444,37 @@ export class BaileysStartupService extends ChannelStartupService {
44444444
throw new Error('Method not available in the Baileys service');
44454445
}
44464446

4447-
private convertLongToNumber(obj: any): any {
4447+
private deserializeMessageBuffers(obj: any): any {
44484448
if (obj === null || obj === undefined) {
44494449
return obj;
44504450
}
44514451

4452-
if (Long.isLong(obj)) {
4453-
return obj.toNumber();
4452+
if (typeof obj === 'object' && !Array.isArray(obj) && !Buffer.isBuffer(obj)) {
4453+
const keys = Object.keys(obj);
4454+
const isIndexedObject = keys.every((key) => !isNaN(Number(key)));
4455+
4456+
if (isIndexedObject && keys.length > 0) {
4457+
const values = keys.sort((a, b) => Number(a) - Number(b)).map((key) => obj[key]);
4458+
return new Uint8Array(values);
4459+
}
4460+
}
4461+
4462+
// Is Buffer?, converter to Uint8Array
4463+
if (Buffer.isBuffer(obj)) {
4464+
return new Uint8Array(obj);
44544465
}
44554466

4467+
// Process arrays recursively
44564468
if (Array.isArray(obj)) {
4457-
return obj.map((item) => this.convertLongToNumber(item));
4469+
return obj.map((item) => this.deserializeMessageBuffers(item));
44584470
}
44594471

4472+
// Process objects recursively
44604473
if (typeof obj === 'object') {
44614474
const converted: any = {};
44624475
for (const key in obj) {
44634476
if (Object.prototype.hasOwnProperty.call(obj, key)) {
4464-
converted[key] = this.convertLongToNumber(obj[key]);
4477+
converted[key] = this.deserializeMessageBuffers(obj[key]);
44654478
}
44664479
}
44674480
return converted;
@@ -4482,8 +4495,8 @@ export class BaileysStartupService extends ChannelStartupService {
44824495
? 'Você'
44834496
: message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)),
44844497
status: status[message.status],
4485-
message: this.convertLongToNumber({ ...message.message }),
4486-
contextInfo: this.convertLongToNumber(contentMsg?.contextInfo),
4498+
message: this.deserializeMessageBuffers({ ...message.message }),
4499+
contextInfo: this.deserializeMessageBuffers(contentMsg?.contextInfo),
44874500
messageType: contentType || 'unknown',
44884501
messageTimestamp: Long.isLong(message.messageTimestamp)
44894502
? message.messageTimestamp.toNumber()

0 commit comments

Comments
 (0)