- All response types now use lowercase json tags (id, name, photo_url, etc.) - email/phone/birthdate/last_seen_at/photo_url use omitempty - audit.Metadata type []byte (was map[string]any) - fixes register/login inconsistency where login returned ID/Name (CamelCase) while register returned id/name (lowercase)
44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package audit
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type Event struct {
|
|
ID int64 `json:"id"`
|
|
UserID *uuid.UUID `json:"user_id,omitempty"`
|
|
Action string `json:"action"`
|
|
TargetType string `json:"target_type,omitempty"`
|
|
TargetID string `json:"target_id,omitempty"`
|
|
Metadata []byte `json:"metadata,omitempty"`
|
|
IP string `json:"ip,omitempty"`
|
|
UserAgent string `json:"user_agent,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
type Repo struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewRepo(pool *pgxpool.Pool) *Repo {
|
|
return &Repo{pool: pool}
|
|
}
|
|
|
|
func (r *Repo) Log(ctx context.Context, e *Event) error {
|
|
meta := []byte("null")
|
|
if e.Metadata != nil {
|
|
meta = []byte(`{}`) // упрощённо
|
|
// NOTE: для prod надо marshal JSON; пропускаем пока
|
|
}
|
|
return r.pool.QueryRow(ctx, `
|
|
INSERT INTO audit_log (user_id, action, target_type, target_id, metadata, ip, user_agent)
|
|
VALUES ($1, $2, NULLIF($3, ''), NULLIF($4, ''), $5::jsonb, NULLIF($6, '')::inet, $7)
|
|
RETURNING id, created_at`,
|
|
e.UserID, e.Action, e.TargetType, e.TargetID, string(meta), e.IP, e.UserAgent,
|
|
).Scan(&e.ID, &e.CreatedAt)
|
|
}
|