Skip to content

Commit

Permalink
refactor(api): after pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
djabarovgeorge authored and tatarco committed Sep 30, 2024
1 parent ee35239 commit bdaaa8e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class NotificationTemplateController {
data: body.data,
__source: query?.__source,
type: WorkflowTypeEnum.REGULAR,
origin: WorkflowOriginEnum.NOVU,
origin: WorkflowOriginEnum.NOVU_CLOUD,
})
);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/app/workflows-v1/workflow-v1.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class WorkflowControllerV1 {
data: body.data,
__source: query?.__source,
type: WorkflowTypeEnum.REGULAR,
origin: WorkflowOriginEnum.NOVU,
origin: WorkflowOriginEnum.NOVU_CLOUD,
})
);
}
Expand Down
1 change: 0 additions & 1 deletion apps/api/src/app/workflows-v2/customTypes.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../dto/workflow-commons-fields';
import { WorkflowResponseDto } from '../dto/workflow-response-dto';

function toResponseWorkflowDto(
export function toResponseWorkflowDto(
template: NotificationTemplateEntity,
preferences: GetPreferencesResponseDto | undefined,
stepIdToControlValuesMap: { [p: string]: ControlValuesEntity }
Expand All @@ -28,7 +28,7 @@ function toResponseWorkflowDto(
steps: getSteps(template, stepIdToControlValuesMap),
name: template.name,
description: template.description,
origin: template.origin || WorkflowOriginEnum.NOVU,
origin: template.origin || WorkflowOriginEnum.NOVU_CLOUD,
updatedAt: template.updatedAt || 'Missing Updated At',
createdAt: template.createdAt || 'Missing Create At',
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { EnvironmentWithUserObjectCommand } from '@novu/application-generic';
import { IsDefined, IsString } from 'class-validator';

export type RequiredProp<T, K extends keyof T> = T & { [P in K]-?: T[P] };

export class DeleteWorkflowCommand extends EnvironmentWithUserObjectCommand {
@IsString()
@IsDefined()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { GetPreferences, GetPreferencesCommand, GetPreferencesResponseDto } from
import { GetWorkflowCommand } from './get-workflow.command';
import { WorkflowNotFoundException } from '../../exceptions/workflow-not-found-exception';
import { WorkflowResponseDto } from '../../dto/workflow-response-dto';
import { toResponseWorkflowDto } from '../../mappers/notification-template-mapper';

@Injectable()
export class GetWorkflowUseCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { StepCreateDto, StepDto, StepUpdateDto } from '../../dto/workflow-common
import { StepUpsertMechanismFailedMissingIdException } from '../../exceptions/step-upsert-mechanism-failed-missing-id.exception';
import { CreateWorkflowDto } from '../../dto/create-workflow-dto';
import { WorkflowResponseDto } from '../../dto/workflow-response-dto';
import { toResponseWorkflowDto } from '../../mappers/notification-template-mapper';

function buildUpsertControlValuesCommand(command: UpsertWorkflowCommand, persistedStep, persistedWorkflow, stepInDto) {
return UpsertControlValuesCommand.create({
Expand Down Expand Up @@ -113,10 +114,10 @@ export class UpsertWorkflowUseCase {
}
await this.upsertPreferences(workflow, command);

return await this.getPresistedPreferences(workflow);
return await this.getPersistedPreferences(workflow);
}

private async getPresistedPreferences(workflow) {
private async getPersistedPreferences(workflow) {
return await this.getPreferencesUseCase.execute(
GetPreferencesCommand.create({
environmentId: workflow._environmentId,
Expand Down Expand Up @@ -172,7 +173,7 @@ export class UpsertWorkflowUseCase {
name: workflowDto.name,
__source: workflowDto.__source || WorkflowCreationSourceEnum.DASHBOARD,
type: WorkflowTypeEnum.BRIDGE,
origin: WorkflowOriginEnum.NOVU,
origin: WorkflowOriginEnum.NOVU_CLOUD,
steps: this.mapSteps(workflowDto.steps),
payloadSchema: {},
active: isWorkflowActive,
Expand Down Expand Up @@ -271,6 +272,7 @@ export class UpsertWorkflowUseCase {
if (!persistedWorkflow?.steps) {
return;
}

for (const persistedStep of persistedWorkflow.steps) {
if (this.isStepUpdateDto(stepUpdateRequest) && persistedStep._templateId === stepUpdateRequest.stepUuid) {
return persistedStep;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class CreateWorkflow {
private createChange: CreateChange,
@Inject(forwardRef(() => AnalyticsService))
private analyticsService: AnalyticsService,
protected moduleRef: ModuleRef
protected moduleRef: ModuleRef,
) {}

async execute(usecaseCommand: CreateWorkflowCommand) {
Expand Down Expand Up @@ -92,7 +92,7 @@ export class CreateWorkflow {
command,
templateSteps,
trigger,
triggerIdentifier
triggerIdentifier,
);

await this.createWorkflowChange(command, storedWorkflow, parentChangeId);
Expand All @@ -107,12 +107,11 @@ export class CreateWorkflow {
}
const service = this.moduleRef.get(
require('@novu/ee-shared-services')?.TranslationsService,
{ strict: false }
{ strict: false },
);

const locales = await service.createTranslationAnalytics(
storedWorkflow
);
const locales =
await service.createTranslationAnalytics(storedWorkflow);

this.analyticsService.track(
'Locale used in workflow - [Translations]',
Expand All @@ -122,14 +121,14 @@ export class CreateWorkflow {
_environment: command.environmentId,
workflowId: storedWorkflow._id,
locales,
}
},
);
}
} catch (e) {
Logger.error(
e,
`Unexpected error while importing enterprise modules`,
'TranslationsService'
'TranslationsService',
);
}

Expand All @@ -144,15 +143,15 @@ export class CreateWorkflow {
for (const variant of variants) {
if (isVariantEmpty(variant)) {
throw new ApiException(
`Variant conditions are required, variant name ${variant.name} id ${variant._id}`
`Variant conditions are required, variant name ${variant.name} id ${variant._id}`,
);
}
}
}

private async createNotificationTrigger(
command: CreateWorkflowCommand,
triggerIdentifier: string
triggerIdentifier: string,
): Promise<INotificationTrigger> {
const contentService = new ContentService();
const { variables, reservedVariables } =
Expand All @@ -163,7 +162,7 @@ export class CreateWorkflow {
const templateCheckIdentifier =
await this.notificationTemplateRepository.findByTriggerIdentifier(
command.environmentId,
triggerIdentifier
triggerIdentifier,
);

const trigger: INotificationTrigger = {
Expand Down Expand Up @@ -200,7 +199,7 @@ export class CreateWorkflow {

private sendTemplateCreationEvent(
command: CreateWorkflowCommand,
triggerIdentifier: string
triggerIdentifier: string,
) {
if (
command.name !== 'On-boarding notification' &&
Expand All @@ -215,15 +214,15 @@ export class CreateWorkflow {
channels: command.steps?.map((i) => i.template?.type),
__source: command.__source,
triggerIdentifier,
}
},
);
}
}

private async createWorkflowChange(
command: CreateWorkflowCommand,
item,
parentChangeId: string
parentChangeId: string,
) {
if (!isBridgeWorkflow(command.type)) {
await this.createChange.execute(
Expand All @@ -234,7 +233,7 @@ export class CreateWorkflow {
type: ChangeEntityTypeEnum.NOTIFICATION_TEMPLATE,
item,
changeId: parentChangeId,
})
}),
);
}
}
Expand All @@ -243,7 +242,7 @@ export class CreateWorkflow {
command: CreateWorkflowCommand,
templateSteps: INotificationTemplateStep[],
trigger: INotificationTrigger,
triggerIdentifier: string
triggerIdentifier: string,
) {
const savedWorkflow = await this.notificationTemplateRepository.create({
_organizationId: command.organizationId,
Expand Down Expand Up @@ -271,7 +270,7 @@ export class CreateWorkflow {

const item = await this.notificationTemplateRepository.findById(
savedWorkflow._id,
command.environmentId
command.environmentId,
);
if (!item)
throw new NotFoundException(`Workflow ${savedWorkflow._id} is not found`);
Expand All @@ -283,7 +282,7 @@ export class CreateWorkflow {

private async storeTemplateSteps(
command: CreateWorkflowCommand,
parentChangeId: string
parentChangeId: string,
): Promise<INotificationTemplateStep[]> {
let parentStepId: string | null = null;
const templateSteps: INotificationTemplateStep[] = [];
Expand Down Expand Up @@ -317,7 +316,7 @@ export class CreateWorkflow {
stepId: step.template.stepId,
parentChangeId,
workflowType: command.type,
})
}),
),
await this.storeVariantSteps({
variants: step.variants,
Expand Down Expand Up @@ -381,7 +380,7 @@ export class CreateWorkflow {
for (const variant of variants) {
if (!variant.template)
throw new ApiException(
`Unexpected error: variants message template is missing`
`Unexpected error: variants message template is missing`,
);

const variantTemplate = await this.createMessageTemplate.execute(
Expand All @@ -404,7 +403,7 @@ export class CreateWorkflow {
actor: variant.template.actor,
parentChangeId,
workflowType,
})
}),
);

variantsList.push({
Expand Down Expand Up @@ -450,13 +449,13 @@ export class CreateWorkflow {
blueprintId: command.blueprintId,
__source: command.__source,
type: WorkflowTypeEnum.REGULAR,
origin: command.origin ?? WorkflowOriginEnum.NOVU,
origin: command.origin ?? WorkflowOriginEnum.NOVU_CLOUD,
});
}

private normalizeSteps(commandSteps: NotificationStep[]): NotificationStep[] {
const steps = JSON.parse(
JSON.stringify(commandSteps)
JSON.stringify(commandSteps),
) as NotificationStep[];

return steps.map((step) => {
Expand All @@ -474,7 +473,7 @@ export class CreateWorkflow {

private async handleFeeds(
steps: NotificationStepEntity[],
command: CreateWorkflowCommand
command: CreateWorkflowCommand,
): Promise<NotificationStepEntity[]> {
for (let i = 0; i < steps.length; i += 1) {
const step = steps[i];
Expand Down Expand Up @@ -517,7 +516,7 @@ export class CreateWorkflow {
organizationId: command.organizationId,
userId: command.userId,
changeId: FeedRepository.createObjectId(),
})
}),
);
}
}
Expand All @@ -531,7 +530,7 @@ export class CreateWorkflow {
}

private async handleGroup(
command: CreateWorkflowCommand
command: CreateWorkflowCommand,
): Promise<NotificationGroupEntity> {
if (!command.notificationGroup?.name)
throw new NotFoundException(`Notification group was not provided`);
Expand All @@ -558,7 +557,7 @@ export class CreateWorkflow {
userId: command.userId,
type: ChangeEntityTypeEnum.NOTIFICATION_GROUP,
changeId: NotificationGroupRepository.createObjectId(),
})
}),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const variantSchemePart = {
},
origin: {
type: Schema.Types.String,
default: WorkflowOriginEnum.NOVU,
default: WorkflowOriginEnum.NOVU_CLOUD,
},
filters: [
{
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/src/types/notification-templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export enum WorkflowTypeEnum {
*
* The `WorkflowOriginEnum` is used to specify the source of the workflow,
* which helps determine which endpoint to call during the Preview & Execution phase.
* * - 'novu' indicates that the workflow originates from Novu's platform, so the Novu-hosted endpoint is used.
* * - 'novu-cloud' indicates that the workflow originates from Novu's platform, so the Novu-hosted endpoint is used.
* * - 'external' indicates that the workflow originates from an external source, requiring a call to a customer-hosted Bridge endpoint.
*/
export enum WorkflowOriginEnum {
NOVU = 'novu',
NOVU_CLOUD = 'novu-cloud',
EXTERNAL = 'external',
}

Expand Down

0 comments on commit bdaaa8e

Please sign in to comment.