Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
127 lines
4.9 KiB
Go
127 lines
4.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
const offerSelect = `
|
|
SELECT id::text, company_name, street, postcode, city, country, first_name, last_name, email,
|
|
terms_document_id, terms_version, terms_sha256, notice_version, flow_version,
|
|
contract_language, terms_accepted, offered_at,
|
|
state, verify_token_hash, verify_expires_at, receipt_mail_sent_at, verified_at,
|
|
COALESCE(tenant_id::text, ''), contract_accepted_at, trial_started_at, trial_expires_at,
|
|
activation_mail_sent_at
|
|
FROM trial_offers `
|
|
|
|
func scanOffer(row pgx.Row) (*TrialOffer, error) {
|
|
var o TrialOffer
|
|
err := row.Scan(&o.ID, &o.CompanyName, &o.Street, &o.Postcode, &o.City, &o.Country,
|
|
&o.FirstName, &o.LastName, &o.Email,
|
|
&o.TermsDocumentID, &o.TermsVersion, &o.TermsSHA256, &o.NoticeVersion, &o.FlowVersion,
|
|
&o.ContractLanguage, &o.TermsAccepted, &o.OfferedAt,
|
|
&o.State, &o.VerifyTokenHash, &o.VerifyExpiresAt, &o.ReceiptMailSentAt, &o.VerifiedAt,
|
|
&o.TenantID, &o.ContractAcceptedAt, &o.TrialStartedAt, &o.TrialExpiresAt,
|
|
&o.ActivationMailSentAt)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &o, nil
|
|
}
|
|
|
|
func (p *Postgres) CreateTrialOffer(ctx context.Context, o TrialOffer) (*TrialOffer, error) {
|
|
if o.ContractLanguage == "" {
|
|
o.ContractLanguage = "de"
|
|
}
|
|
row := p.pool.QueryRow(ctx, `
|
|
INSERT INTO trial_offers (company_name, street, postcode, city, country, first_name, last_name, email,
|
|
terms_document_id, terms_version, terms_sha256, notice_version, flow_version, contract_language,
|
|
terms_accepted, offered_at, verify_token_hash, verify_expires_at)
|
|
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18)
|
|
RETURNING id::text`,
|
|
o.CompanyName, o.Street, o.Postcode, o.City, o.Country, o.FirstName, o.LastName, o.Email,
|
|
o.TermsDocumentID, o.TermsVersion, o.TermsSHA256, o.NoticeVersion, o.FlowVersion, o.ContractLanguage,
|
|
o.TermsAccepted, o.OfferedAt, o.VerifyTokenHash, o.VerifyExpiresAt)
|
|
var id string
|
|
if err := row.Scan(&id); err != nil {
|
|
if isUniqueViolation(err) {
|
|
return nil, ErrConflict
|
|
}
|
|
return nil, err
|
|
}
|
|
return p.GetTrialOffer(ctx, id)
|
|
}
|
|
|
|
func (p *Postgres) GetTrialOffer(ctx context.Context, id string) (*TrialOffer, error) {
|
|
o, err := scanOffer(p.pool.QueryRow(ctx, offerSelect+`WHERE id = $1::uuid`, id))
|
|
if isInvalidUUID(err) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return o, err
|
|
}
|
|
|
|
func (p *Postgres) GetTrialOfferByToken(ctx context.Context, tokenHash string) (*TrialOffer, error) {
|
|
return scanOffer(p.pool.QueryRow(ctx, offerSelect+`WHERE verify_token_hash = $1`, tokenHash))
|
|
}
|
|
|
|
func (p *Postgres) GetTrialOfferByTenant(ctx context.Context, tenantID string) (*TrialOffer, error) {
|
|
o, err := scanOffer(p.pool.QueryRow(ctx, offerSelect+`WHERE tenant_id = $1::uuid ORDER BY offered_at DESC LIMIT 1`, tenantID))
|
|
if isInvalidUUID(err) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return o, err
|
|
}
|
|
|
|
func (p *Postgres) LatestTrialOffer(ctx context.Context, email string) (*TrialOffer, error) {
|
|
return scanOffer(p.pool.QueryRow(ctx, offerSelect+`WHERE lower(email) = lower($1) ORDER BY offered_at DESC LIMIT 1`, email))
|
|
}
|
|
|
|
// moveOffer runs a guarded UPDATE … RETURNING; no row means the offer is
|
|
// missing (ErrNotFound) or in another state (ErrConflict).
|
|
func (p *Postgres) moveOffer(ctx context.Context, id, set, where string, args ...any) (*TrialOffer, error) {
|
|
all := append([]any{id}, args...)
|
|
tag, err := p.pool.Exec(ctx, `UPDATE trial_offers SET `+set+` WHERE id = $1::uuid AND `+where, all...)
|
|
if isInvalidUUID(err) {
|
|
return nil, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
if _, gerr := p.GetTrialOffer(ctx, id); gerr != nil {
|
|
return nil, gerr
|
|
}
|
|
return nil, ErrConflict
|
|
}
|
|
return p.GetTrialOffer(ctx, id)
|
|
}
|
|
|
|
func (p *Postgres) SetTrialOfferToken(ctx context.Context, id, tokenHash string, expires time.Time) (*TrialOffer, error) {
|
|
return p.moveOffer(ctx, id, `verify_token_hash = $2, verify_expires_at = $3`, `state = 'email_verification_pending'`, tokenHash, expires)
|
|
}
|
|
|
|
func (p *Postgres) VerifyTrialOffer(ctx context.Context, id string, at time.Time) (*TrialOffer, error) {
|
|
return p.moveOffer(ctx, id, `state = 'verified', verified_at = $2`, `state = 'email_verification_pending'`, at)
|
|
}
|
|
|
|
func (p *Postgres) ActivateTrialOffer(ctx context.Context, id, tenantID string, acceptedAt, expiresAt time.Time) (*TrialOffer, error) {
|
|
return p.moveOffer(ctx, id,
|
|
`state = 'activated', tenant_id = $2::uuid, contract_accepted_at = $3, trial_started_at = $3, trial_expires_at = $4`,
|
|
`state = 'verified'`, tenantID, acceptedAt, expiresAt)
|
|
}
|
|
|
|
func (p *Postgres) MarkTrialOfferMail(ctx context.Context, id, which string, at time.Time) error {
|
|
col := map[string]string{"receipt": "receipt_mail_sent_at", "activation": "activation_mail_sent_at"}[which]
|
|
if col == "" {
|
|
return ErrConflict
|
|
}
|
|
_, err := p.moveOffer(ctx, id, col+` = $2`, `true`, at)
|
|
return err
|
|
}
|