{
  "key": "who-is-out",
  "name": "HRPoint - Who is out today",
  "summary": "Each weekday morning, posts in a Teams channel how many people are away today, with dates and part of the day only (names only when you switch them on in the flow).",
  "connectors": [
    "SharePoint (standard)",
    "Microsoft Teams (standard)"
  ],
  "consent": [
    "A SharePoint connection in the owner's name that can read HRPoint Away Calendar on the Hub (everyone in HRPoint Staff can). The flow reads dates, part of the day and the name only, never why.",
    "A Microsoft Teams connection in the owner's name with permission to post in the chosen channel, 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": {
          "hubSite": "https://contoso.sharepoint.com/sites/HRPoint",
          "hubPath": "/sites/HRPoint",
          "homeLink": "https://contoso.sharepoint.com/sites/HRPoint/SitePages/HRPoint.aspx",
          "whoIsOutTeamId": "",
          "whoIsOutChannelId": "",
          "showNames": false
        },
        "runAfter": {},
        "description": "Paste the values HRPoint shows in Settings > Teams and flows."
      },
      "Today": {
        "type": "Compose",
        "runAfter": {
          "HRPoint_settings": [
            "Succeeded"
          ]
        },
        "inputs": "@{convertFromUtc(utcNow(), 'GMT Standard Time', 'yyyy-MM-dd')}"
      },
      "Away_today": {
        "type": "InitializeVariable",
        "runAfter": {
          "Today": [
            "Succeeded"
          ]
        },
        "inputs": {
          "variables": [
            {
              "name": "away",
              "type": "array",
              "value": []
            }
          ]
        }
      },
      "End_day_windows": {
        "type": "Compose",
        "runAfter": {
          "Away_today": [
            "Succeeded"
          ]
        },
        "inputs": [
          {
            "from": "@{outputs('Today')}",
            "to": "@{formatDateTime(addDays(outputs('Today'), 31), 'yyyy-MM-dd')}"
          },
          {
            "from": "@{formatDateTime(addDays(outputs('Today'), 31), 'yyyy-MM-dd')}",
            "to": "@{formatDateTime(addDays(outputs('Today'), 92), 'yyyy-MM-dd')}"
          },
          {
            "from": "@{formatDateTime(addDays(outputs('Today'), 92), 'yyyy-MM-dd')}",
            "to": "@{formatDateTime(addDays(outputs('Today'), 366), 'yyyy-MM-dd')}"
          },
          {
            "from": "@{formatDateTime(addDays(outputs('Today'), 366), 'yyyy-MM-dd')}",
            "to": "@{formatDateTime(addDays(outputs('Today'), 3660), 'yyyy-MM-dd')}"
          }
        ]
      },
      "For_each_window": {
        "type": "Foreach",
        "runAfter": {
          "End_day_windows": [
            "Succeeded"
          ]
        },
        "foreach": "@outputs('End_day_windows')",
        "runtimeConfiguration": {
          "concurrency": {
            "repetitions": 1
          }
        },
        "actions": {
          "Read_rows_ending_in_the_window": {
            "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/HRPointAwayCalendar'&$select=Id,HRPPersonKey,HRPStart,HRPEnd,HRPStartPart,HRPEndPart,HRPSourceRef,HRPDisplayName&$filter=HRPEnd ge '@{items('For_each_window')?['from']}' and HRPEnd lt '@{items('For_each_window')?['to']}'&$top=5000",
                "parameters/headers": {
                  "Accept": "application/json;odata=nometadata"
                }
              },
              "authentication": "@parameters('$authentication')"
            }
          },
          "Rows_that_cover_today": {
            "type": "Query",
            "runAfter": {
              "Read_rows_ending_in_the_window": [
                "Succeeded"
              ]
            },
            "inputs": {
              "from": "@body('Read_rows_ending_in_the_window')?['value']",
              "where": "@and(not(empty(item()?['HRPStart'])), lessOrEquals(item()?['HRPStart'], outputs('Today')))"
            }
          },
          "For_each_candidate": {
            "type": "Foreach",
            "runAfter": {
              "Rows_that_cover_today": [
                "Succeeded"
              ]
            },
            "foreach": "@body('Rows_that_cover_today')",
            "runtimeConfiguration": {
              "concurrency": {
                "repetitions": 1
              }
            },
            "actions": {
              "Has_a_source": {
                "type": "If",
                "runAfter": {},
                "expression": "@not(empty(items('For_each_candidate')?['HRPSourceRef']))",
                "actions": {
                  "Read_a_newer_row": {
                    "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/HRPointAwayCalendar'&$select=Id&$filter=HRPSourceRef eq '@{items('For_each_candidate')?['HRPSourceRef']}' and ID gt @{items('For_each_candidate')?['Id']}&$top=1",
                        "parameters/headers": {
                          "Accept": "application/json;odata=nometadata"
                        }
                      },
                      "authentication": "@parameters('$authentication')"
                    }
                  },
                  "Is_the_latest": {
                    "type": "If",
                    "runAfter": {
                      "Read_a_newer_row": [
                        "Succeeded"
                      ]
                    },
                    "expression": "@equals(length(body('Read_a_newer_row')?['value']), 0)",
                    "actions": {
                      "Add_the_latest": {
                        "type": "AppendToArrayVariable",
                        "runAfter": {},
                        "inputs": {
                          "name": "away",
                          "value": {
                            "key": "@{items('For_each_candidate')?['HRPPersonKey']}",
                            "name": "@{items('For_each_candidate')?['HRPDisplayName']}",
                            "part": "@{if(equals(if(equals(items('For_each_candidate')?['HRPStart'], if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd'])), if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), if(equals(items('For_each_candidate')?['HRPStart'], outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), 'pm'), 'pm', 'full'), if(equals(if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd']), outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPEndPart']), 'full', items('For_each_candidate')?['HRPEndPart']), 'am'), 'am', 'full'), 'full'))), 'am'), 'am', if(equals(if(equals(items('For_each_candidate')?['HRPStart'], if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd'])), if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), if(equals(items('For_each_candidate')?['HRPStart'], outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), 'pm'), 'pm', 'full'), if(equals(if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd']), outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPEndPart']), 'full', items('For_each_candidate')?['HRPEndPart']), 'am'), 'am', 'full'), 'full'))), 'pm'), 'pm', if(equals(if(equals(items('For_each_candidate')?['HRPStart'], if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd'])), if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), if(equals(items('For_each_candidate')?['HRPStart'], outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), 'pm'), 'pm', 'full'), if(equals(if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd']), outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPEndPart']), 'full', items('For_each_candidate')?['HRPEndPart']), 'am'), 'am', 'full'), 'full'))), 'hours'), 'part', 'full')))}"
                          }
                        }
                      }
                    }
                  }
                },
                "else": {
                  "actions": {
                    "Add_the_row": {
                      "type": "AppendToArrayVariable",
                      "runAfter": {},
                      "inputs": {
                        "name": "away",
                        "value": {
                          "key": "@{items('For_each_candidate')?['HRPPersonKey']}",
                          "name": "@{items('For_each_candidate')?['HRPDisplayName']}",
                          "part": "@{if(equals(if(equals(items('For_each_candidate')?['HRPStart'], if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd'])), if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), if(equals(items('For_each_candidate')?['HRPStart'], outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), 'pm'), 'pm', 'full'), if(equals(if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd']), outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPEndPart']), 'full', items('For_each_candidate')?['HRPEndPart']), 'am'), 'am', 'full'), 'full'))), 'am'), 'am', if(equals(if(equals(items('For_each_candidate')?['HRPStart'], if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd'])), if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), if(equals(items('For_each_candidate')?['HRPStart'], outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), 'pm'), 'pm', 'full'), if(equals(if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd']), outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPEndPart']), 'full', items('For_each_candidate')?['HRPEndPart']), 'am'), 'am', 'full'), 'full'))), 'pm'), 'pm', if(equals(if(equals(items('For_each_candidate')?['HRPStart'], if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd'])), if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), if(equals(items('For_each_candidate')?['HRPStart'], outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPStartPart']), 'full', items('For_each_candidate')?['HRPStartPart']), 'pm'), 'pm', 'full'), if(equals(if(empty(items('For_each_candidate')?['HRPEnd']), items('For_each_candidate')?['HRPStart'], items('For_each_candidate')?['HRPEnd']), outputs('Today')), if(equals(if(empty(items('For_each_candidate')?['HRPEndPart']), 'full', items('For_each_candidate')?['HRPEndPart']), 'am'), 'am', 'full'), 'full'))), 'hours'), 'part', 'full')))}"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "Anyone_away": {
        "type": "If",
        "runAfter": {
          "For_each_window": [
            "Succeeded"
          ]
        },
        "expression": "@and(greater(length(variables('away')), 0), not(empty(outputs('HRPoint_settings')?['whoIsOutChannelId'])))",
        "actions": {
          "Who": {
            "type": "Select",
            "runAfter": {},
            "inputs": {
              "from": "@variables('away')",
              "select": "@item()?['key']"
            }
          },
          "Away_full": {
            "type": "Query",
            "runAfter": {
              "Who": [
                "Succeeded"
              ]
            },
            "inputs": {
              "from": "@variables('away')",
              "where": "@equals(item()?['part'], 'full')"
            }
          },
          "Away_am": {
            "type": "Query",
            "runAfter": {
              "Away_full": [
                "Succeeded"
              ]
            },
            "inputs": {
              "from": "@variables('away')",
              "where": "@equals(item()?['part'], 'am')"
            }
          },
          "Away_pm": {
            "type": "Query",
            "runAfter": {
              "Away_am": [
                "Succeeded"
              ]
            },
            "inputs": {
              "from": "@variables('away')",
              "where": "@equals(item()?['part'], 'pm')"
            }
          },
          "Away_part": {
            "type": "Query",
            "runAfter": {
              "Away_pm": [
                "Succeeded"
              ]
            },
            "inputs": {
              "from": "@variables('away')",
              "where": "@equals(item()?['part'], 'part')"
            }
          },
          "Names": {
            "type": "Select",
            "runAfter": {
              "Away_part": [
                "Succeeded"
              ]
            },
            "inputs": {
              "from": "@variables('away')",
              "select": {
                "type": "TextBlock",
                "wrap": true,
                "spacing": "Small",
                "text": "@{item()?['name']}: away @{if(equals(item()?['part'], 'full'), 'all day', if(equals(item()?['part'], 'am'), 'in the morning', if(equals(item()?['part'], 'pm'), 'in the afternoon', 'for part of the day')))}"
              }
            }
          },
          "Summary": {
            "type": "Compose",
            "runAfter": {
              "Names": [
                "Succeeded"
              ]
            },
            "inputs": "@{substring(concat(if(equals(length(body('Away_full')), 0), '', concat(string(length(body('Away_full'))), ' all day, ')), if(equals(length(body('Away_am')), 0), '', concat(string(length(body('Away_am'))), ' in the morning, ')), if(equals(length(body('Away_pm')), 0), '', concat(string(length(body('Away_pm'))), ' in the afternoon, ')), if(equals(length(body('Away_part')), 0), '', concat(string(length(body('Away_part'))), ' for part of the day, '))), 0, sub(length(concat(if(equals(length(body('Away_full')), 0), '', concat(string(length(body('Away_full'))), ' all day, ')), if(equals(length(body('Away_am')), 0), '', concat(string(length(body('Away_am'))), ' in the morning, ')), if(equals(length(body('Away_pm')), 0), '', concat(string(length(body('Away_pm'))), ' in the afternoon, ')), if(equals(length(body('Away_part')), 0), '', concat(string(length(body('Away_part'))), ' for part of the day, ')))), 2))}."
          },
          "Card": {
            "type": "Compose",
            "runAfter": {
              "Summary": [
                "Succeeded"
              ]
            },
            "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(union(body('Who'), body('Who'))), 1), '1 away today', concat(string(length(union(body('Who'), body('Who')))), ' away today'))}",
                  "wrap": true,
                  "size": "Medium",
                  "weight": "Bolder"
                },
                {
                  "type": "TextBlock",
                  "text": "@{outputs('Summary')}",
                  "wrap": true
                },
                {
                  "type": "Container",
                  "items": "@if(outputs('HRPoint_settings')?['showNames'], body('Names'), createArray())"
                }
              ],
              "actions": [
                {
                  "type": "Action.OpenUrl",
                  "title": "Open HRPoint",
                  "url": "@{outputs('HRPoint_settings')?['homeLink']}"
                }
              ]
            }
          },
          "Post_in_the_channel": {
            "type": "OpenApiConnection",
            "runAfter": {
              "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')?['whoIsOutTeamId']}",
                "body/recipient/channelId": "@{outputs('HRPoint_settings')?['whoIsOutChannelId']}",
                "body/messageBody": "@{string(outputs('Card'))}"
              },
              "authentication": "@parameters('$authentication')"
            }
          }
        }
      }
    }
  }
}
