UNPKG

@unchainedshop/plugins

Version:

Official plugin collection for the Unchained Engine with payment, delivery, and pricing adapters

43 lines (42 loc) 1.66 kB
import { MessagingDirector, WorkerAdapter, WorkerDirector, } from '@unchainedshop/core'; export const MessageWorker = { ...WorkerAdapter, key: 'shop.unchained.worker-plugin.message', label: 'Send Message by combining payload with a template and start concrete jobs', version: '1.0.0', type: 'MESSAGE', doWork: async ({ template, ...payload }, unchainedAPI, workId) => { try { const templateResolver = MessagingDirector.getTemplate(template); if (!templateResolver) throw new Error(`No template resolver found for template ${template}`); const workConfigurations = await templateResolver({ template, ...payload, }, unchainedAPI); if (workConfigurations.length > 0) { const forked = await Promise.all(workConfigurations.map(async (workConfiguration) => { const work = await unchainedAPI.modules.worker.addWork({ ...workConfiguration, originalWorkId: workId, }); delete work.input; return work; })); return { success: true, result: { forked } }; } return { success: true, result: { forked: [] } }; } catch (err) { return { success: false, error: { name: err.name, message: err.message, stack: err.stack, }, }; } }, }; WorkerDirector.registerAdapter(MessageWorker);