Files
zeekay e285e18128 feat: bootstrap notifyd service layer on hanzoai/base + hanzoai/tasks
Replaces the chi-based notifyd scaffold with the canonical
hanzoai/auto-style layout: hanzoai/base as the HTTP + SQLite + auth
substrate (via the platform plugin for IAM JWT validation and
X-Org-Id injection), hanzoai/tasks for durable async sends, and a
per-tenant Notifier resolver that pulls provider credentials from
KMS through platform.KMSClient.

Library at the module root stays untouched — all 33 provider impls
(plivo, twilio, ses, sendgrid, mail, whatsapp, discord, slack, …)
keep their existing public API. The service wraps them.

cmd/notifyd
  - base.New + platform.MustRegister
  - schema.MustRegister (6 collections: tenants, providers,
    templates, events, messages, meter)
  - tasks worker registered when TASKS_ADDR is set; sync path
    runs the same Deliver activity directly when not

internal/
  - boot/      thin platform.KMSClient wiring (env-driven)
  - schema/    6 base collections
  - routes/    /v1/notify/{send,messages,providers,templates,events,metering,health}
               + convenience /send/{sms,email,voice,whatsapp}
  - tenant/    KMS-backed resolver builds library Notifier per send
  - template/  draft → pending → approved → published → archived
  - event/     catalog routing (fills missing channel / template)
  - metering/  per-send ledger writer + aggregate query
  - tasks/     NotifySendWorkflow + Deliver activity (idempotent on
               replay; reloads message row before resending)
  - zaprpc/    procedure name constants (server impl follow-up)

pkg/
  - client/    public HTTP SDK (Send / Message / Providers / Templates)
  - types/     wire shapes shared by routes and client

cucloud: fmt.Errorf(msg) → errors.New(msg) for the go 1.26 vet pass
that landed with this dependency bump. Behaviour unchanged.

Boot smoke-tested locally:
  - SQLite migrations apply cleanly
  - /v1/notify/health returns {"service":"notify","status":"ok"}
  - missing X-Org-Id returns 401 on every authenticated route
  - send with stub tenant returns the expected base validation error
2026-06-01 16:06:27 -07:00

94 lines
2.5 KiB
Go

package routes
import (
"net/http"
"github.com/hanzoai/base"
"github.com/hanzoai/base/apis"
"github.com/hanzoai/base/core"
"github.com/hanzoai/base/tools/router"
"github.com/hanzoai/dbx"
"github.com/hanzoai/notify/internal/schema"
"github.com/hanzoai/notify/pkg/types"
)
type eventInput struct {
Name string `json:"name"`
Channels []string `json:"channels,omitempty"`
TemplateID string `json:"template_id,omitempty"`
RateLimit int `json:"rate_limit,omitempty"`
Enabled bool `json:"enabled"`
}
// mountEvents installs CRUD over the event catalog.
func mountEvents(r *router.Router[*core.RequestEvent], app *base.Base) {
r.GET("/v1/notify/events", func(e *core.RequestEvent) error {
org, err := orgFromRequest(e)
if err != nil {
return err
}
rows, err := app.FindRecordsByFilter(
schema.Events,
"tenant = {:tenant}",
"-updated", 200, 0,
dbx.Params{"tenant": org},
)
if err != nil {
return apis.NewInternalServerError("list events", err)
}
out := make([]types.Event, 0, len(rows))
for _, r := range rows {
out = append(out, toEventDTO(r))
}
return e.JSON(http.StatusOK, map[string]any{"items": out})
})
r.POST("/v1/notify/events", func(e *core.RequestEvent) error {
org, err := orgFromRequest(e)
if err != nil {
return err
}
var body eventInput
if err := e.BindBody(&body); err != nil {
return apis.NewBadRequestError("malformed body", err)
}
if body.Name == "" {
return apis.NewBadRequestError("name is required", nil)
}
col, err := app.FindCollectionByNameOrId(schema.Events)
if err != nil {
return apis.NewInternalServerError("collection", err)
}
rec := core.NewRecord(col)
rec.Set("tenant", org)
rec.Set("name", body.Name)
rec.Set("channels", body.Channels)
rec.Set("template_id", body.TemplateID)
rec.Set("rate_limit", body.RateLimit)
rec.Set("enabled", body.Enabled)
if err := app.Save(rec); err != nil {
return apis.NewInternalServerError("save", err)
}
return e.JSON(http.StatusCreated, toEventDTO(rec))
})
}
func toEventDTO(r *core.Record) types.Event {
chans := []string{}
if raw := r.GetStringSlice("channels"); len(raw) > 0 {
chans = raw
}
return types.Event{
ID: r.Id,
TenantSlug: r.GetString("tenant"),
Name: r.GetString("name"),
Channels: chans,
TemplateID: r.GetString("template_id"),
RateLimit: r.GetInt("rate_limit"),
Enabled: r.GetBool("enabled"),
Created: r.GetString("created"),
Updated: r.GetString("updated"),
}
}