0

Inside one resolver I need to get user data. First I need to check if its profile is 'Sales' if yes, using the username returned I have to get the salesPerson data from other http get method, or else, returns some data I can check if is empty.

I have 2 different methods:

userInfo(): Observable<User> {
        return this.httpClient.get('/endpoint/api/auth/me')
            .pipe(
                map(result => result['user'])
            )
    } // in this observable there is the username and profile inside.

getSalesPerson(username) {
    return this.httpClient.get<ISalesRep>(`${this.BASE_URL}/getVendedor?user=${usuario}`);
  } // is this observer there is salesPerson data with SalesPersonId

The observer of both are different. First question: on resolve method of resolver, what should be the type to use? The first one uses IUSer interface and second uses ISalesRep interface.

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): 
  Observable<????> | Promise<????> | ??? { ...  }

The second question: How can make the return?

return this.loginService.userInfo()
    .pipe (
      ???someMethod???((user) => { 
        if ( user.profile == 'Sales' ) {
            return this.otherService.getSalesPerson(user.username)
            .subscribe((data) => { take the SalesPerson Observable }
        } else  {
            return empty observable or some I can check if is empty;
        }
        })
      )
    .subscribe();

I apretiate someone can help. Thanks in advance

1 Answers1

0

I think something along this line should help

this.loginService.userInfo()
    .pipe (
      concatMap((user) => { 
        // User is the result of the userInfo method
        if ( user.profile == 'Sales' ) {
          return this.otherService.getSalesPerson(user.username)
        } else  {
          return of(null)
        }
      }),
      concatMap((salesPerson) => {
        if (!salesPerson) { /* do something else */ }
        // the result of this.otherSerivce.getSalesPerson()
      }),
    )
.subscribe();
Phix
  • 9,364
  • 4
  • 35
  • 62