chore: optimization in instance registration

This commit is contained in:
Davidson Gomes
2024-06-07 08:28:22 -03:00
parent 0bb9940d05
commit eed32a3bd9
13 changed files with 164 additions and 318 deletions

View File

@@ -26,7 +26,6 @@ import makeWASocket, {
ParticipantAction,
prepareWAMessageMedia,
proto,
useMultiFileAuthState,
UserFacingSocketConfig,
WABrowserDescription,
WAMediaUpload,
@@ -462,19 +461,17 @@ export class BaileysStartupService extends ChannelStartupService {
const provider = this.configService.get<ProviderSession>('PROVIDER');
if (provider?.ENABLED) {
return await this.authStateProvider.authStateProvider(this.instance.name);
return await this.authStateProvider.authStateProvider(this.instance.id);
}
if (cache?.REDIS.ENABLED && cache?.REDIS.SAVE_INSTANCES) {
this.logger.info('Redis enabled');
return await useMultiFileAuthStateRedisDb(this.instance.name, this.cache);
return await useMultiFileAuthStateRedisDb(this.instance.id, this.cache);
}
if (db.SAVE_DATA.INSTANCE && db.ENABLED) {
return await useMultiFileAuthStatePrisma(this.instanceId);
return await useMultiFileAuthStatePrisma(this.instance.id);
}
return await useMultiFileAuthState(join(INSTANCE_DIR, this.instance.name));
}
public async connectToWhatsapp(number?: string): Promise<WASocket> {

View File

@@ -70,12 +70,10 @@ export class BusinessStartupService extends ChannelStartupService {
private async post(message: any, params: string) {
try {
const integration = await this.findIntegration();
let urlServer = this.configService.get<WaBusiness>('WA_BUSINESS').URL;
const version = this.configService.get<WaBusiness>('WA_BUSINESS').VERSION;
urlServer = `${urlServer}/${version}/${integration.number}/${params}`;
const headers = { 'Content-Type': 'application/json', Authorization: `Bearer ${integration.token}` };
urlServer = `${urlServer}/${version}/${this.number}/${params}`;
const headers = { 'Content-Type': 'application/json', Authorization: `Bearer ${this.token}` };
const result = await axios.post(urlServer, message, { headers });
return result.data;
} catch (e) {
@@ -150,13 +148,11 @@ export class BusinessStartupService extends ChannelStartupService {
private async downloadMediaMessage(message: any) {
try {
const integration = await this.findIntegration();
const id = message[message.type].id;
let urlServer = this.configService.get<WaBusiness>('WA_BUSINESS').URL;
const version = this.configService.get<WaBusiness>('WA_BUSINESS').VERSION;
urlServer = `${urlServer}/${version}/${id}`;
const headers = { 'Content-Type': 'application/json', Authorization: `Bearer ${integration.token}` };
const headers = { 'Content-Type': 'application/json', Authorization: `Bearer ${this.token}` };
let result = await axios.get(urlServer, { headers });
result = await axios.get(result.data.url, { headers, responseType: 'arraybuffer' });
return result.data;
@@ -851,8 +847,6 @@ export class BusinessStartupService extends ChannelStartupService {
}
private async getIdMedia(mediaMessage: any) {
const integration = await this.findIntegration();
const formData = new FormData();
const fileBuffer = await fs.readFile(mediaMessage.media);
@@ -861,9 +855,9 @@ export class BusinessStartupService extends ChannelStartupService {
formData.append('file', fileBlob);
formData.append('typeFile', mediaMessage.mimetype);
formData.append('messaging_product', 'whatsapp');
const headers = { Authorization: `Bearer ${integration.token}` };
const headers = { Authorization: `Bearer ${this.token}` };
const res = await axios.post(
process.env.API_URL + '/' + process.env.VERSION + '/' + integration.number + '/media',
process.env.API_URL + '/' + process.env.VERSION + '/' + this.number + '/media',
formData,
{ headers },
);