veza/veza-backend-api/internal/models/user.go
senke 3f326e8266
Some checks failed
Veza CI / Rust (Stream Server) (push) Successful in 4m22s
Security Scan / Secret Scanning (gitleaks) (push) Successful in 1m5s
Veza CI / Frontend (Web) (push) Failing after 17m19s
E2E Playwright / e2e (full) (push) Failing after 20m28s
Veza CI / Backend (Go) (push) Successful in 21m31s
Veza CI / Notify on failure (push) Successful in 4s
fix(ci): unblock CI red — gofmt + e2e webserver reuse + orders.hyperswitch_payment_id (Day 4)
Three pre-existing infra issues surfaced by the Day 1→Day 3 push wave.
Each is independent — bundled here because the goal is "ci.yml + e2e.yml
green" before the v1.0.9 tag, and they're all small.

(1) gofmt — ci.yml golangci-lint v2 step

  Five files were unformatted on main. Pre-existing (untouched by my
  Item G work, but the formatter caught them now):
    - internal/api/router.go
    - internal/core/marketplace/reconcile_hyperswitch_test.go
    - internal/models/user.go
    - internal/monitoring/ledger_metrics.go
    - internal/monitoring/ledger_metrics_test.go
  Pure whitespace via `gofmt -w` — no behavior change.

(2) e2e silent-fail — playwright webServer port collision

  The e2e workflow pre-starts the backend in step 9 ("Build + start
  backend API") so it can fail-fast on a non-ok health check. But
  playwright.config.ts had `reuseExistingServer: !process.env.CI` on
  the backend webServer entry — meaning in CI Playwright tried to
  spawn a SECOND backend on port 18080. The spawn collided with
  EADDRINUSE and Playwright silently exited before printing any test
  output. The artifact upload then warned "No files were found"
  because tests/e2e/playwright-report/ never got written, and the job
  ended in `Failure` for an unrelated reason (the artifact upload
  step's GHESNotSupportedError).

  Fix: backend `reuseExistingServer: true` always — workflow + dev
  both pre-start backend on 18080. Vite stays `!CI` because the
  workflow doesn't pre-start it. Comment in playwright.config.ts
  documents the symptom so the next person debugging gets the
  pointer immediately.

(3) orders.hyperswitch_payment_id missing in fresh DBs — migration 080
    skip-branch + 099 ordering drift

  Migration 080 (`add_payment_fields`) wraps its ALTERs in
  "skip if orders doesn't exist". At authoring time orders existed
  earlier in the migration sequence; that ordering has since shifted
  (orders is now created at 099_z_create_orders.sql, AFTER 080).
  Result: in any freshly-migrated DB (CI, fresh dev, future restore
  drills) migration 080 takes the skip branch and the columns are
  never added — even though the Order model and the marketplace code
  rely on them.

  Symptom: every CI run logs
    pq: column "hyperswitch_payment_id" does not exist
  from the periodic ledger_metrics worker. Order checkout would also
  fail to persist payment_id at write time, breaking reconciliation.

  Fix: append-only migration 987 with idempotent
  `ADD COLUMN IF NOT EXISTS` + a partial index on the reconciliation
  hot path. Production envs that did pick up 080 in the original
  order are no-ops; fresh envs converge to the same end state.
  Rollback in migrations/rollback/.

Verified locally:
  $ cd veza-backend-api && go build ./... && VEZA_SKIP_INTEGRATION=1 \
      go test -short -count=1 ./internal/...
  (all green)

SKIP_TESTS=1: backend-only Go + Playwright config + SQL. Frontend
unit tests irrelevant to this commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 12:03:55 +02:00

84 lines
4.2 KiB
Go

package models
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// User représente un utilisateur dans le système
// MIGRATION UUID: User.ID est maintenant un UUID pour cohérence Go↔Rust et alignment ORIGIN
type User struct {
ID uuid.UUID `gorm:"type:uuid;primary_key" json:"id" db:"id"`
Username string `gorm:"not null;size:30" json:"username" db:"username"`
Slug string `gorm:"size:255" json:"slug" db:"slug"`
Email string `gorm:"not null;size:255" json:"email" db:"email"`
PasswordHash string `gorm:"size:255" json:"-" db:"password_hash"`
Password string `gorm:"-" json:"password,omitempty"` // Virtual field for input
TokenVersion int `gorm:"default:0;not null" json:"token_version" db:"token_version"`
FirstName string `gorm:"size:100" json:"first_name" db:"first_name"`
LastName string `gorm:"size:100" json:"last_name" db:"last_name"`
Avatar string `gorm:"type:text" json:"avatar" db:"avatar"`
BannerURL string `gorm:"type:text" json:"banner_url" db:"banner_url"`
Bio string `gorm:"type:text" json:"bio" db:"bio"`
Location string `gorm:"size:100" json:"location" db:"location"`
Birthdate *time.Time `json:"birthdate" db:"birthdate"`
Gender string `gorm:"size:20" json:"gender" db:"gender"`
UsernameChangedAt *time.Time `json:"username_changed_at" db:"username_changed_at"`
Role string `gorm:"type:user_role;not null;default:'user'" json:"role" db:"role"`
IsActive bool `gorm:"default:true" json:"is_active" db:"is_active"`
IsVerified bool `gorm:"default:false" json:"is_verified" db:"is_verified"`
IsBanned bool `gorm:"default:false;not null" json:"is_banned" db:"is_banned"`
IsAdmin bool `gorm:"default:false" json:"is_admin" db:"is_admin"`
IsPublic bool `gorm:"default:true" json:"is_public" db:"is_public"`
LastLoginAt *time.Time `json:"last_login_at" db:"last_login_at"`
LoginCount int `gorm:"default:0;not null" json:"login_count" db:"login_count"`
PasswordChangedAt *time.Time `json:"password_changed_at,omitempty" db:"password_changed_at"` // F016: Password expiration tracking
// v1.0.6: set the first time a user self-promotes to `role='creator'`
// via POST /api/v1/users/me/upgrade-creator. NULL for users who never
// took that path (still 'user', or promoted by an admin out-of-band).
PromotedToCreatorAt *time.Time `json:"promoted_to_creator_at,omitempty" db:"promoted_to_creator_at"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" db:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" db:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
SocialLinks string `gorm:"type:jsonb;default:'{}'" json:"social_links" db:"social_links"`
// Relations
Roles []Role `gorm:"many2many:user_roles;" json:"-"`
TrackLikes []TrackLike `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"-"`
}
// BeforeCreate hook GORM pour générer UUID si non défini
func (u *User) BeforeCreate(tx *gorm.DB) error {
if u.ID == uuid.Nil {
u.ID = uuid.New()
}
return nil
}
// TableName définit le nom de la table pour GORM
func (User) TableName() string {
return "users"
}
// SellableContent représente du contenu vendable
// MIGRATION UUID: UserID migré vers UUID
type SellableContent struct {
ID uuid.UUID `json:"id" gorm:"type:uuid;primaryKey"`
UserID uuid.UUID `gorm:"type:uuid;not null" json:"user_id" db:"user_id"`
Title string `json:"title" db:"title"`
Description string `json:"description" db:"description"`
Price float64 `json:"price" db:"price"`
IsActive bool `json:"is_active" db:"is_active"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
// BeforeCreate hook GORM pour générer UUID si non défini
func (m *SellableContent) BeforeCreate(tx *gorm.DB) error {
if m.ID == uuid.Nil {
m.ID = uuid.New()
}
return nil
}