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
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.
then at the end return it like
Hope it’s more clear now.
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