@avakado.ai/schemas 1.1.26 → 1.1.28

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/models.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  // Generated by scripts/emit-types.mjs. Run `npm run types` after a schema change.
2
2
 
3
- export { Action, AgentModel, AmountSchema, Api, ApiAuthenticationSchema, ApiAuthenticators, BillingLog, Business, BusinessSchema, CallSession, Campaign, CampaignSchema, Channel, ChannelCapabilityDefaults, Collection, Conversation, ConversationSchema, Data, DemonstrationModel, Lead, LeadTemplate, Log, Message, MessageSession, Notification, OAuthAuthorizationCode, OAuthClient, OAuthRefreshToken, OAuthRevokedJti, Payment, PaymentSchema, Plan, Providers, RoleScopes, ScopesEnum, Subscription, SubscriptionSchema, Task, TaskSchema, Template, Ticket, TriggerTemplate, UsageLog, UsageLogSchema, User, Workflow, WorkflowLogs } from "./types.js";
3
+ export { Action, AgentModel, AmountSchema, Api, ApiAuthenticationSchema, ApiAuthenticators, BillingLog, Business, BusinessSchema, CallSession, Campaign, CampaignSchema, Channel, ChannelCapabilityDefaults, Collection, Conversation, ConversationSchema, CronJob, Data, DemonstrationModel, Lead, LeadTemplate, Log, Message, MessageSession, Notification, OAuthAuthorizationCode, OAuthClient, OAuthRefreshToken, OAuthRevokedJti, Payment, PaymentSchema, Plan, Providers, RoleScopes, ScopesEnum, Subscription, SubscriptionSchema, Task, TaskSchema, Template, Ticket, TriggerTemplate, UsageLog, UsageLogSchema, User, Workflow, WorkflowLogs } from "./types.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@avakado.ai/schemas",
3
- "version": "1.1.26",
3
+ "version": "1.1.28",
4
4
  "description": "Canonical Mongo schemas, models, enums and index declarations shared by the Avakado services.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
@@ -0,0 +1,26 @@
1
+ import { Schema } from "mongoose";
2
+ import { defineModel } from "../runtime.js";
3
+
4
+ const CronJobSchema = new Schema({
5
+ _id: { type: String },
6
+ name: { type: String, required: true },
7
+ scheduleType: { type: String, enum: ["cron", "once"], default: "once" },
8
+ schedule: String,
9
+ runAt: Date,
10
+ type: { type: String, enum: ["shell", "http"], required: true },
11
+ command: String,
12
+ url: String,
13
+ method: { type: String, default: "GET" },
14
+ headers: String,
15
+ params: String,
16
+ body: String,
17
+ enabled: { type: Boolean, default: true },
18
+ lastRun: Date,
19
+ lastStatus: String,
20
+ miscIds: { type: Schema.Types.Mixed, default: () => ({}) },
21
+ createdAt: { type: Date, default: Date.now },
22
+ });
23
+
24
+ CronJobSchema.index({ scheduleType: 1, enabled: 1, runAt: 1 });
25
+
26
+ export const CronJob = defineModel("CronJob", CronJobSchema, "CronJobs");
@@ -9,6 +9,7 @@ export { User, ScopesEnum, RoleScopes } from "./User.js";
9
9
  export { AgentModel } from "./Agent.js";
10
10
  export { Channel, ChannelCapabilityDefaults } from "./Channels.js";
11
11
  export { CallSession } from "./CallSessions.js";
12
+ export { CronJob } from "./CronJob.js";
12
13
  export { Plan } from "./Plans.js";
13
14
  export { Api, Providers } from "./ExternalServiceProviders.js";
14
15
  export { Template } from "./market.js";
@@ -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) {
@@ -0,0 +1,3 @@
1
+ // Generated by scripts/emit-types.mjs. Run `npm run types` after a schema change.
2
+
3
+ export { CronJob } from "../../types.js";
package/types.d.ts CHANGED
@@ -235,6 +235,25 @@ export interface ConversationMethods {
235
235
  updateStatus(status?: any): Promise<any>;
236
236
  }
237
237
 
238
+ export interface CronJob {
239
+ _id: string;
240
+ name: string;
241
+ scheduleType?: "cron" | "once";
242
+ schedule?: string;
243
+ runAt?: Date;
244
+ type: "shell" | "http";
245
+ command?: string;
246
+ url?: string;
247
+ method?: string;
248
+ headers?: string;
249
+ params?: string;
250
+ body?: string;
251
+ enabled?: boolean;
252
+ lastRun?: Date;
253
+ lastStatus?: string;
254
+ miscIds?: any;
255
+ createdAt?: Date;
256
+ }
238
257
 
239
258
  export interface DemonstrationModel {
240
259
  transcripts?: any;
@@ -659,6 +678,8 @@ export const Collection: Model<Collection, {}, {}> & CollectionStatics;
659
678
 
660
679
  export const Conversation: Model<Conversation, {}, ConversationMethods>;
661
680
 
681
+ export const CronJob: Model<CronJob, {}, {}>;
682
+
662
683
  export const DemonstrationModel: Model<DemonstrationModel, {}, {}>;
663
684
 
664
685
  export const Lead: Model<Lead, {}, {}>;