Assign an item to the Person who started it

πŸ“˜

How to apply this rule?

821
[
  {
    "type": "source:targetprocess:EntityChanged",
    "entityTypes": [
      "UserStory",
      "Task",
      "Bug",
      "Feature",
      "Epic",
      "Request",
      "PortfolioEpic"
    ],
    "modifications": {
      "created": false,
      "deleted": false,
      "updated": [
        "StartDate"
      ]
    }
  },
  {
    "type": "filter:Relational",
    "or": [
      {
        "and": [
          {
            "target": {
              "type": "field",
              "name": "StartDate",
              "target": {
                "type": "pipelineBlockOutput"
              }
            },
            "value": null,
            "operator": {
              "type": "exists"
            }
          },
          {
            "target": {
              "type": "field",
              "name": "StartDate",
              "target": {
                "type": "field",
                "name": "Previous",
                "target": {
                  "type": "pipelineBlockOutput"
                }
              }
            },
            "value": null,
            "operator": {
              "type": "not exists"
            }
          }
        ]
      }
    ]
  },
  {
    "type": "action:JavaScript",
    "script": "const api = context.getService(\"targetprocess/api/v2\");\nconst userId = args.Author.Id;\n\nconst projects = await api.queryAsync(\"Project\", {\n  select: `{member:projectmembers.where(user.id=${userId}).first.role.id,` +\n    `roles:process.entitystates.where(entitytype.name=\"${args.ResourceType}\" and role!=null).select(role.id).distinct()}`,\n  where: `id=${args.Current.Project.Id}`\n});\nconst project = projects[0];\nconst roleId = project.roles.includes(project.member) ? project.member : project.roles[0];\n\nconst users = await api.queryAsync(\"Assignments\", {\n  result: \"count\",\n  where: `assignable.id=${args.ResourceId} and generalUser.id=${args.Author.Id} and role.id=${roleId}`\n});\nif (users > 0) { return; }\n\nreturn {\n  command: \"targetprocess:CreateResource\",\n  payload: {\n    resourceType: \"Assignment\",\n    fields: {\n      Assignable: { Id: args.ResourceId },\n      Role: { Id: roleId },\n      GeneralUser: { Id: userId }\n    }\n  }\n}"
  }
]