Set Velocity for new Team Iterations from effort of previous Team Iterations of the Team

📘

How to apply this rule?

967

JavaScript for action:

const api = context.getService("targetprocess/api/v2");

// read completed team iterations for the created team iteration's team from targeprocess
const response = await api.queryAsync("TeamIteration", {
    where: `Id!=${args.ResourceId} and Team.Id=${args.Current.Team.Id} and Progress == 1`,
    select: `{id,startDate,effort}`,
    orderBy: 'startDate desc',
    take: 5
});

// if there are no previous team iterations for the created team iteration's team
// there is no need to update anything because we can't estimate velocity
if (response.length <= 0) {
    return [];
}

// calculate average effort of closed team iteration of the created team iteration's team
const avgEffort = response.reduce((acc, x) => acc + x.effort, 0) / response.length;

// return command to update create team iteration with calculated velocity
return [
    {
        command: "targetprocess:UpdateResource",
        payload: {
            resourceType: "TeamIteration",
            resourceId: args.ResourceId,
            fields: {
                Velocity: avgEffort
            }
        }
    }
];

Rule config:

[
  {
    "type": "source:targetprocess:EntityChanged",
    "entityTypes": [
      "TeamIteration"
    ],
    "modifications": {
      "created": true,
      "deleted": false,
      "updated": false
    }
  },
  {
    "type": "action:JavaScript",
    "script": "const api = context.getService(\"targetprocess/api/v2\");\n\n// read completed team iterations for the created team iteration's team from targeprocess\nconst response = await api.queryAsync(\"TeamIteration\", {\n    where: `Id!=${args.ResourceId} and Team.Id=${args.Current.Team.Id} and Progress == 1`,\n    select: `{id,startDate,effort}`,\n    orderBy: 'startDate desc',\n    take: 5\n});\n\n// if there are no previous team iterations for the created team iteration's team\n// there is no need to update anything because we can't estimate velocity\nif (response.length <= 0) {\n    return [];\n}\n\n// calculate average effort of closed team iteration of the created team iteration's team\nconst avgEffort = response.reduce((acc, x) => acc + x.effort, 0) / response.length;\n\n// return command to update create team iteration with calculated velocity\nreturn [\n    {\n        command: \"targetprocess:UpdateResource\",\n        payload: {\n            resourceType: \"TeamIteration\",\n            resourceId: args.ResourceId,\n            fields: {\n                Velocity: avgEffort\n            }\n        }\n    }\n];\n"
  }
]