{
  "name": "Siit - Add User to Entra ID Group",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "siit-entra-add",
        "authentication": "headerAuth",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook - Siit Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [250, 300],
      "webhookId": "siit-entra-add",
      "credentials": {
        "httpHeaderAuth": {
          "id": "1",
          "name": "Siit Webhook Auth"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/v2.0/token",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/x-www-form-urlencoded"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "client_id",
              "value": "YOUR_CLIENT_ID"
            },
            {
              "name": "client_secret",
              "value": "YOUR_CLIENT_SECRET"
            },
            {
              "name": "scope",
              "value": "https://graph.microsoft.com/.default"
            },
            {
              "name": "grant_type",
              "value": "client_credentials"
            }
          ]
        },
        "options": {}
      },
      "id": "get-token",
      "name": "Get Access Token",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [450, 300],
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.microsoft.com/v1.0/groups/{{ $json.body.groupId }}/members/$ref",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $node['Get Access Token'].json.access_token }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "body": "={\"@odata.id\": \"https://graph.microsoft.com/v1.0/users/{{ $json.body.userEmail }}\"}",
        "options": {}
      },
      "id": "add-to-group",
      "name": "Add User to Group",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [650, 300],
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.error }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "id": "check-error",
      "name": "Check for Errors",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [850, 300]
    },
    {
      "parameters": {
        "fromEmail": "automation@yourcompany.com",
        "toEmail": "it-team@yourcompany.com",
        "subject": "=❌ Failed to add user to Entra ID group - {{ $node['Webhook - Siit Trigger'].json.body.groupName }}",
        "emailType": "html",
        "message": "=<h2>❌ Failed to add user to Entra ID group</h2>\n<p><strong>User:</strong> {{ $node['Webhook - Siit Trigger'].json.body.userEmail }}</p>\n<p><strong>Group:</strong> {{ $node['Webhook - Siit Trigger'].json.body.groupName }}</p>\n<p><strong>Request ID:</strong> {{ $node['Webhook - Siit Trigger'].json.body.requestId }}</p>\n<p><strong>Error:</strong> {{ $json.error.message }}</p>\n<p><strong>Time:</strong> {{ $now }}</p>\n<hr>\n<p><em>Workflow: Add to Entra ID Group</em></p>",
        "options": {}
      },
      "id": "email-error",
      "name": "Send Error Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [1050, 200],
      "credentials": {
        "smtp": {
          "id": "2",
          "name": "SMTP Account"
        }
      }
    },
    {
      "parameters": {
        "fromEmail": "automation@yourcompany.com",
        "toEmail": "it-team@yourcompany.com",
        "subject": "=✅ User added to Entra ID group - {{ $node['Webhook - Siit Trigger'].json.body.groupName }}",
        "emailType": "html",
        "message": "=<h2>✅ User successfully added to Entra ID group</h2>\n<p><strong>User:</strong> {{ $node['Webhook - Siit Trigger'].json.body.userEmail }}</p>\n<p><strong>Group:</strong> {{ $node['Webhook - Siit Trigger'].json.body.groupName }}</p>\n<p><strong>Request ID:</strong> {{ $node['Webhook - Siit Trigger'].json.body.requestId }}</p>\n<p><strong>Time:</strong> {{ $now }}</p>\n<hr>\n<p><em>Workflow: Add to Entra ID Group</em></p>",
        "options": {}
      },
      "id": "email-success",
      "name": "Send Success Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [1050, 400],
      "credentials": {
        "smtp": {
          "id": "2",
          "name": "SMTP Account"
        }
      }
    }
  ],
  "connections": {
    "Webhook - Siit Trigger": {
      "main": [
        [
          {
            "node": "Get Access Token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Access Token": {
      "main": [
        [
          {
            "node": "Add User to Group",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add User to Group": {
      "main": [
        [
          {
            "node": "Check for Errors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check for Errors": {
      "main": [
        [
          {
            "node": "Send Error Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Success Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 1,
  "updatedAt": "2024-12-02T00:00:00.000Z",
  "versionId": "1"
}