Is it possible to chain together 3 types of http requests correctly, where each request depends on data from the previous one and the number of required requests can vary?
In my database, there is a team table with a corresponding businessId, a supervisor table with a teamId, and an employee table with a supervisorId.
I hope this explanation so far makes sense.
As I am still learning RxJS, please bear with me if my understanding of these operators is not entirely accurate. Here is how I envision the workflow:
- Retrieve all teams associated with the selected business and save them
- Iterate through each team, saving observables in an array
- Use forkJoin on the array of observables to ensure they all complete
- Use concatMap again to proceed with fetching employees, based on supervisor data
- Create an array of observables for all supervisors
- Again, use forkJoin to get observables for employees
- Finally, subscribe to the last forkJoin to obtain employee data
Here is the current code snippet:
onSelectBusiness(event): void {
this.selectedBusinessId = event.target.value;
this._service.getAllTeams(this.selectedBusinessId).pipe(
concatMap((_teams) => {
let supervisorObservables: any[] = [];
_teams.forEach(t => {
// Need to call this http request for however many different teams there are
this.teams.push(t); // Saving the teams
supervisorObservables.push(this._service.getAllSupervisors(t.teamId))
})
return forkJoin({supervisorObservables}).pipe(
concatMap((_supervisors) => {
let employeeObservables: any[] = [];
_supervisors.forEach(s => {
this.supervisors.push(s); // saving supervisors
// Need to call this http request for as many different supervisors there are
employeeObservables.push(this._service.getAllEmployees(s.supervisorId))
})
return forkJoin({employeeObservables})
})
)
})
).subscribe(
({employeeObservables}) => {
// Even console.logs are not displaying
employeeObservables.forEach(e => {
this.employees.push(e); // Supposed to get my employee data here, but im not
})
}
)
}
The main issue lies in the final subscribe function which does not seem to be functioning as expected. While I am able to retrieve team and supervisor data, employee data remains inaccessible.
I have attempted to introduce another piece of code:
// Same as above code
// ...
return forkJoin({employeeObservables}).pipe(
concatMap(_employees) => {
// However, I do not have another http request to return, which is necessary for concatMap
// Furthermore, subscribing directly to this forkJoin is not feasible due to the requirement of concatMap at the end
}
)
Am I even able to achieve what I desire with the forEach loops and forkJoins within concatMaps? Any help, tips, or feedback would be greatly valued! :)