feat(trial): GET /v1/trial-offers/{id} for the waiting page
ci / shared (pull_request) Successful in 11s
ci / test (pull_request) Successful in 5m33s
ci / image (pull_request) Skipped

The page that sent the offer waits for the e-mail confirmation. When
the link is opened on another device (a phone), that page stayed put
(portal#159, item 1). The status read lets it follow: the state and,
once active, the workspace slug and the trial dates. No e-mail or
company data in the answer; the offer id, which only the sending
browser has, is the key. Same authz group as the other offer routes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sharang ParnerkarandClaude Opus 5.5 committed 2026-11-12 09:23:05 +01:00
1 parent 63ea5790c7
commit 0f8cc33888
3 files changed
+74

No files matched your search

+1
View File
@@ -146,6 +146,7 @@ func NewRouter(s *Server) http.Handler {
mux.HandleFunc("POST /v1/trial-offers", s.createTrialOffer)
mux.HandleFunc("POST /v1/trial-offers/verify", s.verifyTrialOffer)
mux.HandleFunc("POST /v1/trial-offers/{id}/resend", s.resendTrialOffer)
mux.HandleFunc("GET /v1/trial-offers/{id}", s.trialOfferStatus)
mux.HandleFunc("GET /v1/tenants/{id}/contracts/trial/terms.pdf", s.tenantTrialTermsPDF)
// Contract documents (AGB, AVV, TOM, Leistungsbeschreibungen): "<doc>.pdf".
mux.HandleFunc("GET /v1/tenants/{id}/contracts/documents/{file}", s.tenantContractDocumentPDF)
+37
View File
@@ -306,6 +306,43 @@ func (s *Server) resendTrialOffer(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusAccepted, trialOfferResp{OfferID: o.ID, State: o.State, Email: o.Email, OfferedAt: o.OfferedAt, ReceiptMailSent: sent})
}
// trialOfferStatus is GET /v1/trial-offers/{id} (portal-svc): where the offer
// stands, so the page that sent it can follow a confirmation made on another
// device (the link opened on a phone) instead of waiting for a reload. The
// offer id, which only the sending browser has, is the key; the answer holds
// nothing beyond the state and, once active, the workspace and the dates.
func (s *Server) trialOfferStatus(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
o, err := s.Store.GetTrialOffer(ctx, r.PathValue("id"))
if err != nil {
if mapStoreError(w, err) {
return
}
writeError(w, http.StatusInternalServerError, "internal", err.Error())
return
}
resp := trialOfferStatusResp{OfferID: o.ID, State: o.State}
if o.State == store.OfferActivated {
resp.TrialStartedAt, resp.TrialExpiresAt = o.TrialStartedAt, o.TrialExpiresAt
if t, err := s.Store.GetTenant(ctx, o.TenantID); err == nil {
resp.TenantSlug = t.Slug
if t.TrialEndsAt != nil && o.TrialExpiresAt != nil && t.TrialEndsAt.After(*o.TrialExpiresAt) {
resp.TrialExpiresAt = t.TrialEndsAt // extended by staff
}
}
}
writeJSON(w, http.StatusOK, resp)
}
type trialOfferStatusResp struct {
OfferID string `json:"offer_id"`
State string `json:"state"`
TenantSlug string `json:"tenant_slug,omitempty"`
TrialStartedAt *time.Time `json:"trial_started_at,omitempty"`
TrialExpiresAt *time.Time `json:"trial_expires_at,omitempty"`
}
// ─── confirmation and activation ───────────────────────────────────────────
type verifyReq struct {
+36
View File
@@ -262,6 +262,42 @@ func TestTrialOffer_expiredLinkAndResend(t *testing.T) {
})
}
func TestTrialOffer_status(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
rec := &mail.Recorder{}
h.app.Mail = rec
_, raw := h.do("POST", "/v1/trial-offers", trialOffer("phone@firma.de"))
var offer map[string]string
_ = json.Unmarshal(raw, &offer)
id := offer["offer_id"]
resp, raw := h.do("GET", "/v1/trial-offers/"+id, nil)
var st map[string]any
_ = json.Unmarshal(raw, &st)
if resp.StatusCode != 200 || st["state"] != "email_verification_pending" || st["tenant_slug"] != nil || st["trial_expires_at"] != nil {
t.Fatalf("pending %d %s", resp.StatusCode, raw)
}
if strings.Contains(string(raw), "phone@firma.de") {
t.Fatalf("status leaks the e-mail address: %s", raw)
}
// confirmed on another device: the sending page sees it
if resp, raw = h.do("POST", "/v1/trial-offers/verify", map[string]string{"token": lastToken(t, rec)}); resp.StatusCode != 200 {
t.Fatalf("verify %d %s", resp.StatusCode, raw)
}
resp, raw = h.do("GET", "/v1/trial-offers/"+id, nil)
st = map[string]any{}
_ = json.Unmarshal(raw, &st)
if resp.StatusCode != 200 || st["state"] != "activated" || st["tenant_slug"] == nil || st["tenant_slug"] == "" || st["trial_expires_at"] == nil {
t.Fatalf("activated %d %s", resp.StatusCode, raw)
}
if resp, _ = h.do("GET", "/v1/trial-offers/does-not-exist", nil); resp.StatusCode != 404 {
t.Fatalf("unknown offer %d", resp.StatusCode)
}
})
}
func TestTrialOffer_slugCollisionAndFailedActivation(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
rec := &mail.Recorder{}