47 lines
No EOL
1.3 KiB
SQL
47 lines
No EOL
1.3 KiB
SQL
CREATE OR REPLACE VIEW user_tablos
|
|
WITH (security_invoker)
|
|
AS
|
|
SELECT DISTINCT
|
|
t.id,
|
|
ta.user_id,
|
|
t.name,
|
|
t.image,
|
|
t.color,
|
|
t.status,
|
|
t.position,
|
|
t.created_at,
|
|
t.deleted_at,
|
|
-- Access information
|
|
CASE
|
|
WHEN ta.is_admin = TRUE THEN 'admin'
|
|
ELSE 'member'
|
|
END as access_level,
|
|
ta.is_admin as is_admin
|
|
FROM tablos t
|
|
LEFT JOIN tablo_access ta ON t.id = ta.tablo_id
|
|
WHERE
|
|
ta.is_active = TRUE
|
|
AND
|
|
t.deleted_at IS NULL
|
|
ORDER BY t.position ASC, t.created_at DESC;
|
|
|
|
-- Add comment to document the view
|
|
COMMENT ON VIEW user_tablos IS
|
|
'View that returns all tablos accessible to the current authenticated user, including owned tablos and shared tablos with active access';
|
|
|
|
-- Example usage queries:
|
|
|
|
-- 1. Get all tablos for the current user
|
|
-- SELECT * FROM user_tablos;
|
|
|
|
-- 2. Get only tablos where user is owner
|
|
-- SELECT * FROM user_tablos WHERE access_level = 'owner';
|
|
|
|
-- 3. Get only shared tablos where user is admin
|
|
-- SELECT * FROM user_tablos WHERE access_level = 'admin' AND owner_id != (SELECT auth.uid());
|
|
|
|
-- 4. Get tablos by status
|
|
-- SELECT * FROM user_tablos WHERE status = 'in_progress';
|
|
|
|
-- 5. Count tablos by access level
|
|
-- SELECT access_level, COUNT(*) FROM user_tablos GROUP BY access_level; |