Files
tenant-registry/internal/server/admin_preview.go
T
Sharang ParnerkarandClaude Opus 5.5 c99aa6908d
ci / shared (pull_request) Successful in 12s
ci / test (pull_request) Successful in 25m20s
ci / image (pull_request) Skipped
feat(entitlements): Staff preview grants for products not for sale yet
Lets BreakPilot staff open a product that is still disabled in the
catalogue (Werkmeister) for one tenant for a limited time, e.g. a seeded
staging tenant for portal#86, without a stage/prod divergence:

- Migration 0018: tenant_products.preview and preview_expires_at, kept
  apart from a booking's expires_at.
- Entitlement check: a disabled product is allowed only under an active
  preview (answer carries "preview": true); expiry is enforced at check
  time (reason preview_expired). A leftover plain row of a product off
  sale stays unknown_product.
- A real booking (ERP, trial, catalogue upsert) clears the preview and
  its expiry, so it never cuts off a paid entitlement; a preview over a
  booking is refused (409).
- Usage under a preview is accepted, tagged properties.preview=true and
  kept out of the ERP outbox; claims list a preview product only while
  it is active.
- Staff only, audited: POST /v1/admin/tenants/{id}/preview-grants
  {product, expires_at ≤ 180 d}, DELETE …/preview-grants/{product}; a
  named staff member (X-Staff-Subject) is required.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 01:25:04 +02:00

95 lines
3.4 KiB
Go

package server
import (
"context"
"errors"
"net/http"
"time"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
// Preview grants (migration 0018): BreakPilot staff let a tenant use a product
// that is not for sale yet — e.g. Werkmeister on a staging tenant — for a
// limited time. No catalogue or trial visibility, no subscription, no ERP;
// usage under a preview is tagged preview=true and never invoiced. A real
// booking replaces the grant.
// maxPreview bounds a preview grant, so none stays forever by accident.
const maxPreview = 180 * 24 * time.Hour
type previewGrantReq struct {
Product string `json:"product"`
ExpiresAt time.Time `json:"expires_at"`
}
// adminGrantPreview — POST /v1/admin/tenants/{id}/preview-grants.
func (s *Server) adminGrantPreview(w http.ResponseWriter, r *http.Request) {
if _, _, ok := s.staffActor(r); !ok {
writeError(w, http.StatusForbidden, "staff_required", "preview grants are made by a named staff member (X-Staff-Subject)")
return
}
var in previewGrantReq
if !decodeJSON(w, r, &in) {
return
}
now := s.now()
if in.Product == "" || in.ExpiresAt.IsZero() || !in.ExpiresAt.After(now) || in.ExpiresAt.After(now.Add(maxPreview)) {
writeError(w, http.StatusBadRequest, "invalid_input", "product and expires_at (in the future, at most 180 days ahead) are required")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
t, err := s.Store.GetTenant(ctx, r.PathValue("id"))
if err != nil {
if mapStoreError(w, err) {
return
}
writeError(w, http.StatusInternalServerError, "internal", err.Error())
return
}
if _, err := s.Store.GetProduct(ctx, in.Product); err != nil {
writeError(w, http.StatusBadRequest, "unknown_product", "product is not in the catalogue")
return
}
tp, err := s.Store.SetPreviewGrant(ctx, t.ID, in.Product, in.ExpiresAt.UTC())
if errors.Is(err, store.ErrConflict) {
writeError(w, http.StatusConflict, "already_booked", "the tenant has booked this product; a preview grant would not change anything")
return
}
if err != nil {
if mapStoreError(w, err) {
return
}
writeError(w, http.StatusInternalServerError, "internal", err.Error())
return
}
s.emitAudit(ctx, r, store.AuditEvent{
TenantID: t.ID, Action: "entitlement.preview_granted", TargetID: in.Product, TargetType: "product", TargetName: t.Slug,
Product: in.Product, Metadata: map[string]interface{}{"preview_expires_at": in.ExpiresAt.UTC().Format(time.RFC3339)},
})
writeJSON(w, http.StatusCreated, tp)
}
// adminRevokePreview — DELETE /v1/admin/tenants/{id}/preview-grants/{product}.
func (s *Server) adminRevokePreview(w http.ResponseWriter, r *http.Request) {
if _, _, ok := s.staffActor(r); !ok {
writeError(w, http.StatusForbidden, "staff_required", "preview grants are revoked by a named staff member (X-Staff-Subject)")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
tenantID, product := r.PathValue("id"), r.PathValue("product")
if err := s.Store.RevokePreviewGrant(ctx, tenantID, product); err != nil {
if mapStoreError(w, err) {
return
}
writeError(w, http.StatusInternalServerError, "internal", err.Error())
return
}
s.emitAudit(ctx, r, store.AuditEvent{
TenantID: tenantID, Action: "entitlement.preview_revoked", TargetID: product, TargetType: "product", Product: product,
})
w.WriteHeader(http.StatusNoContent)
}