Advertisement
brunobozic

Untitled

Jan 11th, 2023
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION companies.search_companies(
  2.     _search character varying,
  3.     _countries smallint[],
  4.     _areas smallint[],
  5.     _sort_asc boolean,
  6.     _skip integer,
  7.     _take integer
  8. )
  9. RETURNS json
  10. LANGUAGE plpgsql
  11. AS $$
  12. declare
  13.     _count bigint;
  14.     _companies uuid[];
  15. begin    
  16.     _search = trim(_search);
  17.    
  18.     if _search = '' then
  19.         _search = null;
  20.     end if;
  21.    
  22.     if _search is not null then
  23.         _search = '%' || _search || '%';
  24.     end if;
  25.    
  26.     if array_length(_areas, 1) is not null then
  27.         _companies = (select distinct array_agg(company_id) from company_areas where area_id = any(_areas));
  28.     end if;
  29.    
  30.     create temp table _tmp on commit drop as
  31.     select
  32.         c.id
  33.     from
  34.         companies c
  35.     where (
  36.         (_search is null or name ilike _search or company_line ilike _search)
  37.         and
  38.         (array_length(_countries, 1) is null or country = any(_countries))
  39.         and
  40.         (array_length(_companies, 1) is null or id = any(_companies))
  41.     );
  42.     get diagnostics _count = row_count;
  43.    
  44.     return json_build_object(
  45.         'count', _count,
  46.         'page', (
  47.             select json_agg(sub)
  48.             from (
  49.                 select
  50.                     cm.id,
  51.                     cm.name,
  52.                     company_line as companyLine,
  53.                     about,
  54.                     cn.code as countryCode,
  55.                     cn.iso2 as countryIso2,
  56.                     cn.name as country,
  57.                     cm.web,
  58.                     cm.twitter,
  59.                     cm.linkedin,
  60.                     coalesce(array_agg(json_build_object('id', ba.id, 'name', ba.name)) filter (where ba.name is not null), array[]::json[]) as areas,
  61.                     reviews.count as reviews,
  62.                     reviews.score
  63.                 from
  64.                     _tmp tmp
  65.                     inner join companies cm on tmp.id = cm.id
  66.                     inner join countries cn on cm.country = cn.code
  67.                     left outer join company_areas ca on cm.id = ca.company_id
  68.                     left outer join business_areas ba on ca.area_id = ba.id
  69.                     join lateral (
  70.                         select
  71.                             count(id),
  72.                             avg(score) filter (where score is not null)::numeric(3,2) as score
  73.                         from public.company_reviews
  74.                         where company_id = cm.id
  75.                     ) reviews on true
  76.                 group by
  77.                     cm.id, cn.code, reviews.count, reviews.score
  78.                 order by
  79.                     case when _sort_asc is true then cm.name end asc,
  80.                     case when _sort_asc is false then cm.name end desc
  81.                 limit _take
  82.                 offset _skip
  83.             ) sub
  84.         )
  85.     );
  86. end
  87. $$;
  88.  
  89. COMMENT ON FUNCTION companies.search_companies(_search character varying, _countries smallint[], _areas smallint[], _sort_asc boolean, _skip integer, _take integer) IS 'Search companies by search string (name or company line), or by countries or areas selection.
  90. Result is pageable JSON response `{count, data: [...]}`';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement