NOTE: coreOrderWorkflow
, processing
will be combined into coreOrderWorkflow/processing
and set as the status.
Meteor.call("workflow/pushOrderWorkflow", "coreOrderWorkflow", "processing", order);
Meteor.methods({
/**
* workflow/pushOrderWorkflow
* Push the status as the current workflow step,
* move the current status to completed worflow steps
* @summary Update the order workflow
* @param {String} workflow workflow to push to
* @param {String} status - Workflow status
* @param {Order} order - ReactionCore.Schemas.Order, an order object
* @return {Boolean} true if update was successful
*/
"workflow/pushOrderWorkflow": function (workflow, status, order) {
check(workflow, String);
check(status, String);
check(order, Object); // TODO: Validatate as ReactionCore.Schemas.Order
this.unblock();
const result = ReactionCore.Collections.Orders.update({
_id: order._id
}, {
$set: {
// Combine (workflow) "coreOrderWorkflow", (status) "processing" into "coreOrderWorkflow/processing".
// This combo will be used to call the method "workflow/coreOrderWorkflow/processing", if it exists.
"workflow.status": `${workflow}/${status}`
},
$addToSet: {
"workflow.workflow": order.workflow.status
}
});
return result;
}
});
STEP 3: Order update before hook runs and calls a method named after the status (coreOrderWorkflow/processing) to verify that it's allowed to be set on this order.
ReactionCore.Collections.Orders.before.update(function (userId, order, fieldNames, modifier) {
// if we're setting something on the order
if (modifier.$set) {
// Updating status of order e.g. "coreOrderWorkflow/processing"
if (modifier.$set["workflow.status"]) {
let status = modifier.$set["workflow.status"];
let workflowMethod = `workflow/${status}`;
if (Meteor.isServer) {
if (Meteor.server.method_handlers[workflowMethod]) {
const result = Meteor.call(workflowMethod, {
userId,
order,
modifier
});
// Result should be true / false to all or disallow updating the status
return result;
}
}
}
}
});
Checks if the user has dashboard/orders access, return true or false
false
in this situation will stop the Orders.before.update HOOK from updating the order document.
Meteor.methods({
/**
* workflow/coreOrderWorkflow/coreOrderProcessing
* Workflow method that checks permissions for a given user to allow them to
* move an order into the processing phase.
* @param {Object} options An object containing arbitary data
* @return {Boolean} true to allow action, false to cancel execution of hook
*/
"workflow/coreOrderWorkflow/coreOrderProcessing": function (options) {
check(options, Match.OrderHookOptions());
const userId = options.userId;
return ReactionCore.hasPermission(["dashboard/orders"], userId);
}
});
// reaction-core/server/register.js
...
{
template: "coreOrderProcessing",
label: "Order Processing",
// Added status so the template name is no longer tied to the status
// This would allow you to use multiple templates for a given workflow status
status: "processing",
workflow: "coreOrderWorkflow",
audience: ["dashboard/orders"]
}
...
// reaction-schemas/common/schemas.js
...
status: {
type: String,
optional: true
},
...
👍