diff --git a/.changeset/dark-hoops-clean.md b/.changeset/dark-hoops-clean.md new file mode 100644 index 0000000..bb736ff --- /dev/null +++ b/.changeset/dark-hoops-clean.md @@ -0,0 +1,5 @@ +--- +"@codee-sh/medusa-plugin-notification-emails": patch +--- + +Fix external template rendering for Slack and Email workflows by treating `external_*` IDs as registry templates instead of DB templates. Update docs to clarify template ID resolution. diff --git a/README.md b/README.md index 0fb0784..8d6f4e5 100755 --- a/README.md +++ b/README.md @@ -47,6 +47,11 @@ module.exports = defineConfig({ Then continue with configuration and usage guides in `docs/`. +Template IDs are resolved by prefix: + +- IDs starting with `system` or `external` are rendered from the in-memory registry (no DB lookup). +- Other IDs are treated as DB templates and must exist in `mpn_builder_template`. + ## Documentation - [Configuration](./docs/configuration.md) diff --git a/docs/configuration.md b/docs/configuration.md index b41e025..6f5eab0 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -137,6 +137,11 @@ Notes: - if service id is unknown, template registration is skipped - registered templates are available as type `external` +Template ID resolution: + +- IDs starting with `system` or `external` are rendered from the registry and are not looked up in the database. +- Any other ID is treated as a DB template and must exist in `mpn_builder_template`. + ## Minimal complete example ```typescript diff --git a/src/workflows/mpn-builder-services/steps/email-service.ts b/src/workflows/mpn-builder-services/steps/email-service.ts index a8f26ed..57d3c81 100644 --- a/src/workflows/mpn-builder-services/steps/email-service.ts +++ b/src/workflows/mpn-builder-services/steps/email-service.ts @@ -46,12 +46,14 @@ export const emailServiceStep = createStep( const templateId = input.template_id const isSystemTemplateId = templateId?.startsWith(TEMPLATE_TYPES.SYSTEM_TEMPLATE) + const isExternalTemplateId = templateId?.startsWith(TEMPLATE_TYPES.EXTERNAL_TEMPLATE) + const isRegistryTemplate = isSystemTemplateId || isExternalTemplateId let blocks: any[] = [] let template: any = {} - // If it's not a system template, get the blocks from the database - if (!isSystemTemplateId) { + // If it's not a registry template, get the blocks from the database + if (!isRegistryTemplate) { const { result: templateData } = await getTemplateWorkflow(container).run({ input: { template_id: templateId, @@ -68,7 +70,7 @@ export const emailServiceStep = createStep( const { html, text, subject } = await templateEmailService.render({ - templateName: isSystemTemplateId + templateName: isRegistryTemplate ? templateId : "base-template", data: input.data, diff --git a/src/workflows/mpn-builder-services/steps/slack-service.ts b/src/workflows/mpn-builder-services/steps/slack-service.ts index 8ffb729..3f8d8c0 100644 --- a/src/workflows/mpn-builder-services/steps/slack-service.ts +++ b/src/workflows/mpn-builder-services/steps/slack-service.ts @@ -46,11 +46,13 @@ export const slackServiceStep = createStep( const templateId = input.template_id const isSystemTemplateId = templateId?.startsWith(TEMPLATE_TYPES.SYSTEM_TEMPLATE) + const isExternalTemplateId = templateId?.startsWith(TEMPLATE_TYPES.EXTERNAL_TEMPLATE) + const isRegistryTemplate = isSystemTemplateId || isExternalTemplateId let blocks: any[] = [] - // If it's not a system template, get the blocks from the database - if (!isSystemTemplateId) { + // If it's not a registry template, get the blocks from the database + if (!isRegistryTemplate) { const { result: templateData } = await getTemplateWorkflow(container).run({ input: { template_id: templateId, @@ -68,7 +70,7 @@ export const slackServiceStep = createStep( // TODO CHECK WHY .config is undefined in the prepareData method when using external templates const { blocks: renderedBlocks } = await templateSlackService.render({ - templateName: isSystemTemplateId + templateName: isRegistryTemplate ? templateId : "base-template", data: input.data,