-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): Move active workflows endpoints to a decorated contro…
…ller class (no-changelog)
- Loading branch information
Showing
12 changed files
with
226 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/cli/src/controllers/activeWorkflows.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Service } from 'typedi'; | ||
import { Authorized, Get, RestController } from '@/decorators'; | ||
import { WorkflowRequest } from '@/requests'; | ||
import { ActiveWorkflowsService } from '@/services/activeWorkflows.service'; | ||
|
||
@Service() | ||
@Authorized() | ||
@RestController('/active_workflows') | ||
export class ActiveWorkflowsController { | ||
constructor(private readonly activeWorkflowsService: ActiveWorkflowsService) {} | ||
|
||
@Get('/') | ||
async getActiveWorkflows(req: WorkflowRequest.GetAllActive) { | ||
return this.activeWorkflowsService.getAllActiveIdsForUser(req.user); | ||
} | ||
|
||
@Get('/error/:id') | ||
async getActiveError(req: WorkflowRequest.GetActivationError) { | ||
const { | ||
user, | ||
params: { id: workflowId }, | ||
} = req; | ||
return this.activeWorkflowsService.getActivationError(workflowId, user); | ||
} | ||
} |
23 changes: 22 additions & 1 deletion
23
packages/cli/src/databases/repositories/sharedWorkflow.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,31 @@ | ||
import { Service } from 'typedi'; | ||
import { DataSource, Repository } from 'typeorm'; | ||
import { DataSource, type FindOptionsWhere, Repository, In } from 'typeorm'; | ||
import { SharedWorkflow } from '../entities/SharedWorkflow'; | ||
import { type User } from '../entities/User'; | ||
|
||
@Service() | ||
export class SharedWorkflowRepository extends Repository<SharedWorkflow> { | ||
constructor(dataSource: DataSource) { | ||
super(SharedWorkflow, dataSource.manager); | ||
} | ||
|
||
async hasAccess(workflowId: string, user: User) { | ||
const where: FindOptionsWhere<SharedWorkflow> = { | ||
workflowId, | ||
}; | ||
if (!user.hasGlobalScope('workflow:read')) { | ||
where.userId = user.id; | ||
} | ||
return this.exist({ where }); | ||
} | ||
|
||
async getSharedWorkflowIds(workflowIds: string[]) { | ||
const sharedWorkflows = await this.find({ | ||
select: ['workflowId'], | ||
where: { | ||
workflowId: In(workflowIds), | ||
}, | ||
}); | ||
return sharedWorkflows.map((sharing) => sharing.workflowId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Service } from 'typedi'; | ||
|
||
import type { User } from '@db/entities/User'; | ||
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository'; | ||
import { WorkflowRepository } from '@db/repositories/workflow.repository'; | ||
import { ActivationErrorsService } from '@/ActivationErrors.service'; | ||
import { BadRequestError } from '@/errors/response-errors/bad-request.error'; | ||
import { Logger } from '@/Logger'; | ||
|
||
@Service() | ||
export class ActiveWorkflowsService { | ||
constructor( | ||
private readonly logger: Logger, | ||
private readonly workflowRepository: WorkflowRepository, | ||
private readonly sharedWorkflowRepository: SharedWorkflowRepository, | ||
private readonly activationErrorsService: ActivationErrorsService, | ||
) {} | ||
|
||
async getAllActiveIds() { | ||
const activationErrors = await this.activationErrorsService.getAll(); | ||
const activeWorkflowIds = await this.workflowRepository.getActiveIds(); | ||
return activeWorkflowIds.filter((workflowId) => !activationErrors[workflowId]); | ||
} | ||
|
||
async getAllActiveIdsForUser(user: User) { | ||
const activationErrors = await this.activationErrorsService.getAll(); | ||
const activeWorkflowIds = await this.workflowRepository.getActiveIds(); | ||
|
||
const hasFullAccess = user.hasGlobalScope('workflow:list'); | ||
if (hasFullAccess) { | ||
return activeWorkflowIds.filter((workflowId) => !activationErrors[workflowId]); | ||
} | ||
|
||
const sharedWorkflowIds = | ||
await this.sharedWorkflowRepository.getSharedWorkflowIds(activeWorkflowIds); | ||
return sharedWorkflowIds.filter((workflowId) => !activationErrors[workflowId]); | ||
} | ||
|
||
async getActivationError(workflowId: string, user: User) { | ||
const hasAccess = await this.sharedWorkflowRepository.hasAccess(workflowId, user); | ||
if (!hasAccess) { | ||
this.logger.verbose('User attempted to access workflow errors without permissions', { | ||
workflowId, | ||
userId: user.id, | ||
}); | ||
|
||
throw new BadRequestError(`Workflow with ID "${workflowId}" could not be found.`); | ||
} | ||
|
||
return this.activationErrorsService.get(workflowId); | ||
} | ||
} |
Oops, something went wrong.