|
@@ -2102,38 +2102,10 @@ serve(async (req) => {
|
|
|
|
|
|
|
|
// GET /api/call-logs - List all call logs for the user's stores
|
|
// GET /api/call-logs - List all call logs for the user's stores
|
|
|
if (path === 'call-logs' && req.method === 'GET') {
|
|
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' } }
|
|
|
|
|
- )
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Use service role to bypass RLS for call_logs (we've already verified store ownership above)
|
|
|
|
|
- const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
|
|
|
|
|
- const supabaseAdmin = createClient(supabaseUrl, supabaseServiceKey)
|
|
|
|
|
-
|
|
|
|
|
- // Fetch call logs for these stores
|
|
|
|
|
- const { data: callLogs, error: logsError } = await supabaseAdmin
|
|
|
|
|
|
|
+ // Fetch call logs - RLS policy ensures user can only see logs for their stores
|
|
|
|
|
+ const { data: callLogs, error: logsError } = await supabase
|
|
|
.from('call_logs')
|
|
.from('call_logs')
|
|
|
.select('id, store_id, created_at, started_at, ended_at, duration, caller, cost_total')
|
|
.select('id, store_id, created_at, started_at, ended_at, duration, caller, cost_total')
|
|
|
- .in('store_id', storeIds)
|
|
|
|
|
.order('created_at', { ascending: false })
|
|
.order('created_at', { ascending: false })
|
|
|
|
|
|
|
|
if (logsError) {
|
|
if (logsError) {
|
|
@@ -2155,32 +2127,11 @@ serve(async (req) => {
|
|
|
if (callLogMatch && req.method === 'GET') {
|
|
if (callLogMatch && req.method === 'GET') {
|
|
|
const callLogId = callLogMatch[1]
|
|
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) || []
|
|
|
|
|
-
|
|
|
|
|
- // Use service role to bypass RLS for call_logs (we've already verified store ownership above)
|
|
|
|
|
- const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
|
|
|
|
|
- const supabaseAdmin = createClient(supabaseUrl, supabaseServiceKey)
|
|
|
|
|
-
|
|
|
|
|
- // Fetch the specific call log (only if it belongs to user's stores)
|
|
|
|
|
- const { data: callLog, error: logError } = await supabaseAdmin
|
|
|
|
|
|
|
+ // Fetch the specific call log - RLS policy ensures user can only see logs for their stores
|
|
|
|
|
+ const { data: callLog, error: logError } = await supabase
|
|
|
.from('call_logs')
|
|
.from('call_logs')
|
|
|
.select('*')
|
|
.select('*')
|
|
|
.eq('id', callLogId)
|
|
.eq('id', callLogId)
|
|
|
- .in('store_id', storeIds)
|
|
|
|
|
.single()
|
|
.single()
|
|
|
|
|
|
|
|
if (logError || !callLog) {
|
|
if (logError || !callLog) {
|