Registry becomes the entitlement authority (ERP/02, PRODUCT_INTEGRATION_SPEC 2.0):
- migrations 0002/0003: ladder statuses warning|restricted|halted on
tenant_status; products table replaces the hard-coded catalog, seeded with
pruefwerk (+ werkbank add-on), werkpilot, firmwerk; compliance disabled
until the dsms migration (M-MIG-9)
- GET /v1/tenants (keyset pagination, status/kind filters) for product
schedulers and reconcile jobs
- POST /v1/internal/entitlements/check → {allowed, read_only, status, limits,
expires_at, reason}; add-on limits from tenant_products.config
- POST /v1/tenants/{id}/status — the single ladder transition endpoint;
cancel now moves to halted; activate accepts products[] with limits and
provisions them via the catalog's per-env provision_urls
- SyncTenantStatus: coarse tenant_status attribute on every Keycloak org
member after every status change (attributes merged, roles untouched)
- AUTHZ_RULES: per-client (JWT azp) route groups; 403 FORBIDDEN_CLIENT
- INVITE_CLIENT_ID / INVITE_REDIRECT_URI replace the hard-coded dev-portal
invite parameters
- catalog entries carry type, launch_url (per APP_ENV), skus, addons,
trial_days; openapi.yaml + README updated
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
155 lines
4.7 KiB
Go
155 lines
4.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
|
)
|
|
|
|
// The catalog lives in the products table (migration 0003) since M-MIG-5.
|
|
// Launch URLs are resolved for this registry's environment (APP_ENV) so the
|
|
// portal can link straight to the product host.
|
|
|
|
func (s *Server) getCatalog(w http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
|
|
defer cancel()
|
|
products, err := s.Store.ListProducts(ctx, false)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
|
return
|
|
}
|
|
items := make([]store.CatalogEntry, 0, len(products))
|
|
for _, p := range products {
|
|
items = append(items, p.Catalog(s.env()))
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"items": items})
|
|
}
|
|
|
|
// env is the environment key used for per-env product URLs. "dev" reads the
|
|
// stage URLs so a laptop portal links somewhere real.
|
|
func (s *Server) env() string {
|
|
if s.Cfg == nil || s.Cfg.Env == "" || s.Cfg.Env == "dev" {
|
|
return "stage"
|
|
}
|
|
return s.Cfg.Env
|
|
}
|
|
|
|
// product returns an enabled catalog product or ErrNotFound.
|
|
func (s *Server) product(ctx context.Context, key string) (*store.Product, error) {
|
|
p, err := s.Store.GetProduct(ctx, key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !p.Enabled {
|
|
return nil, store.ErrNotFound
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func (s *Server) knownProduct(ctx context.Context, key string) bool {
|
|
_, err := s.product(ctx, key)
|
|
return err == nil
|
|
}
|
|
|
|
type catalogRequestReq struct {
|
|
TenantID string `json:"tenant_id"`
|
|
Product string `json:"product"`
|
|
}
|
|
|
|
// catalogRequest — customer requests a non-subscribed product. Emits an
|
|
// audit event; the ERP outbox (M-MIG-7) turns it into a CRM Lead.
|
|
func (s *Server) catalogRequest(w http.ResponseWriter, r *http.Request) {
|
|
var in catalogRequestReq
|
|
if !decodeJSON(w, r, &in) {
|
|
return
|
|
}
|
|
if in.TenantID == "" || in.Product == "" {
|
|
writeError(w, http.StatusBadRequest, "invalid_input", "tenant_id and product are required")
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
|
defer cancel()
|
|
if !s.knownProduct(ctx, in.Product) {
|
|
writeError(w, http.StatusBadRequest, "unknown_product", "product is not in the catalog")
|
|
return
|
|
}
|
|
if _, err := s.Store.GetTenant(ctx, in.TenantID); err != nil {
|
|
if mapStoreError(w, err) {
|
|
return
|
|
}
|
|
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
|
return
|
|
}
|
|
s.emitAudit(ctx, r, store.AuditEvent{
|
|
TenantID: in.TenantID, Action: "catalog.requested",
|
|
TargetID: in.Product, TargetType: "product",
|
|
Metadata: map[string]interface{}{"product": in.Product},
|
|
})
|
|
writeJSON(w, http.StatusAccepted, map[string]string{
|
|
"status": "accepted",
|
|
"message": "request recorded; sales will be in touch",
|
|
})
|
|
}
|
|
|
|
// catalogTrialRequest — self-serve trial of a product that supports it.
|
|
// Provisions the entitlement immediately (trial_days from the catalog).
|
|
func (s *Server) catalogTrialRequest(w http.ResponseWriter, r *http.Request) {
|
|
var in catalogRequestReq
|
|
if !decodeJSON(w, r, &in) {
|
|
return
|
|
}
|
|
if in.TenantID == "" || in.Product == "" {
|
|
writeError(w, http.StatusBadRequest, "invalid_input", "tenant_id and product are required")
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
|
defer cancel()
|
|
entry, err := s.product(ctx, in.Product)
|
|
if err != nil {
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
writeError(w, http.StatusBadRequest, "unknown_product", "product is not in the catalog")
|
|
return
|
|
}
|
|
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
|
return
|
|
}
|
|
if !entry.SupportsTrial {
|
|
writeError(w, http.StatusBadRequest, "trial_unavailable", "product does not support self-serve trial")
|
|
return
|
|
}
|
|
t, err := s.Store.GetTenant(ctx, in.TenantID)
|
|
if err != nil {
|
|
if mapStoreError(w, err) {
|
|
return
|
|
}
|
|
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
|
return
|
|
}
|
|
days := entry.TrialDays
|
|
if days <= 0 {
|
|
days = 14
|
|
}
|
|
expiresAt := time.Now().UTC().Add(time.Duration(days) * 24 * time.Hour)
|
|
tp, err := s.Store.UpsertTenantProduct(ctx, store.TenantProduct{
|
|
TenantID: in.TenantID, Product: in.Product, Enabled: true,
|
|
Config: map[string]interface{}{"source": "trial"}, ExpiresAt: &expiresAt,
|
|
})
|
|
if err != nil {
|
|
if mapStoreError(w, err) {
|
|
return
|
|
}
|
|
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
|
return
|
|
}
|
|
s.emitAudit(ctx, r, store.AuditEvent{
|
|
TenantID: in.TenantID, Action: "catalog.trial_started",
|
|
TargetID: in.Product, TargetType: "product",
|
|
Metadata: map[string]interface{}{"product": in.Product, "expires_at": expiresAt.Format(time.RFC3339)},
|
|
})
|
|
s.provisionProduct(ctx, r, t, entry, tp)
|
|
writeJSON(w, http.StatusCreated, tp)
|
|
}
|