From 6beabfb07042e1237caae949a9838827972ca865 Mon Sep 17 00:00:00 2001 From: Nuxgrid Date: Wed, 29 Jul 2026 14:50:31 +0200 Subject: [PATCH] chore: finish profile --- control-plane/api/cmd/api/main.go | 5 - control-plane/api/internal/db/db.go | 2 - .../api/internal/profile/GormStore.go | 14 +- control-plane/api/internal/profile/handler.go | 37 +++ control-plane/api/internal/router/router.go | 6 +- control-plane/api/internal/sav/handler.go | 60 ----- control-plane/api/internal/sav/mem.go | 49 ---- control-plane/api/internal/sav/models.go | 12 - control-plane/api/internal/sav/store.go | 43 ---- web/src/App.tsx | 11 - web/src/components/BackofficeLayout.tsx | 6 - web/src/lib/api.ts | 12 +- web/src/pages/Contact.tsx | 211 ------------------ web/src/pages/backoffice/Contact.tsx | 113 ---------- web/src/pages/backoffice/Leads.tsx | 2 + web/src/pages/backoffice/Profile.tsx | 81 ++++++- 16 files changed, 137 insertions(+), 527 deletions(-) delete mode 100644 control-plane/api/internal/sav/handler.go delete mode 100644 control-plane/api/internal/sav/mem.go delete mode 100644 control-plane/api/internal/sav/models.go delete mode 100644 control-plane/api/internal/sav/store.go delete mode 100644 web/src/pages/Contact.tsx delete mode 100644 web/src/pages/backoffice/Contact.tsx diff --git a/control-plane/api/cmd/api/main.go b/control-plane/api/cmd/api/main.go index 70f9516..c4bbfc1 100644 --- a/control-plane/api/cmd/api/main.go +++ b/control-plane/api/cmd/api/main.go @@ -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), } diff --git a/control-plane/api/internal/db/db.go b/control-plane/api/internal/db/db.go index 74adaca..fec3e74 100644 --- a/control-plane/api/internal/db/db.go +++ b/control-plane/api/internal/db/db.go @@ -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{}, ) } diff --git a/control-plane/api/internal/profile/GormStore.go b/control-plane/api/internal/profile/GormStore.go index 8c9cb68..905b0e7 100644 --- a/control-plane/api/internal/profile/GormStore.go +++ b/control-plane/api/internal/profile/GormStore.go @@ -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 } diff --git a/control-plane/api/internal/profile/handler.go b/control-plane/api/internal/profile/handler.go index 7b88118..0ce60d8 100644 --- a/control-plane/api/internal/profile/handler.go +++ b/control-plane/api/internal/profile/handler.go @@ -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}) +} diff --git a/control-plane/api/internal/router/router.go b/control-plane/api/internal/router/router.go index e3d697f..da62b22 100644 --- a/control-plane/api/internal/router/router.go +++ b/control-plane/api/internal/router/router.go @@ -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 { diff --git a/control-plane/api/internal/sav/handler.go b/control-plane/api/internal/sav/handler.go deleted file mode 100644 index d1832ed..0000000 --- a/control-plane/api/internal/sav/handler.go +++ /dev/null @@ -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}) - -} diff --git a/control-plane/api/internal/sav/mem.go b/control-plane/api/internal/sav/mem.go deleted file mode 100644 index 5129d1d..0000000 --- a/control-plane/api/internal/sav/mem.go +++ /dev/null @@ -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 -} diff --git a/control-plane/api/internal/sav/models.go b/control-plane/api/internal/sav/models.go deleted file mode 100644 index 1de9623..0000000 --- a/control-plane/api/internal/sav/models.go +++ /dev/null @@ -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"` -} diff --git a/control-plane/api/internal/sav/store.go b/control-plane/api/internal/sav/store.go deleted file mode 100644 index 4384117..0000000 --- a/control-plane/api/internal/sav/store.go +++ /dev/null @@ -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 -} diff --git a/web/src/App.tsx b/web/src/App.tsx index 2458fe6..13dcc25 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -7,13 +7,11 @@ import { Login } from './pages/Login' import { Register } from './pages/Register' import { Leads } from './pages/backoffice/Leads' import { Demos } from './pages/backoffice/Demos' -import { Messages } from './pages/backoffice/Contact' import { Codes } from './pages/backoffice/Codes' import { Subscription } from './pages/backoffice/Subscription' import { Profile } from './pages/backoffice/Profile' import { PublicLayout } from './components/PublicLayout' import { BackofficeLayout } from './components/BackofficeLayout' -import { Contact } from './pages/Contact' import { useAuth } from './lib/auth' import type { JSX } from 'react' @@ -64,7 +62,6 @@ export function App() { } /> } /> } /> - } /> } /> } /> @@ -106,14 +103,6 @@ export function App() { } /> - - - - } - /> Leads} {isClient && Abonnement} {isAdmin || isClient && Profile} - {isAdmin && Messages} {isAdmin && Démos} {isAdmin && Codes} @@ -96,11 +95,6 @@ export function BackofficeLayout() { Abonnement )} - {isAdmin && ( - - Messages - - )} {isAdmin && ( Démos diff --git a/web/src/lib/api.ts b/web/src/lib/api.ts index 8913a62..1d2f70d 100644 --- a/web/src/lib/api.ts +++ b/web/src/lib/api.ts @@ -107,6 +107,11 @@ export interface UpdateUsername { export interface UpdatePassword { password: string } + +export interface TelegramInfo { + telegram: string +} + // --- Endpoints --- export const api = { @@ -157,6 +162,9 @@ export const api = { request('POST', '/demos/details', { username }), updateUsername: (username: string) => request('POST', '/profile/username', { username }), - updatePassword: (password: string) => - request('POST', '/profile/password', { password }) + updatePassword: (password: string) => + request('POST', '/profile/password', { password }), + getTelegram: () => request('GET', '/profile/telegram'), + setTelegram: (telegram: string) => + request('POST', '/profile/telegram', { telegram }), } diff --git a/web/src/pages/Contact.tsx b/web/src/pages/Contact.tsx deleted file mode 100644 index 8fc8ec6..0000000 --- a/web/src/pages/Contact.tsx +++ /dev/null @@ -1,211 +0,0 @@ -import { useState, type FormEvent } from 'react' -import { - Box, - Button, - Container, - Divider, - Flex, - FormControl, - FormLabel, - FormErrorMessage, - Heading, - Input, - Link, - Stack, - Text, - Textarea, - SimpleGrid, - useToast, -} from '@chakra-ui/react' -import { api, ApiError } from '../lib/api' - -interface ContactForm { - name: string - telegram: string - subject: string - message: string -} - -const initialForm: ContactForm = { - name: '', - telegram: '', - subject: '', - message: '', -} - -// Remplace par le vrai handle / lien de contact Telegram d'Omnex. -const TELEGRAM_CONTACT_URL = 'https://t.me/OMNEX_CORP' - -const TelegramIcon = () => ( - - - -) - -export function Contact() { - const toast = useToast() - const [form, setForm] = useState(initialForm) - const [errors, setErrors] = useState>>({}) - const [submitting, setSubmitting] = useState(false) - - const handleChange = - (field: keyof ContactForm) => - (e: React.ChangeEvent) => { - setForm((prev) => ({ ...prev, [field]: e.target.value })) - } - - const validate = (): boolean => { - const nextErrors: Partial> = {} - if (!form.name.trim()) nextErrors.name = 'Le nom est requis.' - if (!form.telegram.trim()) { - nextErrors.telegram = "L'email est requis." - } - if (!form.subject.trim()) nextErrors.subject = 'Le sujet est requis.' - if (!form.message.trim()) nextErrors.message = 'Le message est requis.' - setErrors(nextErrors) - return Object.keys(nextErrors).length === 0 - } - - const handleSubmit = async (e: FormEvent) => { - e.preventDefault() - if (!validate()) return - - setSubmitting(true) - try { - await api.sendMessage(form.name, form.telegram, form.subject, form.message) - toast({ - title: 'Message envoyé', - description: 'Nous vous répondrons dans les plus brefs délais.', - status: 'success', - duration: 5000, - isClosable: true, - }) - setForm(initialForm) - setErrors({}) - } catch (err) { - const description = - err instanceof ApiError ? err.message : 'Merci de réessayer dans quelques instants.' - toast({ - title: "Erreur lors de l'envoi", - description, - status: 'error', - duration: 5000, - isClosable: true, - }) - } finally { - setSubmitting(false) - } - } - - return ( - - - - Contactez-nous - - Une question, une démo à planifier, un projet ? Écrivez-nous. - - - - - - - - - - - ou par formulaire - - - - - - - - - Nom - - {errors.name} - - - - Telegram - - {errors.telegram} - - - - - Sujet - - {errors.subject} - - - - Message -