KovertKringle.psm1

function Add-KKParticipant {
    <#
    .SYNOPSIS
    Short description
 
    .DESCRIPTION
    Long description
 
    .EXAMPLE
    An example
 
    .NOTES
    General notes
    #>


    [CmdletBinding()]

    param (
        [Parameter(Mandatory)]
        [string] $Name,

        [Parameter(Mandatory)]
        [string] $Email,

        [switch] $Snitch
    )

    if ($Name -notin $Script:Participants.Name) {
        $Obj = [PSCustomObject] @{
            Name = $Name
            Email = $Email
            Snitch = $Snitch
            Paired = $false
            SortKey = [System.Random]::new().Next(1, 1000)
        }
        $null = $Script:Participants.Add($Obj)
    } else {
        Write-Warning "$Name is already a participant!"
    }
}
function Clear-KKPairing {
    <#
    .SYNOPSIS
    Short description
 
    .DESCRIPTION
    Long description
 
    .EXAMPLE
    An example
 
    .NOTES
    General notes
    #>


    $Script:Pairings.Clear()

    foreach ($Person in $Script:Participants) {
        $Person.Paired = $false
    }
}
function Clear-KKParticipant {
    <#
    .SYNOPSIS
    Short description
 
    .DESCRIPTION
    Long description
 
    .EXAMPLE
    An example
 
    .NOTES
    General notes
    #>


    $Script:Participants.Clear()
}
function Get-KKParticipant {
    <#
    .SYNOPSIS
    Short description
 
    .DESCRIPTION
    Long description
 
    .EXAMPLE
    An example
 
    .NOTES
    General notes
    #>


    $Script:Participants
}
function Remove-KKParticipant {
    <#
    .SYNOPSIS
    Short description
 
    .DESCRIPTION
    Long description
 
    .EXAMPLE
    An example
 
    .NOTES
    General notes
    #>


    [CmdletBinding()]

    param (
        [Parameter(Mandatory)]
        [string] $Name
    )

    $Obj = $Script:Participants | Where-Object {$_.Name -eq $Name}
    $Script:Participants.Remove($Obj)
}
function Send-KKNotification {
    <#
    .SYNOPSIS
    Short description
 
    .DESCRIPTION
    Long description
 
    .EXAMPLE
    An example
 
    .NOTES
    General notes
    #>


    # Get your credentials
    $Cred = Get-StoredCredential -Target SendGrid
    $SmtpServer = 'smtp.sendgrid.net'
    $FromEmail = 'windosbk+sendgrid@gmail.com'
    $Subject = 'Your Secret Santa Pairing!'
    $BCC = ($Script:Participants | Where-Object {$_.Snitch}).Email

    foreach ($Pair in $Script:Pairings) {
        $Body = '{0}, you are buying a Secret Santa gift for {1}! Remeber the limit is $20.' -f $Pair.Buyer.Name, $Pair.Receiver.Name
        $ToEmail = $Pair.Buyer.Email

        $Splat = @{
            SmtpServer = $SmtpServer
            Credential = $Cred
            Body = $Body
            Subject = $Subject
            To = $ToEmail
            From = $FromEmail
        }

        if ($BCC) {
            $Splat.Add('Bcc', $BCC)
        }

        Send-MailMessage @Splat
    }
}
function Start-KKPairing {
    <#
    .SYNOPSIS
    Short description
 
    .DESCRIPTION
    Long description
 
    .EXAMPLE
    An example
 
    .NOTES
    General notes
    #>


    foreach ($Person in ($Script:Participants | Sort-Object -Property SortKey)) {
        $Potential = $Script:Participants | Where-Object {$_ -ne $Person -and $_.Paired -eq $false}

        if ($Potential) {
            $Giftee = $Potential | Get-Random

            $Pairing = [PSCustomObject] @{
                Buyer = $Person
                Receiver = $Giftee
            }

            $null = $Script:Pairings.Add($Pairing)

            $Giftee.Paired = $true
        } else {
            Write-Error 'No possible pairings, you may have already run this function.'
            break
        }
        Start-Sleep -Milliseconds 10
    }
}
Export-ModuleMember -Function 'Add-KKParticipant', 'Clear-KKPairing', 'Clear-KKParticipant', 'Get-KKParticipant', 'Remove-KKParticipant', 'Send-KKNotification', 'Start-KKPairing'