@avakado.ai/schemas 1.0.0 → 1.1.0

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.
@@ -0,0 +1,473 @@
1
+ import { Schema } from "mongoose";
2
+ import { defineModel } from "../runtime.js";
3
+
4
+ // Comprehensive scope definitions for all user types
5
+ const ScopesEnum = {
6
+ // Authentication & User Management
7
+ 'auth:login': 'User can login to the system',
8
+ 'auth:register': 'User can register new accounts',
9
+ 'auth:logout': 'User can logout from the system',
10
+ 'auth:refresh': 'User can refresh their access token',
11
+ 'user:read': 'User can read their own profile',
12
+ 'user:update': 'User can update their own profile',
13
+ 'user:delete': 'User can delete their own account',
14
+
15
+ // Business Management
16
+ 'business:read': 'User can read business information',
17
+ 'business:create': 'User can create new businesses',
18
+ 'business:update': 'User can update business information',
19
+ 'business:delete': 'User can delete businesses',
20
+ 'business:manage_members': 'User can manage business members',
21
+ 'business:view_analytics': 'User can view business analytics',
22
+ 'business:export_data': 'User can export business data',
23
+
24
+ // Agent Management
25
+ 'agent:read': 'User can read agent information',
26
+ 'agent:create': 'User can create new agents',
27
+ 'agent:update': 'User can update agent configurations',
28
+ 'agent:delete': 'User can delete agents',
29
+ 'agent:deploy': 'User can deploy agents to channels',
30
+ 'agent:test': 'User can test agent responses',
31
+ 'agent:view_conversations': 'User can view agent conversations',
32
+ 'agent:manage_prompts': 'User can manage agent prompts',
33
+
34
+ // Collection Management
35
+ 'collection:read': 'User can read knowledge collections',
36
+ 'collection:create': 'User can create new collections',
37
+ 'collection:update': 'User can update collection content',
38
+ 'collection:delete': 'User can delete collections',
39
+ 'collection:upload_files': 'User can upload files to collections',
40
+ 'collection:manage_permissions': 'User can manage collection access',
41
+
42
+ // Channel Management
43
+ 'channel:read': 'User can read channel configurations',
44
+ 'channel:create': 'User can create new channels',
45
+ 'channel:update': 'User can update channel settings',
46
+ 'channel:delete': 'User can delete channels',
47
+ 'channel:connect': 'User can connect external platforms',
48
+ 'channel:view_webhooks': 'User can view webhook configurations',
49
+
50
+ // Action Management
51
+ 'action:read': 'User can read available actions',
52
+ 'action:create': 'User can create new actions',
53
+ 'action:update': 'User can update action configurations',
54
+ 'action:delete': 'User can delete actions',
55
+ 'action:test': 'User can test action functionality',
56
+ 'action:manage_integrations': 'User can manage external integrations',
57
+
58
+ // Conversation Management
59
+ 'conversation:read': 'User can read conversations',
60
+ 'conversation:create': 'User can create new conversations',
61
+ 'conversation:update': 'User can update conversation status',
62
+ 'conversation:delete': 'User can delete conversations',
63
+ 'conversation:export': 'User can export conversation data',
64
+ 'conversation:analyze': 'User can analyze conversation patterns',
65
+
66
+ // Ticket Management
67
+ 'ticket:read': 'User can read support tickets',
68
+ 'ticket:create': 'User can create new tickets',
69
+ 'ticket:update': 'User can update ticket status',
70
+ 'ticket:delete': 'User can delete tickets',
71
+ 'ticket:assign': 'User can assign tickets to team members',
72
+ 'ticket:escalate': 'User can escalate tickets',
73
+
74
+ // Analytics & Reporting
75
+ 'analytics:read': 'User can view analytics dashboard',
76
+ 'analytics:export': 'User can export analytics data',
77
+ 'analytics:custom_reports': 'User can create custom reports',
78
+ 'analytics:real_time': 'User can view real-time analytics',
79
+
80
+ // File Management
81
+ 'file:upload': 'User can upload files',
82
+ 'file:download': 'User can download files',
83
+ 'file:delete': 'User can delete files',
84
+ 'file:share': 'User can share files with team members',
85
+
86
+ // Template Management (Marketplace)
87
+ 'template:read': 'User can read available templates',
88
+ 'template:create': 'User can create new templates',
89
+ 'template:update': 'User can update template content',
90
+ 'template:delete': 'User can delete templates',
91
+ 'template:publish': 'User can publish templates to marketplace',
92
+ 'template:subscribe': 'User can subscribe to templates',
93
+
94
+ // System Administration
95
+ 'admin:users': 'User can manage all users',
96
+ 'admin:businesses': 'User can manage all businesses',
97
+ 'admin:system_settings': 'User can modify system settings',
98
+ 'admin:logs': 'User can view system logs',
99
+ 'admin:backup': 'User can perform system backups',
100
+ 'admin:updates': 'User can manage system updates',
101
+
102
+ // Super Admin (Platform Level)
103
+ 'super:all': 'Super admin has access to everything',
104
+ 'super:platform_management': 'User can manage platform-wide settings',
105
+ 'super:billing': 'User can manage billing and subscriptions',
106
+ 'super:support': 'User can manage platform support',
107
+ 'super:marketplace': 'User can manage marketplace content',
108
+
109
+ // Webhook Management
110
+ 'webhook:read': 'User can read webhook configurations',
111
+ 'webhook:create': 'User can create new webhooks',
112
+ 'webhook:update': 'User can update webhook settings',
113
+ 'webhook:delete': 'User can delete webhooks',
114
+ 'webhook:test': 'User can test webhook endpoints',
115
+
116
+ // Subscription Management
117
+ 'subscription:read': 'User can read subscription details',
118
+ 'subscription:upgrade': 'User can upgrade subscription',
119
+ 'subscription:downgrade': 'User can downgrade subscription',
120
+ 'subscription:cancel': 'User can cancel subscription',
121
+ 'subscription:billing': 'User can manage billing information',
122
+
123
+ // Notification Management
124
+ 'notification:read': 'User can read notifications',
125
+ 'notification:update': 'User can update notification status',
126
+ 'notification:delete': 'User can delete notifications',
127
+
128
+ // Integration Management
129
+ 'integration:read': 'User can read integration details',
130
+ 'integration:create': 'User can create new integrations',
131
+ 'integration:update': 'User can update integration configurations',
132
+ 'integration:delete': 'User can delete integrations',
133
+
134
+ // Workflow Management
135
+ 'workflow:read': 'User can read workflow details',
136
+ 'workflow:create': 'User can create new workflows',
137
+ 'workflow:update': 'User can update workflow configurations',
138
+ 'workflow:delete': 'User can delete workflows',
139
+
140
+ // Lead Management
141
+ 'lead:read': 'User can read leads and lead templates',
142
+ 'lead:create': 'User can create new leads',
143
+ 'lead:update': 'User can update lead information',
144
+ 'lead:delete': 'User can delete leads',
145
+ 'lead:import': 'User can bulk import leads',
146
+ 'lead:export': 'User can export lead data',
147
+
148
+ // Message Management
149
+ 'message:read': 'User can read messages',
150
+ 'message:send': 'User can send messages in conversations',
151
+ 'message:delete': 'User can delete messages',
152
+
153
+ // Call Session Management
154
+ 'call:read': 'User can read call sessions and transcripts',
155
+ 'call:initiate': 'User can initiate outbound calls',
156
+ 'call:record': 'User can access call recordings',
157
+
158
+ // Campaign Management
159
+ 'campaign:read': 'User can read campaigns',
160
+ 'campaign:create': 'User can create new campaigns',
161
+ 'campaign:update': 'User can update campaigns',
162
+ 'campaign:delete': 'User can delete campaigns',
163
+ 'campaign:launch': 'User can launch and schedule campaigns',
164
+ 'campaign:analytics': 'User can view campaign analytics',
165
+
166
+ // Team Management
167
+ 'team:read': 'User can read team information',
168
+ 'team:create': 'User can create new teams',
169
+ 'team:update': 'User can update team settings',
170
+ 'team:delete': 'User can delete teams',
171
+ 'team:manage_members': 'User can add or remove team members',
172
+
173
+ // Conversation extras
174
+ 'conversation:assign': 'User can assign conversations to agents or teams',
175
+ 'conversation:reply': 'User can send messages in conversations',
176
+
177
+ // Channel extras
178
+ 'channel:manage_agents': 'User can assign agents to channels',
179
+
180
+ };
181
+
182
+ // Role-based scope mappings
183
+ const RoleScopes = {
184
+ 'superAdmin': [
185
+ 'super:all',
186
+ 'super:platform_management',
187
+ 'super:billing',
188
+ 'super:support',
189
+ 'super:marketplace',
190
+ 'admin:users',
191
+ 'admin:businesses',
192
+ 'admin:system_settings',
193
+ 'admin:logs',
194
+ 'admin:backup',
195
+ 'admin:updates',
196
+ 'business:read',
197
+ 'business:create',
198
+ 'business:update',
199
+ 'business:delete',
200
+ 'business:manage_members',
201
+ 'business:view_analytics',
202
+ 'business:export_data',
203
+ 'agent:read',
204
+ 'agent:create',
205
+ 'agent:update',
206
+ 'agent:delete',
207
+ 'agent:deploy',
208
+ 'agent:test',
209
+ 'agent:view_conversations',
210
+ 'agent:manage_prompts',
211
+ 'collection:read',
212
+ 'collection:create',
213
+ 'collection:update',
214
+ 'collection:delete',
215
+ 'collection:upload_files',
216
+ 'collection:manage_permissions',
217
+ 'channel:read',
218
+ 'channel:create',
219
+ 'channel:update',
220
+ 'channel:delete',
221
+ 'channel:connect',
222
+ 'channel:view_webhooks',
223
+ 'action:read',
224
+ 'action:create',
225
+ 'action:update',
226
+ 'action:delete',
227
+ 'action:test',
228
+ 'action:manage_integrations',
229
+ 'conversation:read',
230
+ 'conversation:create',
231
+ 'conversation:update',
232
+ 'conversation:delete',
233
+ 'conversation:export',
234
+ 'conversation:analyze',
235
+ 'ticket:read',
236
+ 'ticket:create',
237
+ 'ticket:update',
238
+ 'ticket:delete',
239
+ 'ticket:assign',
240
+ 'ticket:escalate',
241
+ 'analytics:read',
242
+ 'analytics:export',
243
+ 'analytics:custom_reports',
244
+ 'analytics:real_time',
245
+ 'file:upload',
246
+ 'file:download',
247
+ 'file:delete',
248
+ 'file:share',
249
+ 'template:read',
250
+ 'template:create',
251
+ 'template:update',
252
+ 'template:delete',
253
+ 'template:publish',
254
+ 'template:subscribe',
255
+ 'webhook:read',
256
+ 'webhook:create',
257
+ 'webhook:update',
258
+ 'webhook:delete',
259
+ 'webhook:test',
260
+ 'subscription:read',
261
+ 'subscription:upgrade',
262
+ 'subscription:downgrade',
263
+ 'subscription:cancel',
264
+ 'subscription:billing',
265
+ 'auth:login',
266
+ 'auth:register',
267
+ 'auth:logout',
268
+ 'auth:refresh',
269
+ 'user:read',
270
+ 'user:update',
271
+ 'user:delete',
272
+ 'notification:read',
273
+ 'notification:update',
274
+ 'notification:delete',
275
+ 'integration:read',
276
+ 'integration:create',
277
+ 'integration:update',
278
+ 'integration:delete',
279
+ 'workflow:read',
280
+ 'workflow:create',
281
+ 'workflow:update',
282
+ 'workflow:delete',
283
+ 'lead:read', 'lead:create', 'lead:update', 'lead:delete', 'lead:import', 'lead:export',
284
+ 'message:read', 'message:send', 'message:delete',
285
+ 'call:read', 'call:initiate', 'call:record',
286
+ 'campaign:read', 'campaign:create', 'campaign:update', 'campaign:delete', 'campaign:launch', 'campaign:analytics',
287
+ 'team:read', 'team:create', 'team:update', 'team:delete', 'team:manage_members',
288
+ 'conversation:assign', 'conversation:reply',
289
+ 'channel:manage_agents',
290
+ ],
291
+ 'admin': [
292
+ 'admin:users',
293
+ 'business:read',
294
+ 'business:update',
295
+ 'business:manage_members',
296
+ 'business:view_analytics',
297
+ 'business:export_data',
298
+ 'agent:read',
299
+ 'agent:create',
300
+ 'agent:update',
301
+ 'agent:delete',
302
+ 'agent:deploy',
303
+ 'agent:test',
304
+ 'agent:view_conversations',
305
+ 'agent:manage_prompts',
306
+ 'collection:read',
307
+ 'collection:create',
308
+ 'collection:update',
309
+ 'collection:delete',
310
+ 'collection:upload_files',
311
+ 'collection:manage_permissions',
312
+ 'channel:read',
313
+ 'channel:create',
314
+ 'channel:update',
315
+ 'channel:delete',
316
+ 'channel:connect',
317
+ 'channel:view_webhooks',
318
+ 'action:read',
319
+ 'action:create',
320
+ 'action:update',
321
+ 'action:delete',
322
+ 'action:test',
323
+ 'action:manage_integrations',
324
+ 'conversation:read',
325
+ 'conversation:create',
326
+ 'conversation:update',
327
+ 'conversation:delete',
328
+ 'conversation:export',
329
+ 'conversation:analyze',
330
+ 'ticket:read',
331
+ 'ticket:create',
332
+ 'ticket:update',
333
+ 'ticket:delete',
334
+ 'ticket:assign',
335
+ 'ticket:escalate',
336
+ 'analytics:read',
337
+ 'analytics:export',
338
+ 'analytics:custom_reports',
339
+ 'analytics:real_time',
340
+ 'file:upload',
341
+ 'file:download',
342
+ 'file:delete',
343
+ 'file:share',
344
+ 'template:read',
345
+ 'template:subscribe',
346
+ 'webhook:read',
347
+ 'webhook:create',
348
+ 'webhook:update',
349
+ 'webhook:delete',
350
+ 'webhook:test',
351
+ 'subscription:read',
352
+ 'subscription:upgrade',
353
+ 'subscription:downgrade',
354
+ 'subscription:cancel',
355
+ 'subscription:billing',
356
+ 'auth:login',
357
+ 'auth:logout',
358
+ 'auth:refresh',
359
+ 'user:read',
360
+ 'user:update',
361
+ 'notification:read',
362
+ 'notification:update',
363
+ 'notification:delete',
364
+ 'integration:read',
365
+ 'integration:create',
366
+ 'integration:update',
367
+ 'integration:delete',
368
+ 'workflow:read',
369
+ 'workflow:create',
370
+ 'workflow:update',
371
+ 'workflow:delete',
372
+ 'lead:read', 'lead:create', 'lead:update', 'lead:delete', 'lead:import', 'lead:export',
373
+ 'message:read', 'message:send', 'message:delete',
374
+ 'call:read', 'call:initiate', 'call:record',
375
+ 'campaign:read', 'campaign:create', 'campaign:update', 'campaign:delete', 'campaign:launch', 'campaign:analytics',
376
+ 'team:read', 'team:create', 'team:update', 'team:delete', 'team:manage_members',
377
+ 'conversation:assign', 'conversation:reply',
378
+ 'channel:manage_agents',
379
+ ],
380
+ 'manager': [
381
+ 'business:read',
382
+ 'business:view_analytics',
383
+ 'agent:read',
384
+ 'agent:test',
385
+ 'agent:view_conversations',
386
+ 'collection:read',
387
+ 'collection:upload_files',
388
+ 'channel:read',
389
+ 'action:read',
390
+ 'action:test',
391
+ 'conversation:read',
392
+ 'conversation:export',
393
+ 'conversation:analyze',
394
+ 'ticket:read',
395
+ 'ticket:create',
396
+ 'ticket:update',
397
+ 'analytics:read',
398
+ 'analytics:export',
399
+ 'file:upload',
400
+ 'file:download',
401
+ 'file:share',
402
+ 'template:read',
403
+ 'template:subscribe',
404
+ 'webhook:read',
405
+ 'subscription:read',
406
+ 'auth:login',
407
+ 'auth:logout',
408
+ 'auth:refresh',
409
+ 'user:read',
410
+ 'user:update',
411
+ 'notification:read',
412
+ 'workflow:read',
413
+ 'lead:read', 'lead:create', 'lead:update', 'lead:export',
414
+ 'message:read', 'message:send',
415
+ 'call:read', 'call:record',
416
+ 'campaign:read', 'campaign:analytics',
417
+ 'team:read',
418
+ 'conversation:assign', 'conversation:reply',
419
+ ]
420
+ };
421
+
422
+ const UserSchema = new Schema({
423
+ name: String,
424
+ email: { type: String, unique: true },
425
+ password: String,
426
+ role: { type: String, enum: ['admin', 'manager', 'superAdmin'], default: 'admin' },
427
+ scopes: [{ type: String, enum: Object.keys(ScopesEnum) }],
428
+ business: { type: Schema.Types.ObjectId, ref: 'Businesses' },
429
+ isVerified: { type: Boolean, default: false },
430
+ emailToken: String,
431
+ }, {
432
+ timestamps: true
433
+ });
434
+
435
+ // Pre-save middleware to assign default scopes based on role
436
+ UserSchema.pre('save', function (next) {
437
+ if (this.isNew || this.isModified('role')) {
438
+ this.scopes = RoleScopes[this.role] || [];
439
+ }
440
+ next();
441
+ });
442
+
443
+ // Method to check if user has a specific scope
444
+ UserSchema.methods.hasScope = function (scope) {
445
+ return this.scopes.includes(scope) || this.scopes.includes('super:all');
446
+ };
447
+
448
+ // Method to check if user has any of the provided scopes
449
+ UserSchema.methods.hasAnyScope = function (scopes) {
450
+ if (this.scopes.includes('super:all')) return true;
451
+ return scopes.some(scope => this.scopes.includes(scope));
452
+ };
453
+
454
+ // Method to check if user has all of the provided scopes
455
+ UserSchema.methods.hasAllScopes = function (scopes) {
456
+ if (this.scopes.includes('super:all')) return true;
457
+ return scopes.every(scope => this.scopes.includes(scope));
458
+ };
459
+
460
+ // Method to add scopes to user
461
+ UserSchema.methods.addScopes = function (scopes) {
462
+ const newScopes = scopes.filter(scope => !this.scopes.includes(scope));
463
+ this.scopes.push(...newScopes);
464
+ return this.save();
465
+ };
466
+
467
+ // Method to remove scopes from user
468
+ UserSchema.methods.removeScopes = function (scopes) {
469
+ this.scopes = this.scopes.filter(scope => !scopes.includes(scope));
470
+ return this.save();
471
+ };
472
+ const User = defineModel('Users', UserSchema, "Users");
473
+ export { User, ScopesEnum, RoleScopes };
@@ -0,0 +1,44 @@
1
+ import { Schema } from "mongoose";
2
+ import { defineModel } from "../runtime.js";
3
+
4
+ const WorkflowSchema = new Schema({
5
+ name: { type: String, default: "Untitled Workflow" },
6
+ status: { type: String, enum: ["draft", "active", "disabled"], default: "draft" },
7
+ nodes: Object,
8
+ connections: Object,
9
+ business: { type: Schema.Types.ObjectId, ref: "Businesses" },
10
+ createdBy: { type: Schema.Types.ObjectId, ref: "Users" },
11
+ }, {
12
+ timestamps: true,
13
+ });
14
+ WorkflowSchema.index({ createdBy: 1 });
15
+ WorkflowSchema.index({ status: 1 });
16
+ WorkflowSchema.methods.updateStatus = function (status) {
17
+ this.status = status;
18
+ return this.save();
19
+ };
20
+ WorkflowSchema.methods.getNextNode = function ({ currentNode = "", outputPortsOfCurentNode = [] } = {}) {
21
+ const nextNodes = [];
22
+ if (!currentNode) return [Object.keys(this.nodes).find((ele) => this.nodes[ele].type === "trigger")];
23
+ const { type } = this.nodes[currentNode];
24
+ Object.values(this.connections).forEach((ele) => {
25
+ if (!ele?.from || !ele?.to) {
26
+ console.log("errored no from or to");
27
+ return;
28
+ }
29
+ const [fromNodeId, , portId] = ele.from.split("/");
30
+ if (fromNodeId !== currentNode) {
31
+ return;
32
+ }
33
+ const [toNodeId] = ele.to.split("/");
34
+ if (type === "conditionalNode") {
35
+ if (outputPortsOfCurentNode.includes(portId)) nextNodes.push(toNodeId);
36
+ return;
37
+ }
38
+ nextNodes.push(toNodeId);
39
+ });
40
+ return nextNodes;
41
+ };
42
+
43
+ export { WorkflowSchema };
44
+ export const Workflow = defineModel("Workflow", WorkflowSchema, "Workflow");
@@ -0,0 +1,22 @@
1
+ import { Schema } from "mongoose";
2
+ import { defineModel } from "../runtime.js";
3
+
4
+ const WorkflowLogsSchema = new Schema({
5
+ workflowId: String,
6
+ trigger: {
7
+ conversationId: String,
8
+ },
9
+ executionId: String,
10
+ nodeId: String,
11
+ lable: String,
12
+ nodeInput: Schema.Types.Mixed,
13
+ nodeInputError: Schema.Types.Mixed,
14
+ nodeOutput: Schema.Types.Mixed,
15
+ nodeError: Schema.Types.Mixed,
16
+ nodeStatus: { type: String, enum: ["pending", "in-progress", "completed", "failed", "skipped"], default: "pending" },
17
+ }, {
18
+ timestamps: true
19
+ }
20
+ );
21
+
22
+ export const WorkflowLogs = defineModel('WorkflowLogs', WorkflowLogsSchema, 'WorkflowLogs');