118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"inou/lib"
|
|
)
|
|
|
|
type DossierResponse struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
DOB string `json:"dob"`
|
|
Found bool `json:"found"`
|
|
WeightUnit string `json:"weight_unit,omitempty"`
|
|
HeightUnit string `json:"height_unit,omitempty"`
|
|
IsProvider bool `json:"is_provider,omitempty"`
|
|
ProviderName string `json:"provider_name,omitempty"`
|
|
AwayEnabled bool `json:"away_enabled,omitempty"`
|
|
AwayMessage string `json:"away_message,omitempty"`
|
|
}
|
|
|
|
type DossierCreateRequest struct {
|
|
EmailHash string `json:"email_hash"`
|
|
Email string `json:"email"`
|
|
InvitedBy string `json:"invited_by"`
|
|
}
|
|
|
|
func handleDossier(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if r.Method == "POST" {
|
|
handleDossierCreate(w, r)
|
|
return
|
|
}
|
|
|
|
dossierHex := r.URL.Query().Get("dossier")
|
|
emailHash := r.URL.Query().Get("email_hash")
|
|
first := r.URL.Query().Get("first")
|
|
|
|
var d *lib.Dossier
|
|
var err error
|
|
|
|
// Note: This is the login/registration endpoint, uses nil ctx
|
|
if dossierHex != "" {
|
|
d, err = lib.DossierGet(nil, dossierHex)
|
|
} else if emailHash != "" {
|
|
d, err = lib.DossierGetByEmailHash(nil, emailHash)
|
|
} else if first == "1" {
|
|
d, err = lib.DossierGetFirst(nil)
|
|
} else {
|
|
http.Error(w, "dossier, email_hash, or first parameter required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if err != nil || d == nil {
|
|
json.NewEncoder(w).Encode(DossierResponse{Found: false})
|
|
return
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(DossierResponse{
|
|
ID: d.DossierID,
|
|
Name: d.Name,
|
|
DOB: d.DateOfBirth,
|
|
Found: true,
|
|
WeightUnit: d.WeightUnit,
|
|
HeightUnit: d.HeightUnit,
|
|
IsProvider: d.IsProvider,
|
|
ProviderName: d.ProviderName,
|
|
AwayEnabled: d.AwayEnabled,
|
|
AwayMessage: d.AwayMessage,
|
|
})
|
|
}
|
|
|
|
func handleDossierCreate(w http.ResponseWriter, r *http.Request) {
|
|
var req DossierCreateRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "invalid request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if req.EmailHash == "" {
|
|
http.Error(w, "email_hash required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Check if already exists (nil ctx - registration endpoint)
|
|
existing, err := lib.DossierGetByEmailHash(nil, req.EmailHash)
|
|
if err == nil && existing != nil {
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"id": existing.DossierID,
|
|
"exists": true,
|
|
})
|
|
return
|
|
}
|
|
|
|
// Create new dossier
|
|
d := &lib.Dossier{
|
|
EmailHash: req.EmailHash,
|
|
Email: req.Email,
|
|
CreatedAt: time.Now().Unix(),
|
|
}
|
|
if req.InvitedBy != "" {
|
|
d.InvitedByDossierID = req.InvitedBy
|
|
}
|
|
|
|
if err := lib.DossierWrite(nil, d); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"id": d.DossierID,
|
|
"exists": false,
|
|
})
|
|
}
|