In my project, I have the need to perform 2 API calls in sequence. The second call does not depend on the first one, and both calls update the database. However, when using the code below, the update for the second call is happening multiple times, leading to multiple updates on the same record which should be avoided. Any assistance on this matter would be greatly appreciated.
updateUserCommentsObservable(): Observable<any> {
if (!this.userComments) {
return EMPTY;
}
const source = this.arrayGroupBy<TrailerComparisonUserComment>(
this.userComments,
comment => comment.trailerComparisonId);
return from(Object.keys(source)).pipe(
mergeMap(x => this.trailerComparisonService.updateUserComments(
x, source[x])
)
);
}
this.updateUserCommentsObservable().pipe(
flatMap(() => from(this.trailerComparisons).pipe(
mergeMap(trailerComparison =>
this.trailerComparisonService.saveReviewComplete(
trailerComparison.trailerComparisonId))
)
)
).subscribe(() => {
this.userComments = [];
this.disableStage1Review = true;
let snackBarRef = this.snackBar.open('Well done! Stage1 Review Complete has been successfully saved.', 'Dismiss');
}, error => {
console.log("Error", error);
let snackBarRef = this.snackBar.open('Error! ' + error.error.message, 'Dismiss');
});