Currently working on a function that utilizes concatMap to perform sequential HTTP calls, such as adding a person, using the returned information to add a contact, and then adding some accounts.
This function takes in a list (in my case, portfolios) and for each portfolio, I must make calls to addAccount and addInvestorAccount.
For example, if there are two portfolios, it would require two separate sets of calls to these endpoints (addInvestorAccount depends on the result of addAccount).
I'm trying to figure out how to dynamically create these sets of HTTP calls and append them to the current stream. I've looked into forkJoin, but it seems to be more suitable for parallel independent calls.
I hope this explanation is clear. Here is a snippet of the code:
AddPerson(person: Person, portfolios: Portfolio[]) {
const addPerson = new AddPersonRequest();
// additional request props
return this.hostSvc.put(addPersonRequest)
.concatMap((response: Person) => {
const emailContactRequest = new CreateContactRequest();
emailContactRequest.PersonId = response.Id;
// additional request props
return this.hostSvc.put(emailContactRequest)
})
.concatMap((response: AccountName) => {
const addAccountRequest = new AddAccountRequest();
addAccountRequest.Data = new Account();
addAccountRequest.Data.AccountId = response.Id
addAccountRequest.Data.PortfolioId = portfolios[0].Id
// additional request props
return this.hostSvc.put(addAccountRequest);
})
.concatMap((response: Account) => {
const addInvestorAccountRequest = new AddInvestorAccountRequest();
addInvestorAccountRequest.Data = new InvestorAccount();
addInvestorAccountRequest.Data.AccountType = response.AccountType
// additional request props
return this.hostSvc.put(addInvestorAccountRequest);
}, (error) => console.log(error))
}
In the code provided above, you'll notice I'm using Portfolio[0], but I actually need to iterate through all portfolios for addAccountRequest and addInvestorAccountRequest.
Thank you!