File size: 3,165 Bytes
4327358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import * as process from 'node:process';

import { ExpressAdapter } from '@bull-board/express';
import { BullBoardModule } from '@bull-board/nestjs';
import { RedisModule } from '@liaoliaots/nestjs-redis';
import { BullModule } from '@nestjs/bullmq';
import { AppsController } from '@waha/apps/app_sdk/api/apps.controller';
import { BullAuthMiddleware } from '@waha/apps/app_sdk/auth';
import { AppsDisabledService } from '@waha/apps/app_sdk/services/AppsDisabledService';
import { AppsEnabledService } from '@waha/apps/app_sdk/services/AppsEnabledService';
import { AppsService } from '@waha/apps/app_sdk/services/IAppsService';
import { ChatwootLocalesController } from '@waha/apps/chatwoot/api/chatwoot.locales.controller';
import { ChatWootExports } from '@waha/apps/chatwoot/chatwoot.module';
import { parseBool } from '@waha/helpers';
import { RMutexModule } from '@waha/modules/rmutex';

const IMPORTS = [
  BullModule.forRoot({
    connection: {
      url: process.env.REDIS_URL || 'redis://:redis@localhost:6379',
      maxRetriesPerRequest: null,
    },
    prefix: `waha-${process.env.WAHA_WORKER_ID}`,
  }),
  RedisModule.forRoot({
    closeClient: true,
    config: {
      url: process.env.REDIS_URL || 'redis://:redis@localhost:6379',
      onClientCreated: async (client) => {
        try {
          await client.ping();
        } catch (err) {
          console.error('[Redis] Connection failed:', err);
          process.exit(1); // Stop the app if Redis is unavailable
        }
      },
    },
  }),
  RMutexModule,
  BullBoardModule.forRoot({
    route: '/jobs',
    adapter: ExpressAdapter,
    middleware: BullAuthMiddleware(),
    boardOptions: {
      uiConfig: {
        boardTitle: 'Jobs | WAHA',
        boardLogo: {
          path: '/dashboard/layout/images/logo-white.svg',
          width: 35,
          height: 35,
        },
        favIcon: {
          default: '/dashboard/favicon.ico',
          alternative: '/dashboard/favicon.ico',
        },
        miscLinks: [
          {
            text: '๐Ÿ“Š Dashboard',
            url: '/dashboard',
          },
          {
            text: '๐Ÿ“š Swagger (OpenAPI)',
            url: '/',
          },
        ],
      },
    },
  }),
  ...ChatWootExports.imports,
];

const AppsEnabled = {
  imports: IMPORTS,
  controllers: [AppsController, ...ChatWootExports.controllers],
  providers: [
    {
      provide: AppsService,
      useClass: AppsEnabledService,
    },
    ...ChatWootExports.providers,
  ],
};

const AppsDisabled = {
  providers: [
    {
      provide: AppsService,
      useClass: AppsDisabledService,
    },
  ],
  imports: [],
  controllers: [AppsController, ChatwootLocalesController],
};

function checkApiKey() {
  const key = process.env.WHATSAPP_API_KEY || process.env.WAHA_API_KEY;
  if (!key) {
    return;
  }
  const plain = process.env.WAHA_API_KEY_PLAIN;
  if (!plain) {
    throw Error(
      'WAHA_API_KEY set, please provide WAHA_API_KEY_PLAIN when WAHA_APPS_ENABLED',
    );
  }
}

const enabled = parseBool(process.env.WAHA_APPS_ENABLED);
if (enabled) {
  checkApiKey();
}
export const AppsModuleExports = enabled ? AppsEnabled : AppsDisabled;