-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): adding instant preview endpoint
- Loading branch information
Showing
29 changed files
with
1,290 additions
and
81 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
12 changes: 12 additions & 0 deletions
12
apps/api/src/app/environments/render/email-preview-renderer.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,12 @@ | ||
import { EMAIL_EDITOR_JSON_KEY, EmailPreviewResult } from '@novu/shared'; | ||
import { Renderer } from './output-render-factory'; | ||
import { expendSchema, TiptapNode } from './email-schema-extender'; | ||
|
||
export class EmailPreviewRenderer implements Renderer<EmailPreviewResult> { | ||
render(controlValues: Record<string, unknown>): EmailPreviewResult { | ||
const subject = (controlValues.subject as string) || 'Default Email Subject'; | ||
const body = expendSchema(controlValues[EMAIL_EDITOR_JSON_KEY] as TiptapNode); | ||
|
||
return { subject, body: JSON.stringify(body) }; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
apps/api/src/app/environments/render/email-schema-extender.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,75 @@ | ||
/* eslint-disable */ | ||
export type TiptapNode = { | ||
type: string; | ||
content?: TiptapNode[]; | ||
text?: string; | ||
attr?: Record<string, any>; | ||
}; | ||
|
||
export function expendSchema(schema: TiptapNode): TiptapNode { | ||
const content = schema.content!.map(processNodeRecursive).filter((x) => Boolean(x)) as TiptapNode[]; | ||
|
||
return { ...schema, content }; | ||
} | ||
|
||
function processItemNode(node: TiptapNode, item: any): TiptapNode { | ||
if (node.type === 'text' && typeof node.text === 'string') { | ||
const regex = /{{(novu\.item\.(\w+))}}/g; | ||
node.text = node.text.replace(regex, (_, key: string) => { | ||
const propertyName = key.split('.')[2]; | ||
|
||
return item[propertyName] !== undefined ? item[propertyName] : _; | ||
}); | ||
} | ||
|
||
if (node.content) { | ||
node.content = node.content.map((innerNode) => processItemNode(innerNode, item)); | ||
} | ||
|
||
return node; | ||
} | ||
|
||
const processNodeRecursive = (node: TiptapNode): TiptapNode | null => { | ||
if (node.type === 'show') { | ||
const whenValue = node.attr?.when; | ||
if (whenValue !== 'true') { | ||
return null; | ||
} | ||
} | ||
|
||
if (hasEachAttr(node)) { | ||
return handleFor(node); | ||
} | ||
|
||
return processNodeContent(node); | ||
}; | ||
|
||
const processNodeContent = (node: TiptapNode): TiptapNode | null => { | ||
if (node.content) { | ||
node.content = node.content.map(processNodeRecursive).filter(Boolean) as TiptapNode[]; | ||
} | ||
return node; | ||
}; | ||
|
||
function hasEachAttr(node: TiptapNode): node is TiptapNode & { attr: { each: any } } { | ||
return node.attr !== undefined && node.attr.each !== undefined; | ||
} | ||
|
||
function handleFor(node: TiptapNode & { attr: { each: any } }) { | ||
const items = node.attr.each; | ||
const newContent: TiptapNode[] = []; | ||
|
||
for (const item of items) { | ||
const newNode = { ...node }; | ||
newNode.content = | ||
newNode.content?.map((innerNode) => { | ||
return processItemNode(innerNode, item); | ||
}) || []; | ||
|
||
if (newNode.content) { | ||
newContent.push(...newNode.content); | ||
} | ||
} | ||
|
||
return { type: 'container', content: newContent }; | ||
} |
Oops, something went wrong.