cmd/erpsetup applies what the registry expects in a live ERPNext: tenant_id/tenant_slug custom fields on Customer and Lead, the Usage Record doctype, and Frappe Webhooks (Sales Order submit, Sales Invoice overdue, Payment Entry submit, Subscription cancelled) signed with the registry's webhook secret. It checks before it creates, so it can run again at any time, and -dry-run only reports. It ships in the image as /erpsetup. Tenant creation refuses slugs the portal serves itself (admin, start, install, docs, …), mirroring the portal's signup check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
53 lines
1.8 KiB
Go
53 lines
1.8 KiB
Go
// erpsetup — applies the live-ERPNext configuration the tenant-registry needs
|
|
// (custom fields, the Usage Record doctype, the Frappe Webhooks). Idempotent;
|
|
// run with -dry-run first. It never submits, cancels or deletes ERP documents.
|
|
//
|
|
// ERP_BASE_URL=https://erp.breakpilot.com ERP_API_KEY=… ERP_API_SECRET=… \
|
|
// ERP_WEBHOOK_SECRET=… erpsetup -webhook-url https://registry.breakpilot.com/v1/internal/erp/webhook -dry-run
|
|
//
|
|
// The API user needs System Manager for this run (it creates a DocType and
|
|
// Webhooks); the registry's day-to-day key can then have a narrower role.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/erpsetup"
|
|
)
|
|
|
|
func main() {
|
|
dry := flag.Bool("dry-run", false, "report what would be created, change nothing")
|
|
hook := flag.String("webhook-url", "", "registry webhook receiver, e.g. https://registry.breakpilot.com/v1/internal/erp/webhook")
|
|
lead := flag.String("lead-doctype", envOr("ERP_LEAD_DOCTYPE", "Lead"), `"Lead" (ERPNext) or "CRM Lead" (Frappe CRM)`)
|
|
flag.Parse()
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
defer stop()
|
|
steps, err := erpsetup.Run(ctx, erpsetup.Config{
|
|
BaseURL: os.Getenv("ERP_BASE_URL"),
|
|
APIKey: os.Getenv("ERP_API_KEY"),
|
|
APISecret: os.Getenv("ERP_API_SECRET"),
|
|
WebhookURL: *hook,
|
|
WebhookSecret: os.Getenv("ERP_WEBHOOK_SECRET"),
|
|
LeadDoctype: *lead,
|
|
DryRun: *dry,
|
|
Log: func(f string, a ...any) { fmt.Printf(f+"\n", a...) },
|
|
})
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf("%d steps, done%s\n", len(steps), map[bool]string{true: " (dry run, nothing changed)", false: ""}[*dry])
|
|
}
|
|
|
|
func envOr(k, d string) string {
|
|
if v := os.Getenv(k); v != "" {
|
|
return v
|
|
}
|
|
return d
|
|
}
|