@@ -28,7 +28,7 @@ const emailSchema = joi.object().keys({
2828 userUUID : joi . string ( ) . uuid ( ) ,
2929 email : joi . string ( ) . email ( ) ,
3030 handle : joi . string ( ) ,
31- } ) . min ( 1 ) . required ( )
31+ } ) . min ( 1 )
3232 ) ,
3333 data : joi . object ( ) . keys ( {
3434 subject : joi . string ( ) ,
@@ -74,6 +74,15 @@ function validator(data, schema) {
7474 return true ;
7575}
7676
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+ */
7786function * completeMissingFields ( details , findEmail , findUserId ) {
7887 const getFieldsByUserId = [ ] ;
7988 const getFieldsByHandle = [ ] ;
@@ -127,20 +136,26 @@ function* completeMissingFields(details, findEmail, findUserId) {
127136 }
128137 }
129138 }
130- const foundUsersByUUID = yield tcApiHelper . getUsersByUserUUIDs ( getFieldsByUserUUID ) ;
139+ const foundUsersByUUID = yield tcApiHelper . getUsersByUserUUIDs ( getFieldsByUserUUID , true ) ;
131140 if ( ! _ . isEmpty ( foundUsersByUUID ) ) {
132141 for ( const user of getFieldsByUserUUID ) {
133142 const found = _ . find ( foundUsersByUUID , [ 'id' , user . userUUID ] ) || { } ;
134143 if ( ! _ . isUndefined ( found . externalProfiles ) && ! _ . isEmpty ( found . externalProfiles ) ) {
135144 _ . assign ( user , { userId : _ . toInteger ( _ . get ( found . externalProfiles [ 0 ] , 'externalId' ) ) } ) ;
136145 }
146+ if ( ! _ . isUndefined ( found . handle ) && _ . isUndefined ( user . handle ) ) {
147+ _ . assign ( user , { handle : found . handle } ) ;
148+ }
137149 }
150+
138151 if ( findEmail ) {
139152 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 ) ) {
142156 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 ] ) || { } ;
144159 if ( ! _ . isUndefined ( found . email ) ) {
145160 _ . assign ( user , { email : found . email } ) ;
146161 }
@@ -162,6 +177,7 @@ function* handle(message) {
162177 switch ( data . serviceId ) {
163178 case constants . SETTINGS_EMAIL_SERVICE_ID :
164179 if ( validator ( data , emailSchema ) ) {
180+ // find missing emails and userIds
165181 yield completeMissingFields ( data . details , true , true ) ;
166182 yield tcApiHelper . notifyUserViaEmail ( data ) ;
167183 }
@@ -173,6 +189,7 @@ function* handle(message) {
173189 break ;
174190 case constants . SETTINGS_WEB_SERVICE_ID :
175191 if ( validator ( data , webSchema ) ) {
192+ // find missing userIds
176193 yield completeMissingFields ( data . details , false , true ) ;
177194 const _notifications = yield tcApiHelper . notifyUserViaWeb ( data ) ;
178195 if ( _notifications ) {
0 commit comments