Estimation: Save Happiness Score result to a custom field

This automation rule will apply average results of happiness voting to a custom field

📘

How to apply this rule?

2414

🚧

Don't forget to replace "Custom Field Name" with the actual custom field that should be updated with your automation

{
  "pipeline": [
    {
      "type": "source:generic",
      "clientId": "estimator",
      "payloadType": "result-applied"
    },
    {
      "type": "action:JavaScript",
      "script": "const customFieldName = 'Custom Field Name'\r\nconst average = arr => {\r\n  const av = arr.reduce((p, c) => p + c, 0) / arr.length\r\n  return Math.round(av * 100) / 100\r\n}\r\nconst getUpdateCommand = (entity, votes) => {\r\n  const voteValues = votes.map(v => parseFloat(v.value))\r\n  const averageValue = average(voteValues)\r\n  return {\r\n    command: \"targetprocess:UpdateResource\",\r\n    payload: {\r\n      resourceId: parseInt(entity.id, 10),\r\n      resourceType: entity.entityType,\r\n      fields: {\r\n        [customFieldName]: averageValue\r\n      }\r\n    }\r\n  }\r\n}\r\nif (args.payload.session.type === 'hapiness') {\r\n  const entity = args.payload.session.entity\r\n  return getUpdateCommand(entity, args.payload.votes)\r\n} else {\r\n  console.info('Not supported session type.')\r\n}"
    }
  ]
}