-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding possibilty to gather messages from Inxmail in #13
- Loading branch information
Showing
1 changed file
with
203 additions
and
0 deletions.
There are no files selected for viewing
203 changes: 203 additions & 0 deletions
203
AptecoPSFramework/plugins/InxmailPro/Public/PeopleStage/get-messages.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
|
||
|
||
function Get-Messages { | ||
|
||
[CmdletBinding(DefaultParameterSetName = 'Object')] | ||
param ( | ||
[Parameter(Mandatory=$true, ParameterSetName = 'Object')][Hashtable]$InputHashtable # This creates a new entry in joblog | ||
,[Parameter(Mandatory=$true, ParameterSetName = 'Job')][Int]$JobId # This uses an existing joblog entry | ||
) | ||
|
||
begin { | ||
|
||
|
||
#----------------------------------------------- | ||
# MODULE INIT | ||
#----------------------------------------------- | ||
|
||
$moduleName = "MESSAGES" | ||
|
||
|
||
#----------------------------------------------- | ||
# START TIMER | ||
#----------------------------------------------- | ||
|
||
$processStart = [datetime]::now | ||
|
||
|
||
#----------------------------------------------- | ||
# CHECK INPUT AND SET JOBLOG | ||
#----------------------------------------------- | ||
|
||
# Log the job in the database | ||
Set-JobLogDatabase | ||
|
||
Switch ( $PSCmdlet.ParameterSetName ) { | ||
|
||
"Object" { | ||
|
||
# Create a new job | ||
$JobId = Add-JobLog | ||
$jobParams = [Hashtable]@{ | ||
"JobId" = $JobId | ||
"Plugin" = $script:settings.plugin.guid | ||
"InputParam" = $InputHashtable | ||
"Status" = "Starting" | ||
"DebugMode" = $Script:debugMode | ||
"Type" = $moduleName | ||
} | ||
Update-JobLog @jobParams | ||
|
||
break | ||
} | ||
|
||
"Job" { | ||
|
||
# Get the jobs information | ||
$job = Get-JobLog -JobId $JobId -ConvertInput | ||
$InputHashtable = $job.input | ||
|
||
# Update the job with more information | ||
$jobParams = [Hashtable]@{ | ||
"JobId" = $JobId | ||
"Plugin" = $script:settings.plugin.guid | ||
"Status" = "Starting" | ||
"Type" = $moduleName | ||
} | ||
Update-JobLog @jobParams | ||
|
||
break | ||
} | ||
|
||
} | ||
|
||
|
||
#----------------------------------------------- | ||
# LOG | ||
#----------------------------------------------- | ||
|
||
# Start the log | ||
Write-Log -message $Script:logDivider | ||
Write-Log -message $moduleName -Severity INFO | ||
|
||
# Log the params, if existing | ||
Write-Log -message "INPUT:" | ||
if ( $InputHashtable ) { | ||
$InputHashtable.Keys | ForEach-Object { | ||
$param = $_ | ||
Write-Log -message " $( $param ) = '$( $InputHashtable[$param] )'" -writeToHostToo $false | ||
} | ||
} | ||
|
||
#----------------------------------------------- | ||
# DEBUG MODE | ||
#----------------------------------------------- | ||
|
||
Write-Log "Debug Mode: $( $Script:debugMode )" | ||
|
||
|
||
#----------------------------------------------- | ||
# OPEN DEFAULT DUCKDB CONNECTION (NOT JOBLOG) | ||
#----------------------------------------------- | ||
|
||
#Open-DuckDBConnection | ||
|
||
|
||
} | ||
|
||
process { | ||
|
||
$mailings = Get-Mailing -Type REGULAR_MAILING -All -IsApproved | ||
Write-Log "Loaded $( $mailings.Count ) mailing drafts from Inxmail" -severity INFO #-WriteToHostToo $false | ||
|
||
# Load and filter list into array of mailings objects | ||
$mailingsList = [System.Collections.ArrayList]@() | ||
$mailings | ForEach-Object { | ||
$mailing = $_ | ||
[void]$mailingsList.add( | ||
[Mailing]@{ | ||
mailingId=$mailing.id | ||
mailingName=( $mailing.name -replace '[^\w\s]', '' ) | ||
} | ||
) | ||
} | ||
|
||
# Transform the mailings array into the needed output format | ||
$columns = @( | ||
@{ | ||
name="id" | ||
expression={ $_.mailingId } | ||
} | ||
@{ | ||
name="name" | ||
expression={ $_.toString() } | ||
} | ||
) | ||
|
||
$messages = [System.Collections.ArrayList]@() | ||
[void]$messages.AddRange(@( $mailingsList | Select-Object $columns )) | ||
|
||
If ( $messages.count -gt 0 ) { | ||
|
||
Write-Log "Loaded $( $messages.Count ) messages" -severity INFO #-WriteToHostToo $false | ||
|
||
} else { | ||
|
||
$msg = "No messages loaded -> please check!" | ||
Write-Log -Message $msg -Severity ERROR | ||
throw [System.IO.InvalidDataException] $msg | ||
|
||
} | ||
|
||
|
||
#----------------------------------------------- | ||
# STOP TIMER | ||
#----------------------------------------------- | ||
|
||
$processEnd = [datetime]::now | ||
$processDuration = New-TimeSpan -Start $processStart -End $processEnd | ||
Write-Log -Message "Needed $( [int]$processDuration.TotalSeconds ) seconds in total" | ||
|
||
|
||
#----------------------------------------------- | ||
# CLOSE DEFAULT DUCKDB CONNECTION | ||
#----------------------------------------------- | ||
|
||
Close-DuckDBConnection | ||
|
||
|
||
#----------------------------------------------- | ||
# RETURN VALUES TO PEOPLESTAGE | ||
#----------------------------------------------- | ||
|
||
# log the return into database and close connection | ||
$jobReturnParams = [Hashtable]@{ | ||
"JobId" = $JobId | ||
"Status" = "Finished" | ||
"Finished" = $true | ||
"Successful" = $messages.Count | ||
"Failed" = 0 # TODO needs correction | ||
"Totalseconds" = $processDuration.TotalSeconds | ||
"OutputArray" = $return | ||
} | ||
Update-JobLog @jobReturnParams | ||
Close-JobLogDatabase -Name "JobLog" | ||
|
||
|
||
# return the results | ||
Switch ( $PSCmdlet.ParameterSetName ) { | ||
"Object" { | ||
$messages | ||
break | ||
} | ||
# Otherwise the results are now in the database | ||
} | ||
|
||
} | ||
|
||
end { | ||
|
||
} | ||
|
||
} | ||
|