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