Message Worker
Routes messages through template resolvers to create concrete delivery work items (email, SMS, push notifications, etc.).
Included in Base Preset
This plugin is part of the base preset and loaded automatically. Using the base preset is strongly recommended, so explicit installation is usually not required.
Installation
import '@unchainedshop/plugins/worker/message';
Purpose
The Message Worker is the central routing hub for all notifications in Unchained:
- Receives a template name and payload
- Resolves the template using the Messaging Director
- Creates concrete work items (EMAIL, PUSH, TWILIO, etc.)
- Links all created work to the original message work
Usage
mutation SendWelcomeMessage {
createWork(
type: "MESSAGE"
input: {
template: "ACCOUNT_ACTION"
userId: "user-id"
action: "verify-email"
# ... any additional payload for the template
}
) {
_id
status
}
}
Input Parameters
| Parameter | Type | Description |
|---|---|---|
template | String | Name of the registered template (required) |
...payload | Any | Additional data passed to the template resolver |
Registering Templates
Templates are registered using the Messaging Director:
import { MessagingDirector } from '@unchainedshop/core';
MessagingDirector.registerTemplate('ORDER_CONFIRMATION', async (payload, context) => {
const { modules } = context;
const { orderId } = payload;
const order = await modules.orders.findOrder({ orderId });
const user = await modules.users.findUserById(order.userId);
const workItems = [];
// Send email
workItems.push({
type: 'EMAIL',
input: {
to: user.emails[0].address,
subject: `Order ${order.orderNumber} confirmed`,
html: `<h1>Thank you for your order!</h1>...`,
},
});
// Send push notification if subscribed
for (const subscription of user.pushSubscriptions || []) {
workItems.push({
type: 'PUSH',
input: {
subscription,
subject: 'https://shop.example.com',
payload: JSON.stringify({
title: 'Order Confirmed',
body: `Order ${order.orderNumber} is confirmed`,
}),
},
});
}
return workItems;
});
Result
{
"forked": [
{ "_id": "email-work-id", "type": "EMAIL", "status": "ALLOCATED" },
{ "_id": "push-work-id", "type": "PUSH", "status": "ALLOCATED" }
]
}
Built-in Templates
Unchained uses the following templates internally:
ACCOUNT_ACTION- Email verification, password resetORDER_CONFIRMATION- Order confirmedDELIVERY- Delivery notificationsERROR_REPORT- Daily error reports
Adapter Details
| Property | Value |
|---|---|
| Key | shop.unchained.worker-plugin.message |
| Type | MESSAGE |
| Source | worker/message.ts |