|
@@ -2100,6 +2100,94 @@ serve(async (req) => {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // GET /api/call-logs - List all call logs for the user's stores
|
|
|
|
|
+ if (path === 'call-logs' && req.method === 'GET') {
|
|
|
|
|
+ // First, get all store IDs that belong to this user
|
|
|
|
|
+ const { data: userStores, error: storesError } = await supabase
|
|
|
|
|
+ .from('stores')
|
|
|
|
|
+ .select('id')
|
|
|
|
|
+ .eq('user_id', user.id)
|
|
|
|
|
+
|
|
|
|
|
+ if (storesError) {
|
|
|
|
|
+ console.error('Error fetching user stores:', storesError)
|
|
|
|
|
+ return new Response(
|
|
|
|
|
+ JSON.stringify({ error: 'Failed to fetch stores' }),
|
|
|
|
|
+ { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const storeIds = userStores?.map(s => s.id) || []
|
|
|
|
|
+
|
|
|
|
|
+ if (storeIds.length === 0) {
|
|
|
|
|
+ return new Response(
|
|
|
|
|
+ JSON.stringify({ success: true, call_logs: [] }),
|
|
|
|
|
+ { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Fetch call logs for these stores
|
|
|
|
|
+ const { data: callLogs, error: logsError } = await supabase
|
|
|
|
|
+ .from('call_logs')
|
|
|
|
|
+ .select('id, store_id, created_at, started_at, ended_at, duration, caller, cost_total')
|
|
|
|
|
+ .in('store_id', storeIds)
|
|
|
|
|
+ .order('created_at', { ascending: false })
|
|
|
|
|
+
|
|
|
|
|
+ if (logsError) {
|
|
|
|
|
+ console.error('Error fetching call logs:', logsError)
|
|
|
|
|
+ return new Response(
|
|
|
|
|
+ JSON.stringify({ error: 'Failed to fetch call logs' }),
|
|
|
|
|
+ { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return new Response(
|
|
|
|
|
+ JSON.stringify({ success: true, call_logs: callLogs || [] }),
|
|
|
|
|
+ { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // GET /api/call-logs/:id - Get a single call log by ID
|
|
|
|
|
+ const callLogMatch = path.match(/^call-logs\/([a-f0-9-]+)$/)
|
|
|
|
|
+ if (callLogMatch && req.method === 'GET') {
|
|
|
|
|
+ const callLogId = callLogMatch[1]
|
|
|
|
|
+
|
|
|
|
|
+ // First, get all store IDs that belong to this user
|
|
|
|
|
+ const { data: userStores, error: storesError } = await supabase
|
|
|
|
|
+ .from('stores')
|
|
|
|
|
+ .select('id')
|
|
|
|
|
+ .eq('user_id', user.id)
|
|
|
|
|
+
|
|
|
|
|
+ if (storesError) {
|
|
|
|
|
+ console.error('Error fetching user stores:', storesError)
|
|
|
|
|
+ return new Response(
|
|
|
|
|
+ JSON.stringify({ error: 'Failed to fetch stores' }),
|
|
|
|
|
+ { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const storeIds = userStores?.map(s => s.id) || []
|
|
|
|
|
+
|
|
|
|
|
+ // Fetch the specific call log (only if it belongs to user's stores)
|
|
|
|
|
+ const { data: callLog, error: logError } = await supabase
|
|
|
|
|
+ .from('call_logs')
|
|
|
|
|
+ .select('*')
|
|
|
|
|
+ .eq('id', callLogId)
|
|
|
|
|
+ .in('store_id', storeIds)
|
|
|
|
|
+ .single()
|
|
|
|
|
+
|
|
|
|
|
+ if (logError || !callLog) {
|
|
|
|
|
+ return new Response(
|
|
|
|
|
+ JSON.stringify({ error: 'Call log not found' }),
|
|
|
|
|
+ { status: 404, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return new Response(
|
|
|
|
|
+ JSON.stringify({ success: true, call_log: callLog }),
|
|
|
|
|
+ { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return new Response(
|
|
return new Response(
|
|
|
JSON.stringify({ error: 'Not found' }),
|
|
JSON.stringify({ error: 'Not found' }),
|
|
|
{ status: 404, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
{ status: 404, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|