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  if metadata_getattr(emailuri,"Address",user_email)<0 then do;
100  putlog 'DCWARN: Unexpected error! Valid emailURI but no email. Weird.';
101  end;
102  end;
103  /* only keep valid emails */
104  if index(user_email,'@') ;
105  /* dump contents for debugging */
106  if _n_<21 then putlog (_all_)(=);
107 run;
108 
109 %local emails;
110 proc sql noprint;
111 select quote(trim(user_email)) into: emails separated by ' ' from work.emails;
112 
113 /* exit if nobody to email */
114 %if %mf_getattrn(emails,NLOBS)=0 %then %do;
115  %put NOTE: No alerts configured (mpe_alerts.sas);
116  %return;
117 %end;
118 
119 /* display email options */
120 data _null_;
121  set sashelp.voption(where=(group='EMAIL'));
122  put optname '=' setting;
123 run;
124 
125 filename __out email (&emails)
126  subject="Table &alert_lib..&alert_ds has been &alert_event";
127 
128 %local SUBMITTED_TXT;
129 %if &alert_event=SUBMITTED %then %do;
130  data _null_;
131  set &mpelib..mpe_submit;
132  where table_id="&dsid" and submit_status_cd='SUBMITTED';
133  call symputx('SUBMITTED_TXT',submitted_reason_txt,'l');
134  run;
135  data _null_;
136  File __out lrecl=32000;
137  put 'Dear user,';
138  put ' ';
139  put "Please be advised that a change to table &alert_lib..&alert_ds has "
140  "been proposed by &from_user on the '&syshostname' SAS server.";
141  put " ";
142  length txt $2048;
143  txt=symget('SUBMITTED_TXT');
144  put "Reason provided: " txt;
145  put " ";
146  put "This is an automated email by Data Controller for SAS. For "
147  "documentation, please visit https://docs.datacontroller.io";
148  run;
149 %end;
150 %else %if &alert_event=APPROVED %then %do;
151  /* there is no approval message */
152  data _null_;
153  File __out lrecl=32000;
154  put 'Dear user,';
155  put ' ';
156  put "Please be advised that a change to table &alert_lib..&alert_ds has "
157  "been approved by &from_user on the '&syshostname' SAS server.";
158  put " ";
159  put "This is an automated email by Data Controller for SAS. For "
160  "documentation, please visit https://docs.datacontroller.io";
161  run;
162 %end;
163 %else %if &alert_event=REJECTED %then %do;
164  data _null_;
165  set &mpelib..mpe_review;
166  where table_id="&dsid" and review_status_id='REJECTED';
167  call symputx('REVIEW_REASON_TXT',REVIEW_REASON_TXT,'l');
168  run;
169  data _null_;
170  File __out lrecl=32000;
171  put 'Dear user,';
172  put ' ';
173  put "Please be advised that a change to table &alert_lib..&alert_ds has "
174  "been rejected by &from_user on the '&syshostname' SAS server.";
175  put " ";
176  length txt $2048;
177  txt=symget('REVIEW_REASON_TXT');
178  put "Reason provided: " txt;
179  put " ";
180  put "This is an automated email by Data Controller for SAS. For "
181  "documentation, please visit https://docs.datacontroller.io";
182  run;
183 %end;
184 
185 filename __out clear;
186 
187 %mend mpe_alerts ;