|
| 1 | +set check_function_bodies = off; |
| 2 | + |
| 3 | +CREATE OR REPLACE FUNCTION public.profile_lookup(lookup_type lookup_type_enum, identifier text) |
| 4 | + RETURNS TABLE(id uuid, avatar_url text, name text, about text, refcode text, tag citext, address citext, chain_id integer, is_public boolean, sendid integer, all_tags text[]) |
| 5 | + LANGUAGE plpgsql |
| 6 | + IMMUTABLE SECURITY DEFINER |
| 7 | +AS $function$ |
| 8 | +begin |
| 9 | + if identifier is null or identifier = '' then raise exception 'identifier cannot be null or empty'; end if; |
| 10 | + if lookup_type is null then raise exception 'lookup_type cannot be null'; end if; |
| 11 | + return query -- |
| 12 | + select case when p.id = ( select auth.uid() ) then p.id end as id, |
| 13 | + p.avatar_url::text as avatar_url, |
| 14 | + p.name::text as name, |
| 15 | + p.about::text as about, |
| 16 | + p.referral_code as refcode, |
| 17 | + t.name as tag, |
| 18 | + sa.address as address, |
| 19 | + sa.chain_id as chain_id, |
| 20 | + case when current_setting('role')::text = 'service_role' then p.is_public |
| 21 | + when p.is_public then true |
| 22 | + else false end as is_public, |
| 23 | + p.send_id as sendid, |
| 24 | + ( select array_agg(t.name::text) |
| 25 | + from tags t |
| 26 | + where t.user_id = p.id and t.status = 'confirmed'::tag_status ) as all_tags |
| 27 | + from profiles p |
| 28 | + join auth.users a on a.id = p.id |
| 29 | + left join tags t on t.user_id = p.id and t.status = 'confirmed'::tag_status |
| 30 | + left join send_accounts sa on sa.user_id = p.id |
| 31 | + where ((lookup_type = 'sendid' and p.send_id::text = identifier) or |
| 32 | + (lookup_type = 'tag' and t.name = identifier::citext) or |
| 33 | + (lookup_type = 'refcode' and p.referral_code = identifier) or |
| 34 | + (lookup_type = 'address' and sa.address = identifier) or |
| 35 | + (lookup_type = 'phone' and a.phone::text = identifier)) |
| 36 | + and (p.is_public -- allow public profiles to be returned |
| 37 | + or ( select auth.uid() ) is not null -- allow profiles to be returned if the user is authenticated |
| 38 | + or current_setting('role')::text = 'service_role') -- allow public profiles to be returned to service role |
| 39 | + limit 1; |
| 40 | +end; |
| 41 | +$function$ |
| 42 | +; |
| 43 | + |
0 commit comments