Public/Push-BookingsIDPMSToBB.ps1

Function Push-BookingsIDPMSToBB {
    <#
    .SYNOPSIS
        Get information of the bookings and process it directly in BrightBooking
    .DESCRIPTION
        Get information of the bookings and process it directly in BrightBooking
    .PARAMETER CSVFile
        The path to the CSVFile which is exported from Hotel Concepts IDPMS
    .PARAMETER BrightBookingApiUrl
        Address of the BrightBooking API, e.g.: https://eu1.api.brightbooking.eu/
    .PARAMETER BrightBookingApiKey
        API key of the user to use to process the import
    .PARAMETER BrightBookingIntegrationName
        Name of the integration to link the users to
    .EXAMPLE
        Push-BookingsIDPMSToBB -CSVFile "[path to IDPMS csv export file]" -BrightBookingApiUrl "https://eu1.api.brightbooking.eu/" -BrightBookingApiKey "[your api key]" -BrightBookingIntegrationName "HotelConcepts IDPMS"
        # Parse the CSV file and let BrightBooking process it directly
    .LINK
        http://wiki.brightbooking.eu/
    #>


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

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

      [Parameter(Mandatory=$True)]
       [string]$BrightBookingIntegrationName
    )

    Process {
        Write-Output "Reading Hotel Concepts IDPMS file: $CSVFile"
        
        $readdata = Import-Csv $CSVFile -Header FromDate,UntilDate,FromTime,UntilTime,RoomName,OrganizationDescription,ActivityDescription
        
        If ($readdata)
        {
            Write-Output "Read $($readdata.Count) reservations"
            
            # put/post to the api
            $access_token = Get-BBAccessToken -BrightBookingApiUrl $BrightBookingApiUrl -BrightBookingApiKey $BrightBookingApiKey
            
            $resturi = [System.Uri]::new([System.Uri]::new($BrightBookingApiUrl), "api/integrations/import/bookings/idpms")                
            
            $Request  = [System.UriBuilder]($resturi)
            
            $body = @{
                "IntegrationName" = $BrightBookingIntegrationName
                "Bookings" = $readdata
                }
            $body = $body | ConvertTo-Json
            
            $hdrs = @{}
            $hdrs.Add("Authorization", "Bearer "+ $access_token)
            
            Try
            {
                $response = Invoke-RestMethod -Uri $Request.Uri -Method Put -Body $body -Headers $hdrs -ContentType 'application/json'
            
                If ($response.StatusCode -eq 200 -or $response.StatusCode -eq 201)
                {
                    Write-Output "Finished synchronizing reservations to BrightBooking successfully"
                }
            } 
            Catch 
            {
                $statusCode = $_.Exception.Response.StatusCode.Value__
                $responseText = $_

                If ($statusCode -eq 304)
                {
                    Write-Output "Finished synchronizing reservations to BrightBooking successfully (reservations were not changed)"
                }
                Else
                {
                    Try
                    {
                        $jsonresponse = $responseText | ConvertFrom-Json
                        If ($jsonresponse.SyncRoot)
                        {
                            $statusMessage = $jsonresponse.SyncRoot | Out-String
                        }
                        Else
                        {                        
                            $statusMessage = $responseText
                        }
                    } 
                    Catch 
                    {
                        $statusMessage = $responseText
                    }

                    Write-Output "Synchronizing reservations failed:"
                    Write-Output "Statuscode: $statusCode, message: $statusMessage"
                    Write-Output ""
                    Write-Output "Please review the messages above, and if needed please review the log in the BrightBooking portal"
                    Write-Output ""
                    Write-Error "Could not process all reservations to BrightBooking"
                }
            }
        }
    }
}