feat(checkout): let the admin choose the details asked at checkout
The admin enables or disables each option (phone, address, Snapchat, Instagram, Telegram, SimpleX, WhatsApp, Signal, notes, preferred time) from a new "Checkout fields" page; the customer's name is always asked. Backend - checkoutfields module: single-row settings (CHECK id = 1), public GET and admin GET/PUT. - orders: answers stored as JSONB (contacts, info_sup); the customer phone moves from customer_phone to contacts.phone (migration 000034 copies existing values before dropping the column). - Orders are validated against the live settings: disabled options and unknown keys are dropped, enabled phone/address are required, and format, length and control characters are checked. - The Telegram notification includes the answers. Frontend - Checkout form generated from the enabled options; admin settings page; answers shown in the order list and detail. - API calls live in src/api (checkoutFields.ts, orders.ts); ApiError keeps the response body so the rejected field can be reported. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
178652ef14
commit
f9bfdc9d3c
@@ -0,0 +1,80 @@
|
||||
package checkoutfields
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service *Service
|
||||
}
|
||||
|
||||
func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
}
|
||||
|
||||
func toPayload(f *CheckoutField) settingsPayload {
|
||||
return settingsPayload{
|
||||
Phone: f.CheckoutPhone,
|
||||
Address: f.CheckoutAddress,
|
||||
Snap: f.CheckoutSnap,
|
||||
Insta: f.CheckoutInsta,
|
||||
Telegram: f.CheckoutTelegram,
|
||||
SimpleX: f.CheckoutSimpleX,
|
||||
Whatsapp: f.CheckoutWhatsapp,
|
||||
Signal: f.CheckoutSignal,
|
||||
Notes: f.CheckoutNotes,
|
||||
Hour: f.CheckoutHour,
|
||||
}
|
||||
}
|
||||
|
||||
func (p settingsPayload) toModel() *CheckoutField {
|
||||
return &CheckoutField{
|
||||
CheckoutPhone: p.Phone,
|
||||
CheckoutAddress: p.Address,
|
||||
CheckoutSnap: p.Snap,
|
||||
CheckoutInsta: p.Insta,
|
||||
CheckoutTelegram: p.Telegram,
|
||||
CheckoutSimpleX: p.SimpleX,
|
||||
CheckoutWhatsapp: p.Whatsapp,
|
||||
CheckoutSignal: p.Signal,
|
||||
CheckoutNotes: p.Notes,
|
||||
CheckoutHour: p.Hour,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) fail(c *gin.Context, err error, msg string) {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "checkout fields not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
|
||||
}
|
||||
|
||||
// Get : lecture publique, utilisée par le storefront pour savoir quels champs
|
||||
// afficher au checkout.
|
||||
func (h *Handler) Get(c *gin.Context) {
|
||||
f, err := h.service.Get(c.Request.Context())
|
||||
if err != nil {
|
||||
h.fail(c, err, "failed to load checkout fields")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toPayload(f))
|
||||
}
|
||||
|
||||
// Update : écriture admin de tous les réglages d'un coup.
|
||||
func (h *Handler) Update(c *gin.Context) {
|
||||
var req settingsPayload
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
f, err := h.service.Update(c.Request.Context(), req.toModel())
|
||||
if err != nil {
|
||||
h.fail(c, err, "failed to update checkout fields")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toPayload(f))
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package checkoutfields
|
||||
|
||||
type CheckoutField struct {
|
||||
ID int16 `gorm:"primaryKey"`
|
||||
CheckoutPhone bool `gorm:"not null;default:false"`
|
||||
CheckoutAddress bool `gorm:"not null;default:false"`
|
||||
CheckoutSnap bool `gorm:"not null;default:false"`
|
||||
CheckoutInsta bool `gorm:"not null;default:false"`
|
||||
CheckoutTelegram bool `gorm:"not null;default:false"`
|
||||
CheckoutSimpleX bool `gorm:"not null;default:false"`
|
||||
CheckoutWhatsapp bool `gorm:"not null;default:false"`
|
||||
CheckoutSignal bool `gorm:"not null;default:false"`
|
||||
CheckoutNotes bool `gorm:"not null;default:false"`
|
||||
CheckoutHour bool `gorm:"not null;default:false"`
|
||||
}
|
||||
|
||||
type settingsPayload struct {
|
||||
Phone bool `json:"phone"`
|
||||
Address bool `json:"address"`
|
||||
Snap bool `json:"snap"`
|
||||
Insta bool `json:"insta"`
|
||||
Telegram bool `json:"telegram"`
|
||||
SimpleX bool `json:"simplex"`
|
||||
Whatsapp bool `json:"whatsapp"`
|
||||
Signal bool `json:"signal"`
|
||||
Notes bool `json:"notes"`
|
||||
Hour bool `json:"hour"`
|
||||
}
|
||||
|
||||
func (CheckoutField) TableName() string { return "checkout_fields" }
|
||||
@@ -0,0 +1,54 @@
|
||||
package checkoutfields
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// singletonID : l'unique ligne de checkout_fields (voir la contrainte
|
||||
// CHECK (id = 1) de la migration 000032).
|
||||
const singletonID int16 = 1
|
||||
|
||||
var ErrNotFound = errors.New("checkout fields not found")
|
||||
|
||||
type Repository interface {
|
||||
Get(ctx context.Context) (*CheckoutField, error)
|
||||
Update(ctx context.Context, f *CheckoutField) (*CheckoutField, error)
|
||||
}
|
||||
|
||||
type gormRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB) Repository {
|
||||
return &gormRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *gormRepository) Get(ctx context.Context) (*CheckoutField, error) {
|
||||
var f CheckoutField
|
||||
err := r.db.WithContext(ctx).First(&f, singletonID).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &f, nil
|
||||
}
|
||||
|
||||
// Update écrit tous les réglages sur la ligne unique. Select("*") est voulu :
|
||||
// sans lui GORM ignore les champs à valeur zéro d'une struct, donc passer
|
||||
// false n'écrirait rien et l'admin ne pourrait jamais désactiver un champ.
|
||||
func (r *gormRepository) Update(ctx context.Context, f *CheckoutField) (*CheckoutField, error) {
|
||||
f.ID = singletonID
|
||||
res := r.db.WithContext(ctx).Model(&CheckoutField{}).Where("id = ?", singletonID).Select("*").Updates(f)
|
||||
if res.Error != nil {
|
||||
return nil, res.Error
|
||||
}
|
||||
if res.RowsAffected == 0 {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return r.Get(ctx)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package checkoutfields
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func RegisterAdminRoutes(rg *gin.RouterGroup, h *Handler, requireAdmin gin.HandlerFunc) {
|
||||
group := rg.Group("/admin/checkout-fields", requireAdmin)
|
||||
group.GET("", h.Get)
|
||||
group.PUT("", h.Update)
|
||||
}
|
||||
|
||||
// RegisterPublicRoutes expose les réglages en lecture seule : le storefront
|
||||
// en a besoin pour n'afficher que les champs demandés au checkout.
|
||||
func RegisterPublicRoutes(rg *gin.RouterGroup, h *Handler) {
|
||||
rg.GET("/checkout-fields", h.Get)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package checkoutfields
|
||||
|
||||
import "context"
|
||||
|
||||
type Service struct {
|
||||
repo Repository
|
||||
}
|
||||
|
||||
func NewService(repo Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context) (*CheckoutField, error) {
|
||||
return s.repo.Get(ctx)
|
||||
}
|
||||
|
||||
// Update remplace tous les réglages. Le nom du client est toujours demandé
|
||||
// (et obligatoire) : il n'est donc pas un réglage.
|
||||
func (s *Service) Update(ctx context.Context, f *CheckoutField) (*CheckoutField, error) {
|
||||
return s.repo.Update(ctx, f)
|
||||
}
|
||||
Reference in New Issue
Block a user