Files

105 lines
4.4 KiB
PowerShell

<#
.SYNOPSIS
Creates offerings (packages) in a CMS through the admin API. Idempotent: a slug that already exists is skipped.
.DESCRIPTION
Logs in with an admin account (the password is asked interactively, as a secure string; it is never
written to disk, to the console or to a log), then creates the offerings from -PackagesFile in file
order with an explicit slug. DisplayOrder is assigned by the server in creation order (0, 1, 2, ...).
Test hosts only unless you deliberately point it somewhere else.
.PARAMETER BaseUrl
Root of the host, e.g. https://test.andreaslop.nl (no trailing /api).
.PARAMETER Email
Admin e-mail address. Asked when omitted.
.PARAMETER PackagesFile
JSON array; default: offerings-asf.json next to this script. Each item: slug, title, description,
price, priceNote, features (array), ctaLabel, layout (card|banner, default card), featured (default false).
.PARAMETER WhatIf
Shows what would be created without logging in or writing anything.
.EXAMPLE
./scripts/Seed-Offerings.ps1 -BaseUrl https://test.andreaslop.nl
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)] [string] $BaseUrl,
[string] $Email,
[string] $PackagesFile = (Join-Path $PSScriptRoot 'offerings-asf.json')
)
$ErrorActionPreference = 'Stop'
$BaseUrl = $BaseUrl.TrimEnd('/')
$api = "$BaseUrl/api/v1"
$packages = Get-Content -LiteralPath $PackagesFile -Raw -Encoding UTF8 | ConvertFrom-Json
if (-not $packages -or $packages.Count -eq 0) { throw "No packages in $PackagesFile." }
# Refuse unfinished templates: no guessed copy ends up in a CMS.
foreach ($p in $packages) {
foreach ($field in 'slug', 'title', 'price') {
$v = $p.$field
if ([string]::IsNullOrWhiteSpace($v) -or $v -like 'TODO*') { throw "Package '$($p.slug)': '$field' is empty or still TODO." }
}
# description, priceNote and ctaLabel may be empty (the API accepts empty strings), but never TODO.
foreach ($field in 'description', 'priceNote', 'ctaLabel') {
if ("$($p.$field)" -like 'TODO*') { throw "Package '$($p.slug)': '$field' is still TODO." }
}
if (-not $p.features -or $p.features.Count -eq 0 -or ($p.features | Where-Object { $_ -like 'TODO*' })) {
throw "Package '$($p.slug)': 'features' is empty or still TODO."
}
}
if ($WhatIfPreference) {
$packages | ForEach-Object { Write-Host "Would create '$($_.slug)' ($($_.title), $($_.price)) on $BaseUrl" }
return
}
if (-not $Email) { $Email = Read-Host 'Admin e-mail' }
$secure = Read-Host "Password for $Email" -AsSecureString
$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
try {
$plain = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
$body = @{ email = $Email; password = $plain } | ConvertTo-Json -Compress
$login = Invoke-RestMethod -Method Post -Uri "$api/auth/login" -ContentType 'application/json' -Body $body
}
finally {
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$plain = $null; $body = $null
}
$headers = @{ Authorization = "Bearer $($login.accessToken)" }
# The public list tells which slugs exist already.
# Windows PowerShell 5.1 hands a JSON array over as ONE object; wrap it so every item is enumerated.
$list = @(Invoke-RestMethod -Method Get -Uri "$api/offerings" -Headers $headers)
$existing = @(foreach ($o in $list) { $o.slug })
foreach ($p in $packages) {
if ($existing -contains $p.slug) { Write-Host "skip $($p.slug) (exists)"; continue }
$payload = [ordered]@{
slug = $p.slug
title = $p.title
description = $p.description
price = $p.price
priceNote = $p.priceNote
features = @($p.features)
ctaLabel = $p.ctaLabel
featured = [bool]$p.featured
layout = if ($p.layout) { $p.layout } else { 'card' }
} | ConvertTo-Json -Depth 5
try {
$created = Invoke-RestMethod -Method Post -Uri "$api/offerings" -Headers $headers -ContentType 'application/json; charset=utf-8' -Body ([Text.Encoding]::UTF8.GetBytes($payload))
Write-Host "create $($created.slug) (displayOrder $($created.displayOrder))"
}
catch {
Write-Error "Creating '$($p.slug)' failed: $($_.Exception.Message) $($_.ErrorDetails.Message)"
}
}
Write-Host 'Result:'
$final = @(Invoke-RestMethod -Method Get -Uri "$api/offerings")
foreach ($o in $final) { Write-Host (" {0,2} {1} {2} [{3}]" -f $o.displayOrder, $o.slug, $o.price, $o.layout) }