Hooks
Hooks in DatAscend are basically json configuration files that define rules that will call some functions automatically on certain conditions, for example: update an entity’s state after a row for another entity is created.
Hooks are stored on /config/{environment}/hooks/{public/private}/definitions/
.
Hook classes are stored on /config/{environment}/hooks/{public/private}/sources/
.
To call for a hook execution(public hooks only), the following endpoint is available:
POST /api/v1/hooks/execute/{type}/{hook_id}
request body:
{ "id": "testing_hooks", "type": "webhook", "path": { "origin": "public", "class": "path/to/class/hook", "method": "hook_is_executing", "action": "select" }, "data": {}, "options": {}}
Example Hook
[{ "id": "after_order_update", "name": "after_order_update", "type": "after", "method": "update", "class": "/application/after_order_update", "execute-method": "calculate_order_delivery_fee", "response-message": "Successfully executed after_order_update.", "response-status": "200", "entities": ["order"], "trigger-rule": { "type": "rule", "logical-operator": "or", "conditions": [{ "type": "field", "parameter": { "field": "order_status", "operator": "eq", "value": "delivered" } }, { "type": "function", "parameter": { "function": "get_order_number", "operator": "eq", "value": "50000" } }, { "type": "rule", "logical-operator": "and", "conditions": [{ "type": "field", "parameter": { "field": "order_number", "operator": "gt", "value": "10000" } }, { "type": "field", "parameter": { "field": "order_number", "operator": "lt", "value": "20000" } } ] } ] }}]
In this example, the class
property is the one that defined the code that will be called when the hook is executed.
Example of the code inside of that class
:
module.exports = class after_order_update { hook_is_executing(hook, execution, data, entities, options, response) { return new Promise((resolve, reject) => { const result = Object.assign(response, { message: execution.response, status: execution.status, time: new Date(), error: false, errors: [], data: [], }); resolve(result); console.dir({ message: execution.response, status: execution.status, time: new Date(), info: { hook: hook.name, message: `${hook.name} is executing because it matches.`, }, match: { id: hook.id, type: hook.type, origin: hook.origin, }, execution: execution, }); }); }};
Other hook endpoints are:
/api/v1/hooks/
: gets all public hooks/api/v1/hooks/entity/{entity}
gets all private hooks from an existing entity