@@ -3,7 +3,7 @@ import { ActivatedRoute, Router } from '@angular/router';
33import get from 'lodash-es/get' ;
44import { FormBuilder , FormControl , FormGroup , ValidatorFn , Validators } from '@angular/forms' ;
55import { NzModalService } from 'ng-zorro-antd/modal' ;
6- import { BehaviorSubject , combineLatest , interval , Subscription } from 'rxjs' ;
6+ import { BehaviorSubject , combineLatest , interval , of , Subscription } from 'rxjs' ;
77import { concatMap , filter , map , switchMap } from 'rxjs/operators' ;
88import { NzNotificationService } from 'ng-zorro-antd/notification' ;
99import { ApiService , UTaskLibOptions } from '../../@services/api.service' ;
@@ -30,19 +30,124 @@ export class TaskComponent implements OnInit, OnDestroy {
3030 private _task$ = new BehaviorSubject < Task | null > ( null ) ;
3131 readonly task$ = this . _task$ . asObservable ( ) ;
3232
33- readonly template$ = this . task$ . pipe ( switchMap ( task => this . api . template . get ( task . template_name ) ) ) ;
33+ readonly template$ = this . task$ . pipe ( switchMap ( task => {
34+ if ( ! task ) {
35+ return of ( null ) ;
36+ }
37+ return this . api . template . get ( task . template_name )
38+ } ) ) ;
3439
3540 private _resolution$ = new BehaviorSubject < Resolution | null > ( null ) ;
3641 readonly resolution$ = this . _resolution$ . asObservable ( ) ;
3742
38- readonly startOver$ = combineLatest ( [ this . meta$ , this . template$ , this . resolution$ ] ) . pipe ( map ( ( [ meta , template , resolution ] ) => {
39- if ( [ 'PAUSED' , 'CANCELLED' , 'BLOCKED_BADREQUEST' ] . indexOf ( resolution ?. state ) === - 1 ) {
43+ private _isResolver$ = combineLatest ( [
44+ this . meta$ , this . template$ , this . task$ , this . resolution$
45+ ] ) . pipe ( map ( ( [ meta , template , task , resolution ] ) => {
46+ if ( meta ?. user_is_admin ) {
47+ return true ;
48+ }
49+
50+ // check if the current user is declared as resolver in the task template
51+ if ( meta && ( template ?. allowed_resolver_usernames ?? [ ] ) . includes ( meta . username ) ) {
52+ return true ;
53+ }
54+
55+ // check if the current user has at least one group declared as resolver in the task template
56+ if ( meta && ( template ?. allowed_resolver_groups ?? [ ] ) . some ( v => meta . user_groups . includes ( v ) ) ) {
57+ return true ;
58+ }
59+
60+ // check if the current user is declared as resolver in the task
61+ if ( meta && ( task ?. resolver_usernames ?? [ ] ) . includes ( meta . username ) ) {
62+ return true ;
63+ }
64+
65+ // check if the current user has at least one group declared as resolver in the task
66+ if ( meta && ( task ?. resolver_groups ?? [ ] ) . some ( v => meta . user_groups . includes ( v ) ) ) {
67+ return true ;
68+ }
69+
70+ // check if the current user is the resolution resolver
71+ if ( meta && resolution && meta . username === resolution . resolver_username ) {
72+ return true ;
73+ }
74+
75+ return false ;
76+ } ) )
77+
78+ readonly canStartOver$ = combineLatest ( [
79+ this . meta$ , this . template$ , this . resolution$ , this . _isResolver$
80+ ] ) . pipe ( map ( ( [ meta , template , resolution , isResolver ] ) => {
81+ if ( ! [ 'PAUSED' , 'CANCELLED' , 'BLOCKED_BADREQUEST' ] . includes ( resolution ?. state ) ) {
4082 return false ;
4183 }
4284
43- return ! ! meta ?. user_is_admin || ! ! template ?. allow_task_start_over ;
85+ if ( meta ?. user_is_admin ) {
86+ return true ;
87+ }
88+
89+ if ( ! template ?. allow_task_start_over ) {
90+ return false ;
91+ }
92+
93+ return isResolver ;
4494 } ) ) ;
4595
96+ readonly canRun$ = combineLatest ( [ this . resolution$ , this . _isResolver$ ] ) . pipe ( map ( ( [ resolution , isResolver ] ) => {
97+ if ( [ 'CANCELLED' , 'RUNNING' , 'DONE' ] . includes ( resolution ?. state ) ) {
98+ return false ;
99+ }
100+
101+ return isResolver ;
102+ } ) ) ;
103+
104+ readonly canPause$ = combineLatest ( [ this . resolution$ , this . _isResolver$ ] ) . pipe ( map ( ( [ resolution , isResolver ] ) => {
105+ if ( [ 'CANCELLED' , 'RUNNING' , 'DONE' , 'PAUSED' ] . includes ( resolution ?. state ) ) {
106+ return false ;
107+ }
108+
109+ return isResolver ;
110+ } ) ) ;
111+
112+ readonly canExtend$ = combineLatest ( [ this . resolution$ , this . _isResolver$ ] ) . pipe ( map ( ( [ resolution , isResolver ] ) => {
113+ if ( resolution ?. state !== 'BLOCKED_MAXRETRIES' ) {
114+ return false ;
115+ }
116+
117+ return isResolver ;
118+ } ) ) ;
119+
120+ readonly canCancel$ = combineLatest ( [ this . resolution$ , this . _isResolver$ ] ) . pipe ( map ( ( [ resolution , isResolver ] ) => {
121+ if ( [ 'CANCELLED' , 'RUNNING' , 'DONE' ] . includes ( resolution ?. state ) ) {
122+ return false ;
123+ }
124+
125+ return isResolver ;
126+ } ) ) ;
127+
128+ readonly canEdit$ = combineLatest ( [ this . resolution$ , this . meta$ ] ) . pipe ( map ( ( [ resolution , meta ] ) => {
129+ if ( resolution ?. state !== 'PAUSED' ) {
130+ return false ;
131+ }
132+
133+ return ! ! meta ?. user_is_admin ;
134+ } ) ) ;
135+
136+ readonly canEditRequest$ = combineLatest ( [ this . resolution$ , this . _isResolver$ ] ) . pipe ( map ( ( [ resolution , isResolver ] ) => {
137+ if ( resolution ?. state !== 'PAUSED' ) {
138+ return false ;
139+ }
140+
141+ return isResolver ;
142+ } ) ) ; ;
143+
144+ readonly canDeleteRequest$ = combineLatest ( [ this . task$ , this . meta$ ] ) . pipe ( map ( ( [ task , meta ] ) => {
145+ if ( [ 'RUNNING' , 'BLOCKED' ] . includes ( task ?. state ) ) {
146+ return false ;
147+ }
148+
149+ return ! ! meta ?. user_is_admin ;
150+ } ) ) ;
46151
47152 validateResolveForm ! : FormGroup ;
48153 validateRejectForm ! : FormGroup ;
0 commit comments