chore: add point pool
This commit is contained in:
@@ -57,48 +57,57 @@ func CalcPointsFromTiers(total float64, tiers []PointsTier) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// PointsPool représente un type de point personnalisable par l'admin
|
||||
// Pool[0] → colonne `point`, Pool[1] → colonne `point_zipette`
|
||||
type PointsPool struct {
|
||||
Key string `json:"key"` // identifiant interne (ex: "pool_0")
|
||||
Name string `json:"name"` // nom affiché (ex: "Cannabis", "Accessoires")
|
||||
Categories []string `json:"categories"` // catégories de produits assignées à ce pool
|
||||
Tiers []PointsTier `json:"tiers"` // barème de points
|
||||
}
|
||||
|
||||
// AppSettings contient les paramètres globaux de l'application
|
||||
type AppSettings struct {
|
||||
PenaltiesEnabled bool `json:"penalties_enabled"`
|
||||
ShowAmendeScore bool `json:"show_amende_score"` // afficher le score d'amendes aux clients/cabine
|
||||
PointsEnabled bool `json:"points_enabled"` // afficher/activer le système de points
|
||||
PointsCategoriesWeed []string `json:"points_categories_weed"` // catégories → pool point (weed)
|
||||
PointsCategoriesZipette []string `json:"points_categories_zipette"` // catégories → pool point_zipette
|
||||
PointsCategoriesTotal []string `json:"points_categories_total"` // catégories → pool total (point, sans séparation)
|
||||
PointsSeparated bool `json:"points_separated"` // true = weed/zipette séparés, false = tout dans point
|
||||
// Barème de points par paliers configurables
|
||||
PointsWeedTiers []PointsTier `json:"points_weed_tiers"`
|
||||
PointsZipetteTiers []PointsTier `json:"points_zipette_tiers"`
|
||||
PointsTotalTiers []PointsTier `json:"points_total_tiers"`
|
||||
ReferralEnabled bool `json:"referral_enabled"` // activer/désactiver le système de parrainage
|
||||
DeliverySchedule DeliverySchedule `json:"delivery_schedule"` // horaires de livraison par jour
|
||||
PostalZones []PostalZone `json:"postal_zones"` // zones de livraison avec minimum de commande
|
||||
PenaltiesEnabled bool `json:"penalties_enabled"`
|
||||
ShowAmendeScore bool `json:"show_amende_score"` // afficher le score d'amendes aux clients/cabine
|
||||
PointsEnabled bool `json:"points_enabled"` // afficher/activer le système de points
|
||||
PointsPools []PointsPool `json:"points_pools"` // types de points personnalisés
|
||||
ReferralEnabled bool `json:"referral_enabled"` // activer/désactiver le système de parrainage
|
||||
DeliverySchedule DeliverySchedule `json:"delivery_schedule"` // horaires de livraison par jour
|
||||
PostalZones []PostalZone `json:"postal_zones"` // zones de livraison avec minimum de commande
|
||||
}
|
||||
|
||||
// DefaultSettings retourne les paramètres par défaut
|
||||
func DefaultSettings() AppSettings {
|
||||
return AppSettings{
|
||||
PenaltiesEnabled: true,
|
||||
ShowAmendeScore: true,
|
||||
PointsEnabled: true,
|
||||
ReferralEnabled: true,
|
||||
PointsCategoriesWeed: []string{},
|
||||
PointsCategoriesZipette: []string{},
|
||||
PointsCategoriesTotal: []string{},
|
||||
PointsSeparated: true,
|
||||
PointsWeedTiers: []PointsTier{
|
||||
{Min: 30, Max: 50, Points: 1},
|
||||
{Min: 60, Max: 150, Points: 2},
|
||||
{Min: 160, Max: 300, Points: 3},
|
||||
{Min: 310, Max: 400, Points: 5},
|
||||
{Min: 401, Max: 0, Points: 10},
|
||||
PenaltiesEnabled: true,
|
||||
ShowAmendeScore: true,
|
||||
PointsEnabled: true,
|
||||
ReferralEnabled: true,
|
||||
PointsPools: []PointsPool{
|
||||
{
|
||||
Key: "pool_0",
|
||||
Name: "Pool 1",
|
||||
Categories: []string{},
|
||||
Tiers: []PointsTier{
|
||||
{Min: 30, Max: 50, Points: 1},
|
||||
{Min: 60, Max: 150, Points: 2},
|
||||
{Min: 160, Max: 300, Points: 3},
|
||||
{Min: 310, Max: 400, Points: 5},
|
||||
{Min: 401, Max: 0, Points: 10},
|
||||
},
|
||||
},
|
||||
{
|
||||
Key: "pool_1",
|
||||
Name: "Pool 2",
|
||||
Categories: []string{},
|
||||
Tiers: []PointsTier{
|
||||
{Min: 30, Max: 100, Points: 1},
|
||||
{Min: 110, Max: 200, Points: 2},
|
||||
{Min: 210, Max: 0, Points: 3},
|
||||
},
|
||||
},
|
||||
},
|
||||
PointsZipetteTiers: []PointsTier{
|
||||
{Min: 30, Max: 100, Points: 1},
|
||||
{Min: 110, Max: 200, Points: 2},
|
||||
{Min: 210, Max: 0, Points: 3},
|
||||
},
|
||||
PointsTotalTiers: []PointsTier{},
|
||||
DeliverySchedule: DefaultDeliverySchedule(),
|
||||
PostalZones: []PostalZone{
|
||||
{Name: "Zone 30€", MinAmount: 30, Codes: []string{"44000", "44100", "44200", "44300"}},
|
||||
@@ -135,37 +144,10 @@ func (d *Database) GetSettings() (AppSettings, error) {
|
||||
settings.ShowAmendeScore = value == "true"
|
||||
case "points_enabled":
|
||||
settings.PointsEnabled = value == "true"
|
||||
case "points_categories_weed":
|
||||
var cats []string
|
||||
if err := json.Unmarshal([]byte(value), &cats); err == nil {
|
||||
settings.PointsCategoriesWeed = cats
|
||||
}
|
||||
case "points_categories_zipette":
|
||||
var cats []string
|
||||
if err := json.Unmarshal([]byte(value), &cats); err == nil {
|
||||
settings.PointsCategoriesZipette = cats
|
||||
}
|
||||
case "points_categories_total":
|
||||
var cats []string
|
||||
if err := json.Unmarshal([]byte(value), &cats); err == nil {
|
||||
settings.PointsCategoriesTotal = cats
|
||||
}
|
||||
case "points_separated":
|
||||
settings.PointsSeparated = value == "true"
|
||||
case "points_weed_tiers":
|
||||
var tiers []PointsTier
|
||||
if err := json.Unmarshal([]byte(value), &tiers); err == nil {
|
||||
settings.PointsWeedTiers = tiers
|
||||
}
|
||||
case "points_zipette_tiers":
|
||||
var tiers []PointsTier
|
||||
if err := json.Unmarshal([]byte(value), &tiers); err == nil {
|
||||
settings.PointsZipetteTiers = tiers
|
||||
}
|
||||
case "points_total_tiers":
|
||||
var tiers []PointsTier
|
||||
if err := json.Unmarshal([]byte(value), &tiers); err == nil {
|
||||
settings.PointsTotalTiers = tiers
|
||||
case "points_pools":
|
||||
var pools []PointsPool
|
||||
if err := json.Unmarshal([]byte(value), &pools); err == nil {
|
||||
settings.PointsPools = pools
|
||||
}
|
||||
case "referral_enabled":
|
||||
settings.ReferralEnabled = value == "true"
|
||||
@@ -192,27 +174,23 @@ func (d *Database) UpdateSettings(s AppSettings) error {
|
||||
}
|
||||
return "false"
|
||||
}
|
||||
if s.PointsCategoriesWeed == nil {
|
||||
s.PointsCategoriesWeed = []string{}
|
||||
|
||||
if s.PointsPools == nil {
|
||||
s.PointsPools = []PointsPool{}
|
||||
}
|
||||
if s.PointsCategoriesZipette == nil {
|
||||
s.PointsCategoriesZipette = []string{}
|
||||
}
|
||||
if s.PointsCategoriesTotal == nil {
|
||||
s.PointsCategoriesTotal = []string{}
|
||||
// S'assurer que chaque pool a des slices non-nil
|
||||
for i := range s.PointsPools {
|
||||
if s.PointsPools[i].Categories == nil {
|
||||
s.PointsPools[i].Categories = []string{}
|
||||
}
|
||||
if s.PointsPools[i].Tiers == nil {
|
||||
s.PointsPools[i].Tiers = []PointsTier{}
|
||||
}
|
||||
}
|
||||
|
||||
weedJSON, err := json.Marshal(s.PointsCategoriesWeed)
|
||||
poolsJSON, err := json.Marshal(s.PointsPools)
|
||||
if err != nil {
|
||||
return fmt.Errorf("erreur sérialisation weed: %w", err)
|
||||
}
|
||||
zipetteJSON, err := json.Marshal(s.PointsCategoriesZipette)
|
||||
if err != nil {
|
||||
return fmt.Errorf("erreur sérialisation zipette: %w", err)
|
||||
}
|
||||
totalJSON, err := json.Marshal(s.PointsCategoriesTotal)
|
||||
if err != nil {
|
||||
return fmt.Errorf("erreur sérialisation total: %w", err)
|
||||
return fmt.Errorf("erreur sérialisation pools: %w", err)
|
||||
}
|
||||
|
||||
tx, err := d.Begin()
|
||||
@@ -224,39 +202,11 @@ func (d *Database) UpdateSettings(s AppSettings) error {
|
||||
upsert := `INSERT INTO app_settings (key, value) VALUES ($1, $2)
|
||||
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value`
|
||||
|
||||
if s.PointsWeedTiers == nil {
|
||||
s.PointsWeedTiers = []PointsTier{}
|
||||
}
|
||||
if s.PointsZipetteTiers == nil {
|
||||
s.PointsZipetteTiers = []PointsTier{}
|
||||
}
|
||||
if s.PointsTotalTiers == nil {
|
||||
s.PointsTotalTiers = []PointsTier{}
|
||||
}
|
||||
weedTiersJSON, err := json.Marshal(s.PointsWeedTiers)
|
||||
if err != nil {
|
||||
return fmt.Errorf("erreur sérialisation weed tiers: %w", err)
|
||||
}
|
||||
zipetteTiersJSON, err := json.Marshal(s.PointsZipetteTiers)
|
||||
if err != nil {
|
||||
return fmt.Errorf("erreur sérialisation zipette tiers: %w", err)
|
||||
}
|
||||
totalTiersJSON, err := json.Marshal(s.PointsTotalTiers)
|
||||
if err != nil {
|
||||
return fmt.Errorf("erreur sérialisation total tiers: %w", err)
|
||||
}
|
||||
|
||||
pairs := [][2]string{
|
||||
{"penalties_enabled", boolStr(s.PenaltiesEnabled)},
|
||||
{"show_amende_score", boolStr(s.ShowAmendeScore)},
|
||||
{"points_enabled", boolStr(s.PointsEnabled)},
|
||||
{"points_categories_weed", string(weedJSON)},
|
||||
{"points_categories_zipette", string(zipetteJSON)},
|
||||
{"points_categories_total", string(totalJSON)},
|
||||
{"points_separated", boolStr(s.PointsSeparated)},
|
||||
{"points_weed_tiers", string(weedTiersJSON)},
|
||||
{"points_zipette_tiers", string(zipetteTiersJSON)},
|
||||
{"points_total_tiers", string(totalTiersJSON)},
|
||||
{"points_pools", string(poolsJSON)},
|
||||
{"referral_enabled", boolStr(s.ReferralEnabled)},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user