1
1
package org .mskcc .cbio .oncokb .service ;
2
2
3
+ import ch .qos .logback .core .Layout ;
3
4
import com .github .seratch .jslack .Slack ;
4
5
import com .github .seratch .jslack .api .model .block .ActionsBlock ;
5
6
import com .github .seratch .jslack .api .model .block .LayoutBlock ;
15
16
import org .apache .commons .lang3 .StringUtils ;
16
17
import org .mskcc .cbio .oncokb .config .application .ApplicationProperties ;
17
18
import org .mskcc .cbio .oncokb .domain .enumeration .LicenseType ;
19
+ import org .mskcc .cbio .oncokb .domain .enumeration .MailType ;
18
20
import org .mskcc .cbio .oncokb .service .dto .UserDTO ;
19
21
import org .slf4j .Logger ;
20
22
import org .slf4j .LoggerFactory ;
23
25
24
26
import java .io .IOException ;
25
27
import java .util .*;
28
+ import java .util .stream .Collectors ;
26
29
27
30
28
31
/**
@@ -37,10 +40,15 @@ public class SlackService {
37
40
38
41
private static final String APPROVE_USER = "approve-user" ;
39
42
43
+ private static final String ACADEMIC_CLARIFICATION_NOTE = "We have sent the clarification email to the user asking why they could not use an institution email to register." ;
44
+ private static final String COMMERCIAL_APPROVE_NOTE = "We have sent the intake form automatically." ;
45
+
40
46
private final ApplicationProperties applicationProperties ;
47
+ private final MailService mailService ;
41
48
42
- public SlackService (ApplicationProperties applicationProperties ) {
49
+ public SlackService (ApplicationProperties applicationProperties , MailService mailService ) {
43
50
this .applicationProperties = applicationProperties ;
51
+ this .mailService = mailService ;
44
52
}
45
53
46
54
@ Async
@@ -49,8 +57,22 @@ public void sendUserRegistrationToChannel(UserDTO user) {
49
57
if (StringUtils .isEmpty (this .applicationProperties .getUserRegistrationWebhook ())) {
50
58
log .debug ("\t Skipped, the webhook is not configured" );
51
59
} else {
60
+ List <LayoutBlock > layoutBlocks = new ArrayList <>();
61
+ if (getAcademicEmailClarifyDomains ().size () > 0 &&
62
+ getAcademicEmailClarifyDomains ().stream ().filter (domain -> user .getEmail ().endsWith (domain )).collect (Collectors .toList ()).size () > 0 &&
63
+ user .getLicenseType ().equals (LicenseType .ACADEMIC )) {
64
+ mailService .sendEmailFromTemplate (user , MailType .CLARIFY_ACADEMIC_NON_INSTITUTE_EMAIL , applicationProperties .getEmailAddresses ().getLicenseAddress (), null , null );
65
+ layoutBlocks = buildAcademicClarifyBlocks (user );
66
+ } else {
67
+ layoutBlocks = buildCommercialApprovalBlocks (user );
68
+ // Send intake form email
69
+ MailType intakeEmailMailType = mailService .getIntakeFormMailType (user .getLicenseType ());
70
+ if (intakeEmailMailType != null ) {
71
+ mailService .sendEmailFromTemplate (user , intakeEmailMailType , applicationProperties .getEmailAddresses ().getLicenseAddress (), null , null );
72
+ }
73
+ }
52
74
Payload payload = Payload .builder ()
53
- .blocks (buildUserApprovalBlocks ( user ) )
75
+ .blocks (layoutBlocks )
54
76
.build ();
55
77
56
78
Slack slack = Slack .getInstance ();
@@ -95,25 +117,67 @@ public Optional<BlockActionPayload.Action> getApproveUserAction(BlockActionPaylo
95
117
return blockActionPayload .getActions ().stream ().filter (action -> action .getActionId ().equalsIgnoreCase (APPROVE_USER )).findFirst ();
96
118
}
97
119
120
+ private List <String > getAcademicEmailClarifyDomains () {
121
+ return Arrays .stream (applicationProperties .getAcademicEmailClarifyDomain ().split ("," )).map (domain -> domain .trim ()).collect (Collectors .toList ());
122
+ }
123
+
98
124
private TextObject getTextObject (String title , String content ) {
99
125
StringBuilder sb = new StringBuilder ();
100
126
sb .append ("*" + title + ":*\n " );
101
127
sb .append (content );
102
128
return MarkdownTextObject .builder ().text (sb .toString ()).build ();
103
129
}
104
130
105
- private List <LayoutBlock > buildUserApprovalBlocks (UserDTO user ) {
131
+ private List <LayoutBlock > buildCommercialApprovalBlocks (UserDTO user ) {
132
+ List <LayoutBlock > blocks = new ArrayList <>();
133
+
134
+ // Add mention
135
+ blocks .add (buildHereMentionBlock ());
136
+
137
+ // Add Title
138
+ blocks .add (buildTitleBlock (user ));
139
+
140
+ // Add user info section
141
+ blocks .add (buildUserInfoBlock (user ));
106
142
143
+ // Add Approve button
144
+ blocks .add (buildApproveButton (user ));
145
+
146
+ // Add Approve button
147
+ blocks .add (buildPlainTextBlock (COMMERCIAL_APPROVE_NOTE ));
148
+
149
+ return blocks ;
150
+ }
151
+
152
+ private List <LayoutBlock > buildAcademicClarifyBlocks (UserDTO user ) {
107
153
List <LayoutBlock > blocks = new ArrayList <>();
108
- blocks .add (SectionBlock .builder ().text (MarkdownTextObject .builder ().text ("<!here>" ).build ()).build ());
109
154
110
- // Title
155
+ // Add mention
156
+ blocks .add (buildHereMentionBlock ());
157
+
158
+ // Add Title
159
+ blocks .add (buildTitleBlock (user ));
160
+
161
+ // Add user info section
162
+ blocks .add (buildUserInfoBlock (user ));
163
+
164
+ // Add Approve button
165
+ blocks .add (buildPlainTextBlock (ACADEMIC_CLARIFICATION_NOTE ));
166
+
167
+ return blocks ;
168
+ }
169
+
170
+ private LayoutBlock buildHereMentionBlock () {
171
+ return SectionBlock .builder ().text (MarkdownTextObject .builder ().text ("<!here>" ).build ()).build ();
172
+ }
173
+
174
+ private LayoutBlock buildTitleBlock (UserDTO user ) {
111
175
List <TextObject > title = new ArrayList <>();
112
176
title .add (getTextObject ("The following user registered an " + user .getLicenseType () + " account" , "" ));
113
- blocks . add ( SectionBlock .builder ().fields (title ).build () );
114
-
177
+ return SectionBlock .builder ().fields (title ).build ();
178
+ }
115
179
116
- // User info section
180
+ private LayoutBlock buildUserInfoBlock ( UserDTO user ) {
117
181
List <TextObject > userInfo = new ArrayList <>();
118
182
userInfo .add (getTextObject ("Email" , user .getEmail ()));
119
183
userInfo .add (getTextObject ("Name" , user .getFirstName () + " " + user .getLastName ()));
@@ -122,16 +186,16 @@ private List<LayoutBlock> buildUserApprovalBlocks(UserDTO user) {
122
186
userInfo .add (getTextObject ("City" , user .getCity ()));
123
187
userInfo .add (getTextObject ("Country" , user .getCountry ()));
124
188
125
- blocks .add (SectionBlock .builder ().fields (userInfo ).build ());
189
+ return SectionBlock .builder ().fields (userInfo ).build ();
190
+ }
126
191
127
- // Approve button
192
+ private LayoutBlock buildApproveButton ( UserDTO user ) {
128
193
ButtonElement button = ButtonElement .builder ()
129
194
.text (PlainTextObject .builder ().emoji (true ).text ("Approve" ).build ())
130
195
.style ("primary" )
131
196
.actionId (APPROVE_USER )
132
197
.value (user .getLogin ())
133
198
.build ();
134
-
135
199
if (user .getLicenseType () != LicenseType .ACADEMIC ) {
136
200
ConfirmationDialogObject confirmationDialogObject = ConfirmationDialogObject .builder ()
137
201
.title (PlainTextObject .builder ().text ("Are you sure?" ).build ())
@@ -141,10 +205,10 @@ private List<LayoutBlock> buildUserApprovalBlocks(UserDTO user) {
141
205
.build ();
142
206
button .setConfirm (confirmationDialogObject );
143
207
}
144
-
145
- blocks .add (ActionsBlock .builder ().elements (Arrays .asList (button )).build ());
146
-
147
- return blocks ;
208
+ return ActionsBlock .builder ().elements (Arrays .asList (button )).build ();
148
209
}
149
210
211
+ private LayoutBlock buildPlainTextBlock (String text ) {
212
+ return SectionBlock .builder ().text (PlainTextObject .builder ().text (text ).build ()).build ();
213
+ }
150
214
}
0 commit comments