skip to Main Content

The below code is to get the user list associated with particular id and this service.ts file.

async getId(id:number){
//below code will give us participants list based on id
let participants = await this.participantRepo.findById(id)

//here I am looping each participant to get their profile picture if it's available
   for (const participant of participants) {
      let result = await this.profilePictureService.getprofilePicture(participant.user);
      //here in **result** I will get profile picture of each user if it's available
    }

const [internalParticipants, externalParticipants] = _partition(
      participants,
      (p: CampParticipants) => p.participantRoleId !== ParticipantRoleEnum.External
    );

 return {
      internalParticipants,
      externalParticipants,
    };

}

Sample response for participants:

{
  "internalParticipants": [
    {
      "id": 515,
      "userId": 126,      "
      "participantRoleId": 1,
      "user": {
        "id": 126,
        "firstName": "Test1",
        "lastName": "Test",
        "mail": "[email protected]",
       }
    },
    {
      "id": 515,
      "userId": 1219,
      "participantRoleId": 2,
       "user": {
        "id": 1219,
        "firstName": "Rob",
        "lastName": "const",
        "mail": "[email protected]",        
      }
    }
],
  "externalParticipants": []
}

Sample response for profile picture:

{
"photoName": "profile-picture.png",
"buffer": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABDgAAAQ4CAYAAADsEGyPAAAAAXNSR0IArc"
}

Suppose if we have profile picture associated only with "userId": 126, how to merge both response to get result as below?

How to combine profile picture response with participants list response?
**

  • FinalResponse/output response

**

{
  "internalParticipants": [
    {
      "id": 515,
      "userId": 126,      "
      "participantRoleId": 1,
      "user": {
        "id": 126,
        "firstName": "Test1",
        "lastName": "Test",
        "mail": "[email protected]",
       }
      "photoName": "profile-picture.png",
      "buffer": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABDgAAAQ4CAYAAADsEGyPAAAAAXNSR0IArc"
    },
    {
      "id": 515,
      "userId": 1219,
      "participantRoleId": 2,
       "user": {
        "id": 1219,
        "firstName": "Rob",
        "lastName": "const",
        "mail": "[email protected]",        
      }
    },
  "externalParticipants": []
}

2

Answers


  1.  let internalParticipants = participants.map(async (pt) => {
                  pt.image = await this.profilePictureService.getprofilePicture(pt.user)
                  return pt; 
                }));
    

    You can map through participants which will add an additional key image which will contain image response object & assign it to any variable. Or else if you want to seperate it out just change logic to below inside map.

    let image = await this.profilePictureService.getprofilePicture(pt.user)
    pt.photoName = image.photoName;
    pt.buffer= image.buffer;
    return pt;
    

    then at the end return it like

    return {
     internalParticipants 
    }
    

    Hope it’s more clear now.

    Login or Signup to reply.
  2. for (let [index, participant] of participants.entries()) {
      let result = await this.profilePictureService.getprofilePicture(
        participant.user
      );
      if (result) participants[index] = { ...participants[index], ...result };
    }
    

    You can use spread operator of javascript if profile picture is available for a perticular user it this will add profile picture’s data in participants’s array

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search