-
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.
- Loading branch information
Showing
14 changed files
with
383 additions
and
174 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
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
20 changes: 0 additions & 20 deletions
20
apps/api/src/app/workflows-v2/shared/build-string-schema.ts
This file was deleted.
Oops, something went wrong.
53 changes: 44 additions & 9 deletions
53
apps/api/src/app/workflows-v2/shared/map-step-type-to-result.mapper.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,11 +1,46 @@ | ||
import { ActionStepEnum, actionStepSchemas, ChannelStepEnum, channelStepSchemas } from '@novu/framework/internal'; | ||
import { JSONSchema } from 'json-schema-to-ts'; | ||
import { StepTypeEnum } from '@novu/shared'; | ||
|
||
export const mapStepTypeToResult = { | ||
[ChannelStepEnum.SMS]: channelStepSchemas[ChannelStepEnum.SMS].result, | ||
[ChannelStepEnum.EMAIL]: channelStepSchemas[ChannelStepEnum.EMAIL].result, | ||
[ChannelStepEnum.PUSH]: channelStepSchemas[ChannelStepEnum.PUSH].result, | ||
[ChannelStepEnum.CHAT]: channelStepSchemas[ChannelStepEnum.CHAT].result, | ||
[ChannelStepEnum.IN_APP]: channelStepSchemas[ChannelStepEnum.IN_APP].result, | ||
[ActionStepEnum.DELAY]: actionStepSchemas[ActionStepEnum.DELAY].result, | ||
[ActionStepEnum.DIGEST]: actionStepSchemas[ActionStepEnum.DIGEST].result, | ||
}; | ||
export function computeResultSchema(stepType: StepTypeEnum, payloadSchema?: JSONSchema) { | ||
const mapStepTypeToResult: Record<ChannelStepEnum & ActionStepEnum, JSONSchema> = { | ||
[ChannelStepEnum.SMS]: channelStepSchemas[ChannelStepEnum.SMS].result, | ||
[ChannelStepEnum.EMAIL]: channelStepSchemas[ChannelStepEnum.EMAIL].result, | ||
[ChannelStepEnum.PUSH]: channelStepSchemas[ChannelStepEnum.PUSH].result, | ||
[ChannelStepEnum.CHAT]: channelStepSchemas[ChannelStepEnum.CHAT].result, | ||
[ChannelStepEnum.IN_APP]: channelStepSchemas[ChannelStepEnum.IN_APP].result, | ||
[ActionStepEnum.DELAY]: actionStepSchemas[ActionStepEnum.DELAY].result, | ||
[ActionStepEnum.DIGEST]: buildDigestResult(payloadSchema), | ||
}; | ||
|
||
return mapStepTypeToResult[stepType]; | ||
} | ||
|
||
function buildDigestResult(payloadSchema?: JSONSchema) { | ||
return { | ||
type: 'object', | ||
properties: { | ||
events: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
properties: { | ||
id: { | ||
type: 'string', | ||
}, | ||
time: { | ||
type: 'string', | ||
}, | ||
payload: payloadSchema || { | ||
type: 'object', | ||
}, | ||
}, | ||
required: ['id', 'time', 'payload'], | ||
additionalProperties: false, | ||
}, | ||
}, | ||
}, | ||
required: ['events'], | ||
additionalProperties: false, | ||
}; | ||
} |
72 changes: 72 additions & 0 deletions
72
.../workflows-v2/usecases/get-step-schema/build-available-variable-schema-usecase.service.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,72 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { JSONSchema } from 'json-schema-to-ts'; | ||
import { NotificationStepEntity } from '@novu/dal'; | ||
import { JSONSchemaDto } from '@novu/shared'; | ||
import { computeResultSchema } from '../../shared'; | ||
|
||
@Injectable() | ||
class BuildAvailableVariableSchemaCommand { | ||
previousSteps: NotificationStepEntity[] | undefined; | ||
payloadSchema: JSONSchema; | ||
} | ||
|
||
export class BuildAvailableVariableSchemaUsecase { | ||
execute(command: BuildAvailableVariableSchemaCommand): JSONSchema { | ||
const { previousSteps, payloadSchema } = command; | ||
|
||
return { | ||
type: 'object', | ||
properties: { | ||
subscriber: buildSubscriberSchema(), | ||
steps: buildPreviousStepsSchema(previousSteps, payloadSchema), | ||
payload: payloadSchema, | ||
}, | ||
additionalProperties: false, | ||
} as const satisfies JSONSchema; | ||
} | ||
} | ||
function buildPreviousStepsSchema(previousSteps: NotificationStepEntity[] | undefined, payloadSchema?: JSONSchema) { | ||
type StepExternalId = string; | ||
let previousStepsProperties: Record<StepExternalId, JSONSchema> = {}; | ||
|
||
previousStepsProperties = (previousSteps || []).reduce( | ||
(acc, step) => { | ||
if (step.stepId && step.template?.type) { | ||
acc[step.stepId] = computeResultSchema(step.template.type, payloadSchema); | ||
} | ||
|
||
return acc; | ||
}, | ||
{} as Record<StepExternalId, JSONSchema> | ||
); | ||
|
||
return { | ||
type: 'object', | ||
properties: previousStepsProperties, | ||
required: [], | ||
additionalProperties: false, | ||
description: 'Previous Steps Results', | ||
} as const satisfies JSONSchema; | ||
} | ||
const buildSubscriberSchema = () => | ||
({ | ||
type: 'object', | ||
description: 'Schema representing the subscriber entity', | ||
properties: { | ||
firstName: { type: 'string', description: "Subscriber's first name" }, | ||
lastName: { type: 'string', description: "Subscriber's last name" }, | ||
email: { type: 'string', description: "Subscriber's email address" }, | ||
phone: { type: 'string', description: "Subscriber's phone number (optional)" }, | ||
avatar: { type: 'string', description: "URL to the subscriber's avatar image (optional)" }, | ||
locale: { type: 'string', description: 'Locale for the subscriber (optional)' }, | ||
subscriberId: { type: 'string', description: 'Unique identifier for the subscriber' }, | ||
isOnline: { type: 'boolean', description: 'Indicates if the subscriber is online (optional)' }, | ||
lastOnlineAt: { | ||
type: 'string', | ||
format: 'date-time', | ||
description: 'The last time the subscriber was online (optional)', | ||
}, | ||
}, | ||
required: ['firstName', 'lastName', 'email', 'subscriberId'], | ||
additionalProperties: false, | ||
}) as const satisfies JSONSchemaDto; |
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.