1
1
/* eslint-disable no-case-declarations */
2
2
import { Injectable } from '@nestjs/common' ;
3
+ import { setOf } from '@seedcompany/common' ;
3
4
import { node , relation } from 'cypher-query-builder' ;
4
5
import { first , intersection } from 'lodash' ;
5
6
import {
@@ -12,7 +13,6 @@ import { ILogger, Logger } from '~/core';
12
13
import { Identity } from '~/core/authentication' ;
13
14
import { DatabaseService } from '~/core/database' ;
14
15
import { ACTIVE , INACTIVE } from '~/core/database/query' ;
15
- import { withoutScope } from '../authorization/dto' ;
16
16
import { ProjectStep } from '../project/dto' ;
17
17
import {
18
18
EngagementStatus ,
@@ -29,7 +29,7 @@ interface StatusRule {
29
29
transitions : Transition [ ] ;
30
30
}
31
31
32
- const rolesThatCanBypassWorkflow : Role [ ] = [ Role . Administrator ] ;
32
+ const rolesThatCanBypassWorkflow = setOf < Role > ( [ Role . Administrator ] ) ;
33
33
34
34
@Injectable ( )
35
35
export class EngagementRules {
@@ -314,7 +314,6 @@ export class EngagementRules {
314
314
315
315
async getAvailableTransitions (
316
316
engagementId : ID ,
317
- currentUserRoles ?: Role [ ] ,
318
317
changeset ?: ID ,
319
318
) : Promise < EngagementStatusTransition [ ] > {
320
319
const session = this . identity . current ;
@@ -330,8 +329,7 @@ export class EngagementRules {
330
329
) ;
331
330
332
331
// If current user is not an approver (based on roles) then don't allow any transitions
333
- currentUserRoles ??= session . roles . map ( withoutScope ) ;
334
- if ( intersection ( approvers , currentUserRoles ) . length === 0 ) {
332
+ if ( session . roles . intersection ( setOf ( approvers ) ) . size === 0 ) {
335
333
return [ ] ;
336
334
}
337
335
@@ -356,28 +354,22 @@ export class EngagementRules {
356
354
return availableTransitionsAccordingToProject ;
357
355
}
358
356
359
- async canBypassWorkflow ( ) {
360
- const session = this . identity . current ;
361
- const roles = session . roles . map ( withoutScope ) ;
362
- return intersection ( rolesThatCanBypassWorkflow , roles ) . length > 0 ;
357
+ canBypassWorkflow ( ) {
358
+ const { roles } = this . identity . current ;
359
+ return roles . intersection ( rolesThatCanBypassWorkflow ) . size > 0 ;
363
360
}
364
361
365
362
async verifyStatusChange (
366
363
engagementId : ID ,
367
364
nextStatus : EngagementStatus ,
368
365
changeset ?: ID ,
369
366
) {
370
- // If current user's roles include a role that can bypass workflow
371
- // stop the check here.
372
- const session = this . identity . current ;
373
- const currentUserRoles = session . roles . map ( withoutScope ) ;
374
- if ( intersection ( rolesThatCanBypassWorkflow , currentUserRoles ) . length > 0 ) {
367
+ if ( this . canBypassWorkflow ( ) ) {
375
368
return ;
376
369
}
377
370
378
371
const transitions = await this . getAvailableTransitions (
379
372
engagementId ,
380
- currentUserRoles ,
381
373
changeset ,
382
374
) ;
383
375
0 commit comments