-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path05_create_email_message.sql
71 lines (67 loc) · 2.09 KB
/
05_create_email_message.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/************************************************************
*
* Function: create_email_message(message JSON)
*
* create a message in the messages table
*
{
recipient: "", -- REQUIRED
sender: "", -- REQUIRED
cc: "",
bcc: "",
subject: "", -- REQUIRED
text_body: "", -- one of: text_body OR html_body is REQUIRED
html_body: "" -- both can be sent but one of them is REQUIRED
}
returns: uuid (as text) of newly inserted message
************************************************************/
create or replace function public.create_email_message(message JSON)
returns text
language plpgsql
-- Set a secure search_path: trusted schema(s), then 'pg_temp'.
-- SET search_path = admin, pg_temp;
as
$$
declare
-- variable declaration
recipient text;
sender text;
cc text;
bcc text;
subject text;
text_body text;
html_body text;
retval text;
begin
/*
if not exists (message->>'recipient') then
RAISE INFO 'messages.recipient missing';
end if
*/
select message->>'recipient',
message->>'sender',
message->>'cc',
message->>'bcc',
message->>'subject',
message->>'text_body',
message->>'html_body' into recipient, sender, cc, bcc, subject, text_body, html_body;
if coalesce(sender, '') = '' then
-- select 'no sender' into retval;
RAISE EXCEPTION 'message.sender missing';
elseif coalesce(recipient, '') = '' then
RAISE EXCEPTION 'message.recipient missing';
elseif coalesce(subject, '') = '' then
RAISE EXCEPTION 'message.subject missing';
elseif coalesce(text_body, '') = '' and coalesce(html_body, '') = '' then
RAISE EXCEPTION 'message.text_body and message.html_body are both missing';
end if;
if coalesce(text_body, '') = '' then
select html_body into text_body;
elseif coalesce(html_body, '') = '' then
select text_body into html_body;
end if;
insert into public.messages(recipient, sender, cc, bcc, subject, text_body, html_body, status, log)
values (recipient, sender, cc, bcc, subject, text_body, html_body, 'ready', '[]'::jsonb) returning id into retval;
return retval;
end;
$$