AMONotifications.psm1

<#
.SYNOPSIS
Creates a new notification.
.DESCRIPTION
This function creates a new notification in the system.
.PARAMETER CreatedBy
Specifies the creator of the notification.
.PARAMETER Title
Specifies the title of the notification.
.PARAMETER Description
Specifies the description of the notification.
.PARAMETER Level
Specifies the level of the notification (Info, Warning, Error, Success).
.PARAMETER DataSourcePath
Specifies the path to the data source.
.EXAMPLE
New-AMONotification -CreatedBy "John" -Title "Notification Title" -Description "Notification Description" -Level "Info"
#>


function New-AMONotification
{
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [string]$CreatedBy,
        [Parameter(Mandatory = $true)]
        [string]$Title,
        [Parameter(Mandatory = $true)]
        [string]$Description,
        [Parameter(Mandatory = $true)]
        [ValidateSet("Info", "Warning", "Error", "Success")]
        [string]$Level,
        [string]$DataSourcePath = "$Repository\..\database.db"
    )

    if ($PSCmdlet.ShouldProcess("Create notification: $Title")) {
        if ($CreatedBy -eq [System.String]::Empty)
        {
            $CreatedBy = "System"
        }
        if (-not (Test-DatabaseExists -DataSourcePath $DataSourcePath))
        {
            throw [System.IO.FileNotFoundException]::new("Database not found at $DataSourcePath")
        }
        $IntLevel = Switch ($Level)
        {
            "Info" { 0 }
            "Warning" { 1 }
            "Error" { 2 }
            "Success" { 3 }
            default { 4 }
        }

        if ($IntLevel -eq 3)
        {
            $Title = "Success: $Title"
        }
        if ($IntLevel -eq 4)
        {
            $Title = "Undefined: $Title"
        }

        $Today = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

        $Query = "INSERT INTO Notification (CreatedBy, CreatedTime, Description, Level, Title, Viewed) VALUES ('System', '$Today', '$Description', '$IntLevel', '$Title', '0')"

        Write-Verbose "Inserting notification..."
        Invoke-BSASQLiteQuery -DataSourcePath $DataSourcePath -Query $Query
        Write-Verbose "Notification inserted."
    }
}

Export-ModuleMember -Function New-AMONotification