{
  "key": "approvals-digest",
  "name": "HRPoint - Approvals digest",
  "summary": "Each weekday morning, tells each approver in Teams how many requests wait for them, and HR how many wait for the HR team.",
  "connectors": [
    "SharePoint (standard)",
    "Microsoft Teams (standard)"
  ],
  "consent": [
    "A SharePoint connection in the owner's name that can read the request lists on the Workspace site and HRPoint People on the Hub: an HR admin or a member of HRPoint HR Team. The flow reads only who each request waits for.",
    "A Microsoft Teams connection in the owner's name, and the Workflows app allowed in the Teams admin center."
  ],
  "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": {
      "Each_weekday_morning": {
        "type": "Recurrence",
        "recurrence": {
          "frequency": "Week",
          "interval": 1,
          "timeZone": "GMT Standard Time",
          "schedule": {
            "weekDays": [
              "Monday",
              "Tuesday",
              "Wednesday",
              "Thursday",
              "Friday"
            ],
            "hours": [
              "8"
            ],
            "minutes": [
              0
            ]
          }
        }
      }
    },
    "actions": {
      "HRPoint_settings": {
        "type": "Compose",
        "inputs": {
          "workspaceSite": "https://contoso.sharepoint.com/sites/HRPointWorkspace",
          "workspacePath": "/sites/HRPointWorkspace",
          "hubSite": "https://contoso.sharepoint.com/sites/HRPoint",
          "hubPath": "/sites/HRPoint",
          "approvalsLink": "https://contoso.sharepoint.com/sites/HRPoint/SitePages/HRPoint.aspx?hrlink=approvals",
          "hrTeamId": "",
          "hrChannelId": ""
        },
        "runAfter": {},
        "description": "Paste the values HRPoint shows in Settings > Teams and flows."
      },
      "Waiting_keys": {
        "type": "InitializeVariable",
        "runAfter": {
          "HRPoint_settings": [
            "Succeeded"
          ]
        },
        "inputs": {
          "variables": [
            {
              "name": "keys",
              "type": "array",
              "value": []
            }
          ]
        }
      },
      "Request_lists": {
        "type": "Compose",
        "runAfter": {
          "Waiting_keys": [
            "Succeeded"
          ]
        },
        "inputs": [
          {
            "list": "Lists/HRPointLeaveRequests",
            "status": "submitted"
          },
          {
            "list": "Lists/HRPointLeaveRequests",
            "status": "cancel-requested"
          },
          {
            "list": "Lists/HRPointChangeRequests",
            "status": "submitted"
          },
          {
            "list": "Lists/HRPointChangeRequests",
            "status": "cancel-requested"
          },
          {
            "list": "Lists/HRPointAgentRequests",
            "status": "submitted"
          },
          {
            "list": "Lists/HRPointAgentRequests",
            "status": "cancel-requested"
          }
        ]
      },
      "For_each_request_list": {
        "type": "Foreach",
        "runAfter": {
          "Request_lists": [
            "Succeeded"
          ]
        },
        "foreach": "@outputs('Request_lists')",
        "runtimeConfiguration": {
          "concurrency": {
            "repetitions": 1
          }
        },
        "actions": {
          "Read_waiting_requests": {
            "type": "OpenApiConnection",
            "runAfter": {},
            "inputs": {
              "host": {
                "connectionName": "shared_sharepointonline",
                "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline",
                "operationId": "HttpRequest"
              },
              "parameters": {
                "dataset": "@{outputs('HRPoint_settings')?['workspaceSite']}",
                "parameters/method": "GET",
                "parameters/uri": "_api/web/GetList(@u)/items?@u='@{outputs('HRPoint_settings')?['workspacePath']}/@{items('For_each_request_list')?['list']}'&$select=Id,HRPAwaitingKey&$filter=HRPStatus eq '@{items('For_each_request_list')?['status']}'&$top=5000",
                "parameters/headers": {
                  "Accept": "application/json;odata=nometadata"
                }
              },
              "authentication": "@parameters('$authentication')"
            }
          },
          "For_each_waiting_request": {
            "type": "Foreach",
            "runAfter": {
              "Read_waiting_requests": [
                "Succeeded"
              ]
            },
            "foreach": "@body('Read_waiting_requests')?['value']",
            "runtimeConfiguration": {
              "concurrency": {
                "repetitions": 1
              }
            },
            "actions": {
              "Add_its_key": {
                "type": "AppendToArrayVariable",
                "runAfter": {},
                "inputs": {
                  "name": "keys",
                  "value": "@items('For_each_waiting_request')?['HRPAwaitingKey']"
                }
              }
            }
          }
        }
      },
      "Each_key_once": {
        "type": "Compose",
        "runAfter": {
          "For_each_request_list": [
            "Succeeded"
          ]
        },
        "inputs": "@union(variables('keys'), variables('keys'))"
      },
      "For_each_key": {
        "type": "Foreach",
        "runAfter": {
          "Each_key_once": [
            "Succeeded"
          ]
        },
        "foreach": "@outputs('Each_key_once')",
        "runtimeConfiguration": {
          "concurrency": {
            "repetitions": 1
          }
        },
        "actions": {
          "Requests_for_this_key": {
            "type": "Query",
            "runAfter": {},
            "inputs": {
              "from": "@variables('keys')",
              "where": "@equals(item(), items('For_each_key'))"
            }
          },
          "Is_a_group": {
            "type": "If",
            "runAfter": {
              "Requests_for_this_key": [
                "Succeeded"
              ]
            },
            "expression": "@startsWith(items('For_each_key'), 'group:')",
            "actions": {
              "Is_HR_set_up": {
                "type": "If",
                "runAfter": {},
                "expression": "@not(empty(outputs('HRPoint_settings')?['hrChannelId']))",
                "actions": {
                  "HR_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": "@{if(equals(length(body('Requests_for_this_key')), 1), '1 request waits', concat(string(length(body('Requests_for_this_key'))), ' requests wait'))} for the HR team in HRPoint",
                          "wrap": true,
                          "size": "Medium",
                          "weight": "Bolder"
                        },
                        {
                          "type": "TextBlock",
                          "text": "Open Approvals in HRPoint to see each request and decide.",
                          "wrap": true
                        }
                      ],
                      "actions": [
                        {
                          "type": "Action.OpenUrl",
                          "title": "Open Approvals",
                          "url": "@{outputs('HRPoint_settings')?['approvalsLink']}"
                        }
                      ]
                    }
                  },
                  "Post_to_the_HR_channel": {
                    "type": "OpenApiConnection",
                    "runAfter": {
                      "HR_card": [
                        "Succeeded"
                      ]
                    },
                    "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('HR_card'))}"
                      },
                      "authentication": "@parameters('$authentication')"
                    }
                  }
                }
              }
            },
            "else": {
              "actions": {
                "Read_the_person": {
                  "type": "OpenApiConnection",
                  "runAfter": {},
                  "inputs": {
                    "host": {
                      "connectionName": "shared_sharepointonline",
                      "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline",
                      "operationId": "HttpRequest"
                    },
                    "parameters": {
                      "dataset": "@{outputs('HRPoint_settings')?['hubSite']}",
                      "parameters/method": "GET",
                      "parameters/uri": "_api/web/GetList(@u)/items?@u='@{outputs('HRPoint_settings')?['hubPath']}/Lists/HRPointPeople'&$select=Id,HRPUpn,HRPEmail,HRPStatus&$filter=HRPPersonKey eq '@{items('For_each_key')}'&$top=1",
                      "parameters/headers": {
                        "Accept": "application/json;odata=nometadata"
                      }
                    },
                    "authentication": "@parameters('$authentication')"
                  }
                },
                "Their_sign_in_name": {
                  "type": "Compose",
                  "runAfter": {
                    "Read_the_person": [
                      "Succeeded"
                    ]
                  },
                  "inputs": "@if(empty(first(body('Read_the_person')?['value'])?['HRPUpn']), first(body('Read_the_person')?['value'])?['HRPEmail'], first(body('Read_the_person')?['value'])?['HRPUpn'])"
                },
                "Has_a_sign_in_name": {
                  "type": "If",
                  "runAfter": {
                    "Their_sign_in_name": [
                      "Succeeded"
                    ]
                  },
                  "expression": "@not(empty(outputs('Their_sign_in_name')))",
                  "actions": {
                    "Approver_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": "@{if(equals(length(body('Requests_for_this_key')), 1), '1 request waits', concat(string(length(body('Requests_for_this_key'))), ' requests wait'))} for your decision in HRPoint",
                            "wrap": true,
                            "size": "Medium",
                            "weight": "Bolder"
                          },
                          {
                            "type": "TextBlock",
                            "text": "Open Approvals in HRPoint to see each request and decide.",
                            "wrap": true
                          }
                        ],
                        "actions": [
                          {
                            "type": "Action.OpenUrl",
                            "title": "Open Approvals",
                            "url": "@{outputs('HRPoint_settings')?['approvalsLink']}"
                          }
                        ]
                      }
                    },
                    "Post_to_the_approver": {
                      "type": "OpenApiConnection",
                      "runAfter": {
                        "Approver_card": [
                          "Succeeded"
                        ]
                      },
                      "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": "@{outputs('Their_sign_in_name')}",
                          "body/messageBody": "@{string(outputs('Approver_card'))}"
                        },
                        "authentication": "@parameters('$authentication')"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
