Skip to content

Commit

Permalink
Validate role for internship projects
Browse files Browse the repository at this point in the history
  • Loading branch information
atGit2021 committed Jun 6, 2024
1 parent 2016be4 commit acb3254
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/components/project/workflow/project-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TransitionType as Type } from '../../workflow/dto';
import { ProjectStep as Step } from '../dto';
import { ProjectWorkflowEvent } from './dto';
import {
hasValidRoleForProjectType,
IsMultiplication,
IsNotMultiplication,
RequireOngoingEngagementsToBeFinalizingCompletion,
Expand Down Expand Up @@ -116,15 +117,15 @@ export const ProjectWorkflow = defineWorkflow({
to: Step.PrepForFinancialEndorsement,
label: 'Endorse Plan',
type: Type.Approve,
conditions: IsNotMultiplication,
conditions: [IsNotMultiplication, hasValidRoleForProjectType],
},
'Pending Consultant Endorsement -> Prep for Financial Endorsement Without Consultant Endorsement':
{
from: Step.PendingConsultantEndorsement,
to: Step.PrepForFinancialEndorsement,
label: 'Do Not Endorse Plan',
type: Type.Neutral,
conditions: IsNotMultiplication,
conditions: [IsNotMultiplication, hasValidRoleForProjectType],
},

// Prep for Financial Endorsement
Expand Down
30 changes: 30 additions & 0 deletions src/components/project/workflow/transitions/conditions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ScopedRole } from '../../../authorization/dto';
import { EngagementService } from '../../../engagement';
import { EngagementStatus } from '../../../engagement/dto';
import { TransitionCondition } from '../../../workflow/transitions/conditions';
Expand All @@ -23,6 +24,35 @@ export const IsMultiplication: Condition = {
},
};

export const hasValidRoleForProjectType: Condition = {
description: 'Validate role by project type',
resolve({ project, session }) {
const validRoles: ScopedRole[] = [
'global:Administrator',
'global:Consultant',
'global:ConsultantManager',
];
const hasAtLeastOneValidRole = validRoles.some((role) =>
session?.roles.includes(role),
);

switch (project.type) {
case 'MomentumTranslation':
return {
status: 'ENABLED',
};
case 'Internship':
return {
status: hasAtLeastOneValidRole ? 'ENABLED' : 'OMIT',
};
default:
return {
status: 'OMIT',
};
}
},
};

export const RequireOngoingEngagementsToBeFinalizingCompletion: Condition = {
description:
'All engagements must be Finalizing Completion or in a terminal status',
Expand Down
2 changes: 2 additions & 0 deletions src/components/project/workflow/transitions/dynamic-step.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ModuleRef } from '@nestjs/core';
import { Session } from '~/common';
import { DynamicState } from '../../../workflow/transitions/dynamic-state';
import { Project, ProjectStep, ProjectStep as Step } from '../../dto';
import { ProjectWorkflowRepository } from '../project-workflow.repository';

export interface ResolveParams {
project: Project;
moduleRef: ModuleRef;
session?: Session;
}

export const BackTo = (
Expand Down
6 changes: 5 additions & 1 deletion src/components/workflow/workflow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const WorkflowService = <W extends Workflow>(workflow: W) => {
dynamicContext: W['context'],
session: Session,
) {
const dynamicContextWithSession = { ...dynamicContext, session };
let available = this.workflow.transitions;

// Filter out non applicable transitions
Expand All @@ -52,7 +53,10 @@ export const WorkflowService = <W extends Workflow>(workflow: W) => {
await Promise.all(
[...new Set(conditions)].map(
async (condition) =>
[condition, await condition.resolve(dynamicContext)] as const,
[
condition,
await condition.resolve(dynamicContextWithSession),
] as const,
),
),
);
Expand Down

0 comments on commit acb3254

Please sign in to comment.