-- This view shows EVERY ad separately (one row per ad)
CREATE OR REPLACE VIEW public_tutor_ads AS
SELECT
-- Ad information
a.ad_id,
a.hourly_rate,
a.description,
a.title,
a.first_30_min_free,
a.search_tags,
a.academic_levels,
a.created_at as ad_created_at,
-- Tutor information (excluding sensitive fields)
t.tutor_id,
t.user_id,
t.total_class_hours_given,
t.average_review_score,
t.total_review_count,
t.about_me,
t.video_url,
t.diploma_url,
t.id_verified,
t.diploma_verified,
t.created_at as tutor_joined_date,
-- Safe user fields (NO email, NO sensitive PII)
u.first_name,
u.last_name,
u.gender,
u.languages,
u.avatar_image_url,
u.country,
u.created_at as user_created_at
FROM ads a
INNER JOIN tutors t ON a.created_by_tutor_id = t.tutor_id
INNER JOIN users u ON t.user_id = u.user_id
WHERE a.is_enabled = true;
-- 3. Grant public access to the view ONLY
GRANT SELECT ON public_tutor_ads TO anon;
GRANT SELECT ON public_tutor_ads TO authenticated;
-- 4. Add helpful comment
COMMENT ON VIEW public_tutor_ads IS
'Public view of tutor ads with only safe, non-sensitive information. Each row represents one ad. Excludes email, r2_id_key, and total_cash_earned.';
-- 5. Verify the view works
-- Run this to test:
SELECT * FROM public_tutor_ads LIMIT 5;
-- This view shows EVERY ad separately (one row per ad)
CREATE OR REPLACE VIEW public_tutor_ads AS
SELECT
-- Ad information
a.ad_id,
a.hourly_rate,
a.description,
a.title,
a.first_30_min_free,
a.search_tags,
a.academic_levels,
a.created_at as ad_created_at,
-- Tutor information (excluding sensitive fields)
t.tutor_id,
t.user_id,
t.total_class_hours_given,
t.average_review_score,
t.total_review_count,
t.about_me,
t.video_url,
t.diploma_url,
t.id_verified,
t.diploma_verified,
t.created_at as tutor_joined_date,
-- Safe user fields (NO email, NO sensitive PII)
u.first_name,
u.last_name,
u.gender,
u.languages,
u.avatar_image_url,
u.country,
u.created_at as user_created_at
FROM ads a
INNER JOIN tutors t ON a.created_by_tutor_id = t.tutor_id
INNER JOIN users u ON t.user_id = u.user_id
WHERE a.is_enabled = true;
-- 3. Grant public access to the view ONLY
GRANT SELECT ON public_tutor_ads TO anon;
GRANT SELECT ON public_tutor_ads TO authenticated;
-- 4. Add helpful comment
COMMENT ON VIEW public_tutor_ads IS
'Public view of tutor ads with only safe, non-sensitive information. Each row represents one ad. Excludes email, r2_id_key, and total_cash_earned.';
-- 5. Verify the view works
-- Run this to test:
SELECT * FROM public_tutor_ads LIMIT 5;