-
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 fix-workflow-editor-name-change-updates-slug…
…-url
- Loading branch information
Showing
23 changed files
with
533 additions
and
196 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
20 changes: 20 additions & 0 deletions
20
apps/api/src/app/environments-v1/usecases/output-renderers/delay-output-renderer.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,20 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { DelayRenderOutput } from '@novu/shared'; | ||
import { RenderCommand } from './render-command'; | ||
import { | ||
DelayTimeControlType, | ||
DelayTimeControlZodSchema, | ||
} from '../../../workflows-v2/shared/schemas/delay-control.schema'; | ||
|
||
@Injectable() | ||
export class DelayOutputRendererUsecase { | ||
execute(renderCommand: RenderCommand): DelayRenderOutput { | ||
const delayTimeControlType: DelayTimeControlType = DelayTimeControlZodSchema.parse(renderCommand.controlValues); | ||
|
||
return { | ||
amount: delayTimeControlType.amount, | ||
type: delayTimeControlType.type, | ||
unit: delayTimeControlType.unit, | ||
}; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
apps/api/src/app/environments-v1/usecases/output-renderers/digest-output-renderer.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,44 @@ | ||
import { Injectable, InternalServerErrorException } from '@nestjs/common'; | ||
import { DigestRenderOutput } from '@novu/shared'; | ||
import { RenderCommand } from './render-command'; | ||
import { | ||
DigestControlSchemaType, | ||
DigestControlZodSchema, | ||
isDigestRegularControl, | ||
isDigestTimedControl, | ||
} from '../../../workflows-v2/shared/schemas/digest-control.schema'; | ||
|
||
@Injectable() | ||
export class DigestOutputRendererUsecase { | ||
execute(renderCommand: RenderCommand): DigestRenderOutput { | ||
const parse: DigestControlSchemaType = DigestControlZodSchema.parse(renderCommand.controlValues); | ||
if ( | ||
isDigestRegularControl(parse) && | ||
parse.amount && | ||
parse.unit && | ||
parse.lookBackWindow && | ||
parse.lookBackWindow.amount && | ||
parse.lookBackWindow.unit | ||
) { | ||
return { | ||
amount: parse.amount, | ||
unit: parse.unit, | ||
digestKey: parse.digestKey, | ||
lookBackWindow: { | ||
amount: parse.lookBackWindow.amount, | ||
unit: parse.lookBackWindow.unit, | ||
}, | ||
}; | ||
} | ||
if (isDigestTimedControl(parse) && parse.cron) { | ||
return { | ||
cron: parse.cron, | ||
digestKey: parse.digestKey, | ||
}; | ||
} | ||
throw new InternalServerErrorException({ | ||
message: `Invalid digest control value data sent for rendering`, | ||
controlValues: renderCommand.controlValues, | ||
}); | ||
} | ||
} |
111 changes: 45 additions & 66 deletions
111
apps/api/src/app/environments-v1/usecases/output-renderers/in-app-output-renderer.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 |
---|---|---|
@@ -1,87 +1,66 @@ | ||
// Concrete Renderer for In-App Message Preview | ||
import { InAppRenderOutput, RedirectTargetEnum } from '@novu/shared'; | ||
import { z } from 'zod'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { RenderCommand } from './render-command'; | ||
import { | ||
InAppActionType, | ||
InAppControlType, | ||
InAppControlZodSchema, | ||
InAppRedirectType, | ||
} from '../../../workflows-v2/shared'; | ||
|
||
@Injectable() | ||
export class InAppOutputRendererUsecase { | ||
execute(renderCommand: RenderCommand): InAppRenderOutput { | ||
const inApp = InAppRenderOutputSchema.optional().parse(renderCommand.controlValues); | ||
const inApp: InAppControlType = InAppControlZodSchema.parse(renderCommand.controlValues); | ||
if (!inApp) { | ||
throw new Error('Invalid in-app control value data'); | ||
} | ||
|
||
const { primaryAction, secondaryAction, redirect } = inApp; | ||
|
||
return { | ||
subject: inApp.subject, | ||
body: inApp.body, | ||
avatar: inApp.avatar, | ||
primaryAction: | ||
inApp.primaryAction && | ||
inApp.primaryAction.label && | ||
inApp.primaryAction.redirect && | ||
inApp.primaryAction.redirect.url | ||
? { | ||
label: inApp.primaryAction.label, | ||
redirect: { | ||
url: inApp.primaryAction.redirect.url, | ||
target: inApp.primaryAction.redirect.target as RedirectTargetEnum, | ||
}, | ||
} | ||
: undefined, | ||
secondaryAction: | ||
inApp.secondaryAction && | ||
inApp.secondaryAction.label && | ||
inApp.secondaryAction.redirect && | ||
inApp.secondaryAction.redirect.url | ||
? { | ||
label: inApp.secondaryAction?.label, | ||
redirect: { | ||
url: inApp.secondaryAction?.redirect.url, | ||
target: inApp.secondaryAction?.redirect.target as RedirectTargetEnum, | ||
}, | ||
} | ||
: undefined, | ||
redirect: | ||
inApp.redirect && inApp.redirect.url | ||
? { | ||
url: inApp.redirect.url, | ||
target: inApp.redirect.target as RedirectTargetEnum, | ||
} | ||
: undefined, | ||
primaryAction: this.buildActionIfAllPartsAvailable(primaryAction), | ||
secondaryAction: this.buildActionIfAllPartsAvailable(secondaryAction), | ||
redirect: this.buildRedirect(redirect), | ||
data: inApp.data as Record<string, unknown>, | ||
}; | ||
} | ||
|
||
private buildRedirect(redirect?: InAppRedirectType) { | ||
if (!(redirect && redirect.url && isValidURL(redirect.url))) { | ||
return undefined; | ||
} | ||
|
||
return { | ||
url: redirect.url, | ||
target: redirect.target as RedirectTargetEnum, | ||
}; | ||
} | ||
|
||
private buildActionIfAllPartsAvailable(action?: InAppActionType) { | ||
if (!(action && action.label && action.redirect && action.redirect.url && isValidURL(action.redirect.url))) { | ||
return undefined; | ||
} | ||
|
||
return { | ||
label: action.label, | ||
redirect: { | ||
url: action.redirect.url.toLowerCase().trim(), | ||
target: action.redirect.target as RedirectTargetEnum, | ||
}, | ||
}; | ||
} | ||
} | ||
const RedirectTargetEnumSchema = z.enum(['_self', '_blank', '_parent', '_top', '_unfencedTop']); | ||
function isValidURL(urlString: string): boolean { | ||
try { | ||
const url = new URL(urlString); | ||
|
||
const InAppRenderOutputSchema = z.object({ | ||
subject: z.string().optional(), | ||
body: z.string(), | ||
avatar: z.string().optional(), | ||
primaryAction: z | ||
.object({ | ||
label: z.string().optional(), | ||
redirect: z.object({ | ||
url: z.string().optional(), | ||
target: RedirectTargetEnumSchema.optional(), // Optional target | ||
}), | ||
}) | ||
.optional(), | ||
secondaryAction: z | ||
.object({ | ||
label: z.string().optional(), | ||
redirect: z.object({ | ||
url: z.string().optional(), | ||
target: RedirectTargetEnumSchema.optional(), // Optional target | ||
}), | ||
}) | ||
.optional(), // Optional secondary action | ||
data: z.record(z.unknown()).optional(), // Optional data | ||
redirect: z | ||
.object({ | ||
url: z.string().optional(), | ||
target: RedirectTargetEnumSchema.optional(), // Optional target | ||
}) | ||
.optional(), | ||
}); | ||
return url.protocol === 'http:' || url.protocol === 'https:'; // Ensure it's HTTP or HTTPS | ||
} catch (error) { | ||
return false; // The URL is invalid | ||
} | ||
} |
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.