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..0401575 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 { @@ -576,6 +613,12 @@ type trialContract struct { AcceptingPersonName string `json:"accepting_person_name"` AcceptingEmail string `json:"accepting_email"` CompanyName string `json:"company_name"` + // The address the offer was made with, so the company profile can + // suggest it as the master data (portal#159, item 3). + Street string `json:"street"` + Postcode string `json:"postcode"` + City string `json:"city"` + Country string `json:"country"` } type contractStatus struct { @@ -622,6 +665,7 @@ func (s *Server) listTenantContracts(w http.ResponseWriter, r *http.Request) { TrialStartedAt: o.TrialStartedAt, TrialExpiresAt: end, TermsVersion: o.TermsVersion, TermsDate: date, TermsSHA256: o.TermsSHA256, AcceptingPersonName: o.FirstName + " " + o.LastName, AcceptingEmail: o.Email, CompanyName: o.CompanyName, + Street: o.Street, Postcode: o.Postcode, City: o.City, Country: o.Country, } } else if err != nil && !errors.Is(err, store.ErrNotFound) { writeError(w, http.StatusInternalServerError, "internal", err.Error()) diff --git a/internal/server/trialoffers_test.go b/internal/server/trialoffers_test.go index 2838630..1592a08 100644 --- a/internal/server/trialoffers_test.go +++ b/internal/server/trialoffers_test.go @@ -2,6 +2,7 @@ package server_test import ( "bytes" + "context" "encoding/json" "io" "net/http" @@ -201,6 +202,12 @@ func TestTrialOffer_offerConfirmActivate(t *testing.T) { c.AVV.Status != "not_concluded" || c.TOMs.Status != "pending" || c.SaaS.Status != "not_concluded" { t.Fatalf("contracts %d %s", resp.StatusCode, raw) } + // the address of the offer, for the company profile's suggestions + for _, want := range []string{`"street":"Hauptstr. 1"`, `"postcode":"78224"`, `"city":"Singen"`, `"country":"DE"`} { + if !strings.Contains(string(raw), want) { + t.Fatalf("contracts lack %s: %s", want, raw) + } + } resp, raw = h.do("GET", "/v1/tenants/"+tn.ID+"/contracts/trial/terms.pdf", nil) if resp.StatusCode != 200 || !bytes.Equal(raw, v.PDF) || !strings.HasPrefix(resp.Header.Get("Content-Disposition"), "attachment") { t.Fatalf("contract pdf %d %v", resp.StatusCode, resp.Header) @@ -262,6 +269,63 @@ 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) + } + + // staff extended the trial: the status shows the later end + before, _ := st["trial_expires_at"].(string) + tn, err := h.store.GetTenantBySlug(context.Background(), st["tenant_slug"].(string)) + if err != nil { + t.Fatal(err) + } + req, _ := http.NewRequest("POST", h.srv.URL+"/v1/admin/tenants/"+tn.ID+"/trial", strings.NewReader(`{"days":30}`)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Portal-User-Email", "staff@breakpilot.com") + ext, err := http.DefaultClient.Do(req) + if err != nil || ext.StatusCode != 200 { + t.Fatalf("extend %v %v", err, ext) + } + _ = ext.Body.Close() + _, raw = h.do("GET", "/v1/trial-offers/"+id, nil) + st = map[string]any{} + _ = json.Unmarshal(raw, &st) + if after, _ := st["trial_expires_at"].(string); after == "" || after <= before { + t.Fatalf("extended end not shown: before %q, now %s", before, raw) + } + }) +} + func TestTrialOffer_slugCollisionAndFailedActivation(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { rec := &mail.Recorder{}