const afterHook = (req, res, next) => {
  res.on('finish', () => {
    if (req.url.includes('/api/workflow/v2/approve/save')) {
      const { approve } = req.body;
      const ins_id = approve.instance;
      broker.call('objectql.update', {
        objectName: 'instances',
        id: ins_id,
        doc: {},
      })
    }else if(req.url.includes('/api/workflow/engine')){
        var hashData = req.body;
        const approve_from_client = hashData['Approvals'][0];
        const ins_id = approve_from_client.instance;
        broker.call('objectql.update', {
            objectName: 'instances',
            id: ins_id,
            doc: {},
        })
    }else if(req.url.includes('/api/workflow/relocate')){
        var hashData = req.body;
        const instance_from_client = hashData['Instances'][0];
        const ins_id = instance_from_client._id;
        broker.call('objectql.update', {
            objectName: 'instances',
            id: ins_id,
            doc: {},
        })
    }
  });
  next();
};

module.exports = afterHook;