-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/email load globalTemplateVars async #2950
Merged
michaelbromley
merged 5 commits into
vendure-ecommerce:master
from
mschipperheyn:feat/email-generator-vars-wrapper
Jul 17, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b1d8fd3
feat(email-plugin): Initial concept EmailThemeInjector
mschipperheyn 883876f
feat(email-plugin): Prevent globalTemplateVars overwrite and ensure t…
mschipperheyn aea96a3
feat(email-plugin): Create GlobalTemplateVarsFn as alternative for st…
mschipperheyn 32b9696
docs(email-plugin): Remove some outdated docs
mschipperheyn df113e0
feat(email-plugin): Add test for async globalTemplateVars
mschipperheyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import { | |
DefaultLogger, | ||
EventBus, | ||
Injector, | ||
JobQueueService, | ||
LanguageCode, | ||
Logger, | ||
LogLevel, | ||
|
@@ -239,6 +240,37 @@ describe('EmailPlugin', () => { | |
expect(onSend.mock.calls[0][0].subject).toBe('Hello baz'); | ||
}); | ||
|
||
it('loads globalTemplateVars async', async () => { | ||
const handler = new EmailEventListener('test') | ||
.on(MockEvent) | ||
.setFrom('"test from" <[email protected]>') | ||
.setRecipient(() => '[email protected]') | ||
.setSubject('Job {{ name }}, {{ primaryColor }}'); | ||
|
||
await initPluginWithHandlers([handler], { | ||
globalTemplateVars: async (_ctxLocal: RequestContext, injector: Injector) => { | ||
const jobQueueService = injector.get(JobQueueService); | ||
const jobQueue = await jobQueueService.createQueue({ | ||
name: 'hello-service', | ||
// eslint-disable-next-line | ||
process: async job => { | ||
return 'hello'; | ||
}, | ||
}); | ||
const name = jobQueue.name; | ||
|
||
return { | ||
name, | ||
primaryColor: 'blue', | ||
}; | ||
}, | ||
}); | ||
|
||
eventBus.publish(new MockEvent(ctx, true)); | ||
await pause(); | ||
expect(onSend.mock.calls[0][0].subject).toBe(`Job hello-service, blue`); | ||
}); | ||
|
||
it('interpolates from', async () => { | ||
const handler = new EmailEventListener('test') | ||
.on(MockEvent) | ||
|
@@ -922,7 +954,10 @@ class FakeCustomSender implements EmailSender { | |
const pause = () => new Promise(resolve => setTimeout(resolve, 100)); | ||
|
||
class MockEvent extends VendureEvent { | ||
constructor(public ctx: RequestContext, public shouldSend: boolean) { | ||
constructor( | ||
public ctx: RequestContext, | ||
public shouldSend: boolean, | ||
) { | ||
super(); | ||
} | ||
} | ||
|
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 |
---|---|---|
|
@@ -124,6 +124,38 @@ import { | |
* </mj-table> | ||
* ``` | ||
* | ||
* ### Setting global variables using `globalTemplateVars` | ||
* | ||
* `globalTemplateVars` is an object that can be passed to the configuration of the Email Plugin with static object variables. | ||
* You can also pass an async function that will be called with the `RequestContext` and the `Injector` so you can access services | ||
* and e.g. load channel specific theme configurations. | ||
* | ||
* @example | ||
* ```ts | ||
* EmailPlugin.init({ | ||
* globalTemplateVars: { | ||
* primaryColor: '#FF0000', | ||
* fromAddress: '[email protected]' | ||
* } | ||
* }) | ||
* ``` | ||
* or | ||
* ```ts | ||
* EmailPlugin.init({ | ||
* globalTemplateVars: async (ctx, injector) => { | ||
* const myAsyncService = injector.get(MyAsyncService); | ||
* const asyncValue = await myAsyncService.get(ctx); | ||
* const channel = ctx.channel; | ||
* const { primaryColor } = channel.customFields.theme; | ||
* const theme = { | ||
* primaryColor, | ||
* asyncValue, | ||
* }; | ||
* return theme; | ||
* } | ||
* }) | ||
* ``` | ||
* | ||
* ### Handlebars helpers | ||
* | ||
* The following helper functions are available for use in email templates: | ||
|
@@ -378,9 +410,13 @@ export class EmailPlugin implements OnApplicationBootstrap, OnApplicationShutdow | |
const { type } = handler; | ||
try { | ||
const injector = new Injector(this.moduleRef); | ||
let globalTemplateVars = this.options.globalTemplateVars; | ||
if (typeof globalTemplateVars === 'function') { | ||
globalTemplateVars = await globalTemplateVars(event.ctx, injector); | ||
} | ||
const result = await handler.handle( | ||
event as any, | ||
EmailPlugin.options.globalTemplateVars, | ||
globalTemplateVars as { [key: string]: any }, | ||
injector, | ||
); | ||
if (!result) { | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting
docsWeight 0
will force this type to the top of theEmailPluginOptions
docs page, which is not what we want. Better to have the mainEmailPluginOptions
interface at the top. So remove this to be safe add it to theEmailPluginOptions
doc block to ensure it always stays at the top.