Files
template-vitrine/backend/internal/modules/checkoutfields/repository.go
T
Xor290andClaude Sonnet 5.5 f9bfdc9d3c
ci-api / test (push) Successful in 20m38s
ci-web / test (push) Successful in 11m20s
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>
2026-10-04 16:16:22 +02:00

55 lines
1.4 KiB
Go

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)
}