-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-reports-function.sql
More file actions
41 lines (39 loc) · 1.02 KB
/
Copy pathget-reports-function.sql
File metadata and controls
41 lines (39 loc) · 1.02 KB
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
-- Function to get reports with readable coordinates
-- Run this in your Supabase SQL Editor
CREATE OR REPLACE FUNCTION get_reports_with_coordinates()
RETURNS TABLE (
id UUID,
description TEXT,
status TEXT,
created_at TIMESTAMP WITH TIME ZONE,
priority TEXT,
longitude DOUBLE PRECISION,
latitude DOUBLE PRECISION,
category_name TEXT,
category_color TEXT,
category_id UUID
)
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
BEGIN
RETURN QUERY
SELECT
r.id,
r.description,
r.status,
r.created_at,
r.priority,
ST_X(r.location) as longitude,
ST_Y(r.location) as latitude,
c.name as category_name,
c.color as category_color,
c.id as category_id
FROM public.reports r
LEFT JOIN public.categories c ON r.category_id = c.id
WHERE r.location IS NOT NULL
ORDER BY r.created_at DESC;
END;
$$;
-- Grant execute permission to authenticated users
GRANT EXECUTE ON FUNCTION get_reports_with_coordinates() TO authenticated;