1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { AccountService } from '@app/shared/account/account.service'
4 import { Account } from '@app/shared/account/account.model'
5 import { RestExtractor, UserService } from '@app/shared'
6 import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
7 import { Subscription } from 'rxjs'
8 import { AuthService, Notifier, RedirectService } from '@app/core'
9 import { User, UserRight } from '../../../../shared'
12 templateUrl: './accounts.component.html',
13 styleUrls: [ './accounts.component.scss' ]
15 export class AccountsComponent implements OnInit, OnDestroy {
19 private routeSub: Subscription
22 private route: ActivatedRoute,
23 private userService: UserService,
24 private accountService: AccountService,
25 private notifier: Notifier,
26 private restExtractor: RestExtractor,
27 private redirectService: RedirectService,
28 private authService: AuthService
32 this.routeSub = this.route.params
34 map(params => params[ 'accountId' ]),
35 distinctUntilChanged(),
36 switchMap(accountId => this.accountService.getAccount(accountId)),
37 tap(account => this.getUserIfNeeded(account)),
38 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
41 account => this.account = account,
43 err => this.notifier.error(err.message)
48 if (this.routeSub) this.routeSub.unsubscribe()
52 this.getUserIfNeeded(this.account)
56 this.redirectService.redirectToHomepage()
59 private getUserIfNeeded (account: Account) {
60 if (!account.userId) return
61 if (!this.authService.isLoggedIn()) return
63 const user = this.authService.getUser()
64 if (user.hasRight(UserRight.MANAGE_USERS)) {
65 this.userService.getUser(account.userId)
67 user => this.user = user,
69 err => this.notifier.error(err.message)