38980-vm/app-9w9pd00g5j41/supabase/migrations/00063_fix_gdpr_anonymize.sql
2026-03-04 18:25:09 +00:00

48 lines
1.6 KiB
PL/PgSQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- GDPR FIX: Complete erasure including auth.users and leads contact info.
CREATE OR REPLACE FUNCTION anonymize_user_data(p_user_id UUID)
RETURNS BOOLEAN AS $$
BEGIN
-- 1. Anonymize user profile
UPDATE profiles SET
username = 'deleted_' || substring(p_user_id::text, 1, 8),
full_name = 'Silinen Kullanıcı',
avatar_url = NULL,
bio = NULL,
updated_at = NOW()
WHERE id = p_user_id;
-- 2. Anonymize provider profile if exists
UPDATE providers SET
company_name = 'Silinen Sağlayıcı',
contact_email = 'deleted_' || substring(p_user_id::text, 1, 8) || '@anonymized.local',
phone = NULL,
website = NULL,
updated_at = NOW()
WHERE user_id = p_user_id;
-- 3. Anonymize leads submitted by this user (GDPR: contact info must be erased)
UPDATE leads SET
email = 'deleted_' || substring(p_user_id::text, 1, 8) || '@anonymized.local',
whatsapp = 'deleted',
timeline_snapshot = NULL,
planned_activities = NULL,
updated_at = NOW()
WHERE user_id = p_user_id;
-- 4. Mark trips as anonymized (keep for provider history, remove personal title)
UPDATE trips SET
title = 'Silinen Seyahat',
updated_at = NOW()
WHERE user_id = p_user_id;
-- 5. Log before deletion
INSERT INTO audit_logs (user_id, action, resource_type, resource_id)
VALUES (p_user_id, 'gdpr_erasure', 'user', p_user_id);
-- 6. Delete from auth.users (requires service_role, which SECURITY DEFINER provides)
DELETE FROM auth.users WHERE id = p_user_id;
RETURN TRUE;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;