Recalculate last run result when Test Case Run is deleted

📘

How to apply this rule?

An automation rule that recalculates last run result when Test Case Run is deleted

[
  {
    "type": "source:targetprocess:EntityChanged",
    "entityTypes": [
      "testcaserun"
    ],
    "modifications": {
      "created": false,
      "deleted": true,
      "updated": false
    }
  },
  {
    "type": "action:JavaScript",
    "script": "const api = context.getService(\"targetprocess/api/v2\");\nconst runs = await api.queryAsync(\"TestCaseRun\", {\n  select: \"{status,endRunDate,comment}\",\n  where: \"testcase.id=\" + args.Current.TestCase.Id,\n  orderBy: \"EndRunDate desc\",\n  take: 1\n});\n\nconst last = runs && runs.length\n  ? { status: runs[0].status, date: new Date(runs[0].endRunDate), comment: runs[0].comment || null }\n  : { status: \"NotRun\", date: null, comment: null };\nconsole.log(last);\n\nreturn {\n  command: \"targetprocess:UpdateResource\",\n  payload: {\n    resourceType: \"TestCase\",\n    resourceId: args.Current.TestCase.Id,\n    fields: {\n      LastRunStatus: last.status,\n      LastFailureComment: last.comment,\n      LastRunDate: last.date\n    }\n  }\n};"
  }
]