Move requests to particular project based on recipient email

📘

How to apply this rule?

The rule will be useful for cases when an email message is sent to the one alias address while email plugin is integrated with another one. It helps to create requests in the desired project based on the email address in "To"/"Cc"

[
  {
    "type": "source:targetprocess:EntityChanged",
    "entityTypes": [
      "message"
    ],
    "modifications": {
      "created": false,
      "deleted": false,
      "updated": [
        "IsProcessed"
      ]
    }
  },
  {
    "or": [
      {
        "and": [
          {
            "value": {
              "type": "constant",
              "value": true
            },
            "target": {
              "name": "IsProcessed",
              "type": "field",
              "target": {
                "type": "pipelineBlockOutput"
              }
            },
            "operator": {
              "type": "is"
            }
          }
        ]
      }
    ],
    "type": "filter:Relational"
  },
  {
    "type": "action:JavaScript",
    "script": "const api = context.getService(\"targetprocess/api/v2\");\nconst messages = await api.queryAsync(\"Message\", {\n  select: \"{recipients,requestId:generals.Max(id)}\",\n  where:`id == ${args.ResourceId}`\n})\n\nif (!messages || messages.length === 0) { return;}\n\nconst requests = messages[0].requestId\nconst recipientsArr = messages[0].recipients.split(\"; \")\n\nlet projectId = null;\n\nfor (const recipient of recipientsArr) {\n  switch (recipient) {\n    case \"[email protected]\": //replace this example with real email address in \"To\" or \"Cc\"\n      projectId = 1 //set here ID of the project where request should be moved after creation\n      break;\n    case \"[email protected]\": //add more examples\n      projectId = 2\n      break;\n  }\n}\n\nreturn {\n  command: \"targetprocess:UpdateResource\",\n  payload: {\n    resourceType: \"Request\",\n    resourceId: requests,\n    fields: {\n      Project: {Id: projectId}\n    }\n  }\n};\n"
  }
]