package campaign import ( "fmt" "os" "gitea.reground.org/will/eec-campaigns/internal/listmonk" ) // SyncTemplate pushes a template file's content into listmonk under name, // creating it (as the default campaign template) if missing or overwriting // its body if it already exists. Unlike campaign sync, a template has no // send-lifecycle status to protect — there's no "draft vs. live" distinction // to guard, so this is always a safe create-or-update, no rejection path. func SyncTemplate(lm *listmonk.Client, name, path string) (*listmonk.Template, error) { body, err := os.ReadFile(path) if err != nil { return nil, err } existing, err := lm.FindTemplateByName(name) if err != nil { return nil, err } if existing == nil { created, err := lm.CreateTemplate(name, string(body), true) if err != nil { return nil, fmt.Errorf("creating template %q: %w", name, err) } return created, nil } if err := lm.UpdateTemplate(existing.ID, name, string(body), true); err != nil { return nil, fmt.Errorf("updating template %q: %w", name, err) } existing.Body = string(body) existing.IsDefault = true return existing, nil }