{
  "key": "approval-cards",
  "name": "HRPoint - Approval cards",
  "summary": "Sends each approver a Teams card when a request waits for them; the card opens the request in HRPoint, where they decide.",
  "connectors": [
    "SharePoint (standard)",
    "Microsoft Teams (standard)"
  ],
  "consent": [
    "A SharePoint connection in the owner's name that can read HRPoint Notifications on the Workspace site: an HR admin or a member of HRPoint HR Team (they read every row of that list).",
    "A Microsoft Teams connection in the owner's name, and the Workflows app allowed in the Teams admin center, so the Flow bot can post the cards."
  ],
  "definition": {
    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "$connections": {
        "defaultValue": {},
        "type": "Object"
      },
      "$authentication": {
        "defaultValue": {},
        "type": "SecureObject"
      }
    },
    "triggers": {
      "When_HRPoint_queues_a_notification": {
        "type": "OpenApiConnection",
        "recurrence": {
          "frequency": "Minute",
          "interval": 5
        },
        "splitOn": "@triggerOutputs()?['body/value']",
        "inputs": {
          "host": {
            "connectionName": "shared_sharepointonline",
            "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline",
            "operationId": "GetOnNewItems"
          },
          "parameters": {
            "dataset": "https://contoso.sharepoint.com/sites/HRPointWorkspace",
            "table": "00000000-0000-0000-0000-000000000000"
          },
          "authentication": "@parameters('$authentication')"
        },
        "description": "HRPoint Notifications on the Workspace site (a hidden list: choose Enter custom value and paste its ID)."
      }
    },
    "actions": {
      "HRPoint_settings": {
        "type": "Compose",
        "inputs": {
          "hrTeamId": "",
          "hrChannelId": ""
        },
        "runAfter": {},
        "description": "Paste the values HRPoint shows in Settings > Teams and flows."
      },
      "Is_a_request_waiting": {
        "type": "If",
        "runAfter": {
          "HRPoint_settings": [
            "Succeeded"
          ]
        },
        "expression": "@and(equals(triggerOutputs()?['body/HRPKind'], 'approval-waiting'), not(empty(triggerOutputs()?['body/HRPLink'])))",
        "actions": {
          "Card": {
            "type": "Compose",
            "runAfter": {},
            "inputs": {
              "type": "AdaptiveCard",
              "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
              "version": "1.4",
              "body": [
                {
                  "type": "TextBlock",
                  "text": "HRPoint",
                  "wrap": true,
                  "size": "Small",
                  "isSubtle": true,
                  "spacing": "None"
                },
                {
                  "type": "TextBlock",
                  "text": "@{triggerOutputs()?['body/Title']} waiting for your decision",
                  "wrap": true,
                  "size": "Medium",
                  "weight": "Bolder"
                },
                {
                  "type": "TextBlock",
                  "text": "Open it in HRPoint to approve, decline or return it. Your decision is recorded in HRPoint with your own account.",
                  "wrap": true
                }
              ],
              "actions": [
                {
                  "type": "Action.OpenUrl",
                  "title": "Open in HRPoint",
                  "url": "@{triggerOutputs()?['body/HRPLink']}"
                }
              ]
            }
          },
          "Has_a_person": {
            "type": "If",
            "runAfter": {
              "Card": [
                "Succeeded"
              ]
            },
            "expression": "@not(empty(triggerOutputs()?['body/HRPRecipientUpn']))",
            "actions": {
              "Post_card_to_the_approver": {
                "type": "OpenApiConnection",
                "runAfter": {},
                "inputs": {
                  "host": {
                    "connectionName": "shared_teams",
                    "apiId": "/providers/Microsoft.PowerApps/apis/shared_teams",
                    "operationId": "PostCardToConversation"
                  },
                  "parameters": {
                    "poster": "Flow bot",
                    "location": "Chat with Flow bot",
                    "body/recipient": "@{triggerOutputs()?['body/HRPRecipientUpn']}",
                    "body/messageBody": "@{string(outputs('Card'))}"
                  },
                  "authentication": "@parameters('$authentication')"
                }
              }
            },
            "else": {
              "actions": {
                "Is_HR_set_up": {
                  "type": "If",
                  "runAfter": {},
                  "expression": "@and(startsWith(triggerOutputs()?['body/HRPRecipientKey'], 'group:'), not(empty(outputs('HRPoint_settings')?['hrChannelId'])))",
                  "actions": {
                    "Post_card_to_the_HR_channel": {
                      "type": "OpenApiConnection",
                      "runAfter": {},
                      "inputs": {
                        "host": {
                          "connectionName": "shared_teams",
                          "apiId": "/providers/Microsoft.PowerApps/apis/shared_teams",
                          "operationId": "PostCardToConversation"
                        },
                        "parameters": {
                          "poster": "Flow bot",
                          "location": "Channel",
                          "body/recipient/groupId": "@{outputs('HRPoint_settings')?['hrTeamId']}",
                          "body/recipient/channelId": "@{outputs('HRPoint_settings')?['hrChannelId']}",
                          "body/messageBody": "@{string(outputs('Card'))}"
                        },
                        "authentication": "@parameters('$authentication')"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
