chore: finish profile
This commit is contained in:
@@ -18,7 +18,6 @@ import (
|
||||
"github.com/omnex/control-plane/api/internal/leads"
|
||||
"github.com/omnex/control-plane/api/internal/profile"
|
||||
"github.com/omnex/control-plane/api/internal/router"
|
||||
"github.com/omnex/control-plane/api/internal/sav"
|
||||
"github.com/omnex/control-plane/api/internal/session"
|
||||
"github.com/omnex/control-plane/api/internal/sub"
|
||||
)
|
||||
@@ -69,7 +68,6 @@ func main() {
|
||||
var demoStore demos.Store
|
||||
var demoPool demos.Pool
|
||||
var codeStore sub.Store
|
||||
var contactStore sav.Store
|
||||
var profileStore profile.Store
|
||||
if cfg.DatabaseURL != "" {
|
||||
gdb, err := db.Open(cfg.DatabaseURL)
|
||||
@@ -89,7 +87,6 @@ func main() {
|
||||
leadStore = leads.NewGormStore(gdb)
|
||||
demoStore = demos.NewGormStore(gdb)
|
||||
demoPool = demos.NewGormPool(gdb)
|
||||
contactStore = sav.NewGormStore(gdb)
|
||||
profileStore = profile.NewGormStore(gdb)
|
||||
log.Printf("persistance: PostgreSQL (GORM)")
|
||||
} else {
|
||||
@@ -98,7 +95,6 @@ func main() {
|
||||
leadStore = leads.NewMemStore()
|
||||
demoStore = demos.NewMemStore()
|
||||
demoPool = demos.NewMemPool()
|
||||
contactStore = sav.NewMemContactStore()
|
||||
log.Printf("persistance: mémoire (dev — définir OMNEX_DATABASE_URL pour PostgreSQL)")
|
||||
}
|
||||
|
||||
@@ -128,7 +124,6 @@ func main() {
|
||||
LeadsH: leads.NewHandler(leadStore),
|
||||
DemosH: demos.NewHandler(demoSvc, helmProv),
|
||||
SubH: sub.NewHandler(codeStore),
|
||||
ContactH: sav.NewHandler(contactStore),
|
||||
ProfileH: profile.NewHandler(profileStore),
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/omnex/control-plane/api/internal/auth"
|
||||
"github.com/omnex/control-plane/api/internal/demos"
|
||||
"github.com/omnex/control-plane/api/internal/leads"
|
||||
"github.com/omnex/control-plane/api/internal/sav"
|
||||
"github.com/omnex/control-plane/api/internal/sub"
|
||||
)
|
||||
|
||||
@@ -45,7 +44,6 @@ func AutoMigrate(gdb *gorm.DB) error {
|
||||
&leads.Lead{},
|
||||
&demos.Demo{},
|
||||
&demos.ExternalResource{},
|
||||
&sav.Contact{},
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ func NewGormStore(db *gorm.DB) *GormStore {
|
||||
type Store interface {
|
||||
UpdateUsername(id, newUsername string) (auth.User, error)
|
||||
UpdatePassword(id, passwordHahs string) (auth.User, error)
|
||||
GetTelegram(id string) (auth.User, error)
|
||||
SetTelegram(id, telegram string) (auth.User, error)
|
||||
}
|
||||
|
||||
func (s *GormStore) UpdateUsername(id, newUsername string) (auth.User, error) {
|
||||
@@ -36,8 +38,16 @@ func (s *GormStore) UpdateUsername(id, newUsername string) (auth.User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *GormStore) UpdatePassword(id, passwordHash string) (auth.User, error) {
|
||||
result := s.db.Model(&auth.User{}).Where("id = ?", id).Update("password", passwordHash)
|
||||
func (s *GormStore) GetTelegram(id string) (auth.User, error) {
|
||||
var user auth.User
|
||||
if err := s.db.First(&user, "id = ?", id).Error; err != nil {
|
||||
return auth.User{}, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *GormStore) SetTelegram(id, telegram string) (auth.User, error) {
|
||||
result := s.db.Model(&auth.User{}).Where("id = ?", id).Update("telegram", telegram)
|
||||
if result.Error != nil {
|
||||
return auth.User{}, result.Error
|
||||
}
|
||||
|
||||
@@ -23,6 +23,10 @@ type newPassword struct {
|
||||
Password string `json:"password" binding:"required,min=8"`
|
||||
}
|
||||
|
||||
type newTelegram struct {
|
||||
Telegram string `json:"telegram" binding:"required,min=3,max=64"`
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateUsernameById(c *gin.Context) {
|
||||
var u newUsername
|
||||
|
||||
@@ -73,3 +77,36 @@ func (h *Handler) UpdatePasswordById(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
func (h *Handler) GetTelegramById(c *gin.Context) {
|
||||
id, ok := c.MustGet("id").(string)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "id invalide"})
|
||||
return
|
||||
}
|
||||
user, err := h.store.GetTelegram(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"telegram": user.Telegram})
|
||||
}
|
||||
|
||||
func (h *Handler) SetTelegramById(c *gin.Context) {
|
||||
var t newTelegram
|
||||
if err := c.ShouldBindJSON(&t); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
||||
return
|
||||
}
|
||||
id, ok := c.MustGet("id").(string)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "id invalide"})
|
||||
return
|
||||
}
|
||||
user, err := h.store.SetTelegram(id, t.Telegram)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "erreur serveur"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"telegram": user.Telegram})
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/omnex/control-plane/api/internal/httpx"
|
||||
"github.com/omnex/control-plane/api/internal/leads"
|
||||
"github.com/omnex/control-plane/api/internal/profile"
|
||||
"github.com/omnex/control-plane/api/internal/sav"
|
||||
"github.com/omnex/control-plane/api/internal/session"
|
||||
"github.com/omnex/control-plane/api/internal/sub"
|
||||
)
|
||||
@@ -26,7 +25,6 @@ type Deps struct {
|
||||
LeadsH *leads.Handler
|
||||
DemosH *demos.Handler
|
||||
SubH *sub.Handler
|
||||
ContactH *sav.Handler
|
||||
ProfileH *profile.Handler
|
||||
}
|
||||
|
||||
@@ -48,7 +46,6 @@ func New(d Deps) *gin.Engine {
|
||||
api.POST("/auth/login", httpx.RateLimit(1, 5), d.AuthH.Login)
|
||||
api.POST("/auth/register", httpx.RateLimit(0.2, 3), d.AuthH.Register)
|
||||
api.POST("/leads", httpx.RateLimit(1, 3), d.LeadsH.Create)
|
||||
api.POST("/send/message", httpx.RateLimit(1, 3), d.ContactH.CallSupport)
|
||||
// Toute route authentifiée (session valide, n'importe quel rôle).
|
||||
authed := api.Group("")
|
||||
authed.Use(auth.RequireAuth(d.Issuer, d.Sessions))
|
||||
@@ -65,6 +62,8 @@ func New(d Deps) *gin.Engine {
|
||||
client.POST("/subscription", d.SubH.AddCode)
|
||||
client.POST("/profile/username", d.ProfileH.UpdateUsernameById)
|
||||
client.POST("/profile/password", d.ProfileH.UpdatePasswordById)
|
||||
client.GET("/profile/telegram", d.ProfileH.GetTelegramById)
|
||||
client.POST("/profile/telegram", d.ProfileH.SetTelegramById)
|
||||
}
|
||||
|
||||
// Espace admin : provisioning des démos (admin uniquement).
|
||||
@@ -73,7 +72,6 @@ func New(d Deps) *gin.Engine {
|
||||
{
|
||||
admin.GET("/codes", d.SubH.ListCodes)
|
||||
admin.POST("/codes", d.SubH.CreateCodeForBuy)
|
||||
admin.GET("/messages", d.ContactH.GetMessage)
|
||||
admin.POST("/profile/username", d.ProfileH.UpdateUsernameById)
|
||||
admin.POST("/profile/password", d.ProfileH.UpdatePasswordById)
|
||||
if d.DemosH != nil {
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
package sav
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/omnex/control-plane/api/internal/auth"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
store Store
|
||||
}
|
||||
|
||||
// NewHandler crée un nouveau Handler.
|
||||
func NewHandler(store Store) *Handler {
|
||||
return &Handler{store: store}
|
||||
}
|
||||
|
||||
type contactSupport struct {
|
||||
Username string `json:"username"`
|
||||
Telegram string `json:"telegram"`
|
||||
Sujet string `json:"sujet"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (h *Handler) CallSupport(c *gin.Context) {
|
||||
var req contactSupport
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
||||
return
|
||||
}
|
||||
|
||||
contact, err := h.store.ContactSupportByUser(req.Username, req.Telegram, req.Sujet, req.Message)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "erreur serveur"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{"success": "message envoyé", "contact": contact})
|
||||
}
|
||||
|
||||
func (h *Handler) GetMessage(c *gin.Context) {
|
||||
p := auth.PrincipalFrom(c)
|
||||
if p == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
||||
return
|
||||
}
|
||||
if p.Role != "admin" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "role non correcte"})
|
||||
return
|
||||
}
|
||||
|
||||
getMessage, err := h.store.GetMessage()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "erreur serveur"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{"messages": getMessage})
|
||||
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package sav
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/omnex/control-plane/api/internal/auth"
|
||||
)
|
||||
|
||||
type MemContactStore struct {
|
||||
mu sync.RWMutex
|
||||
items map[string]Contact
|
||||
}
|
||||
|
||||
func NewMemContactStore() *MemContactStore {
|
||||
return &MemContactStore{
|
||||
items: make(map[string]Contact),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MemContactStore) ContactSupportByUser(username, telegram, sujet, message string) (Contact, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
contact := Contact{
|
||||
ID: uuid.NewString(),
|
||||
Username: auth.NormalizeUsername(username),
|
||||
Telegram: auth.NormalizeUsername(telegram),
|
||||
Sujet: sujet,
|
||||
Message: message,
|
||||
}
|
||||
|
||||
m.items[contact.ID] = contact
|
||||
return contact, nil
|
||||
}
|
||||
|
||||
func (m *MemContactStore) GetMessage() ([]Contact, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
contacts := make([]Contact, 0, len(m.items))
|
||||
for _, c := range m.items {
|
||||
contacts = append(contacts, c)
|
||||
}
|
||||
sort.Slice(contacts, func(i, j int) bool {
|
||||
return contacts[i].CreatedAt.After(contacts[j].CreatedAt)
|
||||
})
|
||||
return contacts, nil
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package sav
|
||||
|
||||
import "time"
|
||||
|
||||
type Contact struct {
|
||||
ID string `gorm:"type:uuid;primaryKey" json:"id"`
|
||||
Username string `gorm:"size:64;not null" json:"username"`
|
||||
Telegram string `gorm:"size:64;not null" json:"telegram"`
|
||||
Sujet string `gorm:"size:64;not null" json:"sujet"`
|
||||
Message string `gorm:"size:64;not null" json:"message"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package sav
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/omnex/control-plane/api/internal/auth"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Store interface {
|
||||
ContactSupportByUser(username, telegram, sujet, message string) (Contact, error)
|
||||
GetMessage() ([]Contact, error)
|
||||
}
|
||||
|
||||
type GormStore struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewGormStore(db *gorm.DB) *GormStore {
|
||||
return &GormStore{db: db}
|
||||
}
|
||||
|
||||
func (s *GormStore) ContactSupportByUser(username, telegram, sujet, message string) (Contact, error) {
|
||||
contact := Contact{
|
||||
ID: uuid.NewString(),
|
||||
Username: auth.NormalizeUsername(username),
|
||||
Telegram: auth.NormalizeUsername(telegram),
|
||||
Sujet: sujet,
|
||||
Message: message,
|
||||
}
|
||||
|
||||
if err := s.db.Create(&contact).Error; err != nil {
|
||||
return Contact{}, err
|
||||
}
|
||||
return contact, nil
|
||||
}
|
||||
|
||||
func (s *GormStore) GetMessage() ([]Contact, error) {
|
||||
var contacts []Contact
|
||||
if err := s.db.Find(&contacts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return contacts, nil
|
||||
}
|
||||
Reference in New Issue
Block a user