Skip to content

Commit ebdcace

Browse files
enzowilliamclaude
andcommitted
refactor: extract send-message normalization helpers and keep everyOne as deprecated alias
- Add src/utils/normalizeSendMessageOptions.ts with normalizeFileName (uses undefined-check so intentional empty fileName is preserved) and resolveMentionsEveryOne (accepts everyOne alias and logs a one-shot deprecation warning). - Apply normalizeFileName in baileys, evolution and meta mediaMessage services, removing the duplicated (data as any).filename cast. - Restore backward compatibility for everyOne: schemas accept it as a deprecated alias, DTOs expose it as @deprecated, and baileys uses resolveMentionsEveryOne when computing mentions. - Fix unrelated axios AxiosHeaderValue type errors surfacing in pre-commit tsc (baileys + chatwoot content-type header casts). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent a68289d commit ebdcace

7 files changed

Lines changed: 55 additions & 19 deletions

File tree

src/api/dto/sendMessage.dto.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export class Options {
1212
linkPreview?: boolean;
1313
encoding?: boolean;
1414
mentionsEveryOne?: boolean;
15+
/** @deprecated use `mentionsEveryOne` instead. Kept for backward compatibility; will be removed in a future release. */
16+
everyOne?: boolean;
1517
mentioned?: string[];
1618
webhookUrl?: string;
1719
}
@@ -42,6 +44,8 @@ export class Metadata {
4244
quoted?: Quoted;
4345
linkPreview?: boolean;
4446
mentionsEveryOne?: boolean;
47+
/** @deprecated use `mentionsEveryOne` instead. Kept for backward compatibility; will be removed in a future release. */
48+
everyOne?: boolean;
4549
mentioned?: string[];
4650
encoding?: boolean;
4751
notConvertSticker?: boolean;

src/api/integrations/channel/evolution/evolution.channel.service.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Events, wa } from '@api/types/wa.types';
1616
import { AudioConverter, Chatwoot, ConfigService, Openai, S3 } from '@config/env.config';
1717
import { BadRequestException, InternalServerErrorException } from '@exceptions';
1818
import { createJid } from '@utils/createJid';
19+
import { normalizeFileName } from '@utils/normalizeSendMessageOptions';
1920
import { sendTelemetry } from '@utils/sendTelemetry';
2021
import axios from 'axios';
2122
import { isBase64, isURL } from 'class-validator';
@@ -597,11 +598,7 @@ export class EvolutionStartupService extends ChannelStartupService {
597598
}
598599

599600
public async mediaMessage(data: SendMediaDto, file?: any, isIntegration = false) {
600-
const mediaData: SendMediaDto = {
601-
...data,
602-
// Normalize filename to fileName (handle case-insensitivity)
603-
fileName: data.fileName || (data as any).filename,
604-
};
601+
const mediaData: SendMediaDto = normalizeFileName({ ...data });
605602

606603
if (file) mediaData.media = file.buffer.toString('base64');
607604

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Events, wa } from '@api/types/wa.types';
2323
import { AudioConverter, Chatwoot, ConfigService, Database, Openai, S3, WaBusiness } from '@config/env.config';
2424
import { BadRequestException, InternalServerErrorException } from '@exceptions';
2525
import { createJid } from '@utils/createJid';
26+
import { normalizeFileName } from '@utils/normalizeSendMessageOptions';
2627
import { status } from '@utils/renderStatus';
2728
import { sendTelemetry } from '@utils/sendTelemetry';
2829
import axios from 'axios';
@@ -1287,11 +1288,7 @@ export class BusinessStartupService extends ChannelStartupService {
12871288
}
12881289

12891290
public async mediaMessage(data: SendMediaDto, file?: any, isIntegration = false) {
1290-
const mediaData: SendMediaDto = {
1291-
...data,
1292-
// Normalize filename to fileName (handle case-insensitivity)
1293-
fileName: data.fileName || (data as any).filename,
1294-
};
1291+
const mediaData: SendMediaDto = normalizeFileName({ ...data });
12951292

12961293
if (file) mediaData.media = file.buffer.toString('base64');
12971294

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import { Instance, Message } from '@prisma/client';
8383
import { createJid } from '@utils/createJid';
8484
import { fetchLatestWaWebVersion } from '@utils/fetchLatestWaWebVersion';
8585
import { makeProxyAgent, makeProxyAgentUndici } from '@utils/makeProxyAgent';
86+
import { normalizeFileName, resolveMentionsEveryOne } from '@utils/normalizeSendMessageOptions';
8687
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
8788
import { status } from '@utils/renderStatus';
8889
import { sendTelemetry } from '@utils/sendTelemetry';
@@ -2372,7 +2373,7 @@ export class BaileysStartupService extends ChannelStartupService {
23722373
throw new NotFoundException('Group not found');
23732374
}
23742375

2375-
if (options?.mentionsEveryOne === true) {
2376+
if (resolveMentionsEveryOne(options)) {
23762377
mentions = group.participants.map((participant) => participant.id);
23772378
} else if (options?.mentioned?.length) {
23782379
mentions = options.mentioned.map((mention) => {
@@ -2829,7 +2830,7 @@ export class BaileysStartupService extends ChannelStartupService {
28292830

28302831
const response = await axios.get(mediaMessage.media, config);
28312832

2832-
mimetype = response.headers['content-type'];
2833+
mimetype = response.headers['content-type'] as string;
28332834
}
28342835
}
28352836

@@ -2979,11 +2980,7 @@ export class BaileysStartupService extends ChannelStartupService {
29792980
}
29802981

29812982
public async mediaMessage(data: SendMediaDto, file?: any, isIntegration = false) {
2982-
const mediaData: SendMediaDto = {
2983-
...data,
2984-
// Normalize filename to fileName (handle case-insensitivity)
2985-
fileName: data.fileName || (data as any).filename,
2986-
};
2983+
const mediaData: SendMediaDto = normalizeFileName({ ...data });
29872984

29882985
if (file) mediaData.media = file.buffer.toString('base64');
29892986

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ export class ChatwootService {
12041204
const response = await axios.get(media, {
12051205
responseType: 'arraybuffer',
12061206
});
1207-
mimeType = response.headers['content-type'];
1207+
mimeType = response.headers['content-type'] as string;
12081208
}
12091209

12101210
let type = 'document';
@@ -2214,7 +2214,7 @@ export class ChatwootService {
22142214
if (isAdsMessage) {
22152215
const imgBuffer = await axios.get(adsMessage.thumbnailUrl, { responseType: 'arraybuffer' });
22162216

2217-
const extension = mimeTypes.extension(imgBuffer.headers['content-type']);
2217+
const extension = mimeTypes.extension(imgBuffer.headers['content-type'] as string);
22182218
const mimeType = extension && mimeTypes.lookup(extension);
22192219

22202220
if (!mimeType) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Logger } from '@config/logger.config';
2+
3+
const logger = new Logger('SendMessageOptions');
4+
const warnedAliases = new Set<string>();
5+
6+
const warnOnce = (key: string, message: string) => {
7+
if (warnedAliases.has(key)) return;
8+
warnedAliases.add(key);
9+
logger.warn(message);
10+
};
11+
12+
export const normalizeFileName = <T extends { fileName?: string }>(data: T): T => {
13+
if (!data) return data;
14+
const legacy = (data as any).filename;
15+
if (data.fileName === undefined && legacy !== undefined) {
16+
return { ...data, fileName: legacy };
17+
}
18+
return data;
19+
};
20+
21+
export const resolveMentionsEveryOne = (input: any): boolean => {
22+
if (!input) return false;
23+
if (input.mentionsEveryOne === true) return true;
24+
if (input.everyOne === true) {
25+
warnOnce(
26+
'everyOne',
27+
'"everyOne" is deprecated and will be removed in a future release. Use "mentionsEveryOne" instead.',
28+
);
29+
return true;
30+
}
31+
return false;
32+
};

src/validate/message.schema.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export const textMessageSchema: JSONSchema7 = {
7878
},
7979
quoted: { ...quotedOptionsSchema },
8080
mentionsEveryOne: { type: 'boolean', enum: [true, false] },
81+
everyOne: { type: 'boolean', enum: [true, false], description: 'Deprecated alias for "mentionsEveryOne".' },
8182
mentioned: {
8283
type: 'array',
8384
minItems: 1,
@@ -108,6 +109,7 @@ export const mediaMessageSchema: JSONSchema7 = {
108109
},
109110
quoted: { ...quotedOptionsSchema },
110111
mentionsEveryOne: { type: 'boolean', enum: [true, false] },
112+
everyOne: { type: 'boolean', enum: [true, false], description: 'Deprecated alias for "mentionsEveryOne".' },
111113
mentioned: {
112114
type: 'array',
113115
minItems: 1,
@@ -134,6 +136,7 @@ export const ptvMessageSchema: JSONSchema7 = {
134136
},
135137
quoted: { ...quotedOptionsSchema },
136138
mentionsEveryOne: { type: 'boolean', enum: [true, false] },
139+
everyOne: { type: 'boolean', enum: [true, false], description: 'Deprecated alias for "mentionsEveryOne".' },
137140
mentioned: {
138141
type: 'array',
139142
minItems: 1,
@@ -160,6 +163,7 @@ export const audioMessageSchema: JSONSchema7 = {
160163
},
161164
quoted: { ...quotedOptionsSchema },
162165
mentionsEveryOne: { type: 'boolean', enum: [true, false] },
166+
everyOne: { type: 'boolean', enum: [true, false], description: 'Deprecated alias for "mentionsEveryOne".' },
163167
mentioned: {
164168
type: 'array',
165169
minItems: 1,
@@ -210,6 +214,7 @@ export const stickerMessageSchema: JSONSchema7 = {
210214
},
211215
quoted: { ...quotedOptionsSchema },
212216
mentionsEveryOne: { type: 'boolean', enum: [true, false] },
217+
everyOne: { type: 'boolean', enum: [true, false], description: 'Deprecated alias for "mentionsEveryOne".' },
213218
mentioned: {
214219
type: 'array',
215220
minItems: 1,
@@ -239,6 +244,7 @@ export const locationMessageSchema: JSONSchema7 = {
239244
},
240245
quoted: { ...quotedOptionsSchema },
241246
mentionsEveryOne: { type: 'boolean', enum: [true, false] },
247+
everyOne: { type: 'boolean', enum: [true, false], description: 'Deprecated alias for "mentionsEveryOne".' },
242248
mentioned: {
243249
type: 'array',
244250
minItems: 1,
@@ -326,6 +332,7 @@ export const pollMessageSchema: JSONSchema7 = {
326332
},
327333
quoted: { ...quotedOptionsSchema },
328334
mentionsEveryOne: { type: 'boolean', enum: [true, false] },
335+
everyOne: { type: 'boolean', enum: [true, false], description: 'Deprecated alias for "mentionsEveryOne".' },
329336
mentioned: {
330337
type: 'array',
331338
minItems: 1,
@@ -383,6 +390,7 @@ export const listMessageSchema: JSONSchema7 = {
383390
},
384391
quoted: { ...quotedOptionsSchema },
385392
mentionsEveryOne: { type: 'boolean', enum: [true, false] },
393+
everyOne: { type: 'boolean', enum: [true, false], description: 'Deprecated alias for "mentionsEveryOne".' },
386394
mentioned: {
387395
type: 'array',
388396
minItems: 1,
@@ -434,6 +442,7 @@ export const buttonsMessageSchema: JSONSchema7 = {
434442
},
435443
quoted: { ...quotedOptionsSchema },
436444
mentionsEveryOne: { type: 'boolean', enum: [true, false] },
445+
everyOne: { type: 'boolean', enum: [true, false], description: 'Deprecated alias for "mentionsEveryOne".' },
437446
mentioned: {
438447
type: 'array',
439448
minItems: 1,

0 commit comments

Comments
 (0)