-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next' into activity-feed-page
- Loading branch information
Showing
133 changed files
with
3,272 additions
and
1,152 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
159 changes: 159 additions & 0 deletions
159
apps/api/src/app/workflows-v2/e2e/workflow-test-data.e2e.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,159 @@ | ||
import { expect } from 'chai'; | ||
import { UserSession } from '@novu/testing'; | ||
import { CreateWorkflowDto, StepTypeEnum, WorkflowCreationSourceEnum, WorkflowTestDataResponseDto } from '@novu/shared'; | ||
|
||
interface ITestStepConfig { | ||
type: StepTypeEnum; | ||
controlValues: Record<string, string>; | ||
} | ||
|
||
describe('Workflow Test Data', function () { | ||
let session: UserSession; | ||
|
||
beforeEach(async () => { | ||
session = new UserSession(); | ||
await session.initialize(); | ||
}); | ||
|
||
describe('GET /v2/workflows/:workflowId/test-data', () => { | ||
describe('single step workflows', () => { | ||
it('should generate correct schema for email notification', async () => { | ||
const emailStep: ITestStepConfig = { | ||
type: StepTypeEnum.EMAIL, | ||
controlValues: { | ||
subject: 'Welcome {{payload.user.name}}', | ||
body: 'Hello {{payload.user.name}}, your order {{payload.order.details.orderId}} is ready', | ||
}, | ||
}; | ||
|
||
const { testData } = await createAndFetchTestData(emailStep); | ||
|
||
expect(testData.payload.type).to.equal('object'); | ||
expect((testData as any).payload.properties.user.type).to.equal('object'); | ||
expect((testData as any).payload.properties.user.properties).to.have.property('name'); | ||
expect((testData as any).payload.properties.order.type).to.equal('object'); | ||
expect((testData as any).payload.properties.order.properties.details.type).to.equal('object'); | ||
expect((testData as any).payload.properties.order.properties.details.properties).to.have.property('orderId'); | ||
|
||
expect(testData.to.type).to.equal('object'); | ||
expect(testData.to.properties).to.have.property('email'); | ||
expect(testData.to.properties).to.have.property('subscriberId'); | ||
}); | ||
|
||
it('should generate correct schema for SMS notification', async () => { | ||
const smsStep: ITestStepConfig = { | ||
type: StepTypeEnum.SMS, | ||
controlValues: { | ||
content: 'Your verification code is {{payload.code}}', | ||
}, | ||
}; | ||
|
||
const { testData } = await createAndFetchTestData(smsStep); | ||
|
||
expect(testData.payload.type).to.equal('object'); | ||
expect(testData.payload.properties).to.have.property('code'); | ||
|
||
expect(testData.to.type).to.equal('object'); | ||
expect(testData.to.properties).to.have.property('phone'); | ||
expect(testData.to.properties).to.have.property('subscriberId'); | ||
}); | ||
|
||
it('should generate correct schema for in-app notification', async () => { | ||
const inAppStep: ITestStepConfig = { | ||
type: StepTypeEnum.IN_APP, | ||
controlValues: { | ||
content: 'New message from {{payload.sender}}', | ||
}, | ||
}; | ||
|
||
const { testData } = await createAndFetchTestData(inAppStep); | ||
|
||
expect(testData.payload.type).to.equal('object'); | ||
expect(testData.payload.properties).to.have.property('sender'); | ||
|
||
expect(testData.to).to.be.an('object'); | ||
expect(testData.to.type).to.equal('object'); | ||
expect(testData.to.properties).to.have.property('subscriberId'); | ||
expect(testData.to.properties).to.not.have.property('email'); | ||
expect(testData.to.properties).to.not.have.property('phone'); | ||
}); | ||
}); | ||
|
||
describe('multi-step workflows', () => { | ||
it('should combine variables from multiple notification steps', async () => { | ||
const steps: ITestStepConfig[] = [ | ||
{ | ||
type: StepTypeEnum.EMAIL, | ||
controlValues: { | ||
subject: 'Order {{payload.orderId}}', | ||
body: 'Status: {{payload.status}}', | ||
}, | ||
}, | ||
{ | ||
type: StepTypeEnum.SMS, | ||
controlValues: { | ||
content: 'Order {{payload.orderId}} update: {{payload.smsUpdate}}', | ||
}, | ||
}, | ||
]; | ||
|
||
const { testData } = await createAndFetchTestData(steps); | ||
|
||
expect(testData.payload.type).to.equal('object'); | ||
expect(testData.payload.properties).to.have.all.keys('orderId', 'status', 'smsUpdate'); | ||
|
||
expect(testData.to.type).to.equal('object'); | ||
expect(testData.to.properties).to.have.all.keys('subscriberId', 'email', 'phone'); | ||
}); | ||
}); | ||
|
||
describe('edge cases', () => { | ||
it('should handle workflow with no steps', async () => { | ||
const { testData } = await createAndFetchTestData([]); | ||
|
||
expect(testData.payload).to.deep.equal({}); | ||
expect(testData.to.properties).to.have.property('subscriberId'); | ||
}); | ||
}); | ||
}); | ||
|
||
async function createAndFetchTestData( | ||
stepsConfig: ITestStepConfig | ITestStepConfig[] | ||
): Promise<{ workflow: any; testData: WorkflowTestDataResponseDto }> { | ||
const steps = Array.isArray(stepsConfig) ? stepsConfig : [stepsConfig]; | ||
const workflow = await createWorkflow(steps); | ||
const testData = await getWorkflowTestData(workflow._id); | ||
|
||
return { workflow, testData }; | ||
} | ||
|
||
async function createWorkflow(steps: ITestStepConfig[]) { | ||
const createWorkflowDto: CreateWorkflowDto = { | ||
name: 'Test Workflow', | ||
workflowId: `test-workflow-${Date.now()}`, | ||
__source: WorkflowCreationSourceEnum.EDITOR, | ||
active: true, | ||
steps: steps.map((step, index) => ({ | ||
name: `Test Step ${index + 1}`, | ||
type: step.type, | ||
})), | ||
}; | ||
|
||
const { body } = await session.testAgent.post('/v2/workflows').send(createWorkflowDto); | ||
const workflow = body.data; | ||
|
||
for (const [index, step] of steps.entries()) { | ||
await session.testAgent | ||
.patch(`/v2/workflows/${workflow._id}/steps/${workflow.steps[index]._id}`) | ||
.send({ controlValues: step.controlValues }); | ||
} | ||
|
||
return workflow; | ||
} | ||
|
||
async function getWorkflowTestData(workflowId: string): Promise<WorkflowTestDataResponseDto> { | ||
const { body } = await session.testAgent.get(`/v2/workflows/${workflowId}/test-data`); | ||
|
||
return body.data; | ||
} | ||
}); |
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
16 changes: 16 additions & 0 deletions
16
apps/api/src/app/workflows-v2/usecases/build-payload-schema/build-payload-schema.command.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,16 @@ | ||
import { EnvironmentWithUserCommand } from '@novu/application-generic'; | ||
import { IsString, IsObject, IsNotEmpty, IsOptional } from 'class-validator'; | ||
|
||
export class BuildPayloadSchemaCommand extends EnvironmentWithUserCommand { | ||
@IsString() | ||
@IsNotEmpty() | ||
workflowId: string; | ||
|
||
/** | ||
* Control values used for preview purposes | ||
* The payload schema is used for control values validation and sanitization | ||
*/ | ||
@IsObject() | ||
@IsOptional() | ||
controlValues?: Record<string, unknown>; | ||
} |
66 changes: 66 additions & 0 deletions
66
apps/api/src/app/workflows-v2/usecases/build-payload-schema/build-payload-schema.usecase.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,66 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ControlValuesEntity, ControlValuesRepository } from '@novu/dal'; | ||
import { ControlValuesLevelEnum, JSONSchemaDto } from '@novu/shared'; | ||
import { Instrument, InstrumentUsecase } from '@novu/application-generic'; | ||
import { flattenObjectValues } from '../../util/utils'; | ||
import { pathsToObject } from '../../util/path-to-object'; | ||
import { extractLiquidTemplateVariables } from '../../util/template-parser/liquid-parser'; | ||
import { convertJsonToSchemaWithDefaults } from '../../util/jsonToSchema'; | ||
import { BuildPayloadSchemaCommand } from './build-payload-schema.command'; | ||
|
||
@Injectable() | ||
export class BuildPayloadSchema { | ||
constructor(private readonly controlValuesRepository: ControlValuesRepository) {} | ||
|
||
@InstrumentUsecase() | ||
async execute(command: BuildPayloadSchemaCommand): Promise<JSONSchemaDto> { | ||
const controlValues = await this.buildControlValues(command); | ||
|
||
if (!controlValues.length) { | ||
return {}; | ||
} | ||
|
||
const templateVars = this.extractTemplateVariables(controlValues); | ||
if (templateVars.length === 0) { | ||
return {}; | ||
} | ||
|
||
const variablesExample = pathsToObject(templateVars, { | ||
valuePrefix: '{{', | ||
valueSuffix: '}}', | ||
}).payload; | ||
|
||
return convertJsonToSchemaWithDefaults(variablesExample); | ||
} | ||
|
||
private async buildControlValues(command: BuildPayloadSchemaCommand) { | ||
let controlValues = command.controlValues ? [command.controlValues] : []; | ||
|
||
if (!controlValues.length) { | ||
controlValues = ( | ||
await this.controlValuesRepository.find( | ||
{ | ||
_environmentId: command.environmentId, | ||
_organizationId: command.organizationId, | ||
_workflowId: command.workflowId, | ||
level: ControlValuesLevelEnum.STEP_CONTROLS, | ||
controls: { $ne: null }, | ||
}, | ||
{ | ||
controls: 1, | ||
_id: 0, | ||
} | ||
) | ||
).map((item) => item.controls); | ||
} | ||
|
||
return controlValues; | ||
} | ||
|
||
@Instrument() | ||
private extractTemplateVariables(controlValues: Record<string, unknown>[]): string[] { | ||
const controlValuesString = controlValues.map(flattenObjectValues).flat().join(' '); | ||
|
||
return extractLiquidTemplateVariables(controlValuesString).validVariables.map((variable) => variable.name); | ||
} | ||
} |
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
Oops, something went wrong.