this.httpService.get('/user/appUser').pipe(
concatMap(users => users),
concatMap(user =>
forkJoin({
billingAddr: this.httpService
.get('/user/appUserAddr', new HttpParams()
.set('where', JSON.stringify([{ pk_addr_name: user['fk_billing_addr_name'] }]))
),
shippingAddr: this.httpService
.get('/user/appUserAddr', new HttpParams()
.set('where', JSON.stringify([{ pk_addr_name: user['fk_shipping_addr_name'] }]))
)
}).pipe(map(addr => {
user['billingAddr'] = addr.billingAddr;
user['shippingAddr'] = addr.shippingAddr;
return user;
}))
),
tap(res => console.log(res)),
toArray()
);
The initial HTTP Request to get '/user/appUser' will retrieve an array similar to the following:
[
{user_name:'test1',fk_billing_addr_name:'addr1-1',fk_shipping_addr_name:'addr1-2'},
{user_name:'test2',fk_billing_addr_name:'addr2-1',fk_shipping_addr_name:'addr2-2'}
]
Next, I will fetch the separate billingAddr and shippingAddr values and append them back to the corresponding user object. This is handled in the second part of the code within the concatMap function.
After processing, the final output will appear as shown below:
[
{user_name:'test1',fk_billing_addr_name:'addr1-1',fk_shipping_addr_name:'addr1-2',billingAddr:[...],shippingAddr:[...]},
{user_name:'test2',fk_billing_addr_name:'addr2-1',fk_shipping_addr_name:'addr2-2',billingAddr:[...],shippingAddr:[...]}
]
Although the current implementation works effectively, there seems to be redundancy in the first concatMap(users=>users) operation. Is it possible to simplify the code by utilizing just one concatMap? Are there any other areas that can be optimized?