Loading...
Searching...
No Matches
mpe_alerts.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief send alerts
4 @details Send emails to users on review, approve, and/or reject
5
6 @param alert_event= either SUBMITTED, APPROVED or REJECTED
7 @param alert_lib= the library of the table being submitted
8 @param alert_ds= the table submitted
9 @param dsid= the staging reference for the submitted table
10
11 <h4> SAS Macros </h4>
12 @li mf_getattrn.sas
13 @li mf_getuser.sas
14
15 @version 9.4
16 @author 4GL Apps Ltd
17 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
18 and may not be re-distributed or re-sold without the express permission of
19 4GL Apps Ltd.
20
21**/
22
23
24%macro mpe_alerts(alert_event=
25 , alert_lib=
26 , alert_ds=
27 , dsid=
28);
29
30/* exit if not configured */
31%global DC_EMAIL_ALERTS;
32%if &DC_EMAIL_ALERTS ne YES %then %do;
33 %put DCNOTE: Email alerts are not configured;
34 %put DCNOTE: (dc_email_alerts=&dc_email_alerts in &mpelib..mpe_config);
35 %return;
36%end;
37
38
39%let alert_event=%upcase(&alert_event);
40%let alert_lib=%upcase(&alert_lib);
41%let alert_ds=%upcase(&alert_ds);
42%let from_user=%mf_getuser();
43
44
45/* get users TO which the email should be sent */
46
47proc sql noprint;
48create table work.users as select distinct a.alert_user,
49 b.user_displayname,
50 b.user_email
51 from &mpelib..mpe_alerts
52 (where=(&dc_dttmtfmt. lt tx_to)) a
53 left join &mpelib..mpe_emails
54 (where=(&dc_dttmtfmt. lt tx_to)) b
55 on upcase(trim(a.alert_user))=upcase(trim(b.user_name))
56 where a.alert_event in ("&alert_event","*ALL*")
57 and a.alert_lib in ("&alert_lib","*ALL*")
58 and a.alert_ds in ("&alert_ds","*ALL*");
59
60/* ensure the submitter is included on the email */
61%local isThere userdisp user_eml;
62%let isThere=0;
63select count(*) into: isThere from &syslast where alert_user="&from_user";
64%if &isThere=0 %then %do;
65 select user_displayname, user_email
66 into: userdisp trimmed, :user_eml trimmed
67 from &mpelib..mpe_emails
68 where &dc_dttmtfmt. lt tx_to
69 and user_name="&from_user";
70 insert into work.users
71 set alert_user="&from_user"
72 ,user_displayname="&userdisp"
73 ,user_email="&user_eml";
74%end;
75
76
77/* if no email / displayname is provided, then extract from metadata */
78data work.emails;
79 set work.users;
80 length emailuri uri text $256; call missing(emailuri,uri); drop emailuri uri;
81
82 /* get displayname */
83 text=cats("omsobj:Person?@Name='",alert_user,"'");
84 if metadata_getnobj(text,1,uri)<=0 then do;
85 putlog "DCWARN: &from_user not found";
86 return;
87 end;
88 else if user_displayname = '' then do;
89 if metadata_getattr(uri,'DisplayName',user_displayname)<0 then do;
90 putlog 'DCWARN: strange err, no displayname attribute of user URI';
91 end;
92 end;
93
94 if index(user_email,'@') then return;
95 /* get email from metadata if not in input table */
96 if metadata_getnasn(uri,"EmailAddresses",1,emailuri)<=0 then do;
97 putlog "DCWARN: " alert_user " has no emails in MPE_EMAILS or metadata!";
98 end;
99 else do;
100 if metadata_getattr(emailuri,"Address",user_email)<0 then do;
101 putlog 'DCWARN: Unexpected error! Valid emailURI but no email. Weird.';
102 end;
103 end;
104 /* only keep valid emails */
105 if index(user_email,'@') ;
106 /* dump contents for debugging */
107 if _n_<21 then putlog (_all_)(=);
108run;
109
110%local emails;
111proc sql noprint;
112select quote(trim(user_email)) into: emails separated by ' ' from work.emails;
113
114/* exit if nobody to email */
115%if %mf_getattrn(emails,NLOBS)=0 %then %do;
116 %put NOTE: No alerts configured (mpe_alerts.sas);
117 %return;
118%end;
119
120/* display email options */
121data _null_;
122 set sashelp.voption(where=(group='EMAIL'));
123 put optname '=' setting;
124run;
125
126filename __out email (&emails)
127 subject="Table &alert_lib..&alert_ds has been &alert_event";
128
129data work.alertmessage;
130 set &mpelib..mpe_config;
131 where &dc_dttmtfmt. lt tx_to;
132 where also var_scope='DC_EMAIL' and var_name="&alert_event._TEMPLATE";
133run;
134%local SUBMITTED_TXT;
135%if &alert_event=SUBMITTED %then %do;
136 data _null_;
137 set &mpelib..mpe_submit;
138 where table_id="&dsid" and submit_status_cd='SUBMITTED';
139 call symputx('SUBMITTED_TXT',submitted_reason_txt,'l');
140 run;
141 data _null_;
142 File __out lrecl=32000;
143 length txt $2048;
144 %if %mf_getattrn(alertmessage,NLOBS)=0 %then %do;
145 put 'Dear user,';
146 put ' ';
147 put "Please be advised that a change to table &alert_lib..&alert_ds has "
148 "been proposed by &from_user on the &syshostname SAS server.";
149 put " ";
150 txt=symget('SUBMITTED_TXT');
151 put "Reason provided: " txt;
152 put " ";
153 put "This is an automated email by Data Controller for SAS. For "
154 "documentation, please visit https://docs.datacontroller.io";
155 %end;
156 %else %do;
157 /* take template from config table */
158 set work.alertmessage;
159 cnt=countw(var_value,'0A'x);
160 do i=1 to cnt;
161 txt=resolve(scan(var_value,i,'0A'x));
162 put txt /;
163 end;
164 %end;
165 run;
166%end;
167%else %if &alert_event=APPROVED %then %do;
168 /* there is no approval message */
169 data _null_;
170 File __out lrecl=32000;
171 length txt $2048;
172 %if %mf_getattrn(alertmessage,NLOBS)=0 %then %do;
173 /* fallback message */
174 put 'Dear user,';
175 put ' ';
176 put "Please be advised that a change to table &alert_lib..&alert_ds has "
177 "been approved by &from_user on the &syshostname SAS server.";
178 put " ";
179 put "This is an automated email by Data Controller for SAS. For "
180 "documentation, please visit https://docs.datacontroller.io";
181 %end;
182 %else %do;
183 /* take template from config table */
184 set work.alertmessage;
185 cnt=countw(var_value,'0A'x);
186 do i=1 to cnt;
187 txt=resolve(scan(var_value,i,'0A'x));
188 put txt /;
189 end;
190 %end;
191 run;
192%end;
193%else %if &alert_event=REJECTED %then %do;
194 data _null_;
195 set &mpelib..mpe_review;
196 where table_id="&dsid" and review_status_id='REJECTED';
197 call symputx('REVIEW_REASON_TXT',REVIEW_REASON_TXT,'l');
198 run;
199 data _null_;
200 File __out lrecl=32000;
201 length txt $2048;
202 %if %mf_getattrn(alertmessage,NLOBS)=0 %then %do;
203 /* fallback message */
204 put 'Dear user,';
205 put ' ';
206 put "Please be advised that a change to table &alert_lib..&alert_ds has "
207 "been rejected by &from_user on the &syshostname SAS server.";
208 put " ";
209 txt=symget('REVIEW_REASON_TXT');
210 put "Reason provided: " txt;
211 put " ";
212 put "This is an automated email by Data Controller for SAS. For "
213 "documentation, please visit https://docs.datacontroller.io";
214 %end;
215 %else %do;
216 /* take template from config table */
217 set work.alertmessage;
218 cnt=countw(var_value,'0A'x);
219 do i=1 to cnt;
220 txt=resolve(scan(var_value,i,'0A'x));
221 put txt /;
222 end;
223 %end;
224 run;
225%end;
226
227filename __out clear;
228
229%mend mpe_alerts ;