diff --git a/src/api/controllers/label.controller.ts b/src/api/controllers/label.controller.ts index 2df112f70..953f55adb 100644 --- a/src/api/controllers/label.controller.ts +++ b/src/api/controllers/label.controller.ts @@ -12,4 +12,8 @@ export class LabelController { public async handleLabel({ instanceName }: InstanceDto, data: HandleLabelDto) { return await this.waMonitor.waInstances[instanceName].handleLabel(data); } -} + + public async syncLabels({ instanceName }: InstanceDto) { + return await this.waMonitor.waInstances[instanceName].syncLabels(); + } +} \ No newline at end of file diff --git a/src/api/integrations/channel/evolution/evolution.channel.service.ts b/src/api/integrations/channel/evolution/evolution.channel.service.ts index 87bea08e6..1be3a5a62 100644 --- a/src/api/integrations/channel/evolution/evolution.channel.service.ts +++ b/src/api/integrations/channel/evolution/evolution.channel.service.ts @@ -876,6 +876,9 @@ export class EvolutionStartupService extends ChannelStartupService { public async fetchLabels() { throw new BadRequestException('Method not available on Evolution Channel'); } + public async syncLabels() { + throw new BadRequestException('Method not available on Evolution Channel'); + } public async handleLabel() { throw new BadRequestException('Method not available on Evolution Channel'); } diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index 1e4808c15..20a44c5f5 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -1759,6 +1759,9 @@ export class BusinessStartupService extends ChannelStartupService { public async fetchLabels() { throw new BadRequestException('Method not available on WhatsApp Business API'); } + public async syncLabels() { + throw new BadRequestException('Method not available on WhatsApp Business API'); + } public async handleLabel() { throw new BadRequestException('Method not available on WhatsApp Business API'); } diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 60e857fcc..ff6e8c8ce 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1,4 +1,4 @@ -import { getCollectionsDto } from '@api/dto/business.dto'; +import { getCollectionsDto } from '@api/dto/business.dto'; import { OfferCallDto } from '@api/dto/call.dto'; import { ArchiveChatDto, @@ -4255,6 +4255,20 @@ export class BaileysStartupService extends ChannelStartupService { })); } + public async syncLabels(): Promise { + // Force Baileys to re-download label app state from WhatsApp (incremental) + // Using true for isLatest = incremental sync (safe, no disconnect) + // Using false would download full snapshot and may cause disconnection + await this.client.resyncAppState(['regular'], true); + + // Wait for LABELS_EDIT and LABELS_ASSOCIATION events to be processed + await new Promise((resolve) => setTimeout(resolve, 3000)); + + // Return the now-updated labels from database + return this.fetchLabels(); + } + + public async handleLabel(data: HandleLabelDto) { const whatsappContact = await this.whatsappNumber({ numbers: [data.number] }); if (whatsappContact.length === 0) { diff --git a/src/api/routes/label.router.ts b/src/api/routes/label.router.ts index bfb4a0859..4983def81 100644 --- a/src/api/routes/label.router.ts +++ b/src/api/routes/label.router.ts @@ -20,6 +20,16 @@ export class LabelRouter extends RouterBroker { return res.status(HttpStatus.OK).json(response); }) + .get(this.routerPath('syncLabels'), ...guards, async (req, res) => { + const response = await this.dataValidate({ + request: req, + schema: null, + ClassRef: LabelDto, + execute: (instance) => labelController.syncLabels(instance), + }); + + return res.status(HttpStatus.OK).json(response); + }) .post(this.routerPath('handleLabel'), ...guards, async (req, res) => { const response = await this.dataValidate({ request: req, @@ -33,4 +43,4 @@ export class LabelRouter extends RouterBroker { } public readonly router: Router = Router(); -} +} \ No newline at end of file