@@ -13,7 +13,7 @@ const config = require('config')
13
13
/**
14
14
* This array contains the list of profile completion metadata object keys that affect the status update
15
15
*/
16
- const METADATA_KEYS_FOR_STATUS_UPDATE = [ 'profile_picture' , 'bio' , 'skills' , 'education' , 'work' , 'language' ]
16
+ const METADATA_KEYS_FOR_STATUS_UPDATE = [ 'profile_picture' , 'bio' , 'skills' , 'education' , 'work' , 'language' , 'country' ]
17
17
18
18
// The component name to be used when logging messages
19
19
const component = 'ProfileCompletionProcessorService'
@@ -27,7 +27,7 @@ async function processProfileUpdateMessage (message) {
27
27
// The eventually updated metadata items by the current event message
28
28
const updatedMetadataItems = {
29
29
bio : ! _ . isEmpty ( _ . get ( message , 'payload.description' ) ) ,
30
- country : ! _ . isEmpty ( _ . get ( message , config . PROFILE_UPDATE_EVENT_COUNTRY_FIELD_NAME ) )
30
+ country : ! _ . isEmpty ( _ . get ( message , `payload. ${ config . PROFILE_UPDATE_EVENT_COUNTRY_FIELD_NAME } ` ) )
31
31
}
32
32
33
33
await handleUpdatedProfileCompletionMetadata ( message , updatedMetadataItems )
@@ -147,22 +147,11 @@ processProfileTraitRemovalMessage.schema = {
147
147
*
148
148
* @param {Object } oldChecklist The old checklist object
149
149
* @param {Object } updatedMetadataItems The updated metadata objects
150
- * @param {String } handle The user handle
151
150
* @returns The new checklist
152
151
*/
153
- async function getNewChecklist ( oldChecklist , updatedMetadataItems , handle ) {
152
+ async function getNewChecklist ( oldChecklist , updatedMetadataItems ) {
154
153
// Initialize the aggregated updatedMetadata object
155
- let updatedMetadata
156
- if ( ! _ . get ( oldChecklist , 'metadata.skills' ) ) {
157
- // The skills trait was not previously set, we need to re-check from member-api if it is done and update the metadata accordingly
158
- updatedMetadata = _ . assign ( { } ,
159
- oldChecklist . metadata ,
160
- updatedMetadataItems ,
161
- { skills : await helper . hasUserEnteredSkills ( handle ) } )
162
- } else {
163
- // The skills trait was previously set, no need to make the call to member-api for checking again
164
- updatedMetadata = _ . assign ( { } , oldChecklist . metadata , updatedMetadataItems )
165
- }
154
+ const updatedMetadata = _ . assign ( { } , oldChecklist . metadata , updatedMetadataItems )
166
155
167
156
if ( _ . isEqual ( oldChecklist . metadata , updatedMetadata ) ) {
168
157
// Nothing has changed in the metadata, return the result with 'updated' flag set to false
@@ -202,23 +191,14 @@ async function getNewChecklist (oldChecklist, updatedMetadataItems, handle) {
202
191
* This function generates the initial structure of the profile completion checklist
203
192
*
204
193
* @param {Object } metadata The metadata to put in the checklist
205
- * @param {String } handle the user handle
206
194
* @returns The initial checklist structure with the the given metadata
207
195
*/
208
- async function getInitialChecklist ( metadata , handle ) {
196
+ async function getInitialChecklist ( metadata ) {
209
197
return {
210
198
status : CHECKLIST_STATUS . PENDING_AT_USER ,
211
199
message : CHECKLIST_MESSAGE . PROFILE_IS_INCOMPLETE ,
212
200
date : new Date ( ) . getTime ( ) ,
213
- metadata : _ . assign ( { } , {
214
- profile_picture : false ,
215
- bio : false ,
216
- skills : await helper . hasUserEnteredSkills ( handle ) ,
217
- education : false ,
218
- work : false ,
219
- language : false ,
220
- country : false
221
- } , metadata )
201
+ metadata : _ . assign ( { } , metadata )
222
202
}
223
203
}
224
204
@@ -232,13 +212,16 @@ async function handleUpdatedProfileCompletionMetadata (message, updatedMetadataI
232
212
// Get the user handle from members api
233
213
const handle = await helper . getHandleByUserId ( _ . get ( message , 'payload.userId' ) )
234
214
215
+ // Check for other updates in user profile and profile traits
216
+ const fullyUpdatedMetadata = await getFullyUpdatedMetadata ( handle , updatedMetadataItems )
217
+
235
218
// context used for logging
236
219
const context = 'handleUpdatedProfileCompletionMetadata'
237
220
238
221
logger . debug ( {
239
222
component,
240
223
context,
241
- message : `Process profile completion trait: { user: '${ handle } ', updatedMetadata: ${ JSON . stringify ( updatedMetadataItems ) } }`
224
+ message : `Process profile completion trait: { user: '${ handle } ', updatedMetadata: ${ JSON . stringify ( fullyUpdatedMetadata ) } }`
242
225
} )
243
226
244
227
// Get the member Onboarding Checklist traits
@@ -258,7 +241,7 @@ async function handleUpdatedProfileCompletionMetadata (message, updatedMetadataI
258
241
isCreate = true
259
242
// The member does not have any onboarding checklist trait, we initialize it
260
243
body [ 0 ] . traits . data . push ( {
261
- [ PROFILE_COMPLETION_TRAIT_PROPERTY_NAME ] : await getInitialChecklist ( updatedMetadataItems , handle )
244
+ [ PROFILE_COMPLETION_TRAIT_PROPERTY_NAME ] : await getInitialChecklist ( fullyUpdatedMetadata )
262
245
} )
263
246
} else {
264
247
isCreate = false
@@ -271,11 +254,11 @@ async function handleUpdatedProfileCompletionMetadata (message, updatedMetadataI
271
254
if ( _ . isUndefined ( body [ 0 ] . traits . data [ 0 ] [ PROFILE_COMPLETION_TRAIT_PROPERTY_NAME ] ) ) {
272
255
// There were no traits data for profile completion checklist
273
256
// We initialize a new one with the updated metadata by the current event message
274
- body [ 0 ] . traits . data [ 0 ] [ PROFILE_COMPLETION_TRAIT_PROPERTY_NAME ] = await getInitialChecklist ( updatedMetadataItems , handle )
257
+ body [ 0 ] . traits . data [ 0 ] [ PROFILE_COMPLETION_TRAIT_PROPERTY_NAME ] = await getInitialChecklist ( fullyUpdatedMetadata )
275
258
} else {
276
259
// traits data for profile completion checklist is already there for the user
277
260
// We update it based on old checklist data and updated metadata by the current event message
278
- const newChecklist = await getNewChecklist ( body [ 0 ] . traits . data [ 0 ] [ PROFILE_COMPLETION_TRAIT_PROPERTY_NAME ] , updatedMetadataItems , handle )
261
+ const newChecklist = await getNewChecklist ( body [ 0 ] . traits . data [ 0 ] [ PROFILE_COMPLETION_TRAIT_PROPERTY_NAME ] , fullyUpdatedMetadata )
279
262
280
263
if ( ! newChecklist . isUpdated ) {
281
264
// The checklist was not updated, there is no need to call member-api
@@ -296,7 +279,7 @@ async function handleUpdatedProfileCompletionMetadata (message, updatedMetadataI
296
279
logger . debug ( {
297
280
component,
298
281
context,
299
- message : `Successfully processed profile completion trait { user: '${ handle } ', updatedMetadata: ${ JSON . stringify ( updatedMetadataItems ) } }`
282
+ message : `Successfully processed profile completion trait { user: '${ handle } ', updatedMetadata: ${ JSON . stringify ( fullyUpdatedMetadata ) } }`
300
283
} )
301
284
}
302
285
@@ -330,6 +313,31 @@ processProfilePictureUploadMessage.schema = {
330
313
} ) . required ( )
331
314
}
332
315
316
+ /**
317
+ * This function retrieves the fully updated metadata items for the member.
318
+ * It gets the traits from the member traits API and returns the fully updated metadata with the updated flags
319
+ *
320
+ * @param {String } handle The member handle
321
+ * @param {Object } updatedMetadataItems The updated metadata generated as a result of the event
322
+ */
323
+ async function getFullyUpdatedMetadata ( handle , updatedMetadataItems ) {
324
+ const member = await helper . getMemberByHandle ( handle )
325
+ const existingTraits = await helper . getMemberTraits ( handle , _ . join ( _ . keys ( TRAITS_TO_PROFILE_COMPLETION_CHECKLIST_METADATA_MAP ) , ',' ) )
326
+ const updateTraitsMetadata = { }
327
+
328
+ for ( const key of _ . keys ( TRAITS_TO_PROFILE_COMPLETION_CHECKLIST_METADATA_MAP ) ) {
329
+ const trait = _ . find ( existingTraits , { traitId : key } )
330
+ updateTraitsMetadata [ TRAITS_TO_PROFILE_COMPLETION_CHECKLIST_METADATA_MAP [ key ] ] = _ . get ( trait , 'traits.data' , [ ] ) . length > 0
331
+ }
332
+
333
+ return _ . assign ( { } , {
334
+ profile_picture : ! _ . isEmpty ( _ . get ( member , 'photoURL' ) ) ,
335
+ bio : ! _ . isEmpty ( _ . get ( member , 'description' ) ) ,
336
+ skills : await helper . hasUserEnteredSkills ( handle ) ,
337
+ country : ! _ . isEmpty ( _ . get ( member , config . PROFILE_UPDATE_EVENT_COUNTRY_FIELD_NAME ) )
338
+ } , updateTraitsMetadata , updatedMetadataItems )
339
+ }
340
+
333
341
module . exports = {
334
342
processProfileUpdateMessage,
335
343
processCreateOrUpdateProfileTraitMessage,
0 commit comments