@oondemand/oon-core-back 0.3.72 → 0.3.74

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oondemand/oon-core-back",
3
- "version": "0.3.72",
3
+ "version": "0.3.74",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/oondemand/oon-platform.git",
@@ -22,6 +22,72 @@ const { sanitizeIntegrationValue } = require("./sanitizer");
22
22
 
23
23
  const READ_ROLES = ["admin", "desenvolvedor"];
24
24
  const WRITE_ROLES = ["admin", "desenvolvedor"];
25
+ const WEBHOOK_AUTO_PROCESS_LIMIT = 50;
26
+ const webhookProvidersPending = new Set();
27
+ let webhookProcessingScheduled = false;
28
+
29
+ function scheduleWebhookProcessing(provider) {
30
+ const providerKey = String(provider || "").trim().toLowerCase();
31
+ if (!providerKey) return;
32
+
33
+ webhookProvidersPending.add(providerKey);
34
+ if (webhookProcessingScheduled) return;
35
+ webhookProcessingScheduled = true;
36
+
37
+ setImmediate(async () => {
38
+ const providers = [...webhookProvidersPending];
39
+ webhookProvidersPending.clear();
40
+ try {
41
+ for (const currentProvider of providers) {
42
+ try {
43
+ const events = await processWebhookInbox({
44
+ provider: currentProvider,
45
+ limit: WEBHOOK_AUTO_PROCESS_LIMIT,
46
+ });
47
+ const queue = await processIntegrationQueue({
48
+ provider: currentProvider,
49
+ limit: WEBHOOK_AUTO_PROCESS_LIMIT,
50
+ });
51
+
52
+ if (
53
+ (Array.isArray(events?.results) && events.results.length >= WEBHOOK_AUTO_PROCESS_LIMIT)
54
+ || (Array.isArray(queue?.results) && queue.results.length >= WEBHOOK_AUTO_PROCESS_LIMIT)
55
+ ) {
56
+ webhookProvidersPending.add(currentProvider);
57
+ }
58
+ } catch (error) {
59
+ console.error(
60
+ `[oon-integrations] Falha ao processar webhook automaticamente para ${currentProvider}: ${String(error?.message || error)}`,
61
+ );
62
+ }
63
+ }
64
+ } finally {
65
+ webhookProcessingScheduled = false;
66
+ if (webhookProvidersPending.size) {
67
+ scheduleWebhookProcessing([...webhookProvidersPending][0]);
68
+ }
69
+ }
70
+ });
71
+ }
72
+
73
+ function publicWebhookUrl(path) {
74
+ const webhookPath = String(path || "").trim();
75
+ const publicAppUrl = String(process.env.PUBLIC_APP_URL || "").trim();
76
+ if (!webhookPath || !publicAppUrl) return webhookPath;
77
+
78
+ try {
79
+ const url = new URL(publicAppUrl);
80
+ const basePath = url.pathname.replace(/\/+$/, "");
81
+ const apiBasePath = basePath.endsWith("/api") ? basePath : `${basePath}/api`;
82
+ const relativePath = webhookPath.startsWith("/") ? webhookPath : `/${webhookPath}`;
83
+ url.pathname = `${apiBasePath}${relativePath}`.replace(/\/{2,}/g, "/");
84
+ url.search = "";
85
+ url.hash = "";
86
+ return url.toString();
87
+ } catch {
88
+ return webhookPath;
89
+ }
90
+ }
25
91
 
26
92
  function number(value, fallback, max = 100) {
27
93
  const parsed = Number(value);
@@ -98,7 +164,8 @@ function buildIntegrationRouter() {
98
164
  eventType: req.body?.eventType || req.body?.event_type || req.body?.topic || "unknown",
99
165
  externalEventId: req.body?.eventId || req.body?.event_id || req.body?.id,
100
166
  });
101
- res.status(202).json({ accepted: true, id: String(event._id), duplicate: event.status !== "Pendente" });
167
+ res.status(200).json({ accepted: true, id: String(event._id), duplicate: event.status !== "Pendente" });
168
+ scheduleWebhookProcessing(req.params.provider);
102
169
  }));
103
170
 
104
171
  router.use(auth);
@@ -137,12 +204,14 @@ function buildIntegrationRouter() {
137
204
  router.get("/providers/:provider/configuration/:instanceId/webhook", rbac(READ_ROLES), asyncHandler(async (req, res) => {
138
205
  const provider = getIntegrationProvider(req.params.provider);
139
206
  const getWebhookAccess = requireProviderExtension(provider, "getWebhookAccess");
140
- res.json({ provider: provider.key, ...await getWebhookAccess(req.params.instanceId) });
207
+ const access = await getWebhookAccess(req.params.instanceId);
208
+ res.json({ provider: provider.key, ...access, webhookPath: publicWebhookUrl(access.webhookPath) });
141
209
  }));
142
210
  router.post("/providers/:provider/configuration/:instanceId/webhook/rotate", rbac(WRITE_ROLES), asyncHandler(async (req, res) => {
143
211
  const provider = getIntegrationProvider(req.params.provider);
144
212
  const rotateWebhookToken = requireProviderExtension(provider, "rotateWebhookToken");
145
- res.json({ provider: provider.key, ...await rotateWebhookToken(req.params.instanceId) });
213
+ const access = await rotateWebhookToken(req.params.instanceId);
214
+ res.json({ provider: provider.key, ...access, webhookPath: publicWebhookUrl(access.webhookPath) });
146
215
  }));
147
216
  router.get("/providers/:provider/mappings", rbac(READ_ROLES), asyncHandler(async (req, res) => {
148
217
  const provider = getIntegrationProvider(req.params.provider);
@@ -265,4 +334,10 @@ function buildIntegrationRouter() {
265
334
  return router;
266
335
  }
267
336
 
268
- module.exports = { buildIntegrationRouter, buildQueueFilter, queueStatusSummary };
337
+ module.exports = {
338
+ buildIntegrationRouter,
339
+ buildQueueFilter,
340
+ publicWebhookUrl,
341
+ queueStatusSummary,
342
+ scheduleWebhookProcessing,
343
+ };