I have an API that returns this example payload -
{
"uid": "string",
"queue": {
"size": 0,
"averageWaitTime": 0,
"inChair": "string",
"status": "OPEN",
"customers": [
{
"uid": "1b3",
"haircut": "SHAPE_UP",
"hasBeard": true,
"useScissors": true
},
{
"uid": "1b2",
"haircut": "SHAPE_UP",
"hasBeard": true,
"useScissors": true
}
]
}
}
What I need to do before returning the response to the calling function, is loop through the customers [] and make further HTTP requests using the "uid" value in each object of the array to obtain extra data that needs to be appended to its respective object. So it would look something like this -
{
"uid": "string",
"queue": {
"size": 0,
"averageWaitTime": 0,
"inChair": "string",
"status": "OPEN",
"customers": [
{
"uid": "1b3",
"haircut": "SHAPE_UP",
"hasBeard": true,
"useScissors": true,
"extra": {
"name": "Fred",
"telephone": "000"
}
},
{
"uid": "1b2",
"haircut": "SHAPE_UP",
"hasBeard": true,
"useScissors": true,
"extra": {
"name": "Fred",
"telephone": "000"
}
}
]
}
}
Here is what I have tried so far -
barberQueue(): Observable<BarberConfigurations> {
return this.http.get<BarberConfigurations>( `${environment.apiEndpoint}/barber/profile` )
.pipe(
mergeMap( ( response: any ) => {
return forkJoin(
response.queue.customers.map( customer => {
return this.customerService.get( customer.uid ).pipe(
map( resp => {
return {
...customer,
extra: resp
};
} )
);
} )
);
} ),
);
}
This is returning just an [] of the customer objects. What I need is still to return the rest of the original payload in the correct structure. I don't think I am quite getting my head around some of the operators.
Can someone help me to understand where I am going wrong?