diff --git a/internal/server/server.go b/internal/server/server.go index e006209..b70cdb5 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -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): ".pdf". mux.HandleFunc("GET /v1/tenants/{id}/contracts/documents/{file}", s.tenantContractDocumentPDF) diff --git a/internal/server/trialoffers.go b/internal/server/trialoffers.go index 6a185c8..e497128 100644 --- a/internal/server/trialoffers.go +++ b/internal/server/trialoffers.go @@ -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 { diff --git a/internal/server/trialoffers_test.go b/internal/server/trialoffers_test.go index 2838630..a07432e 100644 --- a/internal/server/trialoffers_test.go +++ b/internal/server/trialoffers_test.go @@ -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{}