feat(pruefwerk): entry offer on top of a Monitor subscription #153
No files matched your search
@@ -23,6 +23,11 @@ import (
|
||||
// § 7 Abs. 2). A Prüfwerk booking (subscription, ERP or activation grant)
|
||||
// supersedes the entry plan; the route refuses a workspace that has one. A
|
||||
// trial or preview grant of Prüfwerk is replaced.
|
||||
//
|
||||
// A Monitor subscriber (only CRA-CSA-CLD-MON booked) keeps Monitor: the
|
||||
// offer's targets are added to the subscription's grant as entry_targets
|
||||
// until expires_at (entry_until), which recomputeEntitlements carries over
|
||||
// and pruefwerkLimits adds to Monitor's one target.
|
||||
|
||||
type entryOfferReq struct {
|
||||
IncludedTargets int `json:"included_targets"`
|
||||
@@ -74,12 +79,17 @@ func (s *Server) adminEntryOffer(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
||||
return
|
||||
}
|
||||
booked, monitorOnly := false, true
|
||||
for _, sub := range subs {
|
||||
if sub.Status == store.SubscriptionActive && sub.Product == pruefwerkProduct {
|
||||
writeError(w, http.StatusConflict, "already_booked", "the workspace has a Prüfwerk subscription; it supersedes the entry plan")
|
||||
return
|
||||
booked = true
|
||||
monitorOnly = monitorOnly && sub.SKU == monitorPlanSKU
|
||||
}
|
||||
}
|
||||
if booked && !monitorOnly {
|
||||
writeError(w, http.StatusConflict, "already_booked", "the workspace has a Prüfwerk subscription; it supersedes the entry plan")
|
||||
return
|
||||
}
|
||||
prod, err := s.Store.GetProduct(ctx, pruefwerkProduct)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal", "prüfwerk is not in the catalogue: "+err.Error())
|
||||
@@ -94,6 +104,10 @@ func (s *Server) adminEntryOffer(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
||||
return
|
||||
}
|
||||
if booked {
|
||||
s.addEntryToMonitor(w, r, t, prod, tps, in, meta)
|
||||
return
|
||||
}
|
||||
for _, old := range tps {
|
||||
if old.Product != pruefwerkProduct {
|
||||
continue
|
||||
@@ -150,3 +164,45 @@ func hasEntryGrant(tps []store.TenantProduct, now time.Time) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// addEntryToMonitor grants an entry offer on top of a Monitor subscription:
|
||||
// the subscription's grant keeps its source, plan and end, and carries the
|
||||
// offer's targets until in.ExpiresAt.
|
||||
func (s *Server) addEntryToMonitor(w http.ResponseWriter, r *http.Request, t *store.Tenant, prod *store.Product,
|
||||
tps []store.TenantProduct, in entryOfferReq, meta map[string]interface{}) {
|
||||
ctx := r.Context()
|
||||
for _, tp := range tps {
|
||||
if tp.Product != pruefwerkProduct || !tp.Enabled || tp.Config["source"] != "erp" {
|
||||
continue
|
||||
}
|
||||
if prev, ok := tp.Config[entryAddOnUntilKey].(string); ok {
|
||||
meta["previous_expires_at"] = prev
|
||||
}
|
||||
cfg := make(map[string]interface{}, len(tp.Config)+3)
|
||||
for k, v := range tp.Config {
|
||||
cfg[k] = v
|
||||
}
|
||||
cfg[entryAddOnTargetsKey] = in.IncludedTargets
|
||||
cfg[entryAddOnUntilKey] = in.ExpiresAt.UTC().Format(time.RFC3339)
|
||||
delete(cfg, entryOrderRefKey)
|
||||
if in.OrderRef != "" {
|
||||
cfg[entryOrderRefKey] = in.OrderRef
|
||||
meta[entryOrderRefKey] = in.OrderRef
|
||||
}
|
||||
tp.Config = cfg
|
||||
saved, err := s.Store.UpsertTenantProduct(ctx, tp)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
||||
return
|
||||
}
|
||||
meta["with"] = "monitor"
|
||||
s.emitAudit(ctx, r, store.AuditEvent{
|
||||
TenantID: t.ID, Action: "entitlement.entry_granted", TargetID: pruefwerkProduct, TargetType: "product",
|
||||
TargetName: t.Slug, Product: pruefwerkProduct, Metadata: meta,
|
||||
})
|
||||
s.provisionProduct(ctx, r, t, prod, saved)
|
||||
writeJSON(w, http.StatusOK, saved)
|
||||
return
|
||||
}
|
||||
writeError(w, http.StatusConflict, "not_provisioned", "the Monitor subscription has no Prüfwerk grant yet; reconcile the workspace first")
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func (s *Server) evaluateEntitlement(ctx context.Context, in entitlementCheckReq
|
||||
if err != nil {
|
||||
return EntitlementCheck{}, err
|
||||
}
|
||||
out.Limits = pruefwerkLimits(tp.Config, set.PrueftagCap)
|
||||
out.Limits = pruefwerkLimits(tp.Config, set.PrueftagCap, now)
|
||||
}
|
||||
out.ExpiresAt = tp.ExpiresAt
|
||||
return out, nil
|
||||
|
||||
@@ -310,6 +310,16 @@ func (s *Server) recomputeEntitlements(ctx context.Context, r *http.Request, t *
|
||||
if active {
|
||||
sort.Strings(skus)
|
||||
cfg["skus"] = skus
|
||||
// A running entry offer on top of Monitor survives the rebuild.
|
||||
for _, e := range existing {
|
||||
if e.Product == p.Key && pruefwerkEntryAddOn(e.Config, s.now()) > 0 {
|
||||
for _, k := range []string{entryAddOnTargetsKey, entryAddOnUntilKey, entryOrderRefKey} {
|
||||
if v, ok := e.Config[k]; ok {
|
||||
cfg[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if seats > 0 {
|
||||
cfg["seats"] = seats
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
func TestPruefwerkLimitsMonitor(t *testing.T) {
|
||||
seven := 7
|
||||
// A saved cap and stray keys in the config change nothing.
|
||||
got := pruefwerkLimits(map[string]interface{}{"skus": []string{"CRA-CSA-CLD-MON"}, "included_targets": 5}, &seven)
|
||||
got := pruefwerkLimits(map[string]interface{}{"skus": []string{"CRA-CSA-CLD-MON"}, "included_targets": 5}, &seven, time.Now())
|
||||
if got[limitIncludedTargets] != 1 || got[limitMonthlyPruefungen] != 10 || got[limitPrueftagCap] != 0 || got[limitExtraTargets] != 0 {
|
||||
t.Errorf("monitor limits: %v", got)
|
||||
}
|
||||
@@ -20,7 +20,7 @@ func TestPruefwerkLimitsMonitor(t *testing.T) {
|
||||
}
|
||||
// The no-SKU default is still Standard, with extras billed.
|
||||
for _, cfg := range []map[string]interface{}{{}, {"skus": []string{}}, {"source": "trial"}} {
|
||||
got := pruefwerkLimits(cfg, nil)
|
||||
got := pruefwerkLimits(cfg, nil, time.Now())
|
||||
if _, extra := got[limitExtraTargets]; got[limitIncludedTargets] != 5 || got[limitMonthlyPruefungen] != 100 || extra {
|
||||
t.Errorf("default %v: %v", cfg, got)
|
||||
}
|
||||
@@ -43,7 +43,7 @@ func TestMonitorFromERP(t *testing.T) {
|
||||
if !tp.Enabled || tp.Config["source"] != "erp" || tp.ExpiresAt != nil {
|
||||
t.Fatalf("grant: %+v", tp)
|
||||
}
|
||||
return pruefwerkLimits(tp.Config, nil)
|
||||
return pruefwerkLimits(tp.Config, nil, time.Now())
|
||||
}
|
||||
}
|
||||
t.Fatal("no Prüfwerk grant")
|
||||
@@ -89,3 +89,89 @@ func TestMonitorFromERP(t *testing.T) {
|
||||
t.Fatalf("monitor after the entry plan: %v", l)
|
||||
}
|
||||
}
|
||||
|
||||
// An entry offer to a Monitor subscriber adds its targets to Monitor until
|
||||
// its end; the subscription's grant keeps source, plan and end.
|
||||
func TestEntryOfferWithMonitor(t *testing.T) {
|
||||
f := newTeamFixture(t, portal, "portal-svc:*")
|
||||
ctx := context.Background()
|
||||
staff := map[string]string{"X-Staff-Subject": "staff-1", "X-Staff-Email": "staff@breakpilot.test"}
|
||||
month := time.Now().Add(30 * 24 * time.Hour).UTC().Format(time.RFC3339)
|
||||
subscribe := func(id, sku, so string) {
|
||||
t.Helper()
|
||||
if _, err := f.s.Store.UpsertSubscription(ctx, store.Subscription{TenantID: id, Product: pruefwerkProduct, SKU: sku,
|
||||
Plan: planForSKU(sku), Seats: 1, Status: store.SubscriptionActive, ErpSalesOrder: so}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
recompute := func(id string) {
|
||||
t.Helper()
|
||||
cur, _ := f.s.Store.GetTenant(ctx, id)
|
||||
if _, err := f.s.recomputeEntitlements(ctx, nil, cur, "test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
grant := func(id string) store.TenantProduct {
|
||||
t.Helper()
|
||||
tps, _ := f.s.Store.ListTenantProducts(ctx, id)
|
||||
for _, tp := range tps {
|
||||
if tp.Product == pruefwerkProduct {
|
||||
return tp
|
||||
}
|
||||
}
|
||||
t.Fatal("no Prüfwerk grant")
|
||||
return store.TenantProduct{}
|
||||
}
|
||||
|
||||
buyer, _ := f.s.Store.CreateTenant(ctx, store.TenantCreate{Slug: "k-00093", Name: "K-00093"})
|
||||
path := "/v1/admin/tenants/" + buyer.ID + "/entry-offer"
|
||||
subscribe(buyer.ID, monitorPlanSKU, "SO-E1")
|
||||
// Booked but not yet provisioned.
|
||||
if code, _, _ := f.call(t, "POST", path, `{"included_targets":2,"expires_at":"`+month+`"}`, staff); code != 409 {
|
||||
t.Fatalf("before the grant exists: %d", code)
|
||||
}
|
||||
recompute(buyer.ID)
|
||||
if code, _, raw := f.call(t, "POST", path, `{"included_targets":2,"expires_at":"`+month+`","order_ref":"RE-2026-0050"}`, staff); code != 200 {
|
||||
t.Fatalf("entry offer to a Monitor subscriber: %d %s", code, raw)
|
||||
}
|
||||
tp := grant(buyer.ID)
|
||||
if tp.Config["source"] != "erp" || tp.ExpiresAt != nil || pruefwerkPlanOfConfig(tp.Config).Name != "monitor" {
|
||||
t.Fatalf("Monitor grant changed: %+v", tp)
|
||||
}
|
||||
if l := pruefwerkLimits(tp.Config, nil, time.Now()); l[limitIncludedTargets] != 3 || l[limitMonthlyPruefungen] != 10 || l[limitExtraTargets] != 0 {
|
||||
t.Fatalf("limits with the offer: %v", l)
|
||||
}
|
||||
// An ERP event keeps it; once it has ended Monitor is one target again.
|
||||
recompute(buyer.ID)
|
||||
tp = grant(buyer.ID)
|
||||
if l := pruefwerkLimits(tp.Config, nil, time.Now()); l[limitIncludedTargets] != 3 || tp.Config[entryOrderRefKey] != "RE-2026-0050" {
|
||||
t.Fatalf("after recompute: %v", tp.Config)
|
||||
}
|
||||
if l := pruefwerkLimits(tp.Config, nil, time.Now().Add(31*24*time.Hour)); l[limitIncludedTargets] != 1 {
|
||||
t.Fatalf("after the offer's end: %v", l)
|
||||
}
|
||||
tp.Config[entryAddOnUntilKey] = time.Now().Add(-time.Hour).UTC().Format(time.RFC3339)
|
||||
if _, err := f.s.Store.UpsertTenantProduct(ctx, tp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
recompute(buyer.ID)
|
||||
if tp = grant(buyer.ID); tp.Config[entryAddOnTargetsKey] != nil || tp.Config[entryAddOnUntilKey] != nil {
|
||||
t.Fatalf("ended offer kept: %v", tp.Config)
|
||||
}
|
||||
// Audited with the Monitor marker.
|
||||
evs, _, _ := f.s.Store.ListAudit(ctx, store.AuditFilter{TenantID: buyer.ID, Action: "entitlement.entry_granted"})
|
||||
if len(evs) != 1 || evs[0].Metadata["with"] != "monitor" || evs[0].Metadata["order_ref"] != "RE-2026-0050" {
|
||||
t.Fatalf("audit: %+v", evs)
|
||||
}
|
||||
|
||||
// Standard next to Monitor: refused as before.
|
||||
subscribe(buyer.ID, "CRA-CSA-CLD-STD", "SO-E2")
|
||||
if code, _, _ := f.call(t, "POST", path, `{"included_targets":1,"expires_at":"`+month+`"}`, staff); code != 409 {
|
||||
t.Fatalf("with Standard booked: %d", code)
|
||||
}
|
||||
// Other plans ignore entry_targets.
|
||||
cfg := map[string]interface{}{"skus": []string{"CRA-CSA-CLD-STD"}, entryAddOnTargetsKey: 4, entryAddOnUntilKey: month}
|
||||
if l := pruefwerkLimits(cfg, nil, time.Now()); l[limitIncludedTargets] != 5 {
|
||||
t.Fatalf("standard with entry_targets: %v", l)
|
||||
}
|
||||
}
|
||||
@@ -106,7 +106,11 @@ const (
|
||||
entryTargetsMax = 10
|
||||
entryTargetsKey = "included_targets"
|
||||
entryOrderRefKey = "order_ref"
|
||||
entryMaxDuration = 200 * 24 * time.Hour
|
||||
// An entry offer to a Monitor subscriber adds its targets to the
|
||||
// subscription's grant until entry_until (pruefwerkEntryAddOn).
|
||||
entryAddOnTargetsKey = "entry_targets"
|
||||
entryAddOnUntilKey = "entry_until"
|
||||
entryMaxDuration = 200 * 24 * time.Hour
|
||||
)
|
||||
|
||||
// pruefwerkEntryPlan is the entry plan with one included target; the grant's
|
||||
@@ -195,9 +199,26 @@ func entryTargets(v interface{}) (int, bool) {
|
||||
return n, n >= 1 && n <= entryTargetsMax
|
||||
}
|
||||
|
||||
// pruefwerkEntryAddOn is the number of targets a running entry offer adds to
|
||||
// a Monitor grant at now: entry_targets (1..entryTargetsMax) while
|
||||
// entry_until is ahead, else 0.
|
||||
func pruefwerkEntryAddOn(cfg map[string]interface{}, now time.Time) int {
|
||||
n, ok := entryTargets(cfg[entryAddOnTargetsKey])
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
s, _ := cfg[entryAddOnUntilKey].(string)
|
||||
until, err := time.Parse(time.RFC3339, s)
|
||||
if err != nil || !until.After(now) {
|
||||
return 0
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// pruefwerkLimits is the stored config plus the plan's limits and the
|
||||
// workspace's cap, as a new map: the stored config stays as it is.
|
||||
func pruefwerkLimits(cfg map[string]interface{}, prueftagCap *int) map[string]interface{} {
|
||||
// workspace's cap, as a new map: the stored config stays as it is. On
|
||||
// Monitor, a running entry offer adds its targets (pruefwerkEntryAddOn).
|
||||
func pruefwerkLimits(cfg map[string]interface{}, prueftagCap *int, now time.Time) map[string]interface{} {
|
||||
out := make(map[string]interface{}, len(cfg)+3)
|
||||
for k, v := range cfg {
|
||||
out[k] = v
|
||||
@@ -210,6 +231,9 @@ func pruefwerkLimits(cfg map[string]interface{}, prueftagCap *int) map[string]in
|
||||
return out
|
||||
}
|
||||
out[limitIncludedTargets] = plan.IncludedTargets
|
||||
if plan.Name == pruefwerkMonitorPlan.Name {
|
||||
out[limitIncludedTargets] = plan.IncludedTargets + pruefwerkEntryAddOn(cfg, now)
|
||||
}
|
||||
out[limitMonthlyPruefungen] = plan.MonthlyPruefungen
|
||||
if plan.NoExtraTargets {
|
||||
out[limitExtraTargets] = 0
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.breakpilot.com/platform/tenant-registry/internal/authn"
|
||||
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
||||
@@ -67,7 +68,7 @@ func TestPruefwerkLimits(t *testing.T) {
|
||||
if c.skus != nil {
|
||||
cfg["skus"] = c.skus
|
||||
}
|
||||
got := pruefwerkLimits(cfg, c.cap)
|
||||
got := pruefwerkLimits(cfg, c.cap, time.Now())
|
||||
if len(cfg) != 2+boolInt(c.skus != nil) {
|
||||
t.Fatalf("%v: stored config changed: %+v", c.skus, cfg)
|
||||
}
|
||||
@@ -250,14 +251,14 @@ func TestPruefwerkLimitsEntry(t *testing.T) {
|
||||
if c.targets != nil {
|
||||
cfg["included_targets"] = c.targets
|
||||
}
|
||||
got := pruefwerkLimits(cfg, c.cap)
|
||||
got := pruefwerkLimits(cfg, c.cap, time.Now())
|
||||
if got[limitIncludedTargets] != c.want || got[limitMonthlyPruefungen] != 0 || got[limitPrueftagCap] != 0 || got[limitExtraTargets] != 0 {
|
||||
t.Errorf("%s: %v / %v / %v / %v, want %d / 0 / 0 / 0", c.name, got[limitIncludedTargets], got[limitMonthlyPruefungen], got[limitPrueftagCap], got[limitExtraTargets], c.want)
|
||||
}
|
||||
}
|
||||
// The paid plans keep billing extras: no extra_targets, even one left in the config.
|
||||
for _, sku := range []string{"CRA-CSA-CLD-STD", "CRA-CSA-CLD-BUS", "CRA-CSA-CLD-ENT", ""} {
|
||||
got := pruefwerkLimits(map[string]interface{}{"skus": []string{sku}, limitExtraTargets: 0}, nil)
|
||||
got := pruefwerkLimits(map[string]interface{}{"skus": []string{sku}, limitExtraTargets: 0}, nil, time.Now())
|
||||
if _, ok := got[limitExtraTargets]; ok {
|
||||
t.Errorf("%q sends extra_targets: %v", sku, got[limitExtraTargets])
|
||||
}
|
||||
|
||||
+9
-4
@@ -1841,9 +1841,14 @@ paths:
|
||||
A trial workspace becomes active. Sending it again replaces
|
||||
targets, end and reference (staff set the end to 90 days after
|
||||
delivery). While the grant runs, an ERP event without
|
||||
subscriptions does not halt the workspace. Refused (409) for a
|
||||
workspace with a Prüfwerk subscription, or one that is neither
|
||||
trial nor active. Needs a named staff member (X-Staff-Subject).
|
||||
subscriptions does not halt the workspace. A workspace whose only
|
||||
Prüfwerk subscription is Monitor (CRA-CSA-CLD-MON) keeps it: the
|
||||
offer's targets are added to Monitor's grant as entry_targets until
|
||||
expires_at (entry_until), and the entitlement check sends Monitor's
|
||||
one target plus those. Refused (409) for any other Prüfwerk
|
||||
subscription (already_booked), a Monitor subscription without its
|
||||
grant yet (not_provisioned), or a workspace neither trial nor
|
||||
active. Needs a named staff member (X-Staff-Subject).
|
||||
parameters:
|
||||
- { in: path, name: id, required: true, schema: { type: string, format: uuid } }
|
||||
requestBody:
|
||||
@@ -1865,7 +1870,7 @@ paths:
|
||||
"400": { $ref: "#/components/responses/BadRequest" }
|
||||
"403": { description: "No named staff member." }
|
||||
"404": { $ref: "#/components/responses/NotFound" }
|
||||
"409": { description: "Prüfwerk subscription, or a workspace neither trial nor active." }
|
||||
"409": { description: "already_booked (a Prüfwerk subscription other than Monitor), not_provisioned, or invalid_status." }
|
||||
|
||||
/v1/admin/tenants/{id}/preview-grants/{product}:
|
||||
delete:
|
||||
|
||||
Reference in new issue
Block a user