@hed-hog/core 0.0.349 → 0.0.351
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/access-log/access-log.controller.d.ts +16 -0
- package/dist/access-log/access-log.controller.d.ts.map +1 -0
- package/dist/access-log/access-log.controller.js +44 -0
- package/dist/access-log/access-log.controller.js.map +1 -0
- package/dist/access-log/access-log.interceptor.d.ts +9 -0
- package/dist/access-log/access-log.interceptor.d.ts.map +1 -0
- package/dist/access-log/access-log.interceptor.js +46 -0
- package/dist/access-log/access-log.interceptor.js.map +1 -0
- package/dist/access-log/access-log.module.d.ts +3 -0
- package/dist/access-log/access-log.module.d.ts.map +1 -0
- package/dist/access-log/access-log.module.js +29 -0
- package/dist/access-log/access-log.module.js.map +1 -0
- package/dist/access-log/access-log.service.d.ts +47 -0
- package/dist/access-log/access-log.service.d.ts.map +1 -0
- package/dist/access-log/access-log.service.js +113 -0
- package/dist/access-log/access-log.service.js.map +1 -0
- package/dist/access-log/dto/query-access-log.dto.d.ts +8 -0
- package/dist/access-log/dto/query-access-log.dto.d.ts.map +1 -0
- package/dist/access-log/dto/query-access-log.dto.js +42 -0
- package/dist/access-log/dto/query-access-log.dto.js.map +1 -0
- package/dist/auth/auth.controller.d.ts +12 -12
- package/dist/auth/auth.service.d.ts +27 -27
- package/dist/auth/auth.service.d.ts.map +1 -1
- package/dist/auth/auth.service.js +0 -4
- package/dist/auth/auth.service.js.map +1 -1
- package/dist/challenge/challenge.service.d.ts +2 -2
- package/dist/challenge/challenge.service.d.ts.map +1 -1
- package/dist/challenge/challenge.service.js +0 -1
- package/dist/challenge/challenge.service.js.map +1 -1
- package/dist/core/system.module.d.ts.map +1 -1
- package/dist/core/system.module.js +1 -0
- package/dist/core/system.module.js.map +1 -1
- package/dist/core.module.d.ts.map +1 -1
- package/dist/core.module.js +15 -0
- package/dist/core.module.js.map +1 -1
- package/dist/dashboard/dashboard-user/dashboard-user.controller.d.ts +3 -3
- package/dist/dashboard/dashboard-user/dashboard-user.service.d.ts +3 -3
- package/dist/dashboard/dashboard.module.d.ts.map +1 -1
- package/dist/dashboard/dashboard.module.js +4 -1
- package/dist/dashboard/dashboard.module.js.map +1 -1
- package/dist/file/file.controller.js +0 -1
- package/dist/file/file.controller.js.map +1 -1
- package/dist/file/file.service.d.ts +65 -0
- package/dist/file/file.service.d.ts.map +1 -1
- package/dist/file/file.service.js +151 -3
- package/dist/file/file.service.js.map +1 -1
- package/dist/file/provider/abstract.provider.d.ts +8 -0
- package/dist/file/provider/abstract.provider.d.ts.map +1 -1
- package/dist/file/provider/abstract.provider.js.map +1 -1
- package/dist/file/provider/azure.provider.d.ts +4 -0
- package/dist/file/provider/azure.provider.d.ts.map +1 -1
- package/dist/file/provider/azure.provider.js +14 -0
- package/dist/file/provider/azure.provider.js.map +1 -1
- package/dist/file/provider/gcs.provider.d.ts +4 -0
- package/dist/file/provider/gcs.provider.d.ts.map +1 -1
- package/dist/file/provider/gcs.provider.js +14 -0
- package/dist/file/provider/gcs.provider.js.map +1 -1
- package/dist/file/provider/local.provider.d.ts +4 -0
- package/dist/file/provider/local.provider.d.ts.map +1 -1
- package/dist/file/provider/local.provider.js +19 -0
- package/dist/file/provider/local.provider.js.map +1 -1
- package/dist/file/provider/provider.factory.d.ts.map +1 -1
- package/dist/file/provider/provider.factory.js +3 -0
- package/dist/file/provider/provider.factory.js.map +1 -1
- package/dist/file/provider/s3.provider.d.ts +4 -0
- package/dist/file/provider/s3.provider.d.ts.map +1 -1
- package/dist/file/provider/s3.provider.js +17 -0
- package/dist/file/provider/s3.provider.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/mcp/guards/mcp-auth.guard.d.ts +6 -1
- package/dist/mcp/guards/mcp-auth.guard.d.ts.map +1 -1
- package/dist/mcp/guards/mcp-auth.guard.js +29 -2
- package/dist/mcp/guards/mcp-auth.guard.js.map +1 -1
- package/dist/mcp/mcp-api-key.controller.d.ts +32 -0
- package/dist/mcp/mcp-api-key.controller.d.ts.map +1 -0
- package/dist/mcp/mcp-api-key.controller.js +64 -0
- package/dist/mcp/mcp-api-key.controller.js.map +1 -0
- package/dist/mcp/mcp-api-key.service.d.ts +27 -0
- package/dist/mcp/mcp-api-key.service.d.ts.map +1 -0
- package/dist/mcp/mcp-api-key.service.js +74 -0
- package/dist/mcp/mcp-api-key.service.js.map +1 -0
- package/dist/mcp/mcp-audit.service.d.ts +3 -1
- package/dist/mcp/mcp-audit.service.d.ts.map +1 -1
- package/dist/mcp/mcp-audit.service.js +14 -2
- package/dist/mcp/mcp-audit.service.js.map +1 -1
- package/dist/mcp/mcp-registry.service.d.ts +1 -2
- package/dist/mcp/mcp-registry.service.d.ts.map +1 -1
- package/dist/mcp/mcp-registry.service.js +6 -2
- package/dist/mcp/mcp-registry.service.js.map +1 -1
- package/dist/mcp/mcp.controller.d.ts +3 -1
- package/dist/mcp/mcp.controller.d.ts.map +1 -1
- package/dist/mcp/mcp.controller.js +10 -2
- package/dist/mcp/mcp.controller.js.map +1 -1
- package/dist/mcp/mcp.module.d.ts.map +1 -1
- package/dist/mcp/mcp.module.js +80 -1
- package/dist/mcp/mcp.module.js.map +1 -1
- package/dist/mcp/tools/core-ai.mcp-tools.d.ts +45 -0
- package/dist/mcp/tools/core-ai.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-ai.mcp-tools.js +214 -0
- package/dist/mcp/tools/core-ai.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-dashboard-items.mcp-tools.d.ts +21 -0
- package/dist/mcp/tools/core-dashboard-items.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-dashboard-items.mcp-tools.js +89 -0
- package/dist/mcp/tools/core-dashboard-items.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-dashboard.mcp-tools.d.ts +29 -0
- package/dist/mcp/tools/core-dashboard.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-dashboard.mcp-tools.js +149 -0
- package/dist/mcp/tools/core-dashboard.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-event-webhooks.mcp-tools.d.ts +30 -0
- package/dist/mcp/tools/core-event-webhooks.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-event-webhooks.mcp-tools.js +185 -0
- package/dist/mcp/tools/core-event-webhooks.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-files.mcp-tools.d.ts +26 -0
- package/dist/mcp/tools/core-files.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-files.mcp-tools.js +140 -0
- package/dist/mcp/tools/core-files.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-integration-events.mcp-tools.d.ts +26 -0
- package/dist/mcp/tools/core-integration-events.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-integration-events.mcp-tools.js +168 -0
- package/dist/mcp/tools/core-integration-events.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-mail-profile.mcp-tools.d.ts +32 -0
- package/dist/mcp/tools/core-mail-profile.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-mail-profile.mcp-tools.js +178 -0
- package/dist/mcp/tools/core-mail-profile.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-mail-sent.mcp-tools.d.ts +14 -0
- package/dist/mcp/tools/core-mail-sent.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-mail-sent.mcp-tools.js +106 -0
- package/dist/mcp/tools/core-mail-sent.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-mail.mcp-tools.d.ts +36 -0
- package/dist/mcp/tools/core-mail.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-mail.mcp-tools.js +184 -0
- package/dist/mcp/tools/core-mail.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-mcp-chat.mcp-tools.d.ts +21 -0
- package/dist/mcp/tools/core-mcp-chat.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-mcp-chat.mcp-tools.js +118 -0
- package/dist/mcp/tools/core-mcp-chat.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-menus.mcp-tools.d.ts +40 -0
- package/dist/mcp/tools/core-menus.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-menus.mcp-tools.js +268 -0
- package/dist/mcp/tools/core-menus.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-notifications.mcp-tools.d.ts +31 -0
- package/dist/mcp/tools/core-notifications.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-notifications.mcp-tools.js +178 -0
- package/dist/mcp/tools/core-notifications.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-profile.mcp-tools.d.ts +20 -0
- package/dist/mcp/tools/core-profile.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-profile.mcp-tools.js +111 -0
- package/dist/mcp/tools/core-profile.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-roles.mcp-tools.d.ts +70 -0
- package/dist/mcp/tools/core-roles.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-roles.mcp-tools.js +355 -0
- package/dist/mcp/tools/core-roles.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-routes.mcp-tools.d.ts +52 -0
- package/dist/mcp/tools/core-routes.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-routes.mcp-tools.js +254 -0
- package/dist/mcp/tools/core-routes.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-screens.mcp-tools.d.ts +47 -0
- package/dist/mcp/tools/core-screens.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-screens.mcp-tools.js +250 -0
- package/dist/mcp/tools/core-screens.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-sessions.mcp-tools.d.ts +20 -0
- package/dist/mcp/tools/core-sessions.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-sessions.mcp-tools.js +128 -0
- package/dist/mcp/tools/core-sessions.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-settings.mcp-tools.d.ts +25 -0
- package/dist/mcp/tools/core-settings.mcp-tools.d.ts.map +1 -1
- package/dist/mcp/tools/core-settings.mcp-tools.js +138 -0
- package/dist/mcp/tools/core-settings.mcp-tools.js.map +1 -1
- package/dist/mcp/tools/core-system.mcp-tools.d.ts +8 -0
- package/dist/mcp/tools/core-system.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-system.mcp-tools.js +40 -0
- package/dist/mcp/tools/core-system.mcp-tools.js.map +1 -0
- package/dist/mcp/tools/core-webhooks.mcp-tools.d.ts +46 -0
- package/dist/mcp/tools/core-webhooks.mcp-tools.d.ts.map +1 -0
- package/dist/mcp/tools/core-webhooks.mcp-tools.js +266 -0
- package/dist/mcp/tools/core-webhooks.mcp-tools.js.map +1 -0
- package/dist/mcp-chat/mcp-chat.controller.d.ts +5 -1
- package/dist/mcp-chat/mcp-chat.controller.d.ts.map +1 -1
- package/dist/mcp-chat/mcp-chat.controller.js +46 -0
- package/dist/mcp-chat/mcp-chat.controller.js.map +1 -1
- package/dist/mcp-chat/mcp-chat.service.d.ts +31 -3
- package/dist/mcp-chat/mcp-chat.service.d.ts.map +1 -1
- package/dist/mcp-chat/mcp-chat.service.js +776 -38
- package/dist/mcp-chat/mcp-chat.service.js.map +1 -1
- package/dist/notification/dto/create-notification.dto.d.ts +26 -0
- package/dist/notification/dto/create-notification.dto.d.ts.map +1 -0
- package/dist/notification/dto/create-notification.dto.js +86 -0
- package/dist/notification/dto/create-notification.dto.js.map +1 -0
- package/dist/notification/dto/update-progress.dto.d.ts +6 -0
- package/dist/notification/dto/update-progress.dto.d.ts.map +1 -0
- package/dist/notification/dto/update-progress.dto.js +33 -0
- package/dist/notification/dto/update-progress.dto.js.map +1 -0
- package/dist/notification/notification.controller.d.ts +112 -0
- package/dist/notification/notification.controller.d.ts.map +1 -0
- package/dist/notification/notification.controller.js +130 -0
- package/dist/notification/notification.controller.js.map +1 -0
- package/dist/notification/notification.module.d.ts +3 -0
- package/dist/notification/notification.module.d.ts.map +1 -0
- package/dist/notification/notification.module.js +29 -0
- package/dist/notification/notification.module.js.map +1 -0
- package/dist/notification/notification.service.d.ts +112 -0
- package/dist/notification/notification.service.d.ts.map +1 -0
- package/dist/notification/notification.service.js +269 -0
- package/dist/notification/notification.service.js.map +1 -0
- package/dist/pdf/pdf.service.js +1 -1
- package/dist/pdf/pdf.service.js.map +1 -1
- package/dist/profile/profile.controller.d.ts +4 -4
- package/dist/profile/profile.controller.js +0 -1
- package/dist/profile/profile.controller.js.map +1 -1
- package/dist/profile/profile.module.d.ts.map +1 -1
- package/dist/profile/profile.module.js +1 -0
- package/dist/profile/profile.module.js.map +1 -1
- package/dist/profile/profile.service.d.ts +4 -4
- package/dist/session/session.controller.d.ts +3 -3
- package/dist/session/session.service.d.ts +9 -9
- package/dist/setting/dto/update-setting-list.dto.d.ts +9 -0
- package/dist/setting/dto/update-setting-list.dto.d.ts.map +1 -0
- package/dist/setting/dto/update-setting-list.dto.js +44 -0
- package/dist/setting/dto/update-setting-list.dto.js.map +1 -0
- package/dist/setting/setting.controller.d.ts +20 -1
- package/dist/setting/setting.controller.d.ts.map +1 -1
- package/dist/setting/setting.controller.js +12 -0
- package/dist/setting/setting.controller.js.map +1 -1
- package/dist/setting/setting.service.d.ts +20 -1
- package/dist/setting/setting.service.d.ts.map +1 -1
- package/dist/setting/setting.service.js +80 -4
- package/dist/setting/setting.service.js.map +1 -1
- package/dist/task/task.service.d.ts +13 -0
- package/dist/task/task.service.d.ts.map +1 -1
- package/dist/task/task.service.js +85 -0
- package/dist/task/task.service.js.map +1 -1
- package/dist/token/token.service.d.ts.map +1 -1
- package/dist/token/token.service.js +0 -1
- package/dist/token/token.service.js.map +1 -1
- package/dist/user/user.controller.d.ts +11 -11
- package/dist/user/user.service.d.ts +27 -27
- package/dist/webhook-integration/dto/webhook-action.dto.d.ts +22 -0
- package/dist/webhook-integration/dto/webhook-action.dto.d.ts.map +1 -0
- package/dist/webhook-integration/dto/webhook-action.dto.js +120 -0
- package/dist/webhook-integration/dto/webhook-action.dto.js.map +1 -0
- package/dist/webhook-integration/webhook-integration.controller.d.ts +7 -5
- package/dist/webhook-integration/webhook-integration.controller.d.ts.map +1 -1
- package/dist/webhook-integration/webhook-integration.controller.js +18 -5
- package/dist/webhook-integration/webhook-integration.controller.js.map +1 -1
- package/dist/webhook-integration/webhook-integration.service.d.ts +18 -5
- package/dist/webhook-integration/webhook-integration.service.d.ts.map +1 -1
- package/dist/webhook-integration/webhook-integration.service.js +342 -59
- package/dist/webhook-integration/webhook-integration.service.js.map +1 -1
- package/dist/webhook-integration/webhook-utils.d.ts +1 -0
- package/dist/webhook-integration/webhook-utils.d.ts.map +1 -1
- package/dist/webhook-integration/webhook-utils.js +38 -0
- package/dist/webhook-integration/webhook-utils.js.map +1 -1
- package/hedhog/data/menu.yaml +63 -4
- package/hedhog/data/route.yaml +1437 -0
- package/hedhog/data/setting_group.yaml +112 -0
- package/hedhog/data/setting_subgroup.yaml +32 -0
- package/hedhog/frontend/app/access-log/page.tsx.ejs +772 -0
- package/hedhog/frontend/app/account/components/mcp-tokens.tsx.ejs +285 -0
- package/hedhog/frontend/app/account/layout.tsx.ejs +32 -55
- package/hedhog/frontend/app/account/mcp-tokens/page.tsx.ejs +5 -0
- package/hedhog/frontend/app/ai_agent/page.tsx.ejs +10 -3
- package/hedhog/frontend/app/dashboard/dashboard-home-tabs.tsx.ejs +33 -13
- package/hedhog/frontend/app/dashboard/management/tabs/components-tab.tsx.ejs +12 -5
- package/hedhog/frontend/app/dashboard/management/tabs/dashboards-tab.tsx.ejs +12 -5
- package/hedhog/frontend/app/integrations-webhooks/page.tsx.ejs +3559 -1835
- package/hedhog/frontend/app/mcp_chat/chat-components.tsx.ejs +633 -0
- package/hedhog/frontend/app/mcp_chat/chat-types.ts.ejs +16 -0
- package/hedhog/frontend/app/mcp_chat/page.tsx.ejs +583 -384
- package/hedhog/frontend/app/menu/page.tsx.ejs +9 -3
- package/hedhog/frontend/app/roles/page.tsx.ejs +18 -6
- package/hedhog/frontend/app/routes/page.tsx.ejs +9 -3
- package/hedhog/frontend/app/setting-list/page.tsx.ejs +822 -0
- package/hedhog/frontend/app/users/page.tsx.ejs +445 -203
- package/hedhog/frontend/app/users/user-access-sheet.tsx.ejs +310 -304
- package/hedhog/frontend/app/users/user-sheet.tsx.ejs +41 -44
- package/hedhog/frontend/messages/en.json +343 -3
- package/hedhog/frontend/messages/pt.json +343 -3
- package/hedhog/table/access_log.yaml +31 -0
- package/hedhog/table/mcp_api_key.yaml +30 -0
- package/hedhog/table/notification.yaml +72 -0
- package/hedhog/table/webhook_action_log.yaml +49 -0
- package/package.json +5 -4
- package/src/access-log/access-log.controller.ts +29 -0
- package/src/access-log/access-log.interceptor.ts +38 -0
- package/src/access-log/access-log.module.ts +16 -0
- package/src/access-log/access-log.service.ts +129 -0
- package/src/access-log/dto/query-access-log.dto.ts +23 -0
- package/src/auth/auth.service.ts +0 -7
- package/src/challenge/challenge.service.ts +0 -2
- package/src/core/system.module.ts +1 -0
- package/src/core.module.ts +15 -0
- package/src/dashboard/dashboard.module.ts +4 -1
- package/src/file/file.controller.ts +14 -14
- package/src/file/file.service.ts +198 -4
- package/src/file/provider/abstract.provider.ts +10 -0
- package/src/file/provider/azure.provider.ts +24 -0
- package/src/file/provider/gcs.provider.ts +20 -0
- package/src/file/provider/local.provider.ts +30 -2
- package/src/file/provider/provider.factory.ts +3 -0
- package/src/file/provider/s3.provider.ts +22 -0
- package/src/index.ts +5 -0
- package/src/mcp/guards/mcp-auth.guard.ts +64 -32
- package/src/mcp/mcp-api-key.controller.ts +42 -0
- package/src/mcp/mcp-api-key.service.ts +66 -0
- package/src/mcp/mcp-audit.service.ts +47 -36
- package/src/mcp/mcp-registry.service.ts +75 -69
- package/src/mcp/mcp.controller.ts +40 -30
- package/src/mcp/mcp.module.ts +115 -36
- package/src/mcp/tools/core-ai.mcp-tools.ts +156 -0
- package/src/mcp/tools/core-dashboard-items.mcp-tools.ts +63 -0
- package/src/mcp/tools/core-dashboard.mcp-tools.ts +103 -0
- package/src/mcp/tools/core-event-webhooks.mcp-tools.ts +134 -0
- package/src/mcp/tools/core-files.mcp-tools.ts +105 -0
- package/src/mcp/tools/core-integration-events.mcp-tools.ts +118 -0
- package/src/mcp/tools/core-mail-profile.mcp-tools.ts +128 -0
- package/src/mcp/tools/core-mail-sent.mcp-tools.ts +76 -0
- package/src/mcp/tools/core-mail.mcp-tools.ts +130 -0
- package/src/mcp/tools/core-mcp-chat.mcp-tools.ts +81 -0
- package/src/mcp/tools/core-menus.mcp-tools.ts +197 -0
- package/src/mcp/tools/core-notifications.mcp-tools.ts +128 -0
- package/src/mcp/tools/core-profile.mcp-tools.ts +74 -0
- package/src/mcp/tools/core-roles.mcp-tools.ts +288 -0
- package/src/mcp/tools/core-routes.mcp-tools.ts +196 -0
- package/src/mcp/tools/core-screens.mcp-tools.ts +186 -0
- package/src/mcp/tools/core-sessions.mcp-tools.ts +97 -0
- package/src/mcp/tools/core-settings.mcp-tools.ts +171 -53
- package/src/mcp/tools/core-system.mcp-tools.ts +19 -0
- package/src/mcp/tools/core-webhooks.mcp-tools.ts +199 -0
- package/src/mcp-chat/mcp-chat.controller.ts +113 -60
- package/src/mcp-chat/mcp-chat.service.ts +1343 -332
- package/src/notification/dto/create-notification.dto.ts +71 -0
- package/src/notification/dto/update-progress.dto.ts +16 -0
- package/src/notification/notification.controller.ts +83 -0
- package/src/notification/notification.module.ts +16 -0
- package/src/notification/notification.service.ts +324 -0
- package/src/pdf/pdf.service.ts +1 -1
- package/src/profile/profile.controller.ts +1 -1
- package/src/profile/profile.module.ts +1 -0
- package/src/setting/dto/update-setting-list.dto.ts +32 -0
- package/src/setting/setting.controller.ts +9 -0
- package/src/setting/setting.service.ts +99 -11
- package/src/task/task.service.ts +103 -0
- package/src/token/token.service.ts +0 -2
- package/src/webhook-integration/dto/webhook-action.dto.ts +96 -0
- package/src/webhook-integration/webhook-integration.controller.ts +124 -103
- package/src/webhook-integration/webhook-integration.service.ts +1137 -735
- package/src/webhook-integration/webhook-utils.ts +136 -79
|
@@ -1,735 +1,1137 @@
|
|
|
1
|
-
import { DeleteDTO } from '@hed-hog/api';
|
|
2
|
-
import { MailService as MailMainService } from '@hed-hog/api-mail';
|
|
3
|
-
import { PaginationDTO, PaginationService } from '@hed-hog/api-pagination';
|
|
4
|
-
import { PrismaService } from '@hed-hog/api-prisma';
|
|
5
|
-
import { HttpService } from '@nestjs/axios';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
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
|
-
return
|
|
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
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const record = await this.integrations.
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
return
|
|
201
|
-
...record,
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
await this.
|
|
219
|
-
return
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
return this.actions.
|
|
244
|
-
where: {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
)
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
return
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
)
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
1
|
+
import { DeleteDTO } from '@hed-hog/api';
|
|
2
|
+
import { MailService as MailMainService } from '@hed-hog/api-mail';
|
|
3
|
+
import { PaginationDTO, PaginationService } from '@hed-hog/api-pagination';
|
|
4
|
+
import { PrismaService } from '@hed-hog/api-prisma';
|
|
5
|
+
import { HttpService } from '@nestjs/axios';
|
|
6
|
+
import {
|
|
7
|
+
BadRequestException,
|
|
8
|
+
HttpException,
|
|
9
|
+
Injectable,
|
|
10
|
+
NotFoundException,
|
|
11
|
+
} from '@nestjs/common';
|
|
12
|
+
import { EventEmitter } from 'events';
|
|
13
|
+
import { Request, Response } from 'express';
|
|
14
|
+
import { firstValueFrom } from 'rxjs';
|
|
15
|
+
import { MailService } from '../mail/mail.service';
|
|
16
|
+
import { SettingService } from '../setting/setting.service';
|
|
17
|
+
import {
|
|
18
|
+
CreateWebhookActionDTO,
|
|
19
|
+
UpdateWebhookActionDTO,
|
|
20
|
+
} from './dto/webhook-action.dto';
|
|
21
|
+
import {
|
|
22
|
+
createPlainToken,
|
|
23
|
+
createPublicUuid,
|
|
24
|
+
hashToken,
|
|
25
|
+
normalizeJsonObject,
|
|
26
|
+
renderTemplate,
|
|
27
|
+
sanitizeSensitiveData,
|
|
28
|
+
summarizePayload,
|
|
29
|
+
} from './webhook-utils';
|
|
30
|
+
|
|
31
|
+
type ActionExecutionLog = {
|
|
32
|
+
webhookActionId: number;
|
|
33
|
+
actionName: string;
|
|
34
|
+
actionType: 'email' | 'whatsapp_evolution';
|
|
35
|
+
status: 'success' | 'failed';
|
|
36
|
+
durationMs: number;
|
|
37
|
+
requestPayload?: unknown;
|
|
38
|
+
responsePayload?: unknown;
|
|
39
|
+
errorMessage?: string | null;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
type WebhookActionPayload =
|
|
43
|
+
| CreateWebhookActionDTO
|
|
44
|
+
| UpdateWebhookActionDTO
|
|
45
|
+
| Record<string, unknown>;
|
|
46
|
+
|
|
47
|
+
@Injectable()
|
|
48
|
+
export class WebhookIntegrationService {
|
|
49
|
+
private readonly logEmitter = new EventEmitter();
|
|
50
|
+
|
|
51
|
+
constructor(
|
|
52
|
+
private readonly prisma: PrismaService,
|
|
53
|
+
private readonly pagination: PaginationService,
|
|
54
|
+
private readonly http: HttpService,
|
|
55
|
+
private readonly mailMainService: MailMainService,
|
|
56
|
+
private readonly coreMailService: MailService,
|
|
57
|
+
private readonly settingService: SettingService,
|
|
58
|
+
) {}
|
|
59
|
+
|
|
60
|
+
private get integrations() {
|
|
61
|
+
return (this.prisma as any).webhook_integration;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private get actions() {
|
|
65
|
+
return (this.prisma as any).webhook_action;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private get logs() {
|
|
69
|
+
return (this.prisma as any).webhook_call_log;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private get actionLogs() {
|
|
73
|
+
return (this.prisma as any).webhook_action_log;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async list(paginationParams: PaginationDTO) {
|
|
77
|
+
const fields = ['slug', 'name', 'description', 'public_uuid', 'status'];
|
|
78
|
+
const OR = this.prisma.createInsensitiveSearch(fields, paginationParams);
|
|
79
|
+
|
|
80
|
+
const result = await this.pagination.paginate(
|
|
81
|
+
this.integrations,
|
|
82
|
+
paginationParams,
|
|
83
|
+
{
|
|
84
|
+
where: OR.length > 0 ? { OR } : {},
|
|
85
|
+
include: {
|
|
86
|
+
webhook_action: {
|
|
87
|
+
orderBy: [{ order: 'asc' }, { id: 'asc' }],
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
orderBy: [{ updated_at: 'desc' }, { id: 'desc' }],
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
...result,
|
|
96
|
+
data: await this.withPublicUrls(result.data || []),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async get(id: number) {
|
|
101
|
+
const record = await this.integrations.findUnique({
|
|
102
|
+
where: { id },
|
|
103
|
+
include: {
|
|
104
|
+
webhook_action: {
|
|
105
|
+
orderBy: [{ order: 'asc' }, { id: 'asc' }],
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
if (!record) {
|
|
111
|
+
throw new NotFoundException(
|
|
112
|
+
`Webhook integration with id ${id} not found.`,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return this.withPublicUrl(record);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async create(data: any) {
|
|
120
|
+
await this.ensureSlugAvailable(data.slug);
|
|
121
|
+
|
|
122
|
+
let plainToken: string | null = null;
|
|
123
|
+
if (data.require_token) {
|
|
124
|
+
plainToken = createPlainToken();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const record = await this.integrations.create({
|
|
128
|
+
data: {
|
|
129
|
+
...this.normalizeIntegration(data),
|
|
130
|
+
public_uuid: createPublicUuid(),
|
|
131
|
+
token_hash: plainToken ? hashToken(plainToken) : null,
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
...(await this.withPublicUrl(record)),
|
|
137
|
+
plainToken,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async update(id: number, data: any) {
|
|
142
|
+
const existing = await this.get(id);
|
|
143
|
+
if (data.slug && data.slug !== existing.slug) {
|
|
144
|
+
await this.ensureSlugAvailable(data.slug);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let plainToken: string | null = null;
|
|
148
|
+
const updateData = this.normalizeIntegration(data, true);
|
|
149
|
+
if (data.require_token === false) {
|
|
150
|
+
updateData.token_hash = null;
|
|
151
|
+
} else if (data.require_token === true && !existing.token_hash) {
|
|
152
|
+
plainToken = createPlainToken();
|
|
153
|
+
updateData.token_hash = hashToken(plainToken);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const record = await this.integrations.update({
|
|
157
|
+
where: { id },
|
|
158
|
+
data: updateData,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
...(await this.withPublicUrl(record)),
|
|
163
|
+
plainToken,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async delete(data: DeleteDTO) {
|
|
168
|
+
if (!data.ids?.length) {
|
|
169
|
+
throw new BadRequestException(
|
|
170
|
+
'You must select at least one item to delete.',
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return this.integrations.deleteMany({
|
|
175
|
+
where: { id: { in: data.ids } },
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async regenerateUuid(id: number) {
|
|
180
|
+
await this.get(id);
|
|
181
|
+
const record = await this.integrations.update({
|
|
182
|
+
where: { id },
|
|
183
|
+
data: { public_uuid: createPublicUuid() },
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
return this.withPublicUrl(record);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async regenerateToken(id: number) {
|
|
190
|
+
await this.get(id);
|
|
191
|
+
const plainToken = createPlainToken();
|
|
192
|
+
const record = await this.integrations.update({
|
|
193
|
+
where: { id },
|
|
194
|
+
data: {
|
|
195
|
+
require_token: true,
|
|
196
|
+
token_hash: hashToken(plainToken),
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
...(await this.withPublicUrl(record)),
|
|
202
|
+
plainToken,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private async getApplicationBaseUrl() {
|
|
207
|
+
const settings = await this.settingService.getSettingValues([
|
|
208
|
+
'api-url',
|
|
209
|
+
'url',
|
|
210
|
+
]);
|
|
211
|
+
const value = settings?.['api-url'] || settings?.url;
|
|
212
|
+
return typeof value === 'string' ? value.replace(/\/+$/, '') : '';
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private async withPublicUrl<T extends { public_uuid?: string | null }>(
|
|
216
|
+
record: T,
|
|
217
|
+
) {
|
|
218
|
+
const baseUrl = await this.getApplicationBaseUrl();
|
|
219
|
+
return {
|
|
220
|
+
...record,
|
|
221
|
+
public_url:
|
|
222
|
+
baseUrl && record.public_uuid
|
|
223
|
+
? `${baseUrl}/webhook/${record.public_uuid}`
|
|
224
|
+
: null,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private async withPublicUrls<T extends { public_uuid?: string | null }>(
|
|
229
|
+
records: T[],
|
|
230
|
+
) {
|
|
231
|
+
const baseUrl = await this.getApplicationBaseUrl();
|
|
232
|
+
return records.map((record) => ({
|
|
233
|
+
...record,
|
|
234
|
+
public_url:
|
|
235
|
+
baseUrl && record.public_uuid
|
|
236
|
+
? `${baseUrl}/webhook/${record.public_uuid}`
|
|
237
|
+
: null,
|
|
238
|
+
}));
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async listActions(integrationId: number) {
|
|
242
|
+
await this.get(integrationId);
|
|
243
|
+
return this.actions.findMany({
|
|
244
|
+
where: { webhook_integration_id: integrationId },
|
|
245
|
+
orderBy: [{ order: 'asc' }, { id: 'asc' }],
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async createAction(integrationId: number, data: WebhookActionPayload) {
|
|
250
|
+
await this.get(integrationId);
|
|
251
|
+
const normalized = this.normalizeAction(data);
|
|
252
|
+
this.validateActionConfiguration(normalized);
|
|
253
|
+
|
|
254
|
+
return this.actions.create({
|
|
255
|
+
data: {
|
|
256
|
+
...normalized,
|
|
257
|
+
webhook_integration_id: integrationId,
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async updateAction(
|
|
263
|
+
integrationId: number,
|
|
264
|
+
actionId: number,
|
|
265
|
+
data: WebhookActionPayload,
|
|
266
|
+
) {
|
|
267
|
+
const existing = await this.ensureAction(integrationId, actionId);
|
|
268
|
+
const normalized = this.normalizeAction(data, true);
|
|
269
|
+
this.validateActionConfiguration({
|
|
270
|
+
...existing,
|
|
271
|
+
...normalized,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
return this.actions.update({
|
|
275
|
+
where: { id: actionId },
|
|
276
|
+
data: normalized,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async deleteActions(integrationId: number, data: DeleteDTO) {
|
|
281
|
+
await this.get(integrationId);
|
|
282
|
+
if (!data.ids?.length) {
|
|
283
|
+
throw new BadRequestException(
|
|
284
|
+
'You must select at least one item to delete.',
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return this.actions.deleteMany({
|
|
289
|
+
where: {
|
|
290
|
+
webhook_integration_id: integrationId,
|
|
291
|
+
id: { in: data.ids },
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async listLogs(
|
|
297
|
+
integrationId: number,
|
|
298
|
+
paginationParams: PaginationDTO,
|
|
299
|
+
sort: 'asc' | 'desc' = 'desc',
|
|
300
|
+
) {
|
|
301
|
+
await this.get(integrationId);
|
|
302
|
+
|
|
303
|
+
const page = Math.max(Number(paginationParams.page || 1), 1);
|
|
304
|
+
const pageSize = Math.max(Number(paginationParams.pageSize || 10), 1);
|
|
305
|
+
const skip = (page - 1) * pageSize;
|
|
306
|
+
const normalizedSort = sort === 'asc' ? 'asc' : 'desc';
|
|
307
|
+
|
|
308
|
+
const where = { webhook_integration_id: integrationId };
|
|
309
|
+
|
|
310
|
+
const [data, total] = await Promise.all([
|
|
311
|
+
this.logs.findMany({
|
|
312
|
+
where,
|
|
313
|
+
orderBy: [{ created_at: normalizedSort }, { id: normalizedSort }],
|
|
314
|
+
skip,
|
|
315
|
+
take: pageSize,
|
|
316
|
+
}),
|
|
317
|
+
this.logs.count({ where }),
|
|
318
|
+
]);
|
|
319
|
+
|
|
320
|
+
const lastPage = Math.max(1, Math.ceil(total / pageSize));
|
|
321
|
+
const result = {
|
|
322
|
+
total,
|
|
323
|
+
lastPage,
|
|
324
|
+
page,
|
|
325
|
+
pageSize,
|
|
326
|
+
prev: page > 1 ? page - 1 : null,
|
|
327
|
+
next: page < lastPage ? page + 1 : null,
|
|
328
|
+
data,
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
const logIds = (result.data || [])
|
|
332
|
+
.map((item: any) => item.id)
|
|
333
|
+
.filter((id: unknown) => typeof id === 'number');
|
|
334
|
+
|
|
335
|
+
if (!logIds.length || !this.actionLogs) {
|
|
336
|
+
return result;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const actionLogs = await this.actionLogs.findMany({
|
|
340
|
+
where: {
|
|
341
|
+
webhook_call_log_id: { in: logIds },
|
|
342
|
+
},
|
|
343
|
+
orderBy: [{ created_at: 'asc' }, { id: 'asc' }],
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
const actionLogsByCallId = new Map<number, any[]>();
|
|
347
|
+
for (const actionLog of actionLogs || []) {
|
|
348
|
+
const list = actionLogsByCallId.get(actionLog.webhook_call_log_id) || [];
|
|
349
|
+
list.push(actionLog);
|
|
350
|
+
actionLogsByCallId.set(actionLog.webhook_call_log_id, list);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return {
|
|
354
|
+
...result,
|
|
355
|
+
data: (result.data || []).map((item: any) => ({
|
|
356
|
+
...item,
|
|
357
|
+
webhook_action_log: actionLogsByCallId.get(item.id) || [],
|
|
358
|
+
})),
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
streamLogs(integrationId: number, res: Response) {
|
|
363
|
+
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
|
|
364
|
+
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
|
365
|
+
res.setHeader('Connection', 'keep-alive');
|
|
366
|
+
res.setHeader('X-Accel-Buffering', 'no');
|
|
367
|
+
res.flushHeaders();
|
|
368
|
+
|
|
369
|
+
res.write(`data: ${JSON.stringify({ type: 'connected' })}\n\n`);
|
|
370
|
+
|
|
371
|
+
const handler = (payload: unknown) => {
|
|
372
|
+
res.write(`data: ${JSON.stringify({ type: 'log', payload })}\n\n`);
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
const eventKey = `log:${integrationId}`;
|
|
376
|
+
this.logEmitter.on(eventKey, handler);
|
|
377
|
+
|
|
378
|
+
res.on('close', () => {
|
|
379
|
+
this.logEmitter.off(eventKey, handler);
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
async executePublic(uuid: string, body: any, headers: any, req: Request) {
|
|
384
|
+
const startedAt = Date.now();
|
|
385
|
+
const remoteIp = this.getRemoteIp(req);
|
|
386
|
+
const actionLogs: ActionExecutionLog[] = [];
|
|
387
|
+
let integration: any | null = null;
|
|
388
|
+
|
|
389
|
+
try {
|
|
390
|
+
integration = await this.integrations.findUnique({
|
|
391
|
+
where: { public_uuid: uuid },
|
|
392
|
+
include: {
|
|
393
|
+
webhook_action: {
|
|
394
|
+
where: { status: 'active' },
|
|
395
|
+
orderBy: [{ order: 'asc' }, { id: 'asc' }],
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
if (!integration || integration.status !== 'active') {
|
|
401
|
+
throw new NotFoundException('Webhook not found.');
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
this.validateToken(integration, headers);
|
|
405
|
+
this.validateIp(integration, remoteIp);
|
|
406
|
+
this.validateBodySchema(integration, body);
|
|
407
|
+
|
|
408
|
+
const actionResults = [];
|
|
409
|
+
for (const action of integration.webhook_action || []) {
|
|
410
|
+
const actionStartedAt = Date.now();
|
|
411
|
+
|
|
412
|
+
try {
|
|
413
|
+
const result = await this.executeAction(action, body);
|
|
414
|
+
const durationMs = Date.now() - actionStartedAt;
|
|
415
|
+
|
|
416
|
+
actionResults.push({
|
|
417
|
+
id: action.id,
|
|
418
|
+
type: action.type,
|
|
419
|
+
success: true,
|
|
420
|
+
duration_ms: durationMs,
|
|
421
|
+
response: summarizePayload(result.responsePayload),
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
actionLogs.push({
|
|
425
|
+
webhookActionId: action.id,
|
|
426
|
+
actionName: action.name,
|
|
427
|
+
actionType: action.type,
|
|
428
|
+
status: 'success',
|
|
429
|
+
durationMs,
|
|
430
|
+
requestPayload: result.requestPayload,
|
|
431
|
+
responsePayload: result.responsePayload,
|
|
432
|
+
});
|
|
433
|
+
} catch (error) {
|
|
434
|
+
const durationMs = Date.now() - actionStartedAt;
|
|
435
|
+
const errorMessage = this.extractErrorMessage(error);
|
|
436
|
+
|
|
437
|
+
actionResults.push({
|
|
438
|
+
id: action.id,
|
|
439
|
+
type: action.type,
|
|
440
|
+
success: false,
|
|
441
|
+
duration_ms: durationMs,
|
|
442
|
+
error: errorMessage,
|
|
443
|
+
response: summarizePayload(this.extractErrorResponse(error)),
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
actionLogs.push({
|
|
447
|
+
webhookActionId: action.id,
|
|
448
|
+
actionName: action.name,
|
|
449
|
+
actionType: action.type,
|
|
450
|
+
status: 'failed',
|
|
451
|
+
durationMs,
|
|
452
|
+
requestPayload: this.extractErrorRequestPayload(error),
|
|
453
|
+
errorMessage,
|
|
454
|
+
responsePayload: this.extractErrorResponse(error),
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
throw error;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const response = {
|
|
462
|
+
success: true,
|
|
463
|
+
actions: actionResults,
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
const callLog = await this.createCallLog({
|
|
467
|
+
integration,
|
|
468
|
+
uuid,
|
|
469
|
+
status: 'success',
|
|
470
|
+
remoteIp,
|
|
471
|
+
durationMs: Date.now() - startedAt,
|
|
472
|
+
payload: body,
|
|
473
|
+
response,
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
await this.createActionLogs(callLog?.id, actionLogs);
|
|
477
|
+
|
|
478
|
+
if (integration?.id && callLog?.id) {
|
|
479
|
+
this.logEmitter.emit(`log:${integration.id}`, { id: callLog.id });
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
return response;
|
|
483
|
+
} catch (error) {
|
|
484
|
+
const callLog = await this.createCallLog({
|
|
485
|
+
integration,
|
|
486
|
+
uuid,
|
|
487
|
+
status: 'failed',
|
|
488
|
+
remoteIp,
|
|
489
|
+
durationMs: Date.now() - startedAt,
|
|
490
|
+
payload: body,
|
|
491
|
+
response: {
|
|
492
|
+
success: false,
|
|
493
|
+
actions: actionLogs.map((item) => ({
|
|
494
|
+
id: item.webhookActionId,
|
|
495
|
+
type: item.actionType,
|
|
496
|
+
success: item.status === 'success',
|
|
497
|
+
duration_ms: item.durationMs,
|
|
498
|
+
error: item.errorMessage || null,
|
|
499
|
+
})),
|
|
500
|
+
},
|
|
501
|
+
error,
|
|
502
|
+
}).catch(() => null);
|
|
503
|
+
|
|
504
|
+
await this.createActionLogs(callLog?.id, actionLogs).catch(() => undefined);
|
|
505
|
+
|
|
506
|
+
if (integration?.id && callLog?.id) {
|
|
507
|
+
this.logEmitter.emit(`log:${integration.id}`, { id: callLog.id });
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
if (error instanceof HttpException) {
|
|
511
|
+
throw error;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
return {
|
|
515
|
+
success: false,
|
|
516
|
+
actions: actionLogs.map((item) => ({
|
|
517
|
+
id: item.webhookActionId,
|
|
518
|
+
type: item.actionType,
|
|
519
|
+
success: item.status === 'success',
|
|
520
|
+
duration_ms: item.durationMs,
|
|
521
|
+
error: item.errorMessage || null,
|
|
522
|
+
})),
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
private async executeAction(action: any, body: any) {
|
|
528
|
+
if (action.type === 'email') {
|
|
529
|
+
return this.executeEmailAction(action, body);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (action.type === 'whatsapp_evolution') {
|
|
533
|
+
return this.executeWhatsappAction(action, body);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
throw new BadRequestException(`Unsupported action type: ${action.type}`);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
private async executeEmailAction(action: any, body: any) {
|
|
540
|
+
this.validateActionConfiguration(action);
|
|
541
|
+
|
|
542
|
+
const profile = await (this.prisma as any).mail_profile.findUnique({
|
|
543
|
+
where: { id: Number(action.mail_profile_id) },
|
|
544
|
+
});
|
|
545
|
+
const mail = await (this.prisma as any).mail.findUnique({
|
|
546
|
+
where: { id: Number(action.mail_id) },
|
|
547
|
+
include: {
|
|
548
|
+
mail_locale: {
|
|
549
|
+
orderBy: [{ locale_id: 'asc' }],
|
|
550
|
+
},
|
|
551
|
+
},
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
if (!profile || !mail?.mail_locale?.length) {
|
|
555
|
+
throw new BadRequestException(
|
|
556
|
+
'Email action references an invalid profile or template.',
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const template = mail.mail_locale[0];
|
|
561
|
+
const config = this.buildMailConfig(profile);
|
|
562
|
+
const sender = profile.from_name
|
|
563
|
+
? `${profile.from_name} <${profile.from_email}>`
|
|
564
|
+
: profile.from_email;
|
|
565
|
+
const replyTo = profile.reply_to_email
|
|
566
|
+
? profile.reply_to_name
|
|
567
|
+
? `${profile.reply_to_name} <${profile.reply_to_email}>`
|
|
568
|
+
: profile.reply_to_email
|
|
569
|
+
: undefined;
|
|
570
|
+
|
|
571
|
+
const renderedTo = String(renderTemplate(action.email_to, body) ?? '').trim();
|
|
572
|
+
const renderedCc = action.email_cc
|
|
573
|
+
? String(renderTemplate(action.email_cc, body) ?? '').trim()
|
|
574
|
+
: '';
|
|
575
|
+
const renderedBcc = action.email_bcc
|
|
576
|
+
? String(renderTemplate(action.email_bcc, body) ?? '').trim()
|
|
577
|
+
: '';
|
|
578
|
+
|
|
579
|
+
if (!renderedTo) {
|
|
580
|
+
throw new BadRequestException(
|
|
581
|
+
'Email action rendered an empty recipient for email_to.',
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const requestPayload = {
|
|
586
|
+
to: renderedTo,
|
|
587
|
+
cc: renderedCc || undefined,
|
|
588
|
+
bcc: renderedBcc || undefined,
|
|
589
|
+
from: sender,
|
|
590
|
+
replyTo,
|
|
591
|
+
subject: renderTemplate(template.subject, body) as string,
|
|
592
|
+
body: renderTemplate(template.body, body) as string,
|
|
593
|
+
mail_id: mail.id,
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
try {
|
|
597
|
+
this.mailMainService.setConfig(config as any);
|
|
598
|
+
const providerResponse = await this.mailMainService.send(
|
|
599
|
+
requestPayload as any,
|
|
600
|
+
);
|
|
601
|
+
|
|
602
|
+
return {
|
|
603
|
+
requestPayload,
|
|
604
|
+
responsePayload: {
|
|
605
|
+
provider: 'email',
|
|
606
|
+
response: providerResponse || null,
|
|
607
|
+
},
|
|
608
|
+
};
|
|
609
|
+
} finally {
|
|
610
|
+
await this.coreMailService.reloadConfig().catch(() => undefined);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
private async executeWhatsappAction(action: any, body: any) {
|
|
615
|
+
const required = [
|
|
616
|
+
action.whatsapp_target,
|
|
617
|
+
action.whatsapp_template,
|
|
618
|
+
action.whatsapp_instance,
|
|
619
|
+
action.whatsapp_base_url,
|
|
620
|
+
action.whatsapp_token,
|
|
621
|
+
];
|
|
622
|
+
|
|
623
|
+
if (required.some((value) => !String(value || '').trim())) {
|
|
624
|
+
throw new BadRequestException(
|
|
625
|
+
'WhatsApp action is missing required configuration.',
|
|
626
|
+
);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
const baseUrl = String(action.whatsapp_base_url).replace(/\/+$/, '');
|
|
630
|
+
const instance = encodeURIComponent(action.whatsapp_instance);
|
|
631
|
+
const url = `${baseUrl}/message/sendText/${instance}`;
|
|
632
|
+
const targetType =
|
|
633
|
+
action.whatsapp_target_type === 'group' ? 'group' : 'phone';
|
|
634
|
+
const renderedMessage = String(
|
|
635
|
+
renderTemplate(action.whatsapp_template, body) ?? '',
|
|
636
|
+
).trim();
|
|
637
|
+
const renderedTarget = String(
|
|
638
|
+
renderTemplate(action.whatsapp_target, body) ?? '',
|
|
639
|
+
).trim();
|
|
640
|
+
|
|
641
|
+
if (!renderedMessage) {
|
|
642
|
+
throw new BadRequestException(
|
|
643
|
+
'WhatsApp action template rendered an empty message.',
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const target = this.normalizeWhatsappTarget(renderedTarget, targetType);
|
|
648
|
+
const requestPayload = {
|
|
649
|
+
provider: 'evolution',
|
|
650
|
+
url,
|
|
651
|
+
instance: action.whatsapp_instance,
|
|
652
|
+
targetType,
|
|
653
|
+
number: target,
|
|
654
|
+
text: renderedMessage,
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
let response: any;
|
|
658
|
+
try {
|
|
659
|
+
response = await firstValueFrom(
|
|
660
|
+
this.http.post(
|
|
661
|
+
url,
|
|
662
|
+
{
|
|
663
|
+
number: target,
|
|
664
|
+
text: renderedMessage,
|
|
665
|
+
},
|
|
666
|
+
{
|
|
667
|
+
headers: {
|
|
668
|
+
apikey: action.whatsapp_token,
|
|
669
|
+
},
|
|
670
|
+
timeout: 15000,
|
|
671
|
+
},
|
|
672
|
+
),
|
|
673
|
+
);
|
|
674
|
+
} catch (error) {
|
|
675
|
+
this.attachErrorRequestPayload(error, requestPayload);
|
|
676
|
+
throw error;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
return {
|
|
680
|
+
requestPayload,
|
|
681
|
+
responsePayload: {
|
|
682
|
+
status: response?.status || null,
|
|
683
|
+
statusText: response?.statusText || null,
|
|
684
|
+
data: response?.data || null,
|
|
685
|
+
},
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
private validateToken(integration: any, headers: any) {
|
|
690
|
+
if (!integration.require_token) {
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
const provided =
|
|
695
|
+
headers['x-webhook-token'] ||
|
|
696
|
+
headers['X-Webhook-Token'] ||
|
|
697
|
+
headers.authorization?.replace(/^Bearer\s+/i, '');
|
|
698
|
+
|
|
699
|
+
if (!provided || hashToken(String(provided)) !== integration.token_hash) {
|
|
700
|
+
throw new BadRequestException('Invalid webhook token.');
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
private validateIp(integration: any, remoteIp: string | null) {
|
|
705
|
+
const allowedIps = Array.isArray(integration.allowed_ips)
|
|
706
|
+
? integration.allowed_ips
|
|
707
|
+
: [];
|
|
708
|
+
|
|
709
|
+
if (!allowedIps.length) {
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
if (!remoteIp || !allowedIps.includes(remoteIp)) {
|
|
714
|
+
throw new BadRequestException('Remote IP is not allowed.');
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
private validateActionConfiguration(action: any) {
|
|
719
|
+
if (action?.type === 'email') {
|
|
720
|
+
const missing: string[] = [];
|
|
721
|
+
|
|
722
|
+
if (!this.nullableNumber(action.mail_profile_id)) {
|
|
723
|
+
missing.push('mail_profile_id');
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
if (!this.nullableNumber(action.mail_id)) {
|
|
727
|
+
missing.push('mail_id');
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
if (!this.nullableString(action.email_to, true)) {
|
|
731
|
+
missing.push('email_to');
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
if (missing.length) {
|
|
735
|
+
throw new BadRequestException(
|
|
736
|
+
`Email action is missing required configuration: ${missing.join(', ')}.`,
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
if (action?.type === 'whatsapp_evolution') {
|
|
742
|
+
const missing = [
|
|
743
|
+
['whatsapp_target', action.whatsapp_target],
|
|
744
|
+
['whatsapp_template', action.whatsapp_template],
|
|
745
|
+
['whatsapp_instance', action.whatsapp_instance],
|
|
746
|
+
['whatsapp_base_url', action.whatsapp_base_url],
|
|
747
|
+
['whatsapp_token', action.whatsapp_token],
|
|
748
|
+
]
|
|
749
|
+
.filter(([, value]) => !String(value || '').trim())
|
|
750
|
+
.map(([key]) => key);
|
|
751
|
+
|
|
752
|
+
if (missing.length) {
|
|
753
|
+
throw new BadRequestException(
|
|
754
|
+
`WhatsApp action is missing required configuration: ${missing.join(', ')}.`,
|
|
755
|
+
);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
private validateBodySchema(integration: any, body: any) {
|
|
761
|
+
if (!integration.body_schema) {
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
const errors = this.validateJsonSchema(integration.body_schema, body);
|
|
766
|
+
if (errors.length) {
|
|
767
|
+
throw new BadRequestException({
|
|
768
|
+
message: 'Webhook body does not match the configured schema.',
|
|
769
|
+
errors,
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
private validateJsonSchema(schema: any, value: any, path = '$'): string[] {
|
|
775
|
+
if (!schema || typeof schema !== 'object') {
|
|
776
|
+
return [];
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
const errors: string[] = [];
|
|
780
|
+
const type = schema.type;
|
|
781
|
+
|
|
782
|
+
if (type && !this.matchesJsonSchemaType(type, value)) {
|
|
783
|
+
errors.push(`${path} must be ${type}`);
|
|
784
|
+
return errors;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
if (type === 'object' || schema.properties || schema.required) {
|
|
788
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
789
|
+
errors.push(`${path} must be object`);
|
|
790
|
+
return errors;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
for (const requiredKey of schema.required || []) {
|
|
794
|
+
if (value[requiredKey] === undefined || value[requiredKey] === null) {
|
|
795
|
+
errors.push(`${path}.${requiredKey} is required`);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
for (const [key, childSchema] of Object.entries(
|
|
800
|
+
schema.properties || {},
|
|
801
|
+
)) {
|
|
802
|
+
if (value[key] !== undefined) {
|
|
803
|
+
errors.push(
|
|
804
|
+
...this.validateJsonSchema(
|
|
805
|
+
childSchema,
|
|
806
|
+
value[key],
|
|
807
|
+
`${path}.${key}`,
|
|
808
|
+
),
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
if (type === 'array' && schema.items && Array.isArray(value)) {
|
|
815
|
+
value.forEach((item, index) => {
|
|
816
|
+
errors.push(
|
|
817
|
+
...this.validateJsonSchema(schema.items, item, `${path}[${index}]`),
|
|
818
|
+
);
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
return errors;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
private matchesJsonSchemaType(type: string | string[], value: any) {
|
|
826
|
+
const types = Array.isArray(type) ? type : [type];
|
|
827
|
+
return types.some((item) => {
|
|
828
|
+
switch (item) {
|
|
829
|
+
case 'object':
|
|
830
|
+
return (
|
|
831
|
+
value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
832
|
+
);
|
|
833
|
+
case 'array':
|
|
834
|
+
return Array.isArray(value);
|
|
835
|
+
case 'string':
|
|
836
|
+
return typeof value === 'string';
|
|
837
|
+
case 'number':
|
|
838
|
+
return typeof value === 'number';
|
|
839
|
+
case 'integer':
|
|
840
|
+
return Number.isInteger(value);
|
|
841
|
+
case 'boolean':
|
|
842
|
+
return typeof value === 'boolean';
|
|
843
|
+
case 'null':
|
|
844
|
+
return value === null;
|
|
845
|
+
default:
|
|
846
|
+
return true;
|
|
847
|
+
}
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
private async createCallLog(input: {
|
|
852
|
+
integration: any | null;
|
|
853
|
+
uuid: string;
|
|
854
|
+
status: 'success' | 'failed';
|
|
855
|
+
remoteIp: string | null;
|
|
856
|
+
durationMs: number;
|
|
857
|
+
payload: any;
|
|
858
|
+
response?: any;
|
|
859
|
+
error?: any;
|
|
860
|
+
}) {
|
|
861
|
+
if (!this.logs) {
|
|
862
|
+
return null;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
return this.logs.create({
|
|
866
|
+
data: {
|
|
867
|
+
webhook_integration_id: input.integration?.id ?? null,
|
|
868
|
+
public_uuid: input.uuid,
|
|
869
|
+
status: input.status,
|
|
870
|
+
remote_ip: input.remoteIp,
|
|
871
|
+
duration_ms: input.durationMs,
|
|
872
|
+
error_message: input.error ? this.extractErrorMessage(input.error) : null,
|
|
873
|
+
payload_summary: summarizePayload(sanitizeSensitiveData(input.payload)),
|
|
874
|
+
response_summary: summarizePayload(sanitizeSensitiveData(input.response)),
|
|
875
|
+
},
|
|
876
|
+
select: {
|
|
877
|
+
id: true,
|
|
878
|
+
},
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
private async createActionLogs(callLogId: number | undefined, logs: ActionExecutionLog[]) {
|
|
883
|
+
if (!callLogId || !logs.length || !this.actionLogs) {
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
await this.actionLogs.createMany({
|
|
888
|
+
data: logs.map((item) => ({
|
|
889
|
+
webhook_call_log_id: callLogId,
|
|
890
|
+
webhook_action_id: item.webhookActionId,
|
|
891
|
+
action_name: item.actionName,
|
|
892
|
+
action_type: item.actionType,
|
|
893
|
+
status: item.status,
|
|
894
|
+
duration_ms: item.durationMs,
|
|
895
|
+
request_payload: summarizePayload(sanitizeSensitiveData(item.requestPayload)),
|
|
896
|
+
response_payload: summarizePayload(
|
|
897
|
+
sanitizeSensitiveData(item.responsePayload),
|
|
898
|
+
),
|
|
899
|
+
error_message: item.errorMessage || null,
|
|
900
|
+
})),
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
private extractErrorMessage(error: unknown) {
|
|
905
|
+
if (!error) {
|
|
906
|
+
return 'Unknown error';
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
if (error instanceof Error && error.message) {
|
|
910
|
+
return error.message;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
if (typeof error === 'object' && (error as any).message) {
|
|
914
|
+
return String((error as any).message);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
return String(error);
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
private extractErrorResponse(error: unknown) {
|
|
921
|
+
if (!error || typeof error !== 'object') {
|
|
922
|
+
return null;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
const maybeResponse = (error as any).response;
|
|
926
|
+
if (!maybeResponse) {
|
|
927
|
+
return null;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
return {
|
|
931
|
+
status: maybeResponse.status || null,
|
|
932
|
+
statusText: maybeResponse.statusText || null,
|
|
933
|
+
data: maybeResponse.data || null,
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
private extractErrorRequestPayload(error: unknown) {
|
|
938
|
+
if (!error || typeof error !== 'object') {
|
|
939
|
+
return null;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
const maybePayload = (error as any).__webhookRequestPayload;
|
|
943
|
+
return maybePayload || null;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
private attachErrorRequestPayload(error: unknown, requestPayload: unknown) {
|
|
947
|
+
if (!error || typeof error !== 'object') {
|
|
948
|
+
return;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
(error as any).__webhookRequestPayload = requestPayload;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
private normalizeWhatsappTarget(
|
|
955
|
+
renderedTarget: string,
|
|
956
|
+
targetType: 'phone' | 'group',
|
|
957
|
+
) {
|
|
958
|
+
const rawTarget = String(renderedTarget || '').trim();
|
|
959
|
+
if (!rawTarget) {
|
|
960
|
+
throw new BadRequestException(
|
|
961
|
+
'WhatsApp action target template rendered an empty value.',
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
if (targetType === 'group') {
|
|
966
|
+
return rawTarget;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
const normalized = rawTarget.replace(/\D+/g, '');
|
|
970
|
+
if (!normalized) {
|
|
971
|
+
throw new BadRequestException(
|
|
972
|
+
'WhatsApp action target must contain at least one digit for phone targets.',
|
|
973
|
+
);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
return normalized;
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
private getRemoteIp(req: Request) {
|
|
980
|
+
const forwardedFor = req.headers['x-forwarded-for'];
|
|
981
|
+
const forwardedIp = Array.isArray(forwardedFor)
|
|
982
|
+
? forwardedFor[0]
|
|
983
|
+
: forwardedFor?.split(',')[0];
|
|
984
|
+
|
|
985
|
+
return forwardedIp?.trim() || req.ip || req.socket.remoteAddress || null;
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
private async ensureAction(integrationId: number, actionId: number) {
|
|
989
|
+
const record = await this.actions.findFirst({
|
|
990
|
+
where: {
|
|
991
|
+
id: actionId,
|
|
992
|
+
webhook_integration_id: integrationId,
|
|
993
|
+
},
|
|
994
|
+
});
|
|
995
|
+
|
|
996
|
+
if (!record) {
|
|
997
|
+
throw new NotFoundException(
|
|
998
|
+
`Webhook action with id ${actionId} not found.`,
|
|
999
|
+
);
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
return record;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
private async ensureSlugAvailable(slug: string) {
|
|
1006
|
+
const existing = await this.integrations.findUnique({
|
|
1007
|
+
where: { slug: String(slug || '').trim() },
|
|
1008
|
+
select: { id: true },
|
|
1009
|
+
});
|
|
1010
|
+
|
|
1011
|
+
if (existing) {
|
|
1012
|
+
throw new BadRequestException(
|
|
1013
|
+
`Webhook integration "${slug}" already exists.`,
|
|
1014
|
+
);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
private normalizeIntegration(data: any, partial = false) {
|
|
1019
|
+
const normalized: Record<string, any> = {};
|
|
1020
|
+
const assign = (key: string, value: any) => {
|
|
1021
|
+
if (!partial || value !== undefined) {
|
|
1022
|
+
normalized[key] = value;
|
|
1023
|
+
}
|
|
1024
|
+
};
|
|
1025
|
+
|
|
1026
|
+
assign('slug', String(data.slug ?? '').trim());
|
|
1027
|
+
assign('name', String(data.name ?? '').trim());
|
|
1028
|
+
assign('description', this.nullableString(data.description, partial));
|
|
1029
|
+
assign('status', data.status === 'inactive' ? 'inactive' : 'active');
|
|
1030
|
+
assign('require_token', Boolean(data.require_token));
|
|
1031
|
+
assign('allowed_ips', normalizeJsonObject(data.allowed_ips));
|
|
1032
|
+
assign('body_schema', normalizeJsonObject(data.body_schema));
|
|
1033
|
+
|
|
1034
|
+
return normalized;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
private normalizeAction(data: WebhookActionPayload, partial = false) {
|
|
1038
|
+
const normalized: Record<string, any> = {};
|
|
1039
|
+
const assign = (key: string, value: any) => {
|
|
1040
|
+
if (!partial || value !== undefined) {
|
|
1041
|
+
normalized[key] = value;
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
|
|
1045
|
+
assign('type', data.type);
|
|
1046
|
+
assign('name', String(data.name ?? '').trim());
|
|
1047
|
+
assign('order', Number(data.order ?? 0));
|
|
1048
|
+
assign('status', data.status === 'inactive' ? 'inactive' : 'active');
|
|
1049
|
+
assign('mail_profile_id', this.nullableNumber(data.mail_profile_id, partial));
|
|
1050
|
+
assign('mail_id', this.nullableNumber(data.mail_id, partial));
|
|
1051
|
+
assign('email_to', this.nullableString(data.email_to, partial));
|
|
1052
|
+
assign('email_cc', this.nullableString(data.email_cc, partial));
|
|
1053
|
+
assign('email_bcc', this.nullableString(data.email_bcc, partial));
|
|
1054
|
+
assign('whatsapp_target_type', data.whatsapp_target_type || null);
|
|
1055
|
+
assign(
|
|
1056
|
+
'whatsapp_target',
|
|
1057
|
+
this.nullableString(data.whatsapp_target, partial),
|
|
1058
|
+
);
|
|
1059
|
+
assign(
|
|
1060
|
+
'whatsapp_template',
|
|
1061
|
+
this.nullableString(data.whatsapp_template, partial),
|
|
1062
|
+
);
|
|
1063
|
+
assign(
|
|
1064
|
+
'whatsapp_instance',
|
|
1065
|
+
this.nullableString(data.whatsapp_instance, partial),
|
|
1066
|
+
);
|
|
1067
|
+
assign(
|
|
1068
|
+
'whatsapp_base_url',
|
|
1069
|
+
this.nullableString(data.whatsapp_base_url, partial),
|
|
1070
|
+
);
|
|
1071
|
+
assign('whatsapp_token', this.nullableString(data.whatsapp_token, partial));
|
|
1072
|
+
|
|
1073
|
+
return normalized;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
private nullableString(value: any, partial = false) {
|
|
1077
|
+
if (value === undefined) {
|
|
1078
|
+
return partial ? undefined : null;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
if (value === null) {
|
|
1082
|
+
return null;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
const normalized = String(value).trim();
|
|
1086
|
+
return normalized || null;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
private nullableNumber(value: any, partial = false) {
|
|
1090
|
+
if (value === undefined) {
|
|
1091
|
+
return partial ? undefined : null;
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
if (value === null || value === '') {
|
|
1095
|
+
return null;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
return Number(value);
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
private buildMailConfig(profile: any) {
|
|
1102
|
+
switch (profile.provider) {
|
|
1103
|
+
case 'SMTP':
|
|
1104
|
+
return {
|
|
1105
|
+
global: true,
|
|
1106
|
+
type: 'SMTP',
|
|
1107
|
+
host: profile.smtp_host,
|
|
1108
|
+
port: Number(profile.smtp_port || 587),
|
|
1109
|
+
secure: Boolean(profile.smtp_secure),
|
|
1110
|
+
username: profile.smtp_username || '',
|
|
1111
|
+
password: profile.smtp_password || '',
|
|
1112
|
+
};
|
|
1113
|
+
case 'GMAIL':
|
|
1114
|
+
return {
|
|
1115
|
+
global: true,
|
|
1116
|
+
type: 'GMAIL',
|
|
1117
|
+
clientId: profile.gmail_client_id,
|
|
1118
|
+
clientSecret: profile.gmail_client_secret,
|
|
1119
|
+
refreshToken: profile.gmail_refresh_token,
|
|
1120
|
+
from: profile.from_email,
|
|
1121
|
+
};
|
|
1122
|
+
case 'SES':
|
|
1123
|
+
return {
|
|
1124
|
+
global: true,
|
|
1125
|
+
type: 'SES',
|
|
1126
|
+
region: profile.ses_region,
|
|
1127
|
+
accessKeyId: profile.ses_access_key_id,
|
|
1128
|
+
secretAccessKey: profile.ses_secret_access_key,
|
|
1129
|
+
from: profile.from_email,
|
|
1130
|
+
};
|
|
1131
|
+
default:
|
|
1132
|
+
throw new BadRequestException(
|
|
1133
|
+
`Invalid mail profile provider: ${profile.provider}`,
|
|
1134
|
+
);
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
}
|