package server
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"odoo-go/pkg/service"
"odoo-go/pkg/tools"
)
// isSetupNeeded checks if the database has been initialized.
func (s *Server) isSetupNeeded() bool {
var count int
err := s.pool.QueryRow(context.Background(),
`SELECT COUNT(*) FROM res_company`).Scan(&count)
return err != nil || count == 0
}
// handleSetup serves the setup wizard.
func (s *Server) handleSetup(w http.ResponseWriter, r *http.Request) {
if !s.isSetupNeeded() {
http.Redirect(w, r, "/web/login", http.StatusFound)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(`
Odoo — Setup
Odoo Setup
Richten Sie Ihre Datenbank ein
⟳
Datenbank wird eingerichtet...
`))
}
// SetupParams holds the setup wizard form data.
type SetupParams struct {
CompanyName string `json:"company_name"`
Street string `json:"street"`
Zip string `json:"zip"`
City string `json:"city"`
Country string `json:"country"`
Email string `json:"email"`
Phone string `json:"phone"`
VAT string `json:"vat"`
Chart string `json:"chart"`
AdminEmail string `json:"admin_email"`
AdminPassword string `json:"admin_password"`
DemoData bool `json:"demo_data"`
}
// handleSetupInstall processes the setup wizard form submission.
func (s *Server) handleSetupInstall(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var params SetupParams
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
writeJSON(w, map[string]string{"error": "Invalid request"})
return
}
if params.CompanyName == "" {
writeJSON(w, map[string]string{"error": "Firmenname ist erforderlich"})
return
}
if params.AdminEmail == "" || params.AdminPassword == "" {
writeJSON(w, map[string]string{"error": "Admin Email und Passwort sind erforderlich"})
return
}
log.Printf("setup: initializing database for %q", params.CompanyName)
// Hash admin password
hashedPw, err := tools.HashPassword(params.AdminPassword)
if err != nil {
writeJSON(w, map[string]string{"error": fmt.Sprintf("Password hash error: %v", err)})
return
}
// Map country code to name
countryName := "Germany"
phoneCode := "49"
switch params.Country {
case "AT":
countryName = "Austria"
phoneCode = "43"
case "CH":
countryName = "Switzerland"
phoneCode = "41"
}
// Run the seed with user-provided data
setupCfg := service.SetupConfig{
CompanyName: params.CompanyName,
Street: params.Street,
Zip: params.Zip,
City: params.City,
CountryCode: params.Country,
CountryName: countryName,
PhoneCode: phoneCode,
Email: params.Email,
Phone: params.Phone,
VAT: params.VAT,
Chart: params.Chart,
AdminLogin: params.AdminEmail,
AdminPassword: hashedPw,
DemoData: params.DemoData,
}
if err := service.SeedWithSetup(context.Background(), s.pool, setupCfg); err != nil {
log.Printf("setup: error: %v", err)
writeJSON(w, map[string]string{"error": fmt.Sprintf("Setup error: %v", err)})
return
}
log.Printf("setup: database initialized successfully for %q", params.CompanyName)
writeJSON(w, map[string]string{"status": "ok"})
}
func writeJSON(w http.ResponseWriter, v interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(v)
}