For the sake of the example lets say I have a entity called client:
interface Client {
id: number
name: string
address: Address[]
}
interface Address {
id: number
street: string
}
In this case I cannot update the name of client and his addresses in the same call.
Instead I need to:
- Check the address to be created then create one by one (and store the value to be used later)
- Check the address to be updated then updated one by one (and store the value to be used later)
- And finally do another call to chenge the name of the client.
So what I want to do is join this multiple http calls that can happen conditionally in one observable.
This is what I have so far:
updateClientAndClientAddresses(): Observable<Client> {
return of({}).pipe(
******* Conditionally make all the create addresses calls *******
******* Conditionally make all the update addresses calls *******
******* Then make the update name call *******
switchMap(([updateNameResponse: Client, responseOfAllAddressesToBeCreated: Address, responseOfAllAddressesToUpdated: Address]) => {
// Ideally here I will have the results of all the calls made so far.
// Merge everything manually here
return {
...updateClientCallResponse,
addresses: [
...responseOfAllAddressesToBeCreated,
...responseOfAllAddressesToUpdated
]};
}),
)
}
Just to reinforce: Before I do the call to update the name I need to check if:
addressesToBeCreated.lengthif yes I need to do one particular call for every lenght of this array, wait for the response and store the result to be used latter on.
then I need to check if:
addressesToUpdated.lengthif yes I need to do another particular call vor every lenght of this array and wait for the response and store the result to be used latter on.Finally I will do the call to update the name od the client, get the result and then merge with all the responses that I conditionally made before.