@@ -28,7 +28,7 @@ const emailSchema = joi.object().keys({
28
28
userUUID : joi . string ( ) . uuid ( ) ,
29
29
email : joi . string ( ) . email ( ) ,
30
30
handle : joi . string ( ) ,
31
- } ) . min ( 1 ) . required ( )
31
+ } ) . min ( 1 )
32
32
) ,
33
33
data : joi . object ( ) . keys ( {
34
34
subject : joi . string ( ) ,
@@ -74,6 +74,15 @@ function validator(data, schema) {
74
74
return true ;
75
75
}
76
76
77
+
78
+ /**
79
+ * Complete missing user fields of given payload details
80
+ * This function mutates the given details object.
81
+ * @param {Object } details the object which has recipients array
82
+ * @param {Boolean } findEmail true if emails are needed
83
+ * @param {Boolean } findUserId true if userIds are needed
84
+ * @returns {undefined }
85
+ */
77
86
function * completeMissingFields ( details , findEmail , findUserId ) {
78
87
const getFieldsByUserId = [ ] ;
79
88
const getFieldsByHandle = [ ] ;
@@ -127,20 +136,26 @@ function* completeMissingFields(details, findEmail, findUserId) {
127
136
}
128
137
}
129
138
}
130
- const foundUsersByUUID = yield tcApiHelper . getUsersByUserUUIDs ( getFieldsByUserUUID ) ;
139
+ const foundUsersByUUID = yield tcApiHelper . getUsersByUserUUIDs ( getFieldsByUserUUID , true ) ;
131
140
if ( ! _ . isEmpty ( foundUsersByUUID ) ) {
132
141
for ( const user of getFieldsByUserUUID ) {
133
142
const found = _ . find ( foundUsersByUUID , [ 'id' , user . userUUID ] ) || { } ;
134
143
if ( ! _ . isUndefined ( found . externalProfiles ) && ! _ . isEmpty ( found . externalProfiles ) ) {
135
144
_ . assign ( user , { userId : _ . toInteger ( _ . get ( found . externalProfiles [ 0 ] , 'externalId' ) ) } ) ;
136
145
}
146
+ if ( ! _ . isUndefined ( found . handle ) && _ . isUndefined ( user . handle ) ) {
147
+ _ . assign ( user , { handle : found . handle } ) ;
148
+ }
137
149
}
150
+
138
151
if ( findEmail ) {
139
152
const usersHaveId = _ . filter ( getFieldsByUserUUID , u => ! _ . isUndefined ( u . userId ) ) ;
140
- const foundUsersById = yield tcApiHelper . getUsersByHandlesAndUserIds ( [ ] , usersHaveId ) ;
141
- if ( ! _ . isEmpty ( foundUsersById ) ) {
153
+ const usersHaveHandle = _ . filter ( getFieldsByUserUUID , u => _ . isUndefined ( u . userId ) && ! _ . isUndefined ( u . handle ) ) ;
154
+ const foundUser = yield tcApiHelper . getUsersByHandlesAndUserIds ( usersHaveHandle , usersHaveId ) ;
155
+ if ( ! _ . isEmpty ( foundUser ) ) {
142
156
for ( const user of getFieldsByUserUUID ) {
143
- const found = _ . find ( foundUsersById , [ 'userId' , user . userId ] ) || { } ;
157
+ const found = _ . find ( foundUser , ! _ . isUndefined ( user . handle )
158
+ ? [ 'handle' , user . handle ] : [ 'userId' , user . userId ] ) || { } ;
144
159
if ( ! _ . isUndefined ( found . email ) ) {
145
160
_ . assign ( user , { email : found . email } ) ;
146
161
}
@@ -162,6 +177,7 @@ function* handle(message) {
162
177
switch ( data . serviceId ) {
163
178
case constants . SETTINGS_EMAIL_SERVICE_ID :
164
179
if ( validator ( data , emailSchema ) ) {
180
+ // find missing emails and userIds
165
181
yield completeMissingFields ( data . details , true , true ) ;
166
182
yield tcApiHelper . notifyUserViaEmail ( data ) ;
167
183
}
@@ -173,6 +189,7 @@ function* handle(message) {
173
189
break ;
174
190
case constants . SETTINGS_WEB_SERVICE_ID :
175
191
if ( validator ( data , webSchema ) ) {
192
+ // find missing userIds
176
193
yield completeMissingFields ( data . details , false , true ) ;
177
194
const _notifications = yield tcApiHelper . notifyUserViaWeb ( data ) ;
178
195
if ( _notifications ) {
0 commit comments