Public/Get-BBUserSyncStatus.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
Function Get-BBUserSyncStatus {
    <#
    .SYNOPSIS
        Get the status of the sync to GoBright API
    .DESCRIPTION
        Get the status of the sync to GoBright API
    .PARAMETER BrightBookingApiUrl
        Address of the GoBright API, e.g.: https://t1b.gobright.cloud/ (please get this from the 'General Settings' page of the portal)
    .PARAMETER BrightBookingApiToken
        A valid token to access the api
    .EXAMPLE
        Get-BBUserSyncStatus -BrightBookingApiUrl "https://t1b.gobright.cloud/" -BrightBookingApiToken "[already retreived api token]"
    .LINK
        https://support.gobright.com/
    #>


    [CmdletBinding()]
    Param(
      [Parameter(Mandatory=$True)]
       [string]$BrightBookingApiUrl,

      [Parameter(Mandatory=$True)]
       [string]$BrightBookingApiToken,

      [Parameter(Mandatory=$False)]
       [bool]$IncludeLastSyncResult,

      [Parameter(Mandatory=$False)]
       [bool]$ValidateCanStartNew
    )
    Process {
        $pathandquery = "/api/users/batch-import-csv/status";
        if ($IncludeLastSyncResult)
        {
            $pathandquery = $pathandquery +"?includelastresultdetails=true";
        }

        $resturi = [System.Uri]::new([System.Uri]::new($BrightBookingApiUrl), $pathandquery)
        
        $hdrs = @{}
        $hdrs.Add("Authorization", "Bearer "+ $BrightBookingApiToken)

        Try
        {
            $response = Invoke-WebRequest -TimeoutSec 1200 -Uri $resturi -Method Get -Headers $hdrs -UseBasicParsing
        
            If ($response.StatusCode -eq 200 -or $response.StatusCode -eq 201)
            {
                $jsonresponse = $response.Content | ConvertFrom-Json
            } else {
                throw "Failed to get user sync status from the API. (statuscode: $response.StatusCode)"
            }
        }
        Catch 
        {
            $statusCode = $_.Exception.Response.StatusCode.Value__
            $responseText = $_
                        
            Try
            {
                $jsonresponse = $responseText | ConvertFrom-Json
                If ($jsonresponse.SyncRoot)
                {
                    $statusMessage = $jsonresponse.SyncRoot
                }
                Else
                {
                    $statusMessage = $responseText
                }
            } 
            Catch 
            {
                $statusMessage = $responseText
            }
            
            throw "Failed to get user sync status from the API. (statuscode: $statusCode, message: $statusMessage)"
        }
        
        # call succeeded, validate other steps if requested
        if ($ValidateCanStartNew)
        {
            if (!$jsonresponse.IsPossibleToStartNew) {
                $nextCallAllowedAt = $jsonresponse.NextCallAllowedAt

                if ($jsonresponse.IsCurrentlyProcessing) {
                    throw "Unable to start synchronisation now, processing is already busy"
                } else {
                    throw "Unable to start synchronisation now, next allowed from: $nextCallAllowedAt"
                }
            }
        }
        return $jsonresponse
    }
}