1+ ALTER TABLE private .refinedpersons
2+ ALTER COLUMN created_at TYPE timestamptz ,
3+ ALTER COLUMN updated_at TYPE timestamptz ;
4+
5+ ALTER TABLE private .persons
6+ ALTER COLUMN created_at TYPE timestamptz ,
7+ ALTER COLUMN updated_at TYPE timestamptz ;
8+
9+ DROP FUNCTION private .get_contacts_table ;
10+ CREATE FUNCTION private .get_contacts_table(user_id uuid) RETURNS TABLE(source text , email text , name text , status text , image text , location text [], alternate_name text [], alternate_email text [], telephone text [], same_as text [], given_name text , family_name text , job_title text , works_for text , recency timestamptz , seniority timestamptz , occurrence integer , sender integer , recipient integer , conversations integer , replied_conversations integer , tags text [], updated_at timestamptz , created_at timestamptz )
11+ LANGUAGE plpgsql
12+ SET search_path = ' '
13+ AS $$
14+ BEGIN
15+ RETURN QUERY WITH ExportedContacts AS (
16+ SELECT
17+ p .source as source_col,
18+ p .email as email_col,
19+ p .name as name_col,
20+ p .status as status_col,
21+ p .image as image_col,
22+ p .location as location_col,
23+ p .alternate_name as alternate_name_col,
24+ p .alternate_email as alternate_email_col,
25+ p .telephone as telephone_col,
26+ p .same_as as same_as_col,
27+ p .given_name as given_name_col,
28+ p .family_name as family_name_col,
29+ p .job_title as job_title_col,
30+ o .name as works_for_col,
31+ rp .recency as recency_col,
32+ rp .seniority as seniority_col,
33+ rp .occurrence as occurrence_col,
34+ rp .sender as sender_col,
35+ rp .recipient as recipient_col,
36+ rp .conversations as conversations_col,
37+ rp .replied_conversations as replied_conversations_col,
38+ rp .tags as tags_col,
39+ p .updated_at as updated_at_col,
40+ p .created_at as created_at_col,
41+ ROW_NUMBER() OVER (
42+ PARTITION BY p .email
43+ ) AS rn
44+ FROM
45+ private .persons p
46+ INNER JOIN
47+ private .refinedpersons rp ON rp .email = p .email
48+ LEFT JOIN
49+ private .organizations o ON o .id = p .works_for
50+ WHERE
51+ p .user_id = get_contacts_table .user_id
52+ ORDER BY
53+ rp .occurrence DESC , rp .recency DESC
54+ )
55+ SELECT
56+ source_col AS source,
57+ email_col AS email,
58+ name_col AS name,
59+ status_col AS status,
60+ image_col as image,
61+ location_col as location,
62+ alternate_name_col as alternate_name,
63+ alternate_email_col as alternate_email,
64+ telephone_col as telephone,
65+ same_as_col as same_as,
66+ given_name_col as given_name,
67+ family_name_col as family_name,
68+ job_title_col as job_title,
69+ works_for_col as works_for,
70+ recency_col AS recency,
71+ seniority_col AS seniority,
72+ occurrence_col AS occurrence,
73+ sender_col AS sender,
74+ recipient_col AS recipient,
75+ conversations_col AS conversations,
76+ replied_conversations_col AS replied_conversations,
77+ tags_col AS tags,
78+ updated_at_col AS updated_at,
79+ created_at_col AS created_at
80+ FROM
81+ ExportedContacts
82+ WHERE
83+ rn = 1 ;
84+ END;
85+ $$;
86+
87+ DROP FUNCTION private .get_contacts_table_by_emails ;
88+ CREATE FUNCTION private .get_contacts_table_by_emails(user_id uuid, emails text []) RETURNS TABLE(source text , email text , name text , status text , image text , location text [], alternate_name text [], alternate_email text [], telephone text [], same_as text [], given_name text , family_name text , job_title text , works_for text , recency timestamptz , seniority timestamptz , occurrence integer , sender integer , recipient integer , conversations integer , replied_conversations integer , tags text [], updated_at timestamptz , created_at timestamptz )
89+ LANGUAGE plpgsql
90+ SET search_path = ' '
91+ AS $$
92+ BEGIN
93+ RETURN QUERY WITH ExportedContacts AS (
94+ SELECT
95+ p .source as source_col,
96+ p .email as email_col,
97+ p .name as name_col,
98+ p .status as status_col,
99+ p .image as image_col,
100+ p .location as location_col,
101+ p .alternate_name as alternate_name_col,
102+ P .alternate_email as alternate_email_col,
103+ p .telephone as telephone_col,
104+ p .same_as as same_as_col,
105+ p .given_name as given_name_col,
106+ p .family_name as family_name_col,
107+ p .job_title as job_title_col,
108+ o .name as works_for_col,
109+ rp .recency as recency_col,
110+ rp .seniority as seniority_col,
111+ rp .occurrence as occurrence_col,
112+ rp .sender as sender_col,
113+ rp .recipient as recipient_col,
114+ rp .conversations as conversations_col,
115+ rp .replied_conversations as replied_conversations_col,
116+ rp .tags as tags_col,
117+ p .updated_at as updated_at_col,
118+ p .created_at as created_at_col,
119+ ROW_NUMBER() OVER (
120+ PARTITION BY p .email
121+ ) AS rn
122+ FROM
123+ private .persons p
124+ INNER JOIN
125+ private .refinedpersons rp ON rp .email = p .email
126+ LEFT JOIN
127+ private .organizations o ON o .id = p .works_for
128+ WHERE
129+ p .user_id = get_contacts_table_by_emails .user_id
130+ AND
131+ p .email = ANY(get_contacts_table_by_emails .emails )
132+ ORDER BY
133+ rp .occurrence DESC , rp .recency DESC
134+ )
135+ SELECT
136+ source_col AS source,
137+ email_col AS email,
138+ name_col AS name,
139+ status_col AS status,
140+ image_col as image,
141+ location_col as location,
142+ alternate_name_col as alternate_name,
143+ alternate_email_col as alternate_email,
144+ telephone_col as telephone,
145+ same_as_col as same_as,
146+ given_name_col as given_name,
147+ family_name_col as family_name,
148+ job_title_col as job_title,
149+ works_for_col as works_for,
150+ recency_col AS recency,
151+ seniority_col AS seniority,
152+ occurrence_col AS occurrence,
153+ sender_col AS sender,
154+ recipient_col AS recipient,
155+ conversations_col AS conversations,
156+ replied_conversations_col AS replied_conversations,
157+ tags_col AS tags,
158+ updated_at_col AS updated_at,
159+ created_at_col AS created_at
160+ FROM
161+ ExportedContacts
162+ WHERE
163+ rn = 1 ;
164+ END;
165+ $$;
0 commit comments