added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / feathers / services / auth-management / notifier.js
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 var env = require('config').env;\r
18 \r
19 module.exports = function(app) {\r
20 \r
21         function getLink(type, hash) {\r
22                 const url = app.get('otf').url + "account/" + type + '?token=' + hash;\r
23                 return url\r
24         }\r
25 \r
26         function sendEmail(email) {\r
27                 var environment = env.toUpperCase();\r
28                 email.subject = "Open Test Framework (" + environment + ") - " + email.subject;\r
29                 return app.service(app.get('base-path') + 'mailer').create(email).then(function (result) {\r
30                         console.log('Sent email', result)\r
31                 }).catch(err => {\r
32                         console.log('Error sending email: ', email, err)\r
33                 })\r
34         }\r
35 \r
36         return {\r
37                 notifier: function(type, user, notifierOptions) {\r
38                         let tokenLink;\r
39                         let email;\r
40                         let sender = app.get('otf').email;\r
41                         switch (type) {\r
42                                 case 'resendVerifySignup': //sending the user the verification email\r
43                                         tokenLink = getLink('verify', user.verifyToken)\r
44                                         email = {\r
45                                                 from: sender,\r
46                                                 to: user['email'],\r
47                                                 subject: 'Verify Signup',\r
48                                                 html: 'Please verify your email address by clicking the link below.' + '</br>' + tokenLink\r
49 \r
50                                         }\r
51                                         return sendEmail(email)\r
52                                         break\r
53 \r
54                                 case 'verifySignup': // confirming verification\r
55                                         let adminLink = app.get('otf').url + 'user-management?filter=' + user['email'];\r
56 \r
57                                         email = {\r
58                                                 from: sender,\r
59                                                 to: user['email'],\r
60                                                 subject: 'Signup Confirmed',\r
61                                                 html: 'Thanks for verifying your email!' + '</br>' + 'You will be notified when an admin enables your account.'\r
62                                         }\r
63 \r
64                                         let adminEmail = {\r
65                                                 from: sender,\r
66                                                 to: sender,\r
67                                                 subject: 'Approve Verified User',\r
68                                                 html:   'User has verified their email.' + '</br>' +\r
69                                                                 'Details: ' + '</br>' +\r
70                                                                 '   Email: ' + user['email'] + '</br>' +\r
71                                                                 '   First Name: ' + user['firstName'] + '</br>' +\r
72                                                                 '   Last Name: ' + user['lastName'] + '</br>' +\r
73                                                                 '</br>' +\r
74                                                                 'Enable their account by visiting ' + '</br>' + adminLink\r
75                                         }\r
76                                         sendEmail(adminEmail);\r
77                                         return sendEmail(email);\r
78                                         break\r
79 \r
80                                 case 'sendApprovalNotification':\r
81                                         email = {\r
82                                                 from: sender,\r
83                                                 to: user['email'],\r
84                                                 subject: 'Approved',\r
85                                                 html:   'Your account has been approved for access.' + '</br>' +\r
86                                                                 'You can now log into the OTF website: ' + app.get('otf').url\r
87 \r
88                                         }\r
89                                         return sendEmail(email);\r
90                                         break\r
91 \r
92                                 case 'sendResetPwd':\r
93                                         tokenLink = getLink('reset', user.resetToken)\r
94                                         email = {}\r
95                                         return sendEmail(email)\r
96                                         break\r
97 \r
98                                 case 'resetPwd':\r
99                                         tokenLink = getLink('reset', user.resetToken)\r
100                                         email = {}\r
101                                         return sendEmail(email)\r
102                                         break\r
103 \r
104                                 case 'passwordChange':\r
105                                         email = {}\r
106                                         return sendEmail(email)\r
107                                         break\r
108 \r
109                                 case 'identityChange':\r
110                                         tokenLink = getLink('verifyChanges', user.verifyToken)\r
111                                         email = {}\r
112                                         return sendEmail(email)\r
113                                         break\r
114 \r
115                                 default:\r
116                                         break\r
117                         }\r
118                 }\r
119         }\r
120 }\r