Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { EnvironmentWithUserObjectCommand } from '@novu/application-generic';
import { IsDefined, IsString } from 'class-validator';
import { IsDefined, IsOptional, IsString } from 'class-validator';

export class GetWorkflowCommand extends EnvironmentWithUserObjectCommand {
@IsString()
@IsDefined()
workflowIdOrInternalId: string;

@IsString()
@IsOptional()
environmentId?: string;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { Instrument, InstrumentUsecase, PinoLogger } from '@novu/application-generic';
import {
EnvironmentRepository,
IntegrationEntity,
IntegrationRepository,
NotificationStepEntity,
Expand Down Expand Up @@ -29,6 +30,7 @@ export class GetWorkflowUseCase {
private getWorkflowWithPreferencesUseCase: GetWorkflowWithPreferencesUseCase,
private buildStepDataUsecase: BuildStepDataUsecase,
private integrationsRepository: IntegrationRepository,
private environmentRepository: EnvironmentRepository,
private logger: PinoLogger
) {
this.logger.setContext(this.constructor.name);
Expand All @@ -39,11 +41,15 @@ export class GetWorkflowUseCase {
command: GetWorkflowCommand,
workflowDataContainer?: WorkflowDataContainer
): Promise<WorkflowResponseDto> {
const effectiveEnvironmentId = await this.resolveEnvironmentId(command);

const user: UserSessionData = {
...command.user,
environmentId: effectiveEnvironmentId,
};

if (workflowDataContainer) {
const cachedDto = workflowDataContainer.getWorkflowDto(
command.workflowIdOrInternalId,
command.user.environmentId
);
const cachedDto = workflowDataContainer.getWorkflowDto(command.workflowIdOrInternalId, effectiveEnvironmentId);

if (cachedDto) {
this.logger.debug(`Using cached workflow DTO for ${command.workflowIdOrInternalId}`);
Expand All @@ -54,21 +60,40 @@ export class GetWorkflowUseCase {

const workflowWithPreferences = await this.getWorkflowWithPreferencesUseCase.execute(
GetWorkflowWithPreferencesCommand.create({
environmentId: command.user.environmentId,
environmentId: effectiveEnvironmentId,
organizationId: command.user.organizationId,
workflowIdOrInternalId: command.workflowIdOrInternalId,
userId: command.user._id,
})
);

const fullSteps = await this.getFullWorkflowSteps(workflowWithPreferences, command.user);
const fullSteps = await this.getFullWorkflowSteps(workflowWithPreferences, user);
const payloadExample = await generatePayloadExample(workflowWithPreferences);

const workflowDto = toResponseWorkflowDto(workflowWithPreferences, fullSteps, payloadExample);

return workflowDto;
}

private async resolveEnvironmentId(command: GetWorkflowCommand): Promise<string> {
const { environmentId } = command;

if (!environmentId || environmentId === command.user.environmentId) {
return command.user.environmentId;
}

const environment = await this.environmentRepository.findByIdAndOrganization(
environmentId,
command.user.organizationId
);

if (!environment) {
throw new NotFoundException(`Environment ${environmentId} not found`);
}

return environmentId;
}

private async getFullWorkflowSteps(
workflowWithPreferences: NotificationTemplateEntity,
user: UserSessionData
Expand Down
6 changes: 2 additions & 4 deletions apps/api/src/app/workflows-v2/workflow.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,8 @@ export class WorkflowController {
return this.getWorkflowUseCase.execute(
GetWorkflowCommand.create({
workflowIdOrInternalId,
user: {
...user,
environmentId: environmentId || user.environmentId,
},
user,
environmentId,
})
);
}
Expand Down
Loading