Files
T
will 39d02303c7
CI / test (push) Successful in 9s
Fix TestCampaign sending only subscribers, failing listmonk validation
listmonk's /api/campaigns/:id/test handler binds the request into the
same campReq struct create/update use and validates it in full, so a
request with only {"subscribers": [...]} fails with "Invalid length
for name" before subscribers is ever read. TestCampaign now sends the
campaign's full CampaignInput alongside subscribers.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-23 21:51:20 -04:00

44 lines
1.5 KiB
Go

package campaign
import (
"fmt"
"gitea.reground.org/will/eec-campaigns/internal/listmonk"
)
// Send transitions a campaign from draft/paused to running — the one real
// send trigger in this whole tool (see cmd/send, triggered only by a pushed
// send/<slug> git tag or a manual workflow_dispatch, never by a plain
// sync). The campaign is looked up fresh by name each call, so this always
// acts on listmonk's current live state, not whatever sync last saw.
func Send(lm *listmonk.Client, slug string) (*listmonk.Campaign, error) {
camp, err := lm.FindCampaignByName(slug)
if err != nil {
return nil, err
}
if camp == nil {
return nil, fmt.Errorf("no campaign named %q in listmonk — run sync first", slug)
}
if camp.Status != "draft" && camp.Status != "paused" {
return nil, fmt.Errorf("campaign %q is %s in listmonk — only draft or paused campaigns can be sent", slug, camp.Status)
}
if err := lm.SetCampaignStatus(camp.ID, "running"); err != nil {
return nil, err
}
return camp, nil
}
// Test sends a preview of a campaign's current content to the given
// addresses without touching its status — the same mechanism sync's
// automatic preview uses, exposed standalone for on-demand re-previews.
func Test(lm *listmonk.Client, slug string, emails []string) error {
camp, err := lm.FindCampaignByName(slug)
if err != nil {
return err
}
if camp == nil {
return fmt.Errorf("no campaign named %q in listmonk — run sync first", slug)
}
return lm.TestCampaign(camp.ID, camp.AsInput(), emails)
}