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": "test@home.com",
}
},
{
"id": 515,
"userId": 1219,
"participantRoleId": 2,
"user": {
"id": 1219,
"firstName": "Rob",
"lastName": "const",
"mail": "rob.const@home.com",
}
}
],
"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? **
**
{
"internalParticipants": [
{
"id": 515,
"userId": 126, "
"participantRoleId": 1,
"user": {
"id": 126,
"firstName": "Test1",
"lastName": "Test",
"mail": "test@home.com",
}
"photoName": "profile-picture.png",
"buffer": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABDgAAAQ4CAYAAADsEGyPAAAAAXNSR0IArc"
},
{
"id": 515,
"userId": 1219,
"participantRoleId": 2,
"user": {
"id": 1219,
"firstName": "Rob",
"lastName": "const",
"mail": "rob.const@home.com",
}
},
"externalParticipants": []
}
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