11import { BaseActionService } from "./base-action-service"
2- import {
3- SlackTemplateRenderer ,
4- SlackBlock ,
5- } from "../../../templates/slack/types"
6- import { renderInventoryLevel } from "../../../templates/slack/inventory-level"
7- import { renderProductVariant } from "../../../templates/slack/product-variant/product-variant"
8- import { renderProduct } from "../../../templates/slack/product/product"
9- import { renderOrderPlaced } from "../../../templates/slack/order/order-placed"
10- import { renderOrderCompleted } from "../../../templates/slack/order/order-completed"
11- import { renderOrderUpdated } from "../../../templates/slack/order/order-updated"
12- import { renderOrderCanceled } from "../../../templates/slack/order/order-canceled"
13- import { renderOrderArchived } from "../../../templates/slack/order/order-archived"
2+ import { slackService } from "@codee-sh/medusa-plugin-notification-emails/templates/slack"
3+ import { transformContext } from "@codee-sh/medusa-plugin-notification-emails/utils"
144
155export class SlackActionService extends BaseActionService {
166 id = "slack"
@@ -28,24 +18,16 @@ export class SlackActionService extends BaseActionService {
2818
2919 /**
3020 * Initialize default Slack templates
21+ * Slack engine already has all prebuild templates registered
22+ * This method can be used to register custom templates if needed
3123 */
3224 protected initializeTemplates ( ) : void {
33- // Register default templates
34- this . registerTemplate (
35- "inventory-level" ,
36- renderInventoryLevel as any
37- )
38- this . registerTemplate (
39- "product-variant" ,
40- renderProductVariant as any
41- )
42- this . registerTemplate ( "product" , renderProduct as any )
43-
44- this . registerTemplate ( "order-placed" , renderOrderPlaced as any )
45- this . registerTemplate ( "order-completed" , renderOrderCompleted as any )
46- this . registerTemplate ( "order-updated" , renderOrderUpdated as any )
47- this . registerTemplate ( "order-canceled" , renderOrderCanceled as any )
48- this . registerTemplate ( "order-archived" , renderOrderArchived as any )
25+ // Slack engine already has all prebuild templates registered
26+ // You can register custom templates here if needed:
27+ // slackEngine.registerTemplate("custom-template", {
28+ // ...slackEngine.getBaseTemplate(),
29+ // getConfig: () => ({ blocks: [...], translations: {...} })
30+ // })
4931 }
5032
5133 /**
@@ -58,24 +40,28 @@ export class SlackActionService extends BaseActionService {
5840 context : any
5941 contextType ?: string | null
6042 options ?: any
61- } ) : Promise < { text : string ; blocks : SlackBlock [ ] } > {
62- const renderer = this . getTemplate (
63- params . templateName
64- ) as SlackTemplateRenderer | undefined
43+ } ) : Promise < { text : string ; blocks : any [ ] } > {
6544
66- if ( ! renderer ) {
67- throw new Error (
68- `Slack template "${ params . templateName } " not found. Available templates: ${ Array . from ( this . templates_ . keys ( ) ) . join ( ", " ) } `
69- )
70- }
45+ const transformedContext = transformContext ( params . contextType , params . context )
7146
72- const result = renderer ( {
73- context : params . context ,
74- contextType : params . contextType ,
75- options : params . options || { } ,
47+ const { blocks } = await slackService . render ( {
48+ templateName : params . templateName ,
49+ data : {
50+ ...transformedContext ,
51+ backend_url : params . options ?. backendUrl ,
52+ } ,
53+ options : params . options
7654 } )
7755
78- // Handle both sync and async renderers
79- return result instanceof Promise ? await result : result
56+ // For Slack, the 'text' field is a fallback for notifications that don't support blocks.
57+ // We can derive it from the first header block or a generic message.
58+ const fallbackText =
59+ blocks . find ( ( b ) => b . type === "header" && b . text ?. text ) ?. text ?. text ||
60+ `Notification for ${ params . templateName } `
61+
62+ return {
63+ text : fallbackText ,
64+ blocks : blocks ,
65+ }
8066 }
8167}
0 commit comments