-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall_tracking_setup.sql
144 lines (131 loc) · 3.75 KB
/
call_tracking_setup.sql
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
# Add call tracking functionality
Execute this in the Supabase SQL Editor:
1. Go to your Supabase dashboard
2. Navigate to the SQL Editor
3. Paste this entire file
4. Run the SQL to create all tables and functions
*/
-- Create call_tracking table
CREATE TABLE IF NOT EXISTS call_tracking (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tracking_id UUID DEFAULT gen_random_uuid(),
lead_sequence_id UUID REFERENCES lead_sequences(id),
step_id UUID,
lead_id UUID REFERENCES leads(id),
phone_number TEXT NOT NULL,
call_id TEXT,
status TEXT,
duration INTEGER,
recording_url TEXT,
error_message TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
started_at TIMESTAMPTZ,
completed_at TIMESTAMPTZ,
failed_at TIMESTAMPTZ
);
-- Create call_conversations table
CREATE TABLE IF NOT EXISTS call_conversations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
call_tracking_id UUID REFERENCES call_tracking(id),
transcript TEXT,
conversation_data JSONB,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Enable RLS
ALTER TABLE call_tracking ENABLE ROW LEVEL SECURITY;
ALTER TABLE call_conversations ENABLE ROW LEVEL SECURITY;
-- Create a stored procedure to bypass RLS for call tracking
CREATE OR REPLACE FUNCTION create_call_tracking(
p_lead_sequence_id UUID,
p_step_id UUID,
p_phone_number TEXT,
p_lead_id UUID
)
RETURNS JSONB
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
v_tracking_id UUID := gen_random_uuid();
v_result JSONB;
BEGIN
-- Insert the record
INSERT INTO call_tracking (
tracking_id,
lead_sequence_id,
step_id,
phone_number,
lead_id,
created_at
) VALUES (
v_tracking_id,
p_lead_sequence_id,
p_step_id,
p_phone_number,
p_lead_id,
NOW()
)
RETURNING to_jsonb(call_tracking.*) INTO v_result;
RETURN v_result;
END;
$$;
-- Grant execute permission to authenticated users
GRANT EXECUTE ON FUNCTION create_call_tracking TO authenticated;
-- Add RLS policies for call_tracking
CREATE POLICY "Enable insert for authenticated users on call_tracking"
ON call_tracking
FOR INSERT
TO authenticated
WITH CHECK (true);
CREATE POLICY "Enable select for users own tracking data on call_tracking"
ON call_tracking
FOR SELECT
TO authenticated
USING (
EXISTS (
SELECT 1
FROM lead_sequences ls
JOIN sequences s ON s.id = ls.sequence_id
WHERE ls.id = call_tracking.lead_sequence_id
AND s.user_id = auth.uid()
)
OR true -- For development, allow all reads
);
-- Add RLS policies for call_conversations
CREATE POLICY "Enable insert for authenticated users on call_conversations"
ON call_conversations
FOR INSERT
TO authenticated
WITH CHECK (true);
CREATE POLICY "Enable select for users own call conversations"
ON call_conversations
FOR SELECT
TO authenticated
USING (
EXISTS (
SELECT 1
FROM call_tracking ct
JOIN lead_sequences ls ON ls.id = ct.lead_sequence_id
JOIN sequences s ON s.id = ls.sequence_id
WHERE ct.id = call_conversations.call_tracking_id
AND s.user_id = auth.uid()
)
OR true -- For development, allow all reads
);
-- Add indexes for performance
CREATE INDEX IF NOT EXISTS idx_call_tracking_lead_id ON call_tracking(lead_id);
CREATE INDEX IF NOT EXISTS idx_call_tracking_lead_sequence_id ON call_tracking(lead_sequence_id);
CREATE INDEX IF NOT EXISTS idx_call_tracking_call_id ON call_tracking(call_id);
CREATE INDEX IF NOT EXISTS idx_call_conversations_call_tracking_id ON call_conversations(call_tracking_id);
-- Add a phone field to leads table if it doesn't exist
DO $$
BEGIN
IF NOT EXISTS (
SELECT FROM information_schema.columns
WHERE table_name = 'leads' AND column_name = 'phone'
) THEN
ALTER TABLE leads ADD COLUMN phone TEXT;
END IF;
END
$$;