File size: 4,545 Bytes
d125a03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
import { sql } from "drizzle-orm";
import { pgTable, text, varchar, integer, boolean, timestamp, real } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod";

// AI Agent schema
export const agents = pgTable("agents", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  name: text("name").notNull(),
  status: text("status").notNull().default("active"), // active, waiting, issue
  activeCallCount: integer("active_call_count").notNull().default(0),
});

export const insertAgentSchema = createInsertSchema(agents).omit({ id: true });
export type InsertAgent = z.infer<typeof insertAgentSchema>;
export type Agent = typeof agents.$inferSelect;

// Call schema
export const calls = pgTable("calls", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  agentId: varchar("agent_id").notNull(),
  customerId: varchar("customer_id").notNull(),
  status: text("status").notNull().default("active"), // active, on-hold, completed
  duration: integer("duration").notNull().default(0), // in seconds
  startTime: timestamp("start_time").notNull().defaultNow(),
  currentSpeaker: text("current_speaker").notNull().default("ai"), // ai, customer
  sentimentScore: real("sentiment_score").notNull().default(0.5),
  upsellAttempts: integer("upsell_attempts").notNull().default(0),
  upsellSuccess: boolean("upsell_success").notNull().default(false),
});

export const insertCallSchema = createInsertSchema(calls).omit({ id: true });
export type InsertCall = z.infer<typeof insertCallSchema>;
export type Call = typeof calls.$inferSelect;

// Transcript message schema
export const transcriptMessages = pgTable("transcript_messages", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  callId: varchar("call_id").notNull(),
  speaker: text("speaker").notNull(), // ai, customer
  content: text("content").notNull(),
  timestamp: timestamp("timestamp").notNull().defaultNow(),
});

export const insertTranscriptMessageSchema = createInsertSchema(transcriptMessages).omit({ id: true });
export type InsertTranscriptMessage = z.infer<typeof insertTranscriptMessageSchema>;
export type TranscriptMessage = typeof transcriptMessages.$inferSelect;

// Customer schema
export const customers = pgTable("customers", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  name: text("name").notNull(),
  email: text("email").notNull(),
  isVip: boolean("is_vip").notNull().default(false),
  engagementScore: real("engagement_score").notNull().default(50),
  purchaseHistory: integer("purchase_history").notNull().default(0), // total purchases count
  totalSpend: real("total_spend").notNull().default(0),
});

export const insertCustomerSchema = createInsertSchema(customers).omit({ id: true });
export type InsertCustomer = z.infer<typeof insertCustomerSchema>;
export type Customer = typeof customers.$inferSelect;

// Personalized offer schema
export const offers = pgTable("offers", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  customerId: varchar("customer_id").notNull(),
  title: text("title").notNull(),
  description: text("description").notNull(),
  discount: real("discount").notNull().default(0),
  priority: integer("priority").notNull().default(1),
});

export const insertOfferSchema = createInsertSchema(offers).omit({ id: true });
export type InsertOffer = z.infer<typeof insertOfferSchema>;
export type Offer = typeof offers.$inferSelect;

// KPI summary type (not stored, computed)
export type KPISummary = {
  totalCalls: number;
  averageDuration: number;
  upsellSuccessRate: number;
  aiHandledPercentage: number;
  activeAgents: number;
  waitingAgents: number;
  issueAgents: number;
};

// Alert type
export type Alert = {
  id: string;
  level: "info" | "warning" | "critical";
  message: string;
  timestamp: Date;
};

// Timeline event type
export type TimelineEvent = {
  id: string;
  callId: string;
  type: "call_start" | "upsell_attempt" | "sentiment_change" | "transfer" | "call_end";
  description: string;
  timestamp: Date;
};

// Users table (keeping for auth compatibility)
export const users = pgTable("users", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  username: text("username").notNull().unique(),
  password: text("password").notNull(),
});

export const insertUserSchema = createInsertSchema(users).pick({
  username: true,
  password: true,
});

export type InsertUser = z.infer<typeof insertUserSchema>;
export type User = typeof users.$inferSelect;