@avakado.ai/schemas 1.1.26 → 1.1.27
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 +1 -1
- package/src/statusTrigger.js +29 -1
package/package.json
CHANGED
package/src/statusTrigger.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { CAMPAIGN_STATUS, CONVERSATION_STATUS, LEAD_STATUS, TASK_STATUS } from "./enums/index.js";
|
|
3
|
+
import { Workflow } from "./models/Workflow.js";
|
|
3
4
|
import { requireDep } from "./runtime.js";
|
|
4
5
|
|
|
5
6
|
// Status is not written in one place. Conversations go through updateStatus, but also
|
|
@@ -102,8 +103,35 @@ function statusTriggerMessage({ entity, status, previousStatus, businessId, docu
|
|
|
102
103
|
};
|
|
103
104
|
}
|
|
104
105
|
|
|
106
|
+
async function messagesForActiveWorkflows(messages) {
|
|
107
|
+
const grouped = new Map();
|
|
108
|
+
for (const message of messages) {
|
|
109
|
+
const trigger = message.headers?.trigger;
|
|
110
|
+
const businessId = message.headers?.businessId;
|
|
111
|
+
if (!trigger || !businessId) continue;
|
|
112
|
+
const key = `${trigger}\0${businessId}`;
|
|
113
|
+
const bucket = grouped.get(key);
|
|
114
|
+
if (bucket) bucket.messages.push(message);
|
|
115
|
+
else grouped.set(key, { trigger, businessId, messages: [message] });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const publishable = [];
|
|
119
|
+
for (const { trigger, businessId, messages: bucket } of grouped.values()) {
|
|
120
|
+
try {
|
|
121
|
+
const workflows = await Workflow.find({ trigger, business: businessId, status: "active" }).select("_id").limit(1).lean();
|
|
122
|
+
if (workflows.length) publishable.push(...bucket);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
console.error(
|
|
125
|
+
`@avakado.ai/schemas: failed to look up active workflows for trigger ${trigger}`,
|
|
126
|
+
error?.message || error
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return publishable;
|
|
131
|
+
}
|
|
132
|
+
|
|
105
133
|
async function publishStatusChanges(changes) {
|
|
106
|
-
const messages = changes.map(statusTriggerMessage).filter(Boolean);
|
|
134
|
+
const messages = await messagesForActiveWorkflows(changes.map(statusTriggerMessage).filter(Boolean));
|
|
107
135
|
if (!messages.length) return;
|
|
108
136
|
const sendKafkaMessage = requireDep("sendKafkaMessage", "workflow status trigger");
|
|
109
137
|
for (let i = 0; i < messages.length; i += BATCH_SIZE) {
|