Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
3.7 KiB
Go
105 lines
3.7 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
|
|
}
|
|
meta := map[string]interface{}{"preview_expires_at": in.ExpiresAt.UTC().Format(time.RFC3339)}
|
|
// A booking that was later disabled (e.g. a cancelled subscription) is
|
|
// overridden by the grant; the audit trail says so.
|
|
if tps, err := s.Store.ListTenantProducts(ctx, t.ID); err == nil {
|
|
for _, old := range tps {
|
|
if old.Product == in.Product && !old.Preview && !old.Enabled {
|
|
meta["overrides_disabled_booking"] = true
|
|
}
|
|
}
|
|
}
|
|
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: meta,
|
|
})
|
|
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)
|
|
}
|