Inherit Planned Dates from children to parents

📘

How to apply this rule?

Inherit Planned Dates from User Stories to Features

Set Planned Start Date for Feature as the earliest Planned Start Date for its User Stories.
Set Planned End Date for Feature as the latest Planned End Date for its User Stories.

[
  {
    "type": "source:targetprocess:EntityChanged",
    "entityTypes": [
      "UserStory"
    ],
    "modifications": {
      "created": false,
      "deleted": true,
      "updated": [
        "PlannedStartDate",
        "PlannedEndDate",
        "Feature"
      ]
    }
  },
  {
    "type": "action:JavaScript",
    "script": "const parent = \"Feature\";\nconst children = \"UserStories\";\n\nlet parentIds = [];\nif (args.Current[parent]) {\n  parentIds.push(args.Current[parent].Id);\n}\nif (args.Previous && args.Previous[parent] && (!args.Current[parent] || args.Current[parent].Id != args.Previous[parent].Id)) {\n  parentIds.push(args.Previous[parent].Id);\n}\n\nconst api = context.getService(\"targetprocess/api/v2\");\nconst parents = await api.queryAsync(parent, {\n  select: `{id,plannedStartDate,plannedEndDate,targetStart:${children}.MIN(PlannedStartDate),targetEnd:${children}.MAX(PlannedEndDate)}`,\n  where: `id in [${parentIds.join(\",\")}]`\n});\n\nreturn parents.map(item => {\n  return {\n    command: \"targetprocess:UpdateResource\",\n    payload: {\n      resourceType: parent,\n      resourceId: item.id,\n      fields: {\n        PlannedStartDate: item.targetStart || item.plannedStartDate,\n        PlannedEndDate: item.targetEnd || item.plannedEndDate\n      }\n    }\n  };\n});"
  }
]